editor.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { useCallback } from 'react';
  2. import { Nullable, withUtils, type SWRResponseWithUtils } from '@growi/core';
  3. import { SWRResponse } from 'swr';
  4. import useSWRImmutable from 'swr/immutable';
  5. import { apiGet } from '~/client/util/apiv1-client';
  6. import { apiv3Get, apiv3Put } from '~/client/util/apiv3-client';
  7. import { IEditorSettings } from '~/interfaces/editor-settings';
  8. import { SlackChannels } from '~/interfaces/user-trigger-notification';
  9. import {
  10. useCurrentUser, useDefaultIndentSize, useIsGuestUser, useIsReadOnlyUser,
  11. } from './context';
  12. // import { localStorageMiddleware } from './middlewares/sync-to-storage';
  13. import { useSWRxTagsInfo } from './page';
  14. import { useStaticSWR } from './use-static-swr';
  15. export const useWaitingSaveProcessing = (): SWRResponse<boolean, Error> => {
  16. return useStaticSWR('waitingSaveProcessing', undefined, { fallbackData: false });
  17. };
  18. export const useEditingMarkdown = (initialData?: string): SWRResponse<string, Error> => {
  19. return useStaticSWR('editingMarkdown', initialData);
  20. };
  21. type EditorSettingsOperation = {
  22. update: (updateData: Partial<IEditorSettings>) => Promise<void>,
  23. }
  24. // TODO: Enable localStorageMiddleware
  25. // - Unabling localStorageMiddleware occurrs a flickering problem when loading theme.
  26. // - see: https://github.com/weseek/growi/pull/6781#discussion_r1000285786
  27. export const useEditorSettings = (): SWRResponseWithUtils<EditorSettingsOperation, IEditorSettings, Error> => {
  28. const { data: currentUser } = useCurrentUser();
  29. const { data: isGuestUser } = useIsGuestUser();
  30. const { data: isReadOnlyUser } = useIsReadOnlyUser();
  31. const swrResult = useSWRImmutable(
  32. (isGuestUser || isReadOnlyUser) ? null : ['/personal-setting/editor-settings', currentUser?.username],
  33. ([endpoint]) => apiv3Get(endpoint).then(result => result.data),
  34. {
  35. // use: [localStorageMiddleware], // store to localStorage for initialization fastly
  36. // fallbackData: undefined,
  37. },
  38. );
  39. return withUtils<EditorSettingsOperation, IEditorSettings, Error>(swrResult, {
  40. update: async(updateData) => {
  41. const { data, mutate } = swrResult;
  42. if (data == null) {
  43. return;
  44. }
  45. mutate({ ...data, ...updateData }, false);
  46. // invoke API
  47. await apiv3Put('/personal-setting/editor-settings', updateData);
  48. },
  49. });
  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 useSWRImmutable(
  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. };
  77. export type IPageTagsForEditorsOption = {
  78. sync: (tags?: string[]) => void;
  79. }
  80. export const usePageTagsForEditors = (pageId: Nullable<string>): SWRResponse<string[], Error> & IPageTagsForEditorsOption => {
  81. const { data: tagsInfoData } = useSWRxTagsInfo(pageId);
  82. const swrResult = useStaticSWR<string[], Error>('pageTags', undefined);
  83. const { mutate } = swrResult;
  84. const sync = useCallback((): void => {
  85. mutate(tagsInfoData?.tags || [], false);
  86. }, [mutate, tagsInfoData?.tags]);
  87. return {
  88. ...swrResult,
  89. sync,
  90. };
  91. };
  92. export const useIsEnabledUnsavedWarning = (): SWRResponse<boolean, Error> => {
  93. return useStaticSWR<boolean, Error>('isEnabledUnsavedWarning');
  94. };
  95. export const useIsConflict = (): SWRResponse<boolean, Error> => {
  96. return useStaticSWR<boolean, Error>('isConflict', undefined, { fallbackData: false });
  97. };