editor.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. type EditorSettingsOperation = {
  15. update: (updateData: Partial<IEditorSettings>) => void,
  16. turnOffAskingBeforeDownloadLargeFiles: () => void,
  17. }
  18. export const useEditorSettings = (): SWRResponseWithUtils<EditorSettingsOperation, IEditorSettings, Error> => {
  19. const { data: currentUser } = useCurrentUser();
  20. const { data: isGuestUser } = useIsGuestUser();
  21. const swrResult = useSWRImmutable<IEditorSettings>(
  22. isGuestUser ? null : ['/personal-setting/editor-settings', currentUser?.username],
  23. endpoint => apiv3Get(endpoint).then(result => result.data),
  24. { use: [localStorageMiddleware] }, // store to localStorage for initialization fastly
  25. );
  26. return withUtils<EditorSettingsOperation, IEditorSettings, Error>(swrResult, {
  27. update: (updateData) => {
  28. const { data, mutate } = swrResult;
  29. if (data == null) {
  30. return;
  31. }
  32. mutate({ ...data, ...updateData }, false);
  33. // invoke API
  34. apiv3Put('/personal-setting/editor-settings', updateData);
  35. },
  36. turnOffAskingBeforeDownloadLargeFiles: async() => {
  37. const { data, mutate } = swrResult;
  38. if (data == null) {
  39. return;
  40. }
  41. // invoke API
  42. await apiv3Put('/personal-setting/editor-settings', { textlintSettings: { neverAskBeforeDownloadLargeFiles: true } });
  43. // revalidate
  44. mutate();
  45. },
  46. });
  47. };
  48. export const useIsTextlintEnabled = (): SWRResponse<boolean, Error> => {
  49. return useStaticSWR<boolean, Error>('isTextlintEnabled', undefined, { fallbackData: false });
  50. };
  51. export const useCurrentIndentSize = (): SWRResponse<number, Error> => {
  52. const { data: defaultIndentSize } = useDefaultIndentSize();
  53. return useStaticSWR<number, Error>(
  54. defaultIndentSize == null ? null : 'currentIndentSize',
  55. undefined,
  56. { fallbackData: defaultIndentSize },
  57. );
  58. };
  59. /*
  60. * Slack Notification
  61. */
  62. export const useSWRxSlackChannels = (currentPagePath: Nullable<string>): SWRResponse<string[], Error> => {
  63. const shouldFetch: boolean = currentPagePath != null;
  64. return useSWR(
  65. shouldFetch ? ['/pages.updatePost', currentPagePath] : null,
  66. (endpoint, path) => apiGet(endpoint, { path }).then((response: SlackChannels) => response.updatePost),
  67. { fallbackData: [''] },
  68. );
  69. };
  70. export const useIsSlackEnabled = (): SWRResponse<boolean, Error> => {
  71. return useStaticSWR(
  72. 'isSlackEnabled',
  73. undefined,
  74. { fallbackData: false },
  75. );
  76. };
  77. export type IPageTagsForEditorsOption = {
  78. sync: (tags?: string[]) => void;
  79. }
  80. export const usePageTagsForEditors = (pageId: Nullable<string>): SWRResponse<string[], Error> & IPageTagsForEditorsOption => {
  81. const { data: tagsInfoData } = useSWRxTagsInfo(pageId);
  82. const swrResult = useStaticSWR<string[], Error>('pageTags', undefined);
  83. return {
  84. ...swrResult,
  85. sync: (): void => {
  86. const { mutate } = swrResult;
  87. mutate(tagsInfoData?.tags || [], false);
  88. },
  89. };
  90. };
  91. export const useIsEnabledUnsavedWarning = (): SWRResponse<boolean, Error> => {
  92. return useStaticSWR<boolean, Error>('isEnabledUnsavedWarning', undefined, { fallbackData: false });
  93. };