Просмотр исходного кода

implement useResetPageContextsOnNextRouting

Yuki Takei 7 месяцев назад
Родитель
Сommit
4a27980c83

+ 2 - 11
apps/app/src/pages/[[...path]]/index.page.tsx

@@ -1,7 +1,6 @@
 import type { ReactNode, JSX } from 'react';
 import type { ReactNode, JSX } from 'react';
 import React, { useEffect } from 'react';
 import React, { useEffect } from 'react';
 
 
-
 import EventEmitter from 'events';
 import EventEmitter from 'events';
 
 
 import { isClient } from '@growi/core/dist/utils';
 import { isClient } from '@growi/core/dist/utils';
@@ -15,10 +14,6 @@ import { BasicLayout } from '~/components/Layout/BasicLayout';
 import { PageView } from '~/components/PageView/PageView';
 import { PageView } from '~/components/PageView/PageView';
 import { DrawioViewerScript } from '~/components/Script/DrawioViewerScript';
 import { DrawioViewerScript } from '~/components/Script/DrawioViewerScript';
 import { useEditorModeClassName } from '~/services/layout/use-editor-mode-class-name';
 import { useEditorModeClassName } from '~/services/layout/use-editor-mode-class-name';
-import {
-  useIsSharedUser,
-  useIsSearchPage,
-} from '~/states/context';
 import {
 import {
   useCurrentPageData, useCurrentPageId, useCurrentPagePath, usePageNotFound,
   useCurrentPageData, useCurrentPageId, useCurrentPagePath, usePageNotFound,
 } from '~/states/page';
 } from '~/states/page';
