el.xwx.moe/hooks/useCollectivePermissions.ts

35 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-02-11 00:19:59 -06:00
import { Member } from "@/types/global";
import { useEffect, useState } from "react";
2024-07-30 13:57:09 -05:00
import { useCollections } from "./store/collections";
2024-07-31 13:15:50 -05:00
import { useUser } from "./store/user";
2024-02-11 00:19:59 -06:00
export default function useCollectivePermissions(collectionIds: number[]) {
2024-08-01 17:40:08 -05:00
const { data: collections } = useCollections();
2024-02-11 00:19:59 -06:00
2024-08-01 17:40:08 -05:00
const { data: user } = useUser();
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 === user.id
2024-02-11 01:29:11 -06:00
);
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
setPermissions(user.id === collection.ownerId || getPermission);
2024-02-11 01:29:11 -06:00
}
}
}, [user, 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
}