editor.tsx 4.3 KB

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