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

+ 1 - 1
apps/app/src/states/page/internal-atoms.ts

@@ -61,7 +61,7 @@ export const isUntitledPageAtom = atom(
   },
 );
 
-// Remote revision data atoms (migrated from useSWRStatic)
+// Remote revision data atoms
 export const remoteRevisionIdAtom = atom<string>();
 export const remoteRevisionBodyAtom = atom<string>();
 export const remoteRevisionLastUpdateUserAtom = atom<IUserHasId>();

+ 0 - 1
packages/core/src/swr/index.ts

@@ -1,2 +1 @@
-export * from './use-swr-static';
 export * from './with-utils';

+ 0 - 50
packages/core/src/swr/use-swr-static.ts

@@ -1,50 +0,0 @@
-import {
-  type Key,
-  type SWRConfiguration,
-  type SWRResponse,
-  useSWRConfig,
-} from 'swr';
-import useSWRImmutable from 'swr/immutable';
-
-export function useSWRStatic<Data, Error>(key: Key): SWRResponse<Data, Error>;
-export function useSWRStatic<Data, Error>(
-  key: Key,
-  data: Data | undefined,
-): SWRResponse<Data, Error>;
-export function useSWRStatic<Data, Error>(
-  key: Key,
-  data: Data | undefined,
-  configuration: SWRConfiguration<Data, Error> | undefined,
-): SWRResponse<Data, Error>;
-
-export function useSWRStatic<Data, Error>(
-  ...args:
-    | readonly [Key]
-    | readonly [Key, Data | undefined]
-    | readonly [
-        Key,
-        Data | undefined,
-        SWRConfiguration<Data, Error> | undefined,
-      ]
-): SWRResponse<Data, Error> {
-  const [key, data, configuration] = args;
-
-  if (configuration?.fetcher != null) {
-    throw new Error("useSWRStatic does not support 'configuration.fetcher'");
-  }
-
-  const { cache } = useSWRConfig();
-  const swrResponse = useSWRImmutable(key, null, {
-    ...configuration,
-    fallbackData:
-      configuration?.fallbackData ??
-      (key != null ? cache.get(key?.toString())?.data : undefined),
-  });
-
-  // update data
-  if (key != null && data !== undefined) {
-    swrResponse.mutate(data, { optimisticData: data });
-  }
-
-  return swrResponse;
-}

+ 35 - 10
packages/editor/src/client/stores/codemirror-editor.ts

@@ -1,9 +1,9 @@
-import { useMemo, useRef } from 'react';
+import { useEffect, useMemo, useRef } from 'react';
 
-import { useSWRStatic } from '@growi/core/dist/swr';
 import { deepEquals } from '@growi/core/dist/utils';
 import type { ReactCodeMirrorProps, UseCodeMirror } from '@uiw/react-codemirror';
-import type { SWRResponse } from 'swr';
+import { atom, useAtom } from 'jotai';
+import { atomFamily } from 'jotai/utils';
 import deepmerge from 'ts-deepmerge';
 
 import { type UseCodeMirrorEditor, useCodeMirrorEditor } from '../services';
@@ -14,14 +14,33 @@ const isValid = (u: UseCodeMirrorEditor) => {
   return u.state != null && u.view != null;
 };
 
+/**
+ * Atom family to store CodeMirror editor instances by key
+ */
+const codeMirrorEditorAtomFamily = atomFamily((_key: string) => atom<UseCodeMirrorEditor | null>(null));
+
+/**
+ * Result type for useCodeMirrorEditorIsolated hook
+ * Compatible with the previous SWRResponse interface for the data field
+ */
+export type CodeMirrorEditorResult = {
+  data: UseCodeMirrorEditor | undefined;
+};
+
+/**
+ * Hook to manage isolated CodeMirror editor instances using Jotai
+ */
 export const useCodeMirrorEditorIsolated = (
     key: string | null, container?: HTMLDivElement | null, props?: ReactCodeMirrorProps,
-): SWRResponse<UseCodeMirrorEditor> => {
+): CodeMirrorEditorResult => {
 
   const ref = useRef<UseCodeMirrorEditor | null>(null);
   const currentData = ref.current;
 
-  const swrKey = key != null ? `codeMirrorEditor_${key}` : null;
+  // Use a default key if null is provided
+  const atomKey = key ?? 'default';
+  const [storedData, setStoredData] = useAtom(codeMirrorEditorAtomFamily(atomKey));
+
   const mergedProps = useMemo<UseCodeMirror>(() => deepmerge(
     { container },
     props ?? {},
@@ -29,14 +48,20 @@ export const useCodeMirrorEditorIsolated = (
 
   const newData = useCodeMirrorEditor(mergedProps);
 
-  const shouldUpdate = swrKey != null && container != null && (
+  const shouldUpdate = key != null && container != null && (
     currentData == null
     || (isValid(newData) && !isDeepEquals(currentData, newData))
   );
 
-  if (shouldUpdate) {
-    ref.current = newData;
-  }
+  // Update atom when data changes
+  useEffect(() => {
+    if (shouldUpdate) {
+      ref.current = newData;
+      setStoredData(newData);
+    }
+  }, [shouldUpdate, newData, setStoredData]);
 
-  return useSWRStatic(swrKey, shouldUpdate ? newData : undefined);
+  return {
+    data: key != null ? storedData ?? undefined : undefined,
+  };
 };