el.xwx.moe/hooks/usePermissions.tsx

33 lines
973 B
TypeScript
Raw Normal View History

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";
export default function usePermissions(collectionId: number) {
const { data: collections = [] } = useCollections();
const { data: user = {} } = useUser();
const [permissions, setPermissions] = useState<Member | true>();
useEffect(() => {
const collection = collections.find((e) => e.id === collectionId);
if (collection) {
let getPermission: Member | undefined = collection.members.find(
(e) => e.userId === user.id
);
if (
getPermission?.canCreate === false &&
getPermission?.canUpdate === false &&
getPermission?.canDelete === false
)
getPermission = undefined;
setPermissions(user.id === collection.ownerId || getPermission);
}
}, [user, collections, collectionId]);
return permissions;
}