editor.tsx 4.2 KB

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