From 44daffbae66acb6ece6c12b3eef76bf8a5b3b917 Mon Sep 17 00:00:00 2001 From: Isaac Wise Date: Sat, 10 Feb 2024 23:40:26 -0600 Subject: [PATCH] Use memoization for permission checking --- hooks/usePermissions.tsx | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/hooks/usePermissions.tsx b/hooks/usePermissions.tsx index 746897b..2faa105 100644 --- a/hooks/usePermissions.tsx +++ b/hooks/usePermissions.tsx @@ -1,15 +1,13 @@ import useAccountStore from "@/store/account"; import useCollectionStore from "@/store/collections"; import { Member } from "@/types/global"; -import { useEffect, useState } from "react"; +import { useMemo } from "react"; export default function usePermissions(collectionId: number) { const { collections } = useCollectionStore(); - const { account } = useAccountStore(); - const [permissions, setPermissions] = useState(); - useEffect(() => { + const permissions = useMemo(() => { const collection = collections.find((e) => e.id === collectionId); if (collection) { @@ -21,12 +19,13 @@ export default function usePermissions(collectionId: number) { getPermission?.canCreate === false && getPermission?.canUpdate === false && getPermission?.canDelete === false - ) + ) { getPermission = undefined; + } - setPermissions(account.id === collection.ownerId || getPermission); + return account.id === collection.ownerId || getPermission; } }, [account, collections, collectionId]); return permissions; -} +} \ No newline at end of file