frontend/app/composables/Relationship.ts
Jesse Wierzbinski 7f7cf20311
Some checks failed
CodeQL / Analyze (javascript) (push) Failing after 1s
Deploy to GitHub Pages / build (push) Failing after 1s
Deploy to GitHub Pages / deploy (push) Has been skipped
Docker / build (push) Failing after 1s
Mirror to Codeberg / Mirror (push) Failing after 1s
chore: ⬆️ Upgrade to Nuxt 4
2025-07-16 07:48:39 +02:00

50 lines
1.6 KiB
TypeScript

import type { Client } from "@versia/client";
import type { Relationship } from "@versia/client/schemas";
import type { z } from "zod";
export const useRelationship = (
client: MaybeRef<Client | null>,
accountId: MaybeRef<string | null>,
) => {
const relationship = ref(null as z.infer<typeof Relationship> | null);
const isLoading = ref(false);
if (!identity.value) {
return { relationship, isLoading };
}
watchEffect(() => {
if (toValue(accountId)) {
toValue(client)
?.getRelationship(toValue(accountId) ?? "")
.then((res) => {
relationship.value = res.data;
});
}
});
watch(relationship, (newOutput, oldOutput) => {
if (newOutput !== oldOutput && newOutput && oldOutput) {
if (newOutput?.following !== oldOutput?.following) {
isLoading.value = true;
if (newOutput?.following) {
toValue(client)
?.followAccount(toValue(accountId) ?? "")
.finally(() => {
isLoading.value = false;
});
} else {
toValue(client)
?.unfollowAccount(toValue(accountId) ?? "")
.finally(() => {
isLoading.value = false;
});
}
}
// FIXME: Add more relationship changes
}
});
return { relationship, isLoading };
};