Просмотр исходного кода

replace isAdmin with usersAdminHooks

WNomunomu 2 лет назад
Родитель
Сommit
a7ef852797
2 измененных файлов с 15 добавлено и 11 удалено
  1. 3 11
      apps/app/src/components/Sidebar/SidebarNav.tsx
  2. 12 0
      apps/app/src/stores/context.tsx

+ 3 - 11
apps/app/src/components/Sidebar/SidebarNav.tsx

@@ -1,12 +1,12 @@
 import React, {
-  FC, memo, useCallback, useEffect, useState,
+  FC, memo, useCallback,
 } from 'react';
 
 import Link from 'next/link';
 
 import { useUserUISettings } from '~/client/services/user-ui-settings';
 import { SidebarContentsType } from '~/interfaces/ui';
-import { useCurrentUser } from '~/stores/context';
+import { useIsAdmin } from '~/stores/context';
 import { useCurrentSidebarContents } from '~/stores/ui';
 
 import styles from './SidebarNav.module.scss';
@@ -82,17 +82,9 @@ type Props = {
 }
 
 export const SidebarNav: FC<Props> = (props: Props) => {
-
-  const { data: currentUser } = useCurrentUser();
-
-  const [isAdmin, setAdmin] = useState(false);
-
+  const { data: isAdmin } = useIsAdmin();
   const { onItemSelected } = props;
 
-  useEffect(() => {
-    setAdmin(currentUser?.admin === true);
-  }, [currentUser?.admin]);
-
   return (
     <div className={`grw-sidebar-nav ${styles['grw-sidebar-nav']}`} data-vrt-blackout-sidebar-nav>
       <div className="grw-sidebar-nav-primary-container">

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

@@ -229,6 +229,18 @@ export const useIsReadOnlyUser = (): SWRResponse<boolean, Error> => {
   );
 };
 
+export const useIsAdmin = (): SWRResponse<boolean, Error> => {
+  const { data: currentUser, isLoading } = useCurrentUser();
+
+  const isAdminUser = currentUser?.admin !== undefined ? currentUser.admin : false;
+
+  return useSWRImmutable(
+    isLoading ? null : ['isAdminUser', currentUser?._id],
+    () => isAdminUser,
+    { fallbackData: isAdminUser },
+  );
+};
+
 export const useIsEditable = (): SWRResponse<boolean, Error> => {
   const { data: isGuestUser } = useIsGuestUser();
   const { data: isReadOnlyUser } = useIsReadOnlyUser();