Răsfoiți Sursa

update useEditingMarkdown

Shun Miyazawa 3 ani în urmă
părinte
comite
4c794a42a0

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

@@ -57,14 +57,14 @@ import DisplaySwitcher from '../components/Page/DisplaySwitcher';
 // import PageStatusAlert from '../client/js/components/PageStatusAlert';
 // import PageStatusAlert from '../client/js/components/PageStatusAlert';
 import {
 import {
   useCurrentUser,
   useCurrentUser,
-  useIsLatestRevision, useCurrentRevisionId,
+  useIsLatestRevision, useCurrentRevisionId, useRevisionBody,
   useIsForbidden, useIsNotFound, useIsSharedUser,
   useIsForbidden, useIsNotFound, useIsSharedUser,
   useIsEnabledStaleNotification, useIsIdenticalPath,
   useIsEnabledStaleNotification, useIsIdenticalPath,
   useIsSearchServiceConfigured, useIsSearchServiceReachable, useDisableLinkSharing,
   useIsSearchServiceConfigured, useIsSearchServiceReachable, useDisableLinkSharing,
   useDrawioUri, useHackmdUri, useDefaultIndentSize, useIsIndentSizeForced,
   useDrawioUri, useHackmdUri, useDefaultIndentSize, useIsIndentSizeForced,
   useIsAclEnabled, useIsSearchPage, useTemplateTagData, useTemplateBodyData, useIsEnabledAttachTitleHeader,
   useIsAclEnabled, useIsSearchPage, useTemplateTagData, useTemplateBodyData, useIsEnabledAttachTitleHeader,
   useCsrfToken, useIsSearchScopeChildrenAsDefault, useCurrentPageId, useCurrentPathname,
   useCsrfToken, useIsSearchScopeChildrenAsDefault, useCurrentPageId, useCurrentPathname,
-  useIsSlackConfigured, useRendererConfig, useEditingMarkdown,
+  useIsSlackConfigured, useRendererConfig,
   useEditorConfig, useIsAllReplyShown, useIsUploadableFile, useIsUploadableImage, useCustomizedLogoSrc, useIsContainerFluid,
   useEditorConfig, useIsAllReplyShown, useIsUploadableFile, useIsUploadableImage, useCustomizedLogoSrc, useIsContainerFluid,
 } from '../stores/context';
 } from '../stores/context';
 
 
