mirror of
https://github.com/nocobase/nocobase
synced 2024-11-16 04:05:13 +00:00
perf(CollectionHistoryProvider): optimize location context
This commit is contained in:
parent
d7a80d0ad6
commit
d786145fc2
@ -11,8 +11,22 @@ import React, { FC, useEffect } from 'react';
|
|||||||
import { Location, NavigateFunction, NavigateOptions, useLocation, useNavigate } from 'react-router-dom';
|
import { Location, NavigateFunction, NavigateOptions, useLocation, useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
const NavigateNoUpdateContext = React.createContext<NavigateFunction>(null);
|
const NavigateNoUpdateContext = React.createContext<NavigateFunction>(null);
|
||||||
|
NavigateNoUpdateContext.displayName = 'NavigateNoUpdateContext';
|
||||||
|
|
||||||
const LocationNoUpdateContext = React.createContext<Location>(null);
|
const LocationNoUpdateContext = React.createContext<Location>(null);
|
||||||
|
LocationNoUpdateContext.displayName = 'LocationNoUpdateContext';
|
||||||
|
|
||||||
export const LocationSearchContext = React.createContext<string>('');
|
export const LocationSearchContext = React.createContext<string>('');
|
||||||
|
LocationSearchContext.displayName = 'LocationSearchContext';
|
||||||
|
|
||||||
|
const IsAdminPageContext = React.createContext<boolean>(false);
|
||||||
|
IsAdminPageContext.displayName = 'IsAdminPageContext';
|
||||||
|
|
||||||
|
const IsAdminPageProvider: FC = ({ children }) => {
|
||||||
|
const location = useLocation();
|
||||||
|
const isAdminPage = location.pathname.startsWith('/admin');
|
||||||
|
return <IsAdminPageContext.Provider value={isAdminPage}>{children}</IsAdminPageContext.Provider>;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When the URL changes, components that use `useNavigate` will re-render.
|
* When the URL changes, components that use `useNavigate` will re-render.
|
||||||
@ -78,11 +92,17 @@ export const useLocationSearch = () => {
|
|||||||
return React.useContext(LocationSearchContext);
|
return React.useContext(LocationSearchContext);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const useIsAdminPage = () => {
|
||||||
|
return React.useContext(IsAdminPageContext);
|
||||||
|
};
|
||||||
|
|
||||||
export const CustomRouterContextProvider: FC = ({ children }) => {
|
export const CustomRouterContextProvider: FC = ({ children }) => {
|
||||||
return (
|
return (
|
||||||
<NavigateNoUpdateProvider>
|
<NavigateNoUpdateProvider>
|
||||||
<LocationNoUpdateProvider>
|
<LocationNoUpdateProvider>
|
||||||
|
<IsAdminPageProvider>
|
||||||
<LocationSearchProvider>{children}</LocationSearchProvider>
|
<LocationSearchProvider>{children}</LocationSearchProvider>
|
||||||
|
</IsAdminPageProvider>
|
||||||
</LocationNoUpdateProvider>
|
</LocationNoUpdateProvider>
|
||||||
</NavigateNoUpdateProvider>
|
</NavigateNoUpdateProvider>
|
||||||
);
|
);
|
||||||
|
@ -8,8 +8,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import React, { createContext, useCallback, useContext, useMemo } from 'react';
|
import React, { createContext, useCallback, useContext, useMemo } from 'react';
|
||||||
import { useLocation } from 'react-router-dom';
|
|
||||||
import { useAPIClient, useRequest } from '../api-client';
|
import { useAPIClient, useRequest } from '../api-client';
|
||||||
|
import { useIsAdminPage } from '../application/CustomRouterContextProvider';
|
||||||
import { useAppSpin } from '../application/hooks/useAppSpin';
|
import { useAppSpin } from '../application/hooks/useAppSpin';
|
||||||
|
|
||||||
export interface CollectionHistoryContextValue {
|
export interface CollectionHistoryContextValue {
|
||||||
@ -38,9 +38,7 @@ const options = {
|
|||||||
|
|
||||||
export const CollectionHistoryProvider: React.FC = (props) => {
|
export const CollectionHistoryProvider: React.FC = (props) => {
|
||||||
const api = useAPIClient();
|
const api = useAPIClient();
|
||||||
const location = useLocation();
|
const isAdminPage = useIsAdminPage();
|
||||||
|
|
||||||
const isAdminPage = location.pathname.startsWith('/admin');
|
|
||||||
const token = api.auth.getToken() || '';
|
const token = api.auth.getToken() || '';
|
||||||
const { render } = useAppSpin();
|
const { render } = useAppSpin();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user