Przeglądaj źródła

add types for populated page to fix tsc error

Yuki Takei 3 lat temu
rodzic
commit
4d262d3113

+ 1 - 1
packages/app/src/components/PageAlert/TrashPageAlert.tsx

@@ -36,7 +36,7 @@ export const TrashPageAlert = (): JSX.Element => {
 
 
   const lastUpdateUserName = pageData?.lastUpdateUser.name;
   const lastUpdateUserName = pageData?.lastUpdateUser.name;
   const deletedAt = pageData?.deletedAt ? format(new Date(pageData?.deletedAt), 'yyyy/MM/dd HH:mm') : '';
   const deletedAt = pageData?.deletedAt ? format(new Date(pageData?.deletedAt), 'yyyy/MM/dd HH:mm') : '';
-  const revisionId = pageData?.revision._id;
+  const revisionId = pageData?.revision?._id;
 
 
   if (!isTrashPage) {
   if (!isTrashPage) {
     return <></>;
     return <></>;

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

@@ -2,7 +2,9 @@ import React, { useEffect } from 'react';
 
 
 import EventEmitter from 'events';
 import EventEmitter from 'events';
 
 
-import { isClient, pagePathUtils, pathUtils } from '@growi/core';
+import {
+  IDataWithMeta, IPageInfoForEntity, IPagePopulatedToShowRevision, isClient, pagePathUtils, pathUtils,
+} from '@growi/core';
 import ExtensibleCustomError from 'extensible-custom-error';
 import ExtensibleCustomError from 'extensible-custom-error';
 import {
 import {
   NextPage, GetServerSideProps, GetServerSidePropsContext,
   NextPage, GetServerSideProps, GetServerSidePropsContext,
@@ -21,7 +23,6 @@ import { CrowiRequest } from '~/interfaces/crowi-request';
 // import { useRendererSettings } from '~/stores/renderer';
 // import { useRendererSettings } from '~/stores/renderer';
 // import { EditorMode, useEditorMode, useIsMobile } from '~/stores/ui';
 // import { EditorMode, useEditorMode, useIsMobile } from '~/stores/ui';
 import { CustomWindow } from '~/interfaces/global';
 import { CustomWindow } from '~/interfaces/global';
-import { IPageWithMeta } from '~/interfaces/page';
 import { RendererConfig } from '~/interfaces/services/renderer';
 import { RendererConfig } from '~/interfaces/services/renderer';
 import { ISidebarConfig } from '~/interfaces/sidebar-config';
 import { ISidebarConfig } from '~/interfaces/sidebar-config';
 import { PageModel, PageDocument } from '~/server/models/page';
 import { PageModel, PageDocument } from '~/server/models/page';
@@ -77,6 +78,7 @@ const IdenticalPathPage = (): JSX.Element => {
   return <IdenticalPathPage />;
   return <IdenticalPathPage />;
 };
 };
 
 
+type IPageToShowRevisionWithMeta = IDataWithMeta<IPagePopulatedToShowRevision, IPageInfoForEntity>;
 
 
 type Props = CommonProps & {
 type Props = CommonProps & {
   currentUser: string,
   currentUser: string,
@@ -193,9 +195,9 @@ const GrowiPage: NextPage<Props> = (props: Props) => {
 
 
   // const { data: editorMode } = useEditorMode();
   // const { data: editorMode } = useEditorMode();
 
 
-  let pageWithMeta: IPageWithMeta | undefined;
+  let pageWithMeta: IPageToShowRevisionWithMeta | undefined;
   if (props.pageWithMetaStr != null) {
   if (props.pageWithMetaStr != null) {
-    pageWithMeta = JSON.parse(props.pageWithMetaStr) as IPageWithMeta;
+    pageWithMeta = JSON.parse(props.pageWithMetaStr) as IPageToShowRevisionWithMeta;
   }
   }
   useCurrentPageId(pageWithMeta?.data._id);
   useCurrentPageId(pageWithMeta?.data._id);
   useSWRxCurrentPage(undefined, pageWithMeta?.data); // store initial data
   useSWRxCurrentPage(undefined, pageWithMeta?.data); // store initial data
@@ -312,7 +314,7 @@ class MultiplePagesHitsError extends ExtensibleCustomError {
 
 
 }
 }
 
 
-async function getPageData(context: GetServerSidePropsContext, props: Props): Promise<IPageWithMeta|null> {
+async function getPageData(context: GetServerSidePropsContext, props: Props): Promise<IPageToShowRevisionWithMeta|null> {
   const req: CrowiRequest = context.req as CrowiRequest;
   const req: CrowiRequest = context.req as CrowiRequest;
   const { crowi } = req;
   const { crowi } = req;
   const { revisionId } = req.query;
   const { revisionId } = req.query;
@@ -335,7 +337,7 @@ async function getPageData(context: GetServerSidePropsContext, props: Props): Pr
     }
     }
   }
   }
 
 
-  const result: IPageWithMeta = await pageService.findPageAndMetaDataByViewer(pageId, currentPathname, user, true); // includeEmpty = true, isSharedPage = false
+  const result: IPageToShowRevisionWithMeta = await pageService.findPageAndMetaDataByViewer(pageId, currentPathname, user, true); // includeEmpty = true, isSharedPage = false
   const page = result?.data as unknown as PageDocument;
   const page = result?.data as unknown as PageDocument;
 
 
   // populate & check if the revision is latest
   // populate & check if the revision is latest
@@ -348,7 +350,7 @@ async function getPageData(context: GetServerSidePropsContext, props: Props): Pr
   return result;
   return result;
 }
 }
 
 
-async function injectRoutingInformation(context: GetServerSidePropsContext, props: Props, pageWithMeta: IPageWithMeta|null): Promise<void> {
+async function injectRoutingInformation(context: GetServerSidePropsContext, props: Props, pageWithMeta: IPageToShowRevisionWithMeta|null): Promise<void> {
   const req: CrowiRequest = context.req as CrowiRequest;
   const req: CrowiRequest = context.req as CrowiRequest;
   const { crowi } = req;
   const { crowi } = req;
   const Page = crowi.model('Page') as PageModel;
   const Page = crowi.model('Page') as PageModel;

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

@@ -1,10 +1,10 @@
-import { Nullable } from '@growi/core';
+import { IPagePopulatedToShowRevision, Nullable } from '@growi/core';
 import useSWR, { SWRResponse } from 'swr';
 import useSWR, { SWRResponse } from 'swr';
 import useSWRImmutable from 'swr/immutable';
 import useSWRImmutable from 'swr/immutable';
 
 
 import { apiv3Get } from '~/client/util/apiv3-client';
 import { apiv3Get } from '~/client/util/apiv3-client';
 import {
 import {
-  IPageInfo, IPageHasId, IPageInfoForOperation, IPageInfoAll,
+  IPageInfo, IPageInfoForOperation, IPageInfoAll,
 } from '~/interfaces/page';
 } from '~/interfaces/page';
 import { IRecordApplicableGrant, IResIsGrantNormalized } from '~/interfaces/page-grant';
 import { IRecordApplicableGrant, IResIsGrantNormalized } from '~/interfaces/page-grant';
 import { IRevisionsForPagination } from '~/interfaces/revision';
 import { IRevisionsForPagination } from '~/interfaces/revision';
@@ -14,21 +14,21 @@ import { IPageTagsInfo } from '../interfaces/tag';
 
 
 import { useCurrentPageId } from './context';
 import { useCurrentPageId } from './context';
 
 
-export const useSWRxPage = (pageId?: string|null, shareLinkId?: string): SWRResponse<IPageHasId, Error> => {
-  return useSWR<IPageHasId, Error>(
+export const useSWRxPage = (pageId?: string|null, shareLinkId?: string): SWRResponse<IPagePopulatedToShowRevision, Error> => {
+  return useSWR<IPagePopulatedToShowRevision, Error>(
     pageId != null ? ['/page', pageId, shareLinkId] : null,
     pageId != null ? ['/page', pageId, shareLinkId] : null,
     (endpoint, pageId, shareLinkId) => apiv3Get(endpoint, { pageId, shareLinkId }).then(result => result.data.page),
     (endpoint, pageId, shareLinkId) => apiv3Get(endpoint, { pageId, shareLinkId }).then(result => result.data.page),
   );
   );
 };
 };
 
 
-export const useSWRxPageByPath = (path?: string): SWRResponse<IPageHasId, Error> => {
-  return useSWR<IPageHasId, Error>(
+export const useSWRxPageByPath = (path?: string): SWRResponse<IPagePopulatedToShowRevision, Error> => {
+  return useSWR<IPagePopulatedToShowRevision, Error>(
     path != null ? ['/page', path] : null,
     path != null ? ['/page', path] : null,
     (endpoint, path) => apiv3Get(endpoint, { path }).then(result => result.data.page),
     (endpoint, path) => apiv3Get(endpoint, { path }).then(result => result.data.page),
   );
   );
 };
 };
 
 
-export const useSWRxCurrentPage = (shareLinkId?: string, initialData?: IPageHasId): SWRResponse<IPageHasId, Error> => {
+export const useSWRxCurrentPage = (shareLinkId?: string, initialData?: IPagePopulatedToShowRevision): SWRResponse<IPagePopulatedToShowRevision, Error> => {
   const { data: currentPageId } = useCurrentPageId();
   const { data: currentPageId } = useCurrentPageId();
 
 
   const swrResult = useSWRxPage(currentPageId, shareLinkId);
   const swrResult = useSWRxPage(currentPageId, shareLinkId);

+ 17 - 4
packages/core/src/interfaces/page.ts

@@ -1,12 +1,12 @@
 import { Ref } from './common';
 import { Ref } from './common';
 import { HasObjectId } from './has-object-id';
 import { HasObjectId } from './has-object-id';
-import { IRevision, HasRevisionShortbody } from './revision';
+import { IRevision, HasRevisionShortbody, IRevisionHasId } from './revision';
 import { SubscriptionStatusType } from './subscription';
 import { SubscriptionStatusType } from './subscription';
 import { ITag } from './tag';
 import { ITag } from './tag';
-import { IUser } from './user';
+import { IUser, IUserGroupHasId, IUserHasId } from './user';
 
 
 
 
-export interface IPage {
+export type IPage = {
   path: string,
   path: string,
   status: string,
   status: string,
   revision: Ref<IRevision>,
   revision: Ref<IRevision>,
@@ -33,13 +33,26 @@ export interface IPage {
   latestRevision?: Ref<IRevision>,
   latestRevision?: Ref<IRevision>,
 }
 }
 
 
+export type IPagePopulatedToList = Omit<IPageHasId, 'lastUpdateUser'> & {
+  lastUpdateUser: IUserHasId,
+}
+
+export type IPagePopulatedToShowRevision = Omit<IPageHasId, 'lastUpdateUser'|'creator'|'deleteUser'|'grantedGroup'|'revision'|'author'> & {
+  lastUpdateUser: IUserHasId,
+  creator: IUserHasId,
+  deleteUser: IUserHasId,
+  grantedGroup: IUserGroupHasId,
+  revision: IRevisionHasId,
+  author: IUserHasId,
+}
+
 export const PageGrant = {
 export const PageGrant = {
   GRANT_PUBLIC: 1,
   GRANT_PUBLIC: 1,
   GRANT_RESTRICTED: 2,
   GRANT_RESTRICTED: 2,
   GRANT_SPECIFIED: 3, // DEPRECATED
   GRANT_SPECIFIED: 3, // DEPRECATED
   GRANT_OWNER: 4,
   GRANT_OWNER: 4,
   GRANT_USER_GROUP: 5,
   GRANT_USER_GROUP: 5,
-};
+} as const;
 export type PageGrant = typeof PageGrant[keyof typeof PageGrant];
 export type PageGrant = typeof PageGrant[keyof typeof PageGrant];
 
 
 export type IPageHasId = IPage & HasObjectId;
 export type IPageHasId = IPage & HasObjectId;

+ 3 - 0
packages/core/src/interfaces/revision.ts

@@ -1,3 +1,4 @@
+import { HasObjectId } from './has-object-id';
 import { IUser } from './user';
 import { IUser } from './user';
 
 
 export type IRevision = {
 export type IRevision = {
@@ -8,6 +9,8 @@ export type IRevision = {
   updatedAt: Date,
   updatedAt: Date,
 }
 }
 
 
+export type IRevisionHasId = IRevision & HasObjectId;
+
 export type IRevisionsForPagination = {
 export type IRevisionsForPagination = {
   revisions: IRevision[], // revisions in one pagination
   revisions: IRevision[], // revisions in one pagination
   totalCounts: number // total counts
   totalCounts: number // total counts