2024-05-17 08:25:59 +02:00
|
|
|
import type { Mastodon } from "megalodon";
|
|
|
|
|
import type { InstanceWithExtra } from "./Instance";
|
|
|
|
|
|
|
|
|
|
export const useCacheRefresh = (client: MaybeRef<Mastodon | null>) => {
|
2024-05-17 09:19:06 +02:00
|
|
|
if (process.server) return;
|
|
|
|
|
|
2024-05-17 08:25:59 +02:00
|
|
|
const tokenData = useTokenData();
|
|
|
|
|
const me = useMe();
|
|
|
|
|
const instance = useInstance();
|
|
|
|
|
const customEmojis = useCustomEmojis();
|
|
|
|
|
|
|
|
|
|
// Refresh custom emojis and instance data and me on every reload
|
|
|
|
|
watchEffect(async () => {
|
2024-05-17 09:19:06 +02:00
|
|
|
console.log("Clearing cache");
|
2024-05-17 08:25:59 +02:00
|
|
|
if (tokenData.value) {
|
|
|
|
|
await toValue(client)
|
|
|
|
|
?.verifyAccountCredentials()
|
|
|
|
|
.then((res) => {
|
|
|
|
|
me.value = res.data;
|
2024-05-17 09:19:06 +02:00
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
const code = err.response.status;
|
|
|
|
|
|
|
|
|
|
if (code === 401) {
|
|
|
|
|
// Reset tokenData
|
|
|
|
|
tokenData.value = null;
|
|
|
|
|
useEvent("notification:new", {
|
|
|
|
|
type: "error",
|
|
|
|
|
title: "Your session has expired",
|
|
|
|
|
message:
|
|
|
|
|
"You have been logged out. Please log in again.",
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-05-17 08:25:59 +02:00
|
|
|
});
|
|
|
|
|
|
2024-05-17 09:19:06 +02:00
|
|
|
await toValue(client)
|
|
|
|
|
?.getInstanceCustomEmojis()
|
|
|
|
|
.then((res) => {
|
|
|
|
|
customEmojis.value = res.data;
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-05-17 08:25:59 +02:00
|
|
|
|
|
|
|
|
toValue(client)
|
|
|
|
|
?.getInstance()
|
|
|
|
|
.then((res) => {
|
|
|
|
|
instance.value = res.data as InstanceWithExtra;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|