Procházet zdrojové kódy

Merge pull request #7612 from weseek/feat/120699-121329-create-use-is-read-only-user

feat: Create useIsReadOnlyUser hooks
Ryoji Shimizu před 2 roky
rodič
revize
0a14fe1a58
1 změnil soubory, kde provedl 14 přidání a 0 odebrání
  1. 14 0
      apps/app/src/stores/context.tsx

+ 14 - 0
apps/app/src/stores/context.tsx

@@ -215,6 +215,20 @@ export const useIsGuestUser = (): SWRResponse<boolean, Error> => {
   );
 };
 
+export const useIsReadOnlyUser = (): SWRResponse<boolean, Error> => {
+  const { data: currentUser, isLoading: isCurrentUserLoading } = useCurrentUser();
+  const { data: isGuestUser, isLoading: isGuestUserLoding } = useIsGuestUser();
+
+  const isLoading = isCurrentUserLoading || isGuestUserLoding;
+  const isReadOnlyUser = !isGuestUser && !!currentUser?.readOnly;
+
+  return useSWRImmutable(
+    isLoading ? null : ['isReadOnlyUser', isReadOnlyUser, currentUser?._id],
+    () => isReadOnlyUser,
+    { fallbackData: isReadOnlyUser },
+  );
+};
+
 export const useIsEditable = (): SWRResponse<boolean, Error> => {
   const { data: isGuestUser } = useIsGuestUser();
   const { data: isForbidden } = useIsForbidden();