@@ -255,7 +255,7 @@ const GrowiPage: NextPage<Props> = (props: Props) => {
 
 
   const { data: currentPage } = useSWRxCurrentPage(undefined, pageWithMeta?.data ?? null); // store initial data
   const { data: currentPage } = useSWRxCurrentPage(undefined, pageWithMeta?.data ?? null); // store initial data
 
 
-  useEditingMarkdown(pageWithMeta?.data.revision?.body);
+  useRevisionBody(pageWithMeta?.data.revision?.body);
 
 
   const { data: grantData } = useSWRxIsGrantNormalized(pageId);
   const { data: grantData } = useSWRxIsGrantNormalized(pageId);
   const { mutate: mutateSelectedGrant } = useSelectedGrant();
   const { mutate: mutateSelectedGrant } = useSelectedGrant();
@@ -467,20 +467,18 @@ async function injectRoutingInformation(context: GetServerSidePropsContext, prop
 
 
   const page = props.pageWithMeta?.data;
   const page = props.pageWithMeta?.data;
 
 
+  props.isNotFound = !(page != null && !page.isEmpty);
+
   if (props.isIdenticalPathPage) {
   if (props.isIdenticalPathPage) {
     // TBD
     // TBD
-    props.isNotFound = false;
   }
   }
   else if (page == null) {
   else if (page == null) {
-    props.isNotFound = true;
     props.isNotCreatablePage = !isCreatablePage(currentPathname);
     props.isNotCreatablePage = !isCreatablePage(currentPathname);
     // check the page is forbidden or just does not exist.
     // check the page is forbidden or just does not exist.
     const count = isPermalink ? await Page.count({ _id: pageId }) : await Page.count({ path: currentPathname });
     const count = isPermalink ? await Page.count({ _id: pageId }) : await Page.count({ path: currentPathname });
     props.isForbidden = count > 0;
     props.isForbidden = count > 0;
   }
   }
   else {
   else {
-    props.isNotFound = page.isEmpty;
-
     // /62a88db47fed8b2d94f30000 ==> /path/to/page
     // /62a88db47fed8b2d94f30000 ==> /path/to/page
     if (isPermalink && page.isEmpty) {
     if (isPermalink && page.isEmpty) {
       props.currentPathname = page.path;
       props.currentPathname = page.path;

+ 15 - 7
packages/app/src/stores/context.tsx

@@ -186,6 +186,10 @@ export const useIsBlinkedHeaderAtBoot = (initialData?: boolean): SWRResponse<boo
   return useContextSWR('isBlinkedAtBoot', initialData, { fallbackData: false });
   return useContextSWR('isBlinkedAtBoot', initialData, { fallbackData: false });
 };
 };
 
 
+export const useRevisionBody = (initialData?: string): SWRResponse<string, Error> => {
+  return useContextSWR('revisionBody', initialData);
+};
+
 export const useIsUploadableImage = (initialData?: boolean): SWRResponse<boolean, Error> => {
 export const useIsUploadableImage = (initialData?: boolean): SWRResponse<boolean, Error> => {
   return useContextSWR('isUploadableImage', initialData);
   return useContextSWR('isUploadableImage', initialData);
 };
 };
@@ -249,22 +253,26 @@ export const useIsEditable = (): SWRResponse<boolean, Error> => {
   );
   );
 };
 };
 
 
-export const useEditingMarkdown = (initialData?: string): SWRResponse<string, Error> => {
-  const { data: currentPathname } = useCurrentPathname();
-  const { data: templateBodyData } = useTemplateBodyData();
+export const useEditingMarkdown = (): SWRResponse<string, Error> => {
   const { data: isNotFound } = useIsNotFound();
   const { data: isNotFound } = useIsNotFound();
   const { data: isEnabledAttachTitleHeader } = useIsEnabledAttachTitleHeader();
   const { data: isEnabledAttachTitleHeader } = useIsEnabledAttachTitleHeader();
+  const { data: currentPathname } = useCurrentPathname();
+  const { data: revisionBody } = useRevisionBody();
+  const { data: templateBodyData } = useTemplateBodyData();
+
+  const shoudFetch = !(isNotFound && templateBodyData == null && !isEnabledAttachTitleHeader);
+  const key = [isNotFound, isEnabledAttachTitleHeader, currentPathname, revisionBody, templateBodyData];
 
 
   return useSWRImmutable(
   return useSWRImmutable(
-    ['editingMarkdown', currentPathname, templateBodyData, isNotFound, isEnabledAttachTitleHeader],
-    (key: Key, currentPathname: string, templateBodyData: string, isNotFound: boolean, isEnabledAttachTitleHeader: boolean) => {
+    shoudFetch ? key : null,
+    (isNotFound: boolean, isEnabledAttachTitleHeader: boolean, currentPathname: string, revisionBody?: string, templateBodyData?: string) => {
       if (!isNotFound) {
       if (!isNotFound) {
-        return initialData ?? '';
+        return revisionBody ?? '';
       }
       }
 
 
       let initialEditingMarkdown = '';
       let initialEditingMarkdown = '';
       if (isEnabledAttachTitleHeader) {
       if (isEnabledAttachTitleHeader) {
-        initialEditingMarkdown += `${pathUtils.attachTitleHeader(currentPathname)}\n`;
+        initialEditingMarkdown += `${pathUtils.attachTitleHeader(currentPathname ?? '')}\n`;
       }
       }
       if (templateBodyData != null) {
       if (templateBodyData != null) {
         initialEditingMarkdown += `${templateBodyData}\n`;
         initialEditingMarkdown += `${templateBodyData}\n`;