editor.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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, useCurrentPagePath,
  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 = (): SWRResponse<Nullable<string[]>, Error> => {
  63. const { data: currentPagePath } = useCurrentPagePath();
  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 useIsSlackEnabledBydefault = (): SWRResponse<boolean, Error> => {
  72. const { data: slackChannelsData } = useSWRxSlackChannels();
  73. const isSlackEnabledByDefault = (slackChannelsData != null && slackChannelsData.length > 0) || false;
  74. return useSWR(
  75. ['isSlackEnabledByDefault', isSlackEnabledByDefault],
  76. (key: string, isSlackEnabledByDefault: boolean) => isSlackEnabledByDefault,
  77. );
  78. };
  79. export const useIsSlackEnabled = (): SWRResponse<boolean, Error> => {
  80. const { data: isSlackEnabledByDefault } = useIsSlackEnabledBydefault();
  81. return useStaticSWR(
  82. 'isSlackEnabled',
  83. undefined,
  84. { fallbackData: isSlackEnabledByDefault },
  85. );
  86. };