From d786145fc2eb9982f435be02be3b28c180a5c2f3 Mon Sep 17 00:00:00 2001 From: Zeke Zhang <958414905@qq.com> Date: Sun, 3 Nov 2024 17:57:32 +0800 Subject: [PATCH] perf(CollectionHistoryProvider): optimize location context --- .../CustomRouterContextProvider.tsx | 22 ++++++++++++++++++- .../CollectionHistoryProvider.tsx | 6 ++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/core/client/src/application/CustomRouterContextProvider.tsx b/packages/core/client/src/application/CustomRouterContextProvider.tsx index 5ae2ca45fc..e9c2111bf5 100644 --- a/packages/core/client/src/application/CustomRouterContextProvider.tsx +++ b/packages/core/client/src/application/CustomRouterContextProvider.tsx @@ -11,8 +11,22 @@ import React, { FC, useEffect } from 'react'; import { Location, NavigateFunction, NavigateOptions, useLocation, useNavigate } from 'react-router-dom'; const NavigateNoUpdateContext = React.createContext(null); +NavigateNoUpdateContext.displayName = 'NavigateNoUpdateContext'; + const LocationNoUpdateContext = React.createContext(null); +LocationNoUpdateContext.displayName = 'LocationNoUpdateContext'; + export const LocationSearchContext = React.createContext(''); +LocationSearchContext.displayName = 'LocationSearchContext'; + +const IsAdminPageContext = React.createContext(false); +IsAdminPageContext.displayName = 'IsAdminPageContext'; + +const IsAdminPageProvider: FC = ({ children }) => { + const location = useLocation(); + const isAdminPage = location.pathname.startsWith('/admin'); + return {children}; +}; /** * When the URL changes, components that use `useNavigate` will re-render. @@ -78,11 +92,17 @@ export const useLocationSearch = () => { return React.useContext(LocationSearchContext); }; +export const useIsAdminPage = () => { + return React.useContext(IsAdminPageContext); +}; + export const CustomRouterContextProvider: FC = ({ children }) => { return ( - {children} + + {children} + ); diff --git a/packages/core/client/src/collection-manager/CollectionHistoryProvider.tsx b/packages/core/client/src/collection-manager/CollectionHistoryProvider.tsx index 225607c66b..6a3471ab71 100644 --- a/packages/core/client/src/collection-manager/CollectionHistoryProvider.tsx +++ b/packages/core/client/src/collection-manager/CollectionHistoryProvider.tsx @@ -8,8 +8,8 @@ */ import React, { createContext, useCallback, useContext, useMemo } from 'react'; -import { useLocation } from 'react-router-dom'; import { useAPIClient, useRequest } from '../api-client'; +import { useIsAdminPage } from '../application/CustomRouterContextProvider'; import { useAppSpin } from '../application/hooks/useAppSpin'; export interface CollectionHistoryContextValue { @@ -38,9 +38,7 @@ const options = { export const CollectionHistoryProvider: React.FC = (props) => { const api = useAPIClient(); - const location = useLocation(); - - const isAdminPage = location.pathname.startsWith('/admin'); + const isAdminPage = useIsAdminPage(); const token = api.auth.getToken() || ''; const { render } = useAppSpin();