frontend/composables/Relationship.ts

50 lines
1.6 KiB
TypeScript
Raw Normal View History

import type { LysandClient } from "@lysand-org/client";
import type { Relationship } from "@lysand-org/client/types";
import { useCurrentIdentity } from "./Identities";
export const useRelationship = (
client: MaybeRef<LysandClient | null>,
accountId: MaybeRef<string | null>,
) => {
const relationship = ref(null as Relationship | null);
const isLoading = ref(false);
if (!useCurrentIdentity().value) {
return { relationship, isLoading };
}
watchEffect(() => {
2024-06-20 02:07:56 +02:00
if (toValue(accountId)) {
toValue(client)
?.getRelationship(toValue(accountId) ?? "")
.then((res) => {
relationship.value = res.data;
});
2024-06-20 02:07:56 +02:00
}
});
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 };
};