mirror of
https://github.com/DrizzleTime/Foxel.git
synced 2026-09-08 09:06:50 +08:00
refactor: clean up MainLayout component and improve authentication handling
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import {useState, useEffect} from 'react';
|
||||
import {Outlet, useNavigate, useLocation, matchPath} from 'react-router';
|
||||
import {Layout, theme} from 'antd';
|
||||
import { clearAuthData, getStoredUser, isAuthenticated } from '../api';
|
||||
import {clearAuthData, isAuthenticated} from '../api';
|
||||
import useIsMobile from '../hooks/useIsMobile';
|
||||
|
||||
import {useAuth} from '../api/AuthContext';
|
||||
import Sidebar from './components/Sidebar';
|
||||
import Header from './components/Header';
|
||||
import Footer from './components/Footer';
|
||||
@@ -12,10 +12,9 @@ import routes, { type RouteConfig } from '../config/routeConfig';
|
||||
const {Content} = Layout;
|
||||
|
||||
function MainLayout() {
|
||||
const {refreshUser} = useAuth();
|
||||
const isMobile = useIsMobile();
|
||||
const [collapsed, setCollapsed] = useState(isMobile); // 移动设备默认折叠侧边栏
|
||||
// 重命名避免未使用警告,或用于未来扩展
|
||||
const [, setUser] = useState<any>(null);
|
||||
const [collapsed, setCollapsed] = useState(isMobile);
|
||||
const [currentRouteData, setCurrentRouteData] = useState<{
|
||||
routeInfo: RouteConfig | undefined;
|
||||
params: Record<string, string>;
|
||||
@@ -93,11 +92,8 @@ function MainLayout() {
|
||||
navigate('/login');
|
||||
return;
|
||||
}
|
||||
const storedUser = getStoredUser();
|
||||
if (storedUser) {
|
||||
setUser(storedUser);
|
||||
}
|
||||
}, [navigate]);
|
||||
refreshUser();
|
||||
}, [navigate, refreshUser]);
|
||||
|
||||
// 监听路由变化,更新当前路由信息
|
||||
useEffect(() => {
|
||||
@@ -162,9 +158,6 @@ function MainLayout() {
|
||||
}}>
|
||||
{/* 渲染子路由组件 */}
|
||||
<Outlet context={{
|
||||
updateBreadcrumbTitle: (title: string) => {
|
||||
setCurrentRouteData(prev => ({ ...prev, title }));
|
||||
},
|
||||
isMobile
|
||||
}}/>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user