editor.tsx 3.1 KB

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