2025-05-26 11:19:15 +02:00
|
|
|
import type { Relationship } from "@versia/client/schemas";
|
|
|
|
|
import type { z } from "zod";
|
2024-04-29 22:22:44 +02:00
|
|
|
|
2025-08-28 07:41:51 +02:00
|
|
|
export const useRelationship = (accountId: MaybeRef<string | null>) => {
|
2025-05-26 11:19:15 +02:00
|
|
|
const relationship = ref(null as z.infer<typeof Relationship> | null);
|
2024-04-29 22:22:44 +02:00
|
|
|
const isLoading = ref(false);
|
2025-08-28 07:41:51 +02:00
|
|
|
const authStore = useAuthStore();
|
2024-04-29 22:22:44 +02:00
|
|
|
|
2025-08-28 07:41:51 +02:00
|
|
|
if (!authStore.isSignedIn) {
|
2024-04-29 22:22:44 +02:00
|
|
|
return { relationship, isLoading };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
watchEffect(() => {
|
2024-06-20 02:07:56 +02:00
|
|
|
if (toValue(accountId)) {
|
2025-08-28 07:41:51 +02:00
|
|
|
authStore.client
|
|
|
|
|
.getRelationship(toValue(accountId) ?? "")
|
2024-04-29 22:22:44 +02:00
|
|
|
.then((res) => {
|
2024-06-12 03:02:30 +02:00
|
|
|
relationship.value = res.data;
|
2024-04-29 22:22:44 +02:00
|
|
|
});
|
2024-06-20 02:07:56 +02:00
|
|
|
}
|
2024-04-29 22:22:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
watch(relationship, (newOutput, oldOutput) => {
|
|
|
|
|
if (newOutput !== oldOutput && newOutput && oldOutput) {
|
|
|
|
|
if (newOutput?.following !== oldOutput?.following) {
|
|
|
|
|
isLoading.value = true;
|
|
|
|
|
if (newOutput?.following) {
|
2025-08-28 07:41:51 +02:00
|
|
|
authStore.client
|
|
|
|
|
.followAccount(toValue(accountId) ?? "")
|
2024-04-29 22:22:44 +02:00
|
|
|
.finally(() => {
|
|
|
|
|
isLoading.value = false;
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2025-08-28 07:41:51 +02:00
|
|
|
authStore.client
|
|
|
|
|
.unfollowAccount(toValue(accountId) ?? "")
|
2024-04-29 22:22:44 +02:00
|
|
|
.finally(() => {
|
|
|
|
|
isLoading.value = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// FIXME: Add more relationship changes
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return { relationship, isLoading };
|
|
|
|
|
};
|