@@ -99,8 +94,6 @@ const Page: NextPageWithLayout<Props> = (props: Props) => {
   const [isNotFound] = usePageNotFound();
   const [isNotFound] = usePageNotFound();
   const [rendererConfig] = useRendererConfig();
   const [rendererConfig] = useRendererConfig();
   const [, setRedirectFrom] = useRedirectFrom();
   const [, setRedirectFrom] = useRedirectFrom();
-  const [, setIsSharedUser] = useIsSharedUser();
-  const [, setIsSearchPage] = useIsSearchPage();
   const [, setEditingMarkdown] = useEditingMarkdown();
   const [, setEditingMarkdown] = useEditingMarkdown();
 
 
   const { trigger: mutateCurrentPageYjsDataFromApi } = useSWRMUTxCurrentPageYjsData();
   const { trigger: mutateCurrentPageYjsDataFromApi } = useSWRMUTxCurrentPageYjsData();
@@ -116,12 +109,10 @@ const Page: NextPageWithLayout<Props> = (props: Props) => {
   // If initial props and skipSSR, fetch page data on client-side
   // If initial props and skipSSR, fetch page data on client-side
   useInitialCSRFetch(isInitialProps(props) && props.skipSSR);
   useInitialCSRFetch(isInitialProps(props) && props.skipSSR);
 
 
-  // Initialize atom values
+  // Initialize redirectFrom atom values
   useEffect(() => {
   useEffect(() => {
     setRedirectFrom(props.redirectFrom ?? null);
     setRedirectFrom(props.redirectFrom ?? null);
-    setIsSharedUser(false); // this page can't be routed for '/share'
-    setIsSearchPage(false);
-  }, [props.redirectFrom, setRedirectFrom, setIsSharedUser, setIsSearchPage]);
+  }, [props.redirectFrom, setRedirectFrom]);
 
 
   // Optimized effects with minimal dependencies
   // Optimized effects with minimal dependencies
   useEffect(() => {
   useEffect(() => {

+ 3 - 0
apps/app/src/pages/_app.page.tsx

@@ -21,6 +21,7 @@ import { swrGlobalConfiguration } from '~/utils/swr-utils';
 import { getLocaleAtServerSide } from './utils/locale';
 import { getLocaleAtServerSide } from './utils/locale';
 import { useNextjsRoutingPageRegister } from './utils/nextjs-routing-utils';
 import { useNextjsRoutingPageRegister } from './utils/nextjs-routing-utils';
 import { registerTransformerForObjectId } from './utils/objectid-transformer';
 import { registerTransformerForObjectId } from './utils/objectid-transformer';
+import { useResetPageContextsOnNextRouting } from './utils/reset-page-contexts';
 
 
 import '~/styles/prebuilt/vendor.css';
 import '~/styles/prebuilt/vendor.css';
 import '~/styles/style-app.scss';
 import '~/styles/style-app.scss';
@@ -58,6 +59,8 @@ const GrowiAppSubstance = ({ Component, pageProps, userLocale }: GrowiAppProps):
 
 
   useNextjsRoutingPageRegister(pageProps.nextjsRoutingPage);
   useNextjsRoutingPageRegister(pageProps.nextjsRoutingPage);
 
 
+  useResetPageContextsOnNextRouting();
+
   useEffect(() => {
   useEffect(() => {
     const updateLangAttribute = () => {
     const updateLangAttribute = () => {
       if (document.documentElement.getAttribute('lang') !== userLocale) {
       if (document.documentElement.getAttribute('lang') !== userLocale) {

+ 1 - 10
apps/app/src/pages/share/[[...path]]/index.page.tsx

@@ -1,5 +1,5 @@
 import type { ReactNode, JSX } from 'react';
 import type { ReactNode, JSX } from 'react';
-import React, { useEffect } from 'react';
+import React from 'react';
 
 
 import type {
 import type {
   GetServerSideProps, GetServerSidePropsContext,
   GetServerSideProps, GetServerSidePropsContext,
@@ -13,7 +13,6 @@ import { ShareLinkPageView } from '~/components/ShareLinkPageView';
 import type { CommonEachProps } from '~/pages/common-props';
 import type { CommonEachProps } from '~/pages/common-props';
 import { NextjsRoutingType, detectNextjsRoutingType } from '~/pages/utils/nextjs-routing-utils';
 import { NextjsRoutingType, detectNextjsRoutingType } from '~/pages/utils/nextjs-routing-utils';
 import { useCustomTitleForPage } from '~/pages/utils/page-title-customization';
 import { useCustomTitleForPage } from '~/pages/utils/page-title-customization';
-import { useIsSearchPage, useIsSharedUser } from '~/states/context';
 import {
 import {
   useCurrentPageData, useCurrentPagePath,
   useCurrentPageData, useCurrentPagePath,
 } from '~/states/page';
 } from '~/states/page';
@@ -60,8 +59,6 @@ const SharedPage: NextPageWithLayout<Props> = (props: Props) => {
   const [currentPage] = useCurrentPageData();
   const [currentPage] = useCurrentPageData();
   const [currentPagePath] = useCurrentPagePath();
   const [currentPagePath] = useCurrentPagePath();
   const [rendererConfig] = useRendererConfig();
   const [rendererConfig] = useRendererConfig();
-  const [, setIsSharedUser] = useIsSharedUser();
-  const [, setIsSearchPage] = useIsSearchPage();
   const [isLinkSharingDisabled] = useDisableLinkSharing();
   const [isLinkSharingDisabled] = useDisableLinkSharing();
 
 
   // Use custom hooks for navigation and routing
   // Use custom hooks for navigation and routing
@@ -70,12 +67,6 @@ const SharedPage: NextPageWithLayout<Props> = (props: Props) => {
   // If initial props and skipSSR, fetch page data on client-side
   // If initial props and skipSSR, fetch page data on client-side
   useInitialCSRFetch(isInitialProps(props) && props.skipSSR);
   useInitialCSRFetch(isInitialProps(props) && props.skipSSR);
 
 
-  // Initialize atom values
-  useEffect(() => {
-    setIsSharedUser(true);
-    setIsSearchPage(false);
-  }, [setIsSharedUser, setIsSearchPage]);
-
   // If the data on the page changes without router.push, pageWithMeta remains old because getServerSideProps() is not executed
   // If the data on the page changes without router.push, pageWithMeta remains old because getServerSideProps() is not executed
   // So preferentially take page data from useSWRxCurrentPage
   // So preferentially take page data from useSWRxCurrentPage
   const pagePath = currentPagePath ?? props.currentPathname;
   const pagePath = currentPagePath ?? props.currentPathname;

+ 25 - 0
apps/app/src/pages/utils/reset-page-contexts.ts

@@ -0,0 +1,25 @@
+import { useEffect } from 'react';
+
+import { useRouter } from 'next/router';
+
+import { useIsSearchPage, useIsSharedUser } from '~/states/context';
+import { useRedirectFrom } from '~/states/page/redirect';
+
+export const useResetPageContextsOnNextRouting = (): void => {
+  const router = useRouter();
+  const [, setRedirectFrom] = useRedirectFrom();
+  const [, setIsSharedUser] = useIsSharedUser();
+  const [, setIsSearchPage] = useIsSearchPage();
+
+  useEffect(() => {
+    const resetPageContexts = () => {
+      setRedirectFrom(null);
+      setIsSharedUser(false);
+      setIsSearchPage(false);
+    };
+    router.events.on('routeChangeStart', resetPageContexts);
+    return () => {
+      router.events.off('routeChangeStart', resetPageContexts);
+    };
+  }, [router, setIsSearchPage, setIsSharedUser, setRedirectFrom]);
+};