context.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { atom, useAtomValue } from 'jotai';
  2. import { currentUserAtomGetter, growiCloudUriAtomGetter } from './global';
  3. /**
  4. * Atom for checking if current path is identical
  5. */
  6. const isIdenticalPathAtom = atom<boolean>(false);
  7. export const useIsIdenticalPath = () => useAtomValue(isIdenticalPathAtom);
  8. /**
  9. * Atom for checking if current page is forbidden
  10. */
  11. const isForbiddenAtom = atom<boolean>(false);
  12. export const useIsForbidden = () => useAtomValue(isForbiddenAtom);
  13. /**
  14. * Atom for checking if current page is not creatable
  15. */
  16. const isNotCreatableAtom = atom<boolean>(false);
  17. export const useIsNotCreatable = () => useAtomValue(isNotCreatableAtom);
  18. /**
  19. * Computed atom for checking if current user is a guest user
  20. * Depends on currentUser atom
  21. */
  22. const isGuestUserAtom = atom((get) => {
  23. const currentUser = get(currentUserAtomGetter);
  24. return currentUser?._id == null;
  25. });
  26. // export const useIsGuestUser = () => {
  27. // return useAtom(isGuestUserAtom);
  28. // };
  29. export const useIsGuestUser = () => useAtomValue(isGuestUserAtom);
  30. /**
  31. * Computed atom for checking if current user is a read-only user
  32. * Depends on currentUser and isGuestUser atoms
  33. */
  34. const isReadOnlyUserAtom = atom((get) => {
  35. const currentUser = get(currentUserAtomGetter);
  36. const isGuestUser = get(isGuestUserAtom);
  37. return !isGuestUser && !!currentUser?.readOnly;
  38. });
  39. export const useIsReadOnlyUser = () => useAtomValue(isReadOnlyUserAtom);
  40. /**
  41. * Computed atom for checking if current user is an admin
  42. * Depends on currentUser atom
  43. */
  44. const isAdminAtom = atom((get) => {
  45. const currentUser = get(currentUserAtomGetter);
  46. return currentUser?.admin ?? false;
  47. });
  48. export const useIsAdmin = () => useAtomValue(isAdminAtom);
  49. /**
  50. * Atom for checking if current user is a shared user
  51. */
  52. const isSharedUserAtom = atom<boolean>(false);
  53. export const useIsSharedUser = () => useAtomValue(isSharedUserAtom);
  54. /**
  55. * Atom for checking if current page is a search page
  56. */
  57. const isSearchPageAtom = atom<boolean | null>(null);
  58. export const useIsSearchPage = () => useAtomValue(isSearchPageAtom);
  59. /**
  60. * Computed atom for GROWI documentation URL
  61. * Depends on growiCloudUri atom
  62. */
  63. const growiDocumentationUrlAtom = atom((get) => {
  64. const growiCloudUri = get(growiCloudUriAtomGetter);
  65. if (growiCloudUri != null) {
  66. const url = new URL('/help', growiCloudUri);
  67. return url.toString();
  68. }
  69. return 'https://docs.growi.org';
  70. });
  71. export const useGrowiDocumentationUrl = () =>
  72. useAtomValue(growiDocumentationUrlAtom);
  73. /**
  74. * Computed atom for checking if current page is editable
  75. * Depends on multiple atoms: isGuestUser, isReadOnlyUser, isForbidden, isNotCreatable, isIdenticalPath
  76. */
  77. const isEditableAtom = atom((get) => {
  78. const isGuestUser = get(isGuestUserAtom);
  79. const isReadOnlyUser = get(isReadOnlyUserAtom);
  80. const isForbidden = get(isForbiddenAtom);
  81. const isNotCreatable = get(isNotCreatableAtom);
  82. const isIdenticalPath = get(isIdenticalPathAtom);
  83. return (
  84. !isForbidden &&
  85. !isIdenticalPath &&
  86. !isNotCreatable &&
  87. !isGuestUser &&
  88. !isReadOnlyUser
  89. );
  90. });
  91. export const useIsEditable = () => useAtomValue(isEditableAtom);