Shun Miyazawa 3 лет назад
Родитель
Сommit
81761e5244
1 измененных файлов с 24 добавлено и 3 удалено
  1. 24 3
      packages/app/src/stores/page.tsx

+ 24 - 3
packages/app/src/stores/page.tsx

@@ -1,4 +1,4 @@
-import { useEffect, useMemo } from 'react';
+import { useEffect, useMemo, useCallback } from 'react';
 
 import type {
   IPageInfoForEntity, IPagePopulatedToShowRevision, Nullable,
@@ -135,13 +135,34 @@ export const useSWRxPageInfo = (
   return swrResult;
 };
 
+const useSWRxPageRevision = (revisionId: Ref<IRevision>): SWRResponse<IRevisionHasId> => {
+  const key = `/revisions/${revisionId}`;
+
+  return useSWR(key, null, {
+    keepPreviousData: true,
+  });
+};
 
 export const useSWRMUTxPageRevision = (pageId: string, revisionId: Ref<IRevision>): SWRMutationResponse<IRevisionHasId> => {
-  const key = 'pageRevision';
+  const key = `/revisions/${revisionId}`;
+
+  const { data: pageRevisionData } = useSWRxPageRevision(revisionId);
+
+  const fetcher = useCallback(async() => {
+    if (pageRevisionData != null) {
+      return pageRevisionData;
+    }
+
+    const res = await apiv3Get<{ revision: IRevisionHasId }>(`/revisions/${revisionId}`, { pageId });
+    return res.data.revision;
+  }, [pageId, pageRevisionData, revisionId]);
 
   return useSWRMutation(
     key,
-    () => apiv3Get<{ revision: IRevisionHasId }>(`/revisions/${revisionId}`, { pageId }).then(result => result.data.revision),
+    fetcher,
+    {
+      populateCache: true,
+    },
   );
 };