Explorar el Código

Use useIsLatestRevision()

Shun Miyazawa hace 1 año
padre
commit
9fb90c17d2

+ 0 - 2
apps/app/src/components/PageView/PageView.tsx

@@ -128,8 +128,6 @@ export const PageView = (props: Props): JSX.Element => {
     const markdown = page.revision.body;
     const rendererOptions = viewOptions ?? generateSSRViewOptions(rendererConfig, pagePath);
 
-    console.log('markdown', markdown);
-
     return (
       <>
         <PageContentsUtilities />

+ 1 - 4
apps/app/src/pages/[[...path]].page.tsx

@@ -190,7 +190,6 @@ type Props = CommonProps & {
   yjsData: CurrentPageYjsData,
 
   rendererConfig: RendererConfig,
-  isPastRevisionSelected: boolean,
 };
 
 const Page: NextPageWithLayout<Props> = (props: Props) => {
@@ -251,7 +250,7 @@ const Page: NextPageWithLayout<Props> = (props: Props) => {
 
   useCurrentPathname(props.currentPathname);
 
-  const { data: currentPage } = useSWRxCurrentPage(pageWithMeta?.data ?? null, props.isPastRevisionSelected); // store initial data
+  const { data: currentPage } = useSWRxCurrentPage(pageWithMeta?.data ?? null); // store initial data
 
   const { trigger: mutateCurrentPage } = useSWRMUTxCurrentPage();
   const { trigger: mutateCurrentPageYjsDataFromApi } = useSWRMUTxCurrentPageYjsData();
@@ -433,8 +432,6 @@ async function injectPageData(context: GetServerSidePropsContext, props: Props):
   const { crowi } = req;
   const { revisionId } = req.query;
 
-  props.isPastRevisionSelected = revisionId != null;
-
   const Page = crowi.model('Page') as PageModel;
   const PageRedirect = mongooseModel('PageRedirect') as PageRedirectModel;
   const { pageService, configManager } = crowi;

+ 12 - 10
apps/app/src/stores/page.tsx

@@ -54,11 +54,11 @@ export const useTemplateBodyData = (initialData?: string): SWRResponse<string, E
 };
 
 /** "useSWRxCurrentPage" is intended for initial data retrieval only. Use "useSWRMUTxCurrentPage" for revalidation */
-export const useSWRxCurrentPage = (
-    initialData?: IPagePopulatedToShowRevision|null, isPastRevisionSelected = false,
-): SWRResponse<IPagePopulatedToShowRevision|null> => {
+export const useSWRxCurrentPage = (initialData?: IPagePopulatedToShowRevision|null): SWRResponse<IPagePopulatedToShowRevision|null> => {
   const key = 'currentPage';
 
+  const { data: isLatestRevision } = useIsLatestRevision();
+
   const { cache } = useSWRConfig();
 
   // Problem 1: https://github.com/weseek/growi/pull/7772/files#diff-4c1708c4f959974166c15435c6b35950ba01bbf35e7e4b8e99efeb125a8000a7
@@ -83,24 +83,26 @@ export const useSWRxCurrentPage = (
       return true;
     }
 
-    // // mutate When a different revision is opened
-    // if (cachedData.revision?._id != null && initialData.revision?._id != null && cachedData.revision._id !== initialData.revision._id) {
-    //   console.log('initialData', initialData);
-    //   return true;
-    // }
+    // mutate When a different revision is opened
+    if (!isLatestRevision
+        && cachedData.revision?._id != null && initialData.revision?._id != null
+        && cachedData.revision._id !== initialData.revision._id
+    ) {
+      return true;
+    }
 
     return false;
   })();
 
   useEffect(() => {
-    if (shouldMutate || isPastRevisionSelected) {
+    if (shouldMutate) {
       mutate(key, initialData, {
         optimisticData: initialData,
         populateCache: true,
         revalidate: false,
       });
     }
-  }, [initialData, isPastRevisionSelected, key, shouldMutate]);
+  }, [initialData, key, shouldMutate]);
 
   return useSWR(key, null, {
     keepPreviousData: true,