el.xwx.moe/hooks/useCollectivePermissions.ts

36 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-02-11 00:19:59 -06:00
import useAccountStore from "@/store/account";
import { Member } from "@/types/global";
import { useEffect, useState } from "react";
2024-07-30 13:57:09 -05:00
import { useCollections } from "./store/collections";
2024-02-11 00:19:59 -06:00
export default function useCollectivePermissions(collectionIds: number[]) {
2024-07-30 13:57:09 -05:00
const { data: { response: collections } = { response: [] } } =
useCollections();
2024-02-11 00:19:59 -06:00
2024-02-11 01:29:11 -06:00
const { account } = useAccountStore();
2024-02-11 00:19:59 -06:00
2024-02-11 01:29:11 -06:00
const [permissions, setPermissions] = useState<Member | true>();
useEffect(() => {
for (const collectionId of collectionIds) {
const collection = collections.find((e) => e.id === collectionId);
2024-02-11 00:19:59 -06:00
2024-02-11 01:29:11 -06:00
if (collection) {
let getPermission: Member | undefined = collection.members.find(
(e) => e.userId === account.id
);
2024-02-11 00:19:59 -06:00
2024-02-11 01:29:11 -06:00
if (
getPermission?.canCreate === false &&
getPermission?.canUpdate === false &&
getPermission?.canDelete === false
)
getPermission = undefined;
2024-02-11 00:19:59 -06:00
2024-02-11 01:29:11 -06:00
setPermissions(account.id === collection.ownerId || getPermission);
}
}
}, [account, collections, collectionIds]);
2024-02-11 00:19:59 -06:00
2024-02-11 01:29:11 -06:00
return permissions;
2024-02-11 00:19:59 -06:00
}