mirror of
https://github.com/DrizzleTime/Foxel.git
synced 2026-09-05 23:57:19 +08:00
feat(Header, Sidebar): enhance mobile responsiveness by conditionally rendering components
This commit is contained in:
@@ -16,6 +16,7 @@ import { type RouteConfig } from '../../routes';
|
|||||||
import UserAvatar from '../../components/UserAvatar';
|
import UserAvatar from '../../components/UserAvatar';
|
||||||
import { UserRole } from '../../api/types';
|
import { UserRole } from '../../api/types';
|
||||||
import SearchDialog from '../../components/search/SearchDialog';
|
import SearchDialog from '../../components/search/SearchDialog';
|
||||||
|
import useIsMobile from '../../hooks/useIsMobile';
|
||||||
|
|
||||||
const { Header: AntHeader } = Layout;
|
const { Header: AntHeader } = Layout;
|
||||||
interface HeaderProps {
|
interface HeaderProps {
|
||||||
@@ -48,6 +49,7 @@ const Header: React.FC<HeaderProps> = ({
|
|||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const headerRef = useRef<HTMLDivElement>(null);
|
const headerRef = useRef<HTMLDivElement>(null);
|
||||||
const { hasRole } = useAuth();
|
const { hasRole } = useAuth();
|
||||||
|
const isMobileDevice = useIsMobile();
|
||||||
|
|
||||||
// 添加搜索对话框状态
|
// 添加搜索对话框状态
|
||||||
const [searchDialogVisible, setSearchDialogVisible] = useState(false);
|
const [searchDialogVisible, setSearchDialogVisible] = useState(false);
|
||||||
@@ -236,7 +238,7 @@ const Header: React.FC<HeaderProps> = ({
|
|||||||
marginRight: 12
|
marginRight: 12
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
{renderBreadcrumb()}
|
{!isMobileDevice && renderBreadcrumb()}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* 右侧区域:搜索框和用户菜单 */}
|
{/* 右侧区域:搜索框和用户菜单 */}
|
||||||
|
|||||||
@@ -75,18 +75,18 @@ const Sidebar: React.FC<SidebarProps> = ({ collapsed, isMobile = false, onClose,
|
|||||||
// 获取当前选中的菜单项
|
// 获取当前选中的菜单项
|
||||||
const getSelectedKey = () => {
|
const getSelectedKey = () => {
|
||||||
const pathname = location.pathname;
|
const pathname = location.pathname;
|
||||||
|
|
||||||
// 管理后台路径处理
|
// 管理后台路径处理
|
||||||
if (area === 'admin') {
|
if (area === 'admin') {
|
||||||
// 提取 /admin/ 后面的部分
|
// 提取 /admin/ 后面的部分
|
||||||
const adminPath = pathname.replace(/^\/admin\/?/, '');
|
const adminPath = pathname.replace(/^\/admin\/?/, '');
|
||||||
|
|
||||||
// 如果是管理后台首页
|
// 如果是管理后台首页
|
||||||
if (adminPath === '') {
|
if (adminPath === '') {
|
||||||
const defaultRoute = routes.find(route => route.path === '');
|
const defaultRoute = routes.find(route => route.path === '');
|
||||||
return defaultRoute ? defaultRoute.path : '';
|
return defaultRoute ? defaultRoute.path : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
const matchedRoute = routes.find(route => {
|
const matchedRoute = routes.find(route => {
|
||||||
if (route.path.includes(':')) {
|
if (route.path.includes(':')) {
|
||||||
const basePath = route.path.split(':')[0].replace(/\/$/, '');
|
const basePath = route.path.split(':')[0].replace(/\/$/, '');
|
||||||
@@ -94,10 +94,10 @@ const Sidebar: React.FC<SidebarProps> = ({ collapsed, isMobile = false, onClose,
|
|||||||
}
|
}
|
||||||
return adminPath === route.path;
|
return adminPath === route.path;
|
||||||
});
|
});
|
||||||
|
|
||||||
return matchedRoute ? matchedRoute.path : '';
|
return matchedRoute ? matchedRoute.path : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// 主应用路径处理
|
// 主应用路径处理
|
||||||
const matchedRoute = routes.find(route => {
|
const matchedRoute = routes.find(route => {
|
||||||
if (route.path.includes(':')) {
|
if (route.path.includes(':')) {
|
||||||
@@ -109,7 +109,7 @@ const Sidebar: React.FC<SidebarProps> = ({ collapsed, isMobile = false, onClose,
|
|||||||
}
|
}
|
||||||
return pathname === '/' + route.path;
|
return pathname === '/' + route.path;
|
||||||
});
|
});
|
||||||
|
|
||||||
return matchedRoute ? (matchedRoute.path === '/' ? '/' : matchedRoute.path) : '/';
|
return matchedRoute ? (matchedRoute.path === '/' ? '/' : matchedRoute.path) : '/';
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -124,7 +124,7 @@ const Sidebar: React.FC<SidebarProps> = ({ collapsed, isMobile = false, onClose,
|
|||||||
} else {
|
} else {
|
||||||
navigate(key);
|
navigate(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 在移动设备上点击后关闭侧边栏
|
// 在移动设备上点击后关闭侧边栏
|
||||||
if (isMobile && onClose) {
|
if (isMobile && onClose) {
|
||||||
onClose();
|
onClose();
|
||||||
@@ -136,7 +136,7 @@ const Sidebar: React.FC<SidebarProps> = ({ collapsed, isMobile = false, onClose,
|
|||||||
if (area === 'admin') {
|
if (area === 'admin') {
|
||||||
return {
|
return {
|
||||||
logo: logo,
|
logo: logo,
|
||||||
title: 'Foxel 管理后台'
|
title: 'Foxel 面板'
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
@@ -204,7 +204,7 @@ const Sidebar: React.FC<SidebarProps> = ({ collapsed, isMobile = false, onClose,
|
|||||||
onClick={handleMenuClick}
|
onClick={handleMenuClick}
|
||||||
style={{
|
style={{
|
||||||
borderRight: 'none',
|
borderRight: 'none',
|
||||||
flex: 1
|
flex: 1
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Sider>
|
</Sider>
|
||||||
|
|||||||
Reference in New Issue
Block a user