Browse Source

refactor hooks

jam411 3 years ago
parent
commit
167e8f16b4

+ 8 - 9
packages/app/src/components/PageContentFooter.tsx

@@ -2,9 +2,7 @@ import React, { memo } from 'react';
 
 import dynamic from 'next/dynamic';
 
-import {
-  useCurrentCreatedAt, useCurrentUpdatedAt, useCreator, useRevisionAuthor,
-} from '../stores/context';
+import { useSWRxCurrentPage } from '~/stores/page';
 
 import { Skelton } from './Skelton';
 
@@ -12,17 +10,18 @@ export const PageContentFooter = memo((): JSX.Element => {
 
   const AuthorInfo = dynamic(() => import('./Navbar/AuthorInfo'), { ssr: false, loading: () => <p><Skelton width={300} height={20} /></p> });
 
-  const { data: createdAt } = useCurrentCreatedAt();
-  const { data: updatedAt } = useCurrentUpdatedAt();
-  const { data: creator } = useCreator();
-  const { data: revisionAuthor } = useRevisionAuthor();
+  const { data: page } = useSWRxCurrentPage();
+
+  if (page == null) {
+    return <></>;
+  }
 
   return (
     <div className="page-content-footer py-4 d-edit-none d-print-none">
       <div className="grw-container-convertible">
         <div className="page-meta">
-          <AuthorInfo user={creator} date={createdAt} mode="create" locate="footer" />
-          <AuthorInfo user={revisionAuthor} date={updatedAt} mode="update" locate="footer" />
+          <AuthorInfo user={page.creator} date={page.createdAt} mode="create" locate="footer" />
+          <AuthorInfo user={page.revision.author} date={page.updatedAt} mode="update" locate="footer" />
         </div>
       </div>
     </div>

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

@@ -60,7 +60,6 @@ import {
   useIsAclEnabled, useIsUserPage, useIsNotCreatable,
   useCsrfToken, useIsSearchScopeChildrenAsDefault, useCurrentPageId, useCurrentPathname,
   useIsSlackConfigured, useIsBlinkedHeaderAtBoot, useRendererConfig, useEditingMarkdown,
-  useCurrentCreatedAt, useCurrentUpdatedAt, useCreator, useRevisionAuthor,
 } from '../stores/context';
 import { useXss } from '../stores/xss';
 
@@ -250,11 +249,6 @@ const GrowiPage: NextPage<Props> = (props: Props) => {
   useCurrentPathname(props.currentPathname);
   useEditingMarkdown(pageWithMeta?.data.revision.body);
 
-  useCurrentCreatedAt(pageWithMeta?.data.createdAt);
-  useCurrentUpdatedAt(pageWithMeta?.data.updatedAt);
-  useCreator(pageWithMeta?.data.creator);
-  useRevisionAuthor(pageWithMeta?.data.revision.author);
-
   // sync pathname by Shallow Routing https://nextjs.org/docs/routing/shallow-routing
   useEffect(() => {
     if (isClient() && window.location.pathname !== props.currentPathname) {

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

@@ -52,10 +52,6 @@ export const useRevisionId = (initialData?: Nullable<any>): SWRResponse<Nullable
   return useStaticSWR<Nullable<any>, Error>('revisionId', initialData);
 };
 
-export const useRevisionAuthor = (initialData?: Nullable<IUser>): SWRResponse<Nullable<IUser>, Error> => {
-  return useStaticSWR<Nullable<IUser>, Error>('revisionAuthor', initialData);
-};
-
 export const useCurrentPagePath = (initialData?: Nullable<string>): SWRResponse<Nullable<string>, Error> => {
   return useStaticSWR<Nullable<string>, Error>('currentPagePath', initialData);
 };
@@ -68,18 +64,10 @@ export const useCurrentPageId = (initialData?: Nullable<string>): SWRResponse<Nu
   return useStaticSWR<Nullable<string>, Error>('currentPageId', initialData);
 };
 
-export const useCreator = (initialData?: Nullable<IUser>): SWRResponse<Nullable<IUser>, Error> => {
-  return useStaticSWR<Nullable<IUser>, Error>('creator', initialData);
-};
-
 export const useRevisionCreatedAt = (initialData?: Nullable<any>): SWRResponse<Nullable<any>, Error> => {
   return useStaticSWR<Nullable<any>, Error>('revisionCreatedAt', initialData);
 };
 
-export const useCurrentCreatedAt = (initialData?: Nullable<Date>): SWRResponse<Nullable<Date>, Error> => {
-  return useStaticSWR<Nullable<Date>, Error>('createdAt', initialData);
-};
-
 export const useCurrentUpdatedAt = (initialData?: Nullable<Date>): SWRResponse<Nullable<Date>, Error> => {
   return useStaticSWR<Nullable<Date>, Error>('updatedAt', initialData);
 };