editor.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import useSWR, { SWRResponse } from 'swr';
  2. import useSWRImmutable from 'swr/immutable';
  3. import { apiGet } from '~/client/util/apiv1-client';
  4. import { apiv3Get, apiv3Put } from '~/client/util/apiv3-client';
  5. import { Nullable } from '~/interfaces/common';
  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 = (): SWRResponse<IEditorSettings, Error> & EditorSettingsOperation => {
  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 {
  27. ...swrResult,
  28. update: (updateData) => {
  29. const { data, mutate } = swrResult;
  30. if (data == null) {
  31. return;
  32. }
  33. mutate({ ...data, ...updateData }, false);
  34. // invoke API
  35. apiv3Put('/personal-setting/editor-settings', updateData);
  36. },
  37. turnOffAskingBeforeDownloadLargeFiles: async() => {
  38. const { data, mutate } = swrResult;
  39. if (data == null) {
  40. return;
  41. }
  42. // invoke API
  43. await apiv3Put('/personal-setting/editor-settings', { textlintSettings: { neverAskBeforeDownloadLargeFiles: true } });
  44. // revalidate
  45. mutate();
  46. },
  47. };
  48. };
  49. export const useIsTextlintEnabled = (): SWRResponse<boolean, Error> => {
  50. return useStaticSWR<boolean, Error>('isTextlintEnabled', undefined, { fallbackData: false });
  51. };
  52. export const useCurrentIndentSize = (): SWRResponse<number, Error> => {
  53. const { data: defaultIndentSize } = useDefaultIndentSize();
  54. return useStaticSWR<number, Error>(
  55. defaultIndentSize == null ? null : 'currentIndentSize',
  56. undefined,
  57. { fallbackData: defaultIndentSize },
  58. );
  59. };
  60. /*
  61. * Slack Notification
  62. */
  63. export const useSWRxSlackChannels = (currentPagePath: Nullable<string>): SWRResponse<string[], Error> => {
  64. const shouldFetch: boolean = currentPagePath != null;
  65. return useSWR(
  66. shouldFetch ? ['/pages.updatePost', currentPagePath] : null,
  67. (endpoint, path) => apiGet(endpoint, { path }).then((response: SlackChannels) => response.updatePost),
  68. { fallbackData: [''] },
  69. );
  70. };
  71. export const useIsSlackEnabled = (): SWRResponse<boolean, Error> => {
  72. return useStaticSWR(
  73. 'isSlackEnabled',
  74. undefined,
  75. { fallbackData: false },
  76. );
  77. };
  78. export type IPageTagsForEditorsOption = {
  79. sync: (tags?: string[]) => void;
  80. }
  81. export const usePageTagsForEditors = (pageId: Nullable<string>): SWRResponse<string[], Error> & IPageTagsForEditorsOption => {
  82. const { data: tagsInfoData } = useSWRxTagsInfo(pageId);
  83. const swrResult = useStaticSWR<string[], Error>('pageTags', undefined);
  84. return {
  85. ...swrResult,
  86. sync: (): void => {
  87. const { mutate } = swrResult;
  88. mutate(tagsInfoData?.tags || [], false);
  89. },
  90. };
  91. };