Преглед изворни кода

Merge branch 'feat/integrate-implement-page-alert-component' into feat/implement-page-stale-alert

yohei0125 пре 3 година
родитељ
комит
fe088aec3f

+ 4 - 3
packages/app/src/components/PageAlert/PageAlerts.tsx

@@ -1,6 +1,6 @@
-import {FixPageGrantAlert} from "./FixPageGrantAlert";
-import {PageStaleAlert} from "./PageStaleAlert";
-
+import { FixPageGrantAlert } from "./FixPageGrantAlert";
+import { PageGrantAlert } from "./PageGrantAlert";
+import { PageStaleAlert } from "./PageStaleAlert";
 
 
 export const PageAlerts = (): JSX.Element => {
 export const PageAlerts = (): JSX.Element => {
 
 
@@ -10,6 +10,7 @@ export const PageAlerts = (): JSX.Element => {
       <div className="col-sm-12">
       <div className="col-sm-12">
         {/* alerts */}
         {/* alerts */}
         <FixPageGrantAlert/>
         <FixPageGrantAlert/>
+        <PageGrantAlert/>
         <PageStaleAlert/>
         <PageStaleAlert/>
       </div>
       </div>
     </div>
     </div>

+ 53 - 0
packages/app/src/components/PageAlert/PageGrantAlert.tsx

@@ -0,0 +1,53 @@
+import React from 'react';
+import { useSWRxCurrentPage } from '~/stores/page';
+import { useXss } from '~/stores/xss';
+import { useTranslation } from 'react-i18next';
+
+
+export const PageGrantAlert = (): JSX.Element => {
+  const { t } = useTranslation();
+  const { data: pageData } = useSWRxCurrentPage();
+  const { data: xss } = useXss();
+
+  if ( pageData == null || pageData.grant == null || pageData.grant == 1 || xss == null) {
+    return <></>
+  }
+
+  const renderAlertContent = () => {
+    const getGrantLabel = () => {
+      if (pageData.grant == 2) {
+        return (
+          <>
+            <i className="icon-fw icon-link"></i><strong>{t('Anyone with the link')} only</strong>
+          </>
+        )
+      }
+      if (pageData.grant == 4) {
+        return (
+          <>
+            <i className="icon-fw icon-lock"></i><strong>{t('Only me')} only</strong>
+          </>
+        )
+      }
+      if (pageData.grant == 5) {
+        return (
+          <>
+            <i className="icon-fw icon-organization"></i><strong>{xss.process(pageData.grantedGroup.name)} only</strong>
+          </>
+        )
+      }
+    };
+    return (
+      <>
+        {getGrantLabel()} ({t('Browsing of this page is restricted')})
+      </>
+    );
+  };
+
+
+  return (
+    <p className="alert alert-primary py-3 px-4">
+      {renderAlertContent()}
+    </p>
+  );
+}

+ 4 - 0
packages/app/src/pages/[[...path]].page.tsx

@@ -19,6 +19,7 @@ import { IPageWithMeta } from '~/interfaces/page';
 import { serializeUserSecurely } from '~/server/models/serializers/user-serializer';
 import { serializeUserSecurely } from '~/server/models/serializers/user-serializer';
 import { useSWRxCurrentPage, useSWRxPageInfo } from '~/stores/page';
 import { useSWRxCurrentPage, useSWRxPageInfo } from '~/stores/page';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
+import Xss from '~/services/xss';
 
 
 // import { isUserPage, isTrashPage, isSharedPage } from '~/utils/path-utils';
 // import { isUserPage, isTrashPage, isSharedPage } from '~/utils/path-utils';
 
 
@@ -41,6 +42,8 @@ import {
   useCurrentPageId
   useCurrentPageId
 } from '../stores/context';
 } from '../stores/context';
 
 
+import { useXss } from '../stores/xss'
+
 import { CommonProps, getServerSideCommonProps, useCustomTitle } from './commons';
 import { CommonProps, getServerSideCommonProps, useCustomTitle } from './commons';
 import { PageModel } from '~/server/models/page';
 import { PageModel } from '~/server/models/page';
 import { isValidObjectId } from 'mongoose';
 import { isValidObjectId } from 'mongoose';
@@ -92,6 +95,7 @@ const GrowiPage: NextPage<Props> = (props: Props) => {
   // commons
   // commons
   useAppTitle(props.appTitle);
   useAppTitle(props.appTitle);
   useSiteUrl(props.siteUrl);
   useSiteUrl(props.siteUrl);
+  useXss(new Xss())
   // useEditorConfig(props.editorConfig);
   // useEditorConfig(props.editorConfig);
   useConfidential(props.confidential);
   useConfidential(props.confidential);
   useCsrfToken(props.csrfToken);
   useCsrfToken(props.csrfToken);

+ 8 - 0
packages/app/src/stores/xss.ts

@@ -0,0 +1,8 @@
+
+import { useStaticSWR } from './use-static-swr';
+import { SWRResponse } from 'swr';
+import Xss from '~/services/xss';
+
+export const useXss = (initialData?: Xss): SWRResponse<Xss, Error> => {
+  return useStaticSWR<Xss, Error>('xss', initialData);
+};