Browse Source

Merge pull request #6172 from weseek/feat/implement-old-revision-alert

feat: Implement OldRevisionAlert
Yuki Takei 3 years ago
parent
commit
25938b21a8

+ 27 - 0
packages/app/src/components/PageAlert/OldRevisionAlert.tsx

@@ -0,0 +1,27 @@
+import React from 'react';
+
+import Link from 'next/link';
+import { useTranslation } from 'react-i18next';
+
+import { useIsLatestRevision } from '~/stores/context';
+import { useSWRxCurrentPage } from '~/stores/page';
+
+export const OldRevisionAlert = (): JSX.Element => {
+
+  const { t } = useTranslation();
+  const { data: isLatestRevision } = useIsLatestRevision();
+  const { data: page } = useSWRxCurrentPage();
+
+  if (page == null || isLatestRevision == null || isLatestRevision) {
+    return <></>;
+  }
+
+  return (
+    <div className="alert alert-warning">
+      <strong>{ t('Warning') }: </strong> { t('page_page.notice.version') }
+      <Link href={`/${page._id}`}>
+        <a><i className="icon-fw icon-arrow-right-circle"></i>{ t('Show latest') }</a>
+      </Link>
+    </div>
+  );
+};

+ 2 - 0
packages/app/src/components/PageAlert/PageAlerts.tsx

@@ -3,6 +3,7 @@ import React from 'react';
 import dynamic from 'next/dynamic';
 
 import { FixPageGrantAlert } from './FixPageGrantAlert';
+import { OldRevisionAlert } from './OldRevisionAlert';
 import { PageGrantAlert } from './PageGrantAlert';
 import { PageStaleAlert } from './PageStaleAlert';
 
@@ -20,6 +21,7 @@ export const PageAlerts = (): JSX.Element => {
         <PageGrantAlert />
         <TrashPageAlert />
         <PageStaleAlert />
+        <OldRevisionAlert />
       </div>
     </div>
   );

+ 22 - 2
packages/app/src/pages/[[...path]].page.tsx

@@ -36,7 +36,7 @@ import { BasicLayout } from '../components/BasicLayout';
 
 import {
   useCurrentUser, useCurrentPagePath,
-  useOwnerOfCurrentPage,
+  useOwnerOfCurrentPage, useIsLatestRevision,
   useIsForbidden, useIsNotFound, useIsTrashPage, useShared, useShareLinkId, useIsSharedUser, useIsAbleToDeleteCompletely,
   useAppTitle, useSiteUrl, useConfidential, useIsEnabledStaleNotification,
   useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsMailerSetup,
@@ -61,6 +61,7 @@ type Props = CommonProps & {
   // redirectFrom?: string;
 
   // shareLinkId?: string;
+  isLatestRevision: boolean
 
   isForbidden: boolean,
   isNotFound: boolean,
@@ -101,6 +102,7 @@ const GrowiPage: NextPage<Props> = (props: Props) => {
 
   // page
   useCurrentPagePath(props.currentPathname);
+  useIsLatestRevision(props.isLatestRevision);
   // useOwnerOfCurrentPage(props.pageUser != null ? JSON.parse(props.pageUser) : null);
   // useIsForbidden(props.isForbidden);
   // useNotFound(props.isNotFound);
@@ -227,10 +229,14 @@ async function injectPageInformation(context: GetServerSidePropsContext, props:
   const Page = crowi.model('Page');
   const { pageService } = crowi;
 
-  const { user } = req;
+  const { user, originalUrl } = req;
 
   const { currentPathname } = props;
 
+  // retrieve query params
+  const url = new URL(originalUrl, props.siteUrl);
+  const searchParams = new URLSearchParams(url.search);
+
   // determine pageId
   const pageIdStr = currentPathname.substring(1);
   const pageId = isValidObjectId(pageIdStr) ? pageIdStr : null;
@@ -247,8 +253,22 @@ async function injectPageInformation(context: GetServerSidePropsContext, props:
     logger.warn(`Page is ${props.isForbidden ? 'forbidden' : 'not found'}`, currentPathname);
   }
 
+  // Todo: should check if revision document with the specified revisionId actually exist in DB.
+  // if true, replacing page.revision with old revision should be done when populating Revision
+  const revisionId = searchParams.get('revision');
+  const isSpecifiedRevisionExist = true; // dummy
+
+  // check if revision is latest
+  if (revisionId == null || !isSpecifiedRevisionExist) {
+    props.isLatestRevision = true;
+  }
+  else {
+    props.isLatestRevision = page.revision.toString() === revisionId;
+  }
+
   await (page as unknown as PageModel).populateDataToShowRevision();
   props.pageWithMetaStr = JSON.stringify(result);
+
 }
 
 // async function injectPageUserInformation(context: GetServerSidePropsContext, props: Props): Promise<void> {

+ 3 - 0
packages/app/src/stores/context.tsx

@@ -187,6 +187,9 @@ export const useIsEnabledStaleNotification = (initialData?: boolean): SWRRespons
   return useStaticSWR('isEnabledStaleNotification', initialData);
 };
 
+export const useIsLatestRevision = (initialData?: boolean): SWRResponse<boolean, any> => {
+  return useStaticSWR('isLatestRevision', initialData);
+};
 
 
 /** **********************************************************