editor.tsx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 { useStaticSWR } from './use-static-swr';
  13. type EditorSettingsOperation = {
  14. update: (updateData: Partial<IEditorSettings>) => void,
  15. turnOffAskingBeforeDownloadLargeFiles: () => void,
  16. }
  17. export const useEditorSettings = (): SWRResponse<IEditorSettings, Error> & EditorSettingsOperation => {
  18. const { data: currentUser } = useCurrentUser();
  19. const { data: isGuestUser } = useIsGuestUser();
  20. const swrResult = useSWRImmutable<IEditorSettings>(
  21. isGuestUser ? null : ['/personal-setting/editor-settings', currentUser?.username],
  22. endpoint => apiv3Get(endpoint).then(result => result.data),
  23. { use: [localStorageMiddleware] }, // store to localStorage for initialization fastly
  24. );
  25. return {
  26. ...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. };