2024-12-11 16:11:27 +08:00
|
|
|
// contexts/User/index.jsx
|
|
|
|
|
|
2025-05-20 01:58:44 +08:00
|
|
|
import React, { useState, useEffect, useCallback } from 'react';
|
|
|
|
|
import { useLocation } from 'react-router-dom';
|
|
|
|
|
import { isMobile as getIsMobile } from '../../helpers/index.js';
|
2024-12-11 16:11:27 +08:00
|
|
|
|
|
|
|
|
export const StyleContext = React.createContext({
|
|
|
|
|
dispatch: () => null,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const StyleProvider = ({ children }) => {
|
2025-05-20 01:58:44 +08:00
|
|
|
const location = useLocation();
|
|
|
|
|
const initialIsMobile = getIsMobile();
|
|
|
|
|
|
|
|
|
|
const initialPathname = location.pathname;
|
|
|
|
|
let initialShowSiderValue = false;
|
|
|
|
|
let initialInnerPaddingValue = false;
|
|
|
|
|
|
|
|
|
|
if (initialPathname.includes('/console')) {
|
|
|
|
|
initialShowSiderValue = !initialIsMobile;
|
|
|
|
|
initialInnerPaddingValue = true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-11 16:11:27 +08:00
|
|
|
const [state, setState] = useState({
|
2025-05-20 01:58:44 +08:00
|
|
|
isMobile: initialIsMobile,
|
|
|
|
|
showSider: initialShowSiderValue,
|
2025-03-10 19:48:17 +08:00
|
|
|
siderCollapsed: false,
|
2025-05-20 01:58:44 +08:00
|
|
|
shouldInnerPadding: initialInnerPaddingValue,
|
2025-05-20 12:59:47 +08:00
|
|
|
manualSiderControl: false,
|
2024-12-11 16:11:27 +08:00
|
|
|
});
|
|
|
|
|
|
2025-05-20 01:58:44 +08:00
|
|
|
const dispatch = useCallback((action) => {
|
2024-12-11 16:11:27 +08:00
|
|
|
if ('type' in action) {
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
case 'TOGGLE_SIDER':
|
2025-05-20 12:59:47 +08:00
|
|
|
setState((prev) => ({
|
|
|
|
|
...prev,
|
|
|
|
|
showSider: !prev.showSider,
|
|
|
|
|
manualSiderControl: true
|
|
|
|
|
}));
|
2024-12-11 16:11:27 +08:00
|
|
|
break;
|
|
|
|
|
case 'SET_SIDER':
|
2025-05-20 12:59:47 +08:00
|
|
|
setState((prev) => ({
|
|
|
|
|
...prev,
|
|
|
|
|
showSider: action.payload,
|
|
|
|
|
manualSiderControl: action.manual || false
|
|
|
|
|
}));
|
2024-12-11 16:11:27 +08:00
|
|
|
break;
|
|
|
|
|
case 'SET_MOBILE':
|
2025-04-04 12:00:38 +08:00
|
|
|
setState((prev) => ({ ...prev, isMobile: action.payload }));
|
2024-12-11 16:11:27 +08:00
|
|
|
break;
|
2025-03-10 19:48:17 +08:00
|
|
|
case 'SET_SIDER_COLLAPSED':
|
2025-04-04 12:00:38 +08:00
|
|
|
setState((prev) => ({ ...prev, siderCollapsed: action.payload }));
|
|
|
|
|
break;
|
2024-12-11 23:08:52 +08:00
|
|
|
case 'SET_INNER_PADDING':
|
2025-04-04 12:00:38 +08:00
|
|
|
setState((prev) => ({ ...prev, shouldInnerPadding: action.payload }));
|
2024-12-11 21:17:46 +08:00
|
|
|
break;
|
2024-12-11 16:11:27 +08:00
|
|
|
default:
|
2025-04-04 12:00:38 +08:00
|
|
|
setState((prev) => ({ ...prev, ...action }));
|
2024-12-11 16:11:27 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2025-04-04 12:00:38 +08:00
|
|
|
setState((prev) => ({ ...prev, ...action }));
|
2024-12-11 16:11:27 +08:00
|
|
|
}
|
2025-05-20 01:58:44 +08:00
|
|
|
}, []);
|
2024-12-11 16:11:27 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-05-20 01:58:44 +08:00
|
|
|
const updateMobileStatus = () => {
|
2025-05-20 12:59:47 +08:00
|
|
|
const currentIsMobile = getIsMobile();
|
|
|
|
|
if (!currentIsMobile &&
|
|
|
|
|
(location.pathname === '/console' || location.pathname.startsWith('/console/'))) {
|
|
|
|
|
dispatch({ type: 'SET_SIDER', payload: true, manual: false });
|
|
|
|
|
}
|
|
|
|
|
dispatch({ type: 'SET_MOBILE', payload: currentIsMobile });
|
2025-03-10 19:48:17 +08:00
|
|
|
};
|
2025-05-20 01:58:44 +08:00
|
|
|
window.addEventListener('resize', updateMobileStatus);
|
|
|
|
|
return () => window.removeEventListener('resize', updateMobileStatus);
|
2025-05-20 12:59:47 +08:00
|
|
|
}, [dispatch, location.pathname]);
|
2025-03-10 19:48:17 +08:00
|
|
|
|
2025-05-20 01:58:44 +08:00
|
|
|
useEffect(() => {
|
2025-05-20 12:59:47 +08:00
|
|
|
if (state.isMobile && state.showSider && !state.manualSiderControl) {
|
2025-05-20 01:58:44 +08:00
|
|
|
dispatch({ type: 'SET_SIDER', payload: false });
|
|
|
|
|
}
|
2025-05-20 12:59:47 +08:00
|
|
|
}, [state.isMobile, state.showSider, state.manualSiderControl, dispatch]);
|
2025-03-10 19:48:17 +08:00
|
|
|
|
2025-05-20 01:58:44 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
const currentPathname = location.pathname;
|
|
|
|
|
const currentlyMobile = getIsMobile();
|
2025-04-04 12:00:38 +08:00
|
|
|
|
2025-05-20 01:58:44 +08:00
|
|
|
if (currentPathname === '/console' || currentPathname.startsWith('/console/')) {
|
2025-05-20 12:59:47 +08:00
|
|
|
dispatch({
|
|
|
|
|
type: 'SET_SIDER',
|
|
|
|
|
payload: !currentlyMobile,
|
|
|
|
|
manual: false
|
|
|
|
|
});
|
2025-05-20 01:58:44 +08:00
|
|
|
dispatch({ type: 'SET_INNER_PADDING', payload: true });
|
|
|
|
|
} else {
|
2025-05-20 12:59:47 +08:00
|
|
|
dispatch({
|
|
|
|
|
type: 'SET_SIDER',
|
|
|
|
|
payload: false,
|
|
|
|
|
manual: false
|
|
|
|
|
});
|
2025-05-20 01:58:44 +08:00
|
|
|
dispatch({ type: 'SET_INNER_PADDING', payload: false });
|
|
|
|
|
}
|
|
|
|
|
}, [location.pathname, dispatch]);
|
2024-12-11 16:11:27 +08:00
|
|
|
|
2025-05-20 01:58:44 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
const isCollapsed =
|
|
|
|
|
localStorage.getItem('default_collapse_sidebar') === 'true';
|
|
|
|
|
dispatch({ type: 'SET_SIDER_COLLAPSED', payload: isCollapsed });
|
|
|
|
|
}, [dispatch]);
|
2024-12-11 16:11:27 +08:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<StyleContext.Provider value={[state, dispatch]}>
|
|
|
|
|
{children}
|
|
|
|
|
</StyleContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
};
|