import { atom, useAtom } from 'jotai'; import { currentUserAtom, growiCloudUriAtom } from './global'; import type { UseAtom } from './ui/helper'; /** * Atom for checking if current path is identical */ const isIdenticalPathAtom = atom(false); export const useIsIdenticalPath = (): UseAtom => { return useAtom(isIdenticalPathAtom); }; /** * Atom for checking if current page is forbidden */ const isForbiddenAtom = atom(false); export const useIsForbidden = (): UseAtom => { return useAtom(isForbiddenAtom); }; /** * Atom for checking if current page is not creatable */ const isNotCreatableAtom = atom(false); export const useIsNotCreatable = (): UseAtom => { return useAtom(isNotCreatableAtom); }; /** * Computed atom for checking if current user is a guest user * Depends on currentUser atom */ const isGuestUserAtom = atom((get) => { const currentUser = get(currentUserAtom); return currentUser?._id == null; }); export const useIsGuestUser = (): UseAtom => { return useAtom(isGuestUserAtom); }; /** * Computed atom for checking if current user is a read-only user * Depends on currentUser and isGuestUser atoms */ const isReadOnlyUserAtom = atom((get) => { const currentUser = get(currentUserAtom); const isGuestUser = get(isGuestUserAtom); return !isGuestUser && !!currentUser?.readOnly; }); export const useIsReadOnlyUser = (): UseAtom => { return useAtom(isReadOnlyUserAtom); }; /** * Computed atom for checking if current user is an admin * Depends on currentUser atom */ const isAdminAtom = atom((get) => { const currentUser = get(currentUserAtom); return currentUser?.admin ?? false; }); export const useIsAdmin = (): UseAtom => { return useAtom(isAdminAtom); }; /** * Computed atom for GROWI documentation URL * Depends on growiCloudUri atom */ const growiDocumentationUrlAtom = atom((get) => { const growiCloudUri = get(growiCloudUriAtom); if (growiCloudUri != null) { const url = new URL('/help', growiCloudUri); return url.toString(); } return 'https://docs.growi.org'; }); export const useGrowiDocumentationUrl = (): UseAtom => { return useAtom(growiDocumentationUrlAtom); }; /** * Computed atom for checking if current page is editable * Depends on multiple atoms: isGuestUser, isReadOnlyUser, isForbidden, isNotCreatable, isIdenticalPath */ const isEditableAtom = atom((get) => { const isGuestUser = get(isGuestUserAtom); const isReadOnlyUser = get(isReadOnlyUserAtom); const isForbidden = get(isForbiddenAtom); const isNotCreatable = get(isNotCreatableAtom); const isIdenticalPath = get(isIdenticalPathAtom); return (!isForbidden && !isIdenticalPath && !isNotCreatable && !isGuestUser && !isReadOnlyUser); }); export const useIsEditable = (): UseAtom => { return useAtom(isEditableAtom); };