editor.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { useCallback, useEffect } from 'react';
  2. import type { Nullable } from '@growi/core';
  3. import {
  4. type SWRResponseWithUtils,
  5. useSWRStatic,
  6. withUtils,
  7. } from '@growi/core/dist/swr';
  8. import type { EditorSettings } from '@growi/editor';
  9. import useSWR, { type SWRResponse } from 'swr';
  10. import useSWRImmutable from 'swr/immutable';
  11. import { apiGet } from '~/client/util/apiv1-client';
  12. import { apiv3Get, apiv3Put } from '~/client/util/apiv3-client';
  13. import type { SlackChannels } from '~/interfaces/user-trigger-notification';
  14. import {
  15. useCurrentUser,
  16. useDefaultIndentSize,
  17. useIsGuestUser,
  18. useIsReadOnlyUser,
  19. } from '~/stores-universal/context';
  20. // import { localStorageMiddleware } from './middlewares/sync-to-storage';
  21. import { useSWRxTagsInfo } from './page';
  22. export const useWaitingSaveProcessing = (): SWRResponse<boolean, Error> => {
  23. return useSWRStatic('waitingSaveProcessing', undefined, {
  24. fallbackData: false,
  25. });
  26. };
  27. export const useEditingMarkdown = (
  28. initialData?: string,
  29. ): SWRResponse<string, Error> => {
  30. return useSWRStatic('editingMarkdown', initialData);
  31. };
  32. type EditorSettingsOperation = {
  33. update: (updateData: Partial<EditorSettings>) => Promise<void>;
  34. };
  35. // TODO: Enable localStorageMiddleware
  36. // - Unabling localStorageMiddleware occurrs a flickering problem when loading theme.
  37. // - see: https://github.com/growilabs/growi/pull/6781#discussion_r1000285786
  38. export const useEditorSettings = (): SWRResponseWithUtils<
  39. EditorSettingsOperation,
  40. EditorSettings,
  41. Error
  42. > => {
  43. const { data: currentUser } = useCurrentUser();
  44. const { data: isGuestUser } = useIsGuestUser();
  45. const { data: isReadOnlyUser } = useIsReadOnlyUser();
  46. const swrResult = useSWRImmutable(
  47. isGuestUser || isReadOnlyUser
  48. ? null
  49. : ['/personal-setting/editor-settings', currentUser?.username],
  50. ([endpoint]) => {
  51. return apiv3Get(endpoint).then((result) => result.data);
  52. },
  53. {
  54. // use: [localStorageMiddleware], // store to localStorage for initialization fastly
  55. // fallbackData: undefined,
  56. },
  57. );
  58. return withUtils<EditorSettingsOperation, EditorSettings, Error>(swrResult, {
  59. update: async (updateData) => {
  60. const { data, mutate } = swrResult;
  61. if (data == null) {
  62. return;
  63. }
  64. mutate({ ...data, ...updateData }, false);
  65. // invoke API
  66. await apiv3Put('/personal-setting/editor-settings', updateData);
  67. },
  68. });
  69. };
  70. export const useCurrentIndentSize = (): SWRResponse<number, Error> => {
  71. const { data: defaultIndentSize } = useDefaultIndentSize();
  72. return useSWRStatic<number, Error>(
  73. defaultIndentSize == null ? null : 'currentIndentSize',
  74. undefined,
  75. { fallbackData: defaultIndentSize },
  76. );
  77. };
  78. /*
  79. * Slack Notification
  80. */
  81. export const useSWRxSlackChannels = (
  82. currentPagePath: Nullable<string>,
  83. ): SWRResponse<string[], Error> => {
  84. const shouldFetch: boolean = currentPagePath != null;
  85. return useSWR(
  86. shouldFetch ? ['/pages.updatePost', currentPagePath] : null,
  87. ([endpoint, path]) =>
  88. apiGet(endpoint, { path }).then(
  89. (response: SlackChannels) => response.updatePost,
  90. ),
  91. {
  92. revalidateOnFocus: false,
  93. fallbackData: [''],
  94. },
  95. );
  96. };
  97. export const useIsSlackEnabled = (): SWRResponse<boolean, Error> => {
  98. return useSWRStatic('isSlackEnabled', undefined, { fallbackData: false });
  99. };
  100. export type IPageTagsForEditorsOption = {
  101. sync: (tags?: string[]) => void;
  102. };
  103. export const usePageTagsForEditors = (
  104. pageId: Nullable<string>,
  105. ): SWRResponse<string[], Error> & IPageTagsForEditorsOption => {
  106. const { data: tagsInfoData } = useSWRxTagsInfo(pageId);
  107. const swrResult = useSWRStatic<string[], Error>('pageTags', undefined);
  108. const { mutate } = swrResult;
  109. const sync = useCallback((): void => {
  110. mutate(tagsInfoData?.tags || [], false);
  111. }, [mutate, tagsInfoData?.tags]);
  112. return {
  113. ...swrResult,
  114. sync,
  115. };
  116. };
  117. export const useIsEnabledUnsavedWarning = (): SWRResponse<boolean, Error> => {
  118. return useSWRStatic<boolean, Error>('isEnabledUnsavedWarning');
  119. };
  120. export const useReservedNextCaretLine = (
  121. initialData?: number,
  122. ): SWRResponse<number> => {
  123. const swrResponse = useSWRStatic('saveNextCaretLine', initialData, {
  124. fallbackData: 0,
  125. });
  126. const { mutate } = swrResponse;
  127. useEffect(() => {
  128. const handler = (lineNumber: number) => {
  129. mutate(lineNumber);
  130. };
  131. globalEmitter.on('reservedNextCaretLine', handler);
  132. return function cleanup() {
  133. globalEmitter.removeListener('reservedNextCaretLine', handler);
  134. };
  135. }, [mutate]);
  136. return {
  137. ...swrResponse,
  138. };
  139. };