Просмотр исходного кода

migrate useAcceptedUploadFileType

Yuki Takei 6 месяцев назад
Родитель
Сommit
b03c3f882e

+ 2 - 3
apps/app/src/client/components/PageComment/CommentEditor.tsx

@@ -20,9 +20,8 @@ import { uploadAttachments } from '~/client/services/upload-attachments';
 import { toastError } from '~/client/util/toastr';
 import { useCurrentUser } from '~/states/global';
 import { useCurrentPagePath } from '~/states/page';
-import { isSlackConfiguredAtom } from '~/states/server-configurations';
+import { isSlackConfiguredAtom, useAcceptedUploadFileType } from '~/states/server-configurations';
 import { useCommentEditorsDirtyMap } from '~/states/ui/unsaved-warning';
-import { useAcceptedUploadFileType } from '~/stores-universal/context';
 import { useNextThemes } from '~/stores-universal/use-next-themes';
 import { useSWRxPageComment } from '~/stores/comment';
 import {
@@ -81,7 +80,7 @@ export const CommentEditor = (props: CommentEditorProps): JSX.Element => {
   const currentPagePath = useCurrentPagePath();
   const { update: updateComment, post: postComment } = useSWRxPageComment(pageId);
   const { data: isSlackEnabled, mutate: mutateIsSlackEnabled } = useIsSlackEnabled();
-  const { data: acceptedUploadFileType } = useAcceptedUploadFileType();
+  const acceptedUploadFileType = useAcceptedUploadFileType();
   const { data: slackChannelsData } = useSWRxSlackChannels(currentPagePath);
   const isSlackConfigured = useAtomValue(isSlackConfiguredAtom);
   const { data: editorSettings } = useEditorSettings();

+ 2 - 2
apps/app/src/client/components/PageEditor/PageEditor.tsx

@@ -38,13 +38,13 @@ import {
   defaultIndentSizeAtom,
   isEnabledAttachTitleHeaderAtom,
   isIndentSizeForcedAtom,
+  useAcceptedUploadFileType,
 } from '~/states/server-configurations';
 import {
   useCurrentIndentSize, useCurrentIndentSizeActions,
   useEditorMode, EditorMode, useEditingMarkdown, useSelectedGrant,
   useWaitingSaveProcessingActions,
 } from '~/states/ui/editor';
-import { useAcceptedUploadFileType } from '~/stores-universal/context';
 import { useNextThemes } from '~/stores-universal/use-next-themes';
 import {
   useReservedNextCaretLine,
@@ -115,7 +115,7 @@ export const PageEditorSubstance = (props: Props): JSX.Element => {
   const currentIndentSize = useCurrentIndentSize();
   const { mutate: mutateCurrentIndentSize } = useCurrentIndentSizeActions();
   const defaultIndentSize = useAtomValue(defaultIndentSizeAtom);
-  const { data: acceptedUploadFileType } = useAcceptedUploadFileType();
+  const acceptedUploadFileType = useAcceptedUploadFileType();
   const { data: editorSettings } = useEditorSettings();
   const { mutate: mutateIsGrantNormalized } = useSWRxCurrentGrantData(currentPage?._id);
   const user = useCurrentUser();

+ 27 - 0
apps/app/src/states/server-configurations/accepted-upload-file-type.ts

@@ -0,0 +1,27 @@
+import { AcceptedUploadFileType } from '@growi/core';
+import { atom, useAtomValue } from 'jotai';
+
+import {
+  isUploadAllFileAllowedAtom,
+  isUploadEnabledAtom,
+} from './server-configurations';
+
+// Derived atom for accepted upload file type calculation
+const acceptedUploadFileTypeAtom = atom((get) => {
+  const isUploadEnabled = get(isUploadEnabledAtom);
+  const isUploadAllFileAllowed = get(isUploadAllFileAllowedAtom);
+
+  if (!isUploadEnabled) {
+    return AcceptedUploadFileType.NONE;
+  }
+
+  if (isUploadAllFileAllowed) {
+    return AcceptedUploadFileType.ALL;
+  }
+
+  return AcceptedUploadFileType.IMAGE;
+});
+
+export const useAcceptedUploadFileType = (): AcceptedUploadFileType => {
+  return useAtomValue(acceptedUploadFileTypeAtom);
+};

+ 1 - 0
apps/app/src/states/server-configurations/index.ts

@@ -1 +1,2 @@
+export * from './accepted-upload-file-type';
 export * from './server-configurations';

+ 0 - 34
apps/app/src/stores-universal/context.tsx

@@ -1,40 +1,6 @@
 import type EventEmitter from 'node:events';
-import { AcceptedUploadFileType } from '@growi/core';
-import { useAtomValue } from 'jotai';
-import type { SWRResponse } from 'swr';
-import useSWRImmutable from 'swr/immutable';
-
-import {
-  isUploadAllFileAllowedAtom,
-  isUploadEnabledAtom,
-} from '~/states/server-configurations';
 
 declare global {
   // eslint-disable-next-line vars-on-top, no-var
   var globalEmitter: EventEmitter;
 }
-
-/** **********************************************************
- *                     Computed contexts
- *********************************************************** */
-
-export const useAcceptedUploadFileType = (): SWRResponse<
-  AcceptedUploadFileType,
-  Error
-> => {
-  const isUploadEnabled = useAtomValue(isUploadEnabledAtom);
-  const isUploadAllFileAllowed = useAtomValue(isUploadAllFileAllowedAtom);
-
-  return useSWRImmutable(
-    ['acceptedUploadFileType', isUploadEnabled, isUploadAllFileAllowed],
-    ([, isUploadEnabled, isUploadAllFileAllowed]) => {
-      if (!isUploadEnabled) {
-        return AcceptedUploadFileType.NONE;
-      }
-      if (isUploadAllFileAllowed) {
-        return AcceptedUploadFileType.ALL;
-      }
-      return AcceptedUploadFileType.IMAGE;
-    },
-  );
-};