2024-12-07 22:17:22 +01:00
|
|
|
import { toast } from "vue-sonner";
|
2025-07-16 07:48:39 +02:00
|
|
|
import * as m from "~~/paraglide/messages.js";
|
2024-05-17 08:25:59 +02:00
|
|
|
|
2025-08-28 07:41:51 +02:00
|
|
|
export const useCacheRefresh = () => {
|
|
|
|
|
const authStore = useAuthStore();
|
|
|
|
|
const { identity } = storeToRefs(authStore);
|
|
|
|
|
|
|
|
|
|
authStore.client.getInstance().then((res) => {
|
|
|
|
|
authStore.updateActiveIdentity({
|
|
|
|
|
instance: res.data,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-17 08:25:59 +02:00
|
|
|
// Refresh custom emojis and instance data and me on every reload
|
2024-11-29 21:43:41 +01:00
|
|
|
watch(
|
2025-08-28 07:41:51 +02:00
|
|
|
identity,
|
|
|
|
|
async (oldIdentity, newIdentity) => {
|
|
|
|
|
if (newIdentity && newIdentity.id !== oldIdentity?.id) {
|
|
|
|
|
console.info("Refreshing emoji, instance and account cache");
|
|
|
|
|
authStore.client
|
|
|
|
|
.verifyAccountCredentials()
|
2024-11-29 21:43:41 +01:00
|
|
|
.then((res) => {
|
2025-08-28 07:41:51 +02:00
|
|
|
authStore.updateActiveIdentity({
|
|
|
|
|
account: res.data,
|
|
|
|
|
});
|
2024-11-29 21:43:41 +01:00
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
const code = err.response.status;
|
2024-05-17 09:19:06 +02:00
|
|
|
|
2024-11-29 21:43:41 +01:00
|
|
|
if (code === 401) {
|
|
|
|
|
// Reset tokenData
|
2025-08-28 07:41:51 +02:00
|
|
|
authStore.setActiveIdentity(null);
|
2024-12-07 22:17:22 +01:00
|
|
|
toast.error(m.fancy_this_wasp_renew(), {
|
|
|
|
|
description: m.real_weird_deer_stop(),
|
2024-11-29 21:43:41 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-05-17 08:25:59 +02:00
|
|
|
|
2025-08-28 07:41:51 +02:00
|
|
|
authStore.client.getInstanceCustomEmojis().then((res) => {
|
|
|
|
|
authStore.updateActiveIdentity({
|
|
|
|
|
emojis: res.data,
|
2024-11-29 21:43:41 +01:00
|
|
|
});
|
2024-05-17 09:19:06 +02:00
|
|
|
});
|
2025-08-28 07:41:51 +02:00
|
|
|
}
|
2024-11-29 21:43:41 +01:00
|
|
|
},
|
2024-12-04 15:04:08 +01:00
|
|
|
{ flush: "sync", immediate: true },
|
2024-11-29 21:43:41 +01:00
|
|
|
);
|
2024-05-17 08:25:59 +02:00
|
|
|
};
|