editor.tsx 4.3 KB

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