context.ts 3.0 KB

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