editor.tsx 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 { IResGetPageTags } from '~/interfaces/tag';
  8. import { SlackChannels } from '~/interfaces/user-trigger-notification';
  9. import { useSWRxPageTags } from '~/stores/tag';
  10. import {
  11. useCurrentUser, useDefaultIndentSize, useIsGuestUser,
  12. } from './context';
  13. import { localStorageMiddleware } from './middlewares/sync-to-storage';
  14. import { useStaticSWR } from './use-static-swr';
  15. type EditorSettingsOperation = {
  16. update: (updateData: Partial<IEditorSettings>) => void,
  17. turnOffAskingBeforeDownloadLargeFiles: () => void,
  18. }
  19. export const useEditorSettings = (): SWRResponse<IEditorSettings, Error> & EditorSettingsOperation => {
  20. const { data: currentUser } = useCurrentUser();
  21. const { data: isGuestUser } = useIsGuestUser();
  22. const swrResult = useSWRImmutable<IEditorSettings>(
  23. isGuestUser ? null : ['/personal-setting/editor-settings', currentUser?.username],
  24. endpoint => apiv3Get(endpoint).then(result => result.data),
  25. { use: [localStorageMiddleware] }, // store to localStorage for initialization fastly
  26. );
  27. return {
  28. ...swrResult,
  29. update: (updateData) => {
  30. const { data, mutate } = swrResult;
  31. if (data == null) {
  32. return;
  33. }
  34. mutate({ ...data, ...updateData }, false);
  35. // invoke API
  36. apiv3Put('/personal-setting/editor-settings', updateData);
  37. },
  38. turnOffAskingBeforeDownloadLargeFiles: async() => {
  39. const { data, mutate } = swrResult;
  40. if (data == null) {
  41. return;
  42. }
  43. // invoke API
  44. await apiv3Put('/personal-setting/editor-settings', { textlintSettings: { neverAskBeforeDownloadLargeFiles: true } });
  45. // revalidate
  46. mutate();
  47. },
  48. };
  49. };
  50. export const useIsTextlintEnabled = (): SWRResponse<boolean, Error> => {
  51. return useStaticSWR<boolean, Error>('isTextlintEnabled', undefined, { fallbackData: false });
  52. };
  53. export const useCurrentIndentSize = (): SWRResponse<number, Error> => {
  54. const { data: defaultIndentSize } = useDefaultIndentSize();
  55. return useStaticSWR<number, Error>(
  56. defaultIndentSize == null ? null : 'currentIndentSize',
  57. undefined,
  58. { fallbackData: defaultIndentSize },
  59. );
  60. };
  61. /*
  62. * Slack Notification
  63. */
  64. export const useSWRxSlackChannels = (currentPagePath: Nullable<string>): SWRResponse<string[], Error> => {
  65. const shouldFetch: boolean = currentPagePath != null;
  66. return useSWR(
  67. shouldFetch ? ['/pages.updatePost', currentPagePath] : null,
  68. (endpoint, path) => apiGet(endpoint, { path }).then((response: SlackChannels) => response.updatePost),
  69. { fallbackData: [''] },
  70. );
  71. };
  72. export const useIsSlackEnabled = (): SWRResponse<boolean, Error> => {
  73. return useStaticSWR(
  74. 'isSlackEnabled',
  75. undefined,
  76. { fallbackData: false },
  77. );
  78. };