Browse Source

Next Page mutates only once

Shun Miyazawa 1 year ago
parent
commit
ee5bc3b531
2 changed files with 10 additions and 4 deletions
  1. 8 2
      apps/app/src/pages/[[...path]].page.tsx
  2. 2 2
      apps/app/src/stores/yjs.ts

+ 8 - 2
apps/app/src/pages/[[...path]].page.tsx

@@ -225,8 +225,6 @@ const Page: NextPageWithLayout<Props> = (props: Props) => {
   useIsUploadAllFileAllowed(props.isUploadAllFileAllowed);
   useIsUploadAllFileAllowed(props.isUploadAllFileAllowed);
   useIsUploadEnabled(props.isUploadEnabled);
   useIsUploadEnabled(props.isUploadEnabled);
 
 
-  useCurrentPageYjsData(props.yjsData);
-
   const { pageWithMeta } = props;
   const { pageWithMeta } = props;
 
 
   const pageId = pageWithMeta?.data._id;
   const pageId = pageWithMeta?.data._id;
@@ -249,6 +247,8 @@ const Page: NextPageWithLayout<Props> = (props: Props) => {
   const { mutate: mutateTemplateTagData } = useTemplateTagData();
   const { mutate: mutateTemplateTagData } = useTemplateTagData();
   const { mutate: mutateTemplateBodyData } = useTemplateBodyData();
   const { mutate: mutateTemplateBodyData } = useTemplateBodyData();
 
 
+  const { mutate: mutateCurrentPageYjsData } = useCurrentPageYjsData();
+
   useSetupGlobalSocket();
   useSetupGlobalSocket();
   useSetupGlobalSocketForPage(pageId);
   useSetupGlobalSocketForPage(pageId);
 
 
@@ -311,6 +311,10 @@ const Page: NextPageWithLayout<Props> = (props: Props) => {
     mutateTemplateBodyData(props.templateBodyData);
     mutateTemplateBodyData(props.templateBodyData);
   }, [props.templateBodyData, mutateTemplateBodyData]);
   }, [props.templateBodyData, mutateTemplateBodyData]);
 
 
+  useEffect(() => {
+    mutateCurrentPageYjsData(props.yjsData);
+  }, [mutateCurrentPageYjsData, props.yjsData]);
+
   // If the data on the page changes without router.push, pageWithMeta remains old because getServerSideProps() is not executed
   // If the data on the page changes without router.push, pageWithMeta remains old because getServerSideProps() is not executed
   // So preferentially take page data from useSWRxCurrentPage
   // So preferentially take page data from useSWRxCurrentPage
   const pagePath = currentPage?.path ?? pageWithMeta?.data.path ?? props.currentPathname;
   const pagePath = currentPage?.path ?? pageWithMeta?.data.path ?? props.currentPathname;
@@ -495,6 +499,8 @@ async function injectRoutingInformation(context: GetServerSidePropsContext, prop
       hasDraft: crowi.pageService.hasYjsDraft(page._id),
       hasDraft: crowi.pageService.hasYjsDraft(page._id),
       awarenessStateSize: crowi.pageService.getYjsAwarenessStateSize(page._id),
       awarenessStateSize: crowi.pageService.getYjsAwarenessStateSize(page._id),
     };
     };
+
+    console.log('yjsData', props.yjsData);
   }
   }
 }
 }
 
 

+ 2 - 2
apps/app/src/stores/yjs.ts

@@ -13,8 +13,8 @@ type CurrentPageYjsDataUtils = {
   updateAwarenessStateSize(awarenessStateSize: number): void
   updateAwarenessStateSize(awarenessStateSize: number): void
 }
 }
 
 
-export const useCurrentPageYjsData = (initialValue?: CurrentPageYjsDataStates): SWRResponse<CurrentPageYjsDataStates, Error> & CurrentPageYjsDataUtils => {
-  const swrResponse = useSWRStatic<CurrentPageYjsDataStates, Error>('currentPageYjsData', undefined, { fallbackData: initialValue });
+export const useCurrentPageYjsData = (): SWRResponse<CurrentPageYjsDataStates, Error> & CurrentPageYjsDataUtils => {
+  const swrResponse = useSWRStatic<CurrentPageYjsDataStates, Error>('currentPageYjsData', undefined);
 
 
   const updateHasDraft = useCallback((hasDraft: boolean) => {
   const updateHasDraft = useCallback((hasDraft: boolean) => {
     swrResponse.mutate({ ...swrResponse.data, hasDraft });
     swrResponse.mutate({ ...swrResponse.data, hasDraft });