context.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { atom, useAtomValue, useSetAtom } from 'jotai';
  2. import { currentUserAtomGetter, growiCloudUriAtomGetter } from './global';
  3. /**
  4. * Computed atom for checking if current user is a guest user
  5. * Depends on currentUser atom
  6. */
  7. const isGuestUserAtom = atom((get) => {
  8. const currentUser = get(currentUserAtomGetter);
  9. return currentUser?._id == null;
  10. });
  11. // export const useIsGuestUser = () => {
  12. // return useAtom(isGuestUserAtom);
  13. // };
  14. export const useIsGuestUser = () => useAtomValue(isGuestUserAtom);
  15. /**
  16. * Computed atom for checking if current user is a read-only user
  17. * Depends on currentUser and isGuestUser atoms
  18. */
  19. const isReadOnlyUserAtom = atom((get) => {
  20. const currentUser = get(currentUserAtomGetter);
  21. const isGuestUser = get(isGuestUserAtom);
  22. return !isGuestUser && !!currentUser?.readOnly;
  23. });
  24. export const useIsReadOnlyUser = () => useAtomValue(isReadOnlyUserAtom);
  25. /**
  26. * Computed atom for checking if current user is an admin
  27. * Depends on currentUser atom
  28. */
  29. const isAdminAtom = atom((get) => {
  30. const currentUser = get(currentUserAtomGetter);
  31. return currentUser?.admin ?? false;
  32. });
  33. export const useIsAdmin = () => useAtomValue(isAdminAtom);
  34. /**
  35. * Atom for checking if current user is a shared user
  36. */
  37. const isSharedUserAtom = atom<boolean>(false);
  38. export const useIsSharedUser = () => useAtomValue(isSharedUserAtom);
  39. /**
  40. * Atom for checking if current page is a search page
  41. */
  42. const isSearchPageAtom = atom<boolean | null>(null);
  43. /**
  44. * Hook for getting the current search page state
  45. */
  46. export const useIsSearchPage = () => useAtomValue(isSearchPageAtom);
  47. /**
  48. * Hook for setting the current search page state
  49. */
  50. export const useSetSearchPage = () => useSetAtom(isSearchPageAtom);
  51. /**
  52. * Computed atom for GROWI documentation URL
  53. * Depends on growiCloudUri atom
  54. */
  55. const growiDocumentationUrlAtom = atom((get) => {
  56. const growiCloudUri = get(growiCloudUriAtomGetter);
  57. if (growiCloudUri != null) {
  58. const url = new URL('/help', growiCloudUri);
  59. return url.toString();
  60. }
  61. return 'https://docs.growi.org';
  62. });
  63. export const useGrowiDocumentationUrl = () =>
  64. useAtomValue(growiDocumentationUrlAtom);