Browse Source

store initialized IPageWithMeta

Yuki Takei 3 years ago
parent
commit
488ac3cf43
2 changed files with 17 additions and 13 deletions
  1. 7 6
      packages/app/src/pages/[[...path]].page.tsx
  2. 10 7
      packages/app/src/stores/page.tsx

+ 7 - 6
packages/app/src/pages/[[...path]].page.tsx

@@ -17,7 +17,7 @@ import { CrowiRequest } from '~/interfaces/crowi-request';
 // import { EditorMode, useEditorMode, useIsMobile } from '~/stores/ui';
 import { IPageWithMeta } from '~/interfaces/page';
 import { serializeUserSecurely } from '~/server/models/serializers/user-serializer';
-import { useSWRxCurrentPage } from '~/stores/page';
+import { useSWRxCurrentPage, useSWRxPageInfo } from '~/stores/page';
 import loggerFactory from '~/utils/logger';
 
 // import { isUserPage, isTrashPage, isSharedPage } from '~/utils/path-utils';
@@ -41,6 +41,7 @@ import {
 } from '../stores/context';
 
 import { CommonProps, getServerSideCommonProps, useCustomTitle } from './commons';
+import { PageModel } from '~/server/models/page';
 // import { useCurrentPageSWR } from '../stores/page';
 
 
@@ -124,11 +125,12 @@ const GrowiPage: NextPage<Props> = (props: Props) => {
 
   // const { data: editorMode } = useEditorMode();
 
-  let pageWithMeta;
+  let pageWithMeta: IPageWithMeta | undefined;
   if (props.pageWithMetaStr != null) {
     pageWithMeta = JSON.parse(props.pageWithMetaStr) as IPageWithMeta;
   }
-  // useSWRxCurrentPage(page); // TODO: store initial data
+  useSWRxCurrentPage(undefined, pageWithMeta?.data); // store initial data
+  useSWRxPageInfo(pageWithMeta?.data._id, undefined, pageWithMeta?.meta); // store initial data
 
   const classNames: string[] = [];
   // switch (editorMode) {
@@ -188,7 +190,6 @@ const GrowiPage: NextPage<Props> = (props: Props) => {
                 PageAlerts<br />
                 {/* <DisplaySwitcher /> */}
                 DisplaySwitcher<br />
-                revision: {pageWithMeta.data.revision}<br />
                 <div id="page-editor-navbar-bottom-container" className="d-none d-edit-block"></div>
                 {/* <PageStatusAlert /> */}
                 PageStatusAlert
@@ -235,8 +236,8 @@ async function injectPageInformation(context: GetServerSidePropsContext, props:
     logger.warn(`Page is ${props.isForbidden ? 'forbidden' : 'not found'}`, currentPathname);
   }
 
-  // await page.populateDataToShowRevision();
-  props.pageWithMetaStr = JSON.stringify(serializeUserSecurely(result));
+  await (page as unknown as PageModel).populateDataToShowRevision();
+  props.pageWithMetaStr = JSON.stringify(result);
 }
 
 // async function injectPageUserInformation(context: GetServerSidePropsContext, props: Props): Promise<void> {

+ 10 - 7
packages/app/src/stores/page.tsx

@@ -5,7 +5,7 @@ import useSWRInfinite, { SWRInfiniteResponse } from 'swr/infinite';
 import { apiv3Get } from '~/client/util/apiv3-client';
 import { HasObjectId } from '~/interfaces/has-object-id';
 import {
-  IPageInfo, IPageHasId, IPageInfoForOperation, IPageInfoForListing, IDataWithMeta,
+  IPageInfo, IPageHasId, IPageInfoForOperation, IPageInfoForListing, IDataWithMeta, IPageInfoAll,
 } from '~/interfaces/page';
 import { IRecordApplicableGrant, IResIsGrantNormalized } from '~/interfaces/page-grant';
 import { IPagingResult } from '~/interfaces/paging-result';
@@ -18,24 +18,25 @@ import { useCurrentPageId, useCurrentPagePath } from './context';
 import { ITermNumberManagerUtil, useTermNumberManager } from './use-static-swr';
 
 
-export const useSWRxPage = (pageId?: string, shareLinkId?: string): SWRResponse<IPageHasId, Error> => {
-  return useSWR(
+export const useSWRxPage = (pageId?: string, shareLinkId?: string, initialData?: IPageHasId): SWRResponse<IPageHasId, Error> => {
+  return useSWR<IPageHasId, Error>(
     pageId != null ? ['/page', pageId, shareLinkId] : null,
     (endpoint, pageId, shareLinkId) => apiv3Get(endpoint, { pageId, shareLinkId }).then(result => result.data.page),
+    { fallbackData: initialData },
   );
 };
 
 export const useSWRxPageByPath = (path?: string): SWRResponse<IPageHasId, Error> => {
-  return useSWR(
+  return useSWR<IPageHasId, Error>(
     path != null ? ['/page', path] : null,
     (endpoint, path) => apiv3Get(endpoint, { path }).then(result => result.data.page),
   );
 };
 
-export const useSWRxCurrentPage = (shareLinkId?: string): SWRResponse<IPageHasId, Error> => {
+export const useSWRxCurrentPage = (shareLinkId?: string, initialData?: IPageHasId): SWRResponse<IPageHasId, Error> => {
   const { data: currentPageId } = useCurrentPageId();
 
-  return useSWRxPage(currentPageId ?? undefined, shareLinkId);
+  return useSWRxPage(currentPageId ?? undefined, shareLinkId, initialData);
 };
 
 // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -105,14 +106,16 @@ export const useSWRxTagsInfo = (pageId: Nullable<string>): SWRResponse<IPageTags
 export const useSWRxPageInfo = (
     pageId: string | null | undefined,
     shareLinkId?: string | null,
+    initialData?: IPageInfoAll,
 ): SWRResponse<IPageInfo | IPageInfoForOperation, Error> => {
 
   // assign null if shareLinkId is undefined in order to identify SWR key only by pageId
   const fixedShareLinkId = shareLinkId ?? null;
 
-  return useSWRImmutable(
+  return useSWRImmutable<IPageInfo | IPageInfoForOperation, Error>(
     pageId != null ? ['/page/info', pageId, fixedShareLinkId] : null,
     (endpoint, pageId, shareLinkId) => apiv3Get(endpoint, { pageId, shareLinkId }).then(response => response.data),
+    { fallbackData: initialData },
   );
 };