Sfoglia il codice sorgente

useEditingMarkdown to computed SWR

Shun Miyazawa 3 anni fa
parent
commit
aae5378bcc

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

@@ -62,7 +62,7 @@ import {
   useIsEnabledStaleNotification, useIsIdenticalPath,
   useIsSearchServiceConfigured, useIsSearchServiceReachable, useDisableLinkSharing,
   useDrawioUri, useHackmdUri, useDefaultIndentSize, useIsIndentSizeForced,
-  useIsAclEnabled, useIsSearchPage, useTemplateTagData, useTemplateBodyData,
+  useIsAclEnabled, useIsSearchPage, useTemplateTagData, useTemplateBodyData, useIsEnabledAttachTitleHeader,
   useCsrfToken, useIsSearchScopeChildrenAsDefault, useCurrentPageId, useCurrentPathname,
   useIsSlackConfigured, useRendererConfig, useEditingMarkdown,
   useEditorConfig, useIsAllReplyShown, useIsUploadableFile, useIsUploadableImage, useCustomizedLogoSrc, useIsContainerFluid,
@@ -220,6 +220,7 @@ const GrowiPage: NextPage<Props> = (props: Props) => {
   useTemplateTagData(props.templateTagData);
   useTemplateBodyData(props.templateBodyData);
 
+  useIsEnabledAttachTitleHeader(props.isEnabledAttachTitleHeader);
   useIsSearchServiceConfigured(props.isSearchServiceConfigured);
   useIsSearchServiceReachable(props.isSearchServiceReachable);
   useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
@@ -254,15 +255,7 @@ const GrowiPage: NextPage<Props> = (props: Props) => {
 
   const { data: currentPage } = useSWRxCurrentPage(undefined, pageWithMeta?.data ?? null); // store initial data
 
-  let initialEditingMarkdown = '';
-  if (props.isEnabledAttachTitleHeader && pageWithMeta == null) {
-    initialEditingMarkdown += `${attachTitleHeader(props.currentPathname)}\n`;
-  }
-  if (props.templateBodyData != null) {
-    initialEditingMarkdown += `${props.templateBodyData}\n`;
-  }
-
-  useEditingMarkdown(pageWithMeta?.data.revision?.body ?? initialEditingMarkdown ?? '');
+  useEditingMarkdown(pageWithMeta?.data.revision?.body);
 
   const { data: grantData } = useSWRxIsGrantNormalized(pageId);
   const { mutate: mutateSelectedGrant } = useSelectedGrant();
@@ -476,6 +469,7 @@ async function injectRoutingInformation(context: GetServerSidePropsContext, prop
 
   if (props.isIdenticalPathPage) {
     // TBD
+    props.isNotFound = false;
   }
   else if (page == null) {
     props.isNotFound = true;

+ 27 - 5
packages/app/src/stores/context.tsx

@@ -1,7 +1,8 @@
-import { IUser, pagePathUtils } from '@growi/core';
+import { IUser, pagePathUtils, pathUtils } from '@growi/core';
 import { HtmlElementNode } from 'rehype-toc';
 import { Key, SWRResponse, useSWRConfig } from 'swr';
 import useSWRImmutable from 'swr/immutable';
+import { boolean } from 'yargs';
 
 
 import { SupportedActionType } from '~/interfaces/activity';
@@ -189,10 +190,6 @@ export const useIsBlinkedHeaderAtBoot = (initialData?: boolean): SWRResponse<boo
   return useContextSWR('isBlinkedAtBoot', initialData, { fallbackData: false });
 };
 
-export const useEditingMarkdown = (initialData?: string): SWRResponse<string, Error> => {
-  return useContextSWR('currentMarkdown', initialData);
-};
-
 export const useIsUploadableImage = (initialData?: boolean): SWRResponse<boolean, Error> => {
   return useContextSWR('isUploadableImage', initialData);
 };
@@ -255,3 +252,28 @@ export const useIsEditable = (): SWRResponse<boolean, Error> => {
     },
   );
 };
+
+export const useEditingMarkdown = (initialData?: string): SWRResponse<string, Error> => {
+  const { data: currentPathname } = useCurrentPathname();
+  const { data: templateBodyData } = useTemplateBodyData();
+  const { data: isNotFound } = useIsNotFound();
+  const { data: isEnabledAttachTitleHeader } = useIsEnabledAttachTitleHeader();
+
+  return useSWRImmutable(
+    ['editingMarkdown', currentPathname, templateBodyData, isNotFound, isEnabledAttachTitleHeader],
+    (key: Key, currentPathname: string, templateBodyData: string, isNotFound: boolean, isEnabledAttachTitleHeader: boolean) => {
+      if (!isNotFound) {
+        return initialData ?? '';
+      }
+
+      let initialEditingMarkdown = '';
+      if (isEnabledAttachTitleHeader) {
+        initialEditingMarkdown += `${pathUtils.attachTitleHeader(currentPathname)}\n`;
+      }
+      if (templateBodyData != null) {
+        initialEditingMarkdown += `${templateBodyData}\n`;
+      }
+      return initialEditingMarkdown;
+    },
+  );
+};