training_data/apps/web/src/app/main/layout/NavigationMenu.tsx

56 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-02-26 21:08:38 +08:00
import { useAuth } from "@web/src/providers/auth-provider";
2025-02-25 22:04:17 +08:00
import { Menu } from "antd";
2025-02-26 21:08:38 +08:00
import { useMemo } from "react";
2025-02-25 22:04:17 +08:00
import { useNavigate, useLocation } from "react-router-dom";
2025-02-06 16:32:31 +08:00
export const NavigationMenu = () => {
2025-02-25 22:04:17 +08:00
const navigate = useNavigate();
2025-02-26 21:08:38 +08:00
const { isAuthenticated } = useAuth();
2025-02-25 22:04:17 +08:00
const { pathname } = useLocation();
2025-02-26 21:08:38 +08:00
const menuItems = useMemo(() => {
const baseItems = [
{ key: "home", path: "/", label: "首页" },
{ key: "path", path: "/path", label: "学习路径" },
2025-02-27 21:45:40 +08:00
{ key: "courses", path: "/courses", label: "全部课程" },
2025-02-26 21:08:38 +08:00
];
2025-02-27 12:21:41 +08:00
2025-02-26 21:08:38 +08:00
if (!isAuthenticated) {
return baseItems;
} else {
return [
...baseItems,
2025-02-27 12:21:41 +08:00
{ key: "my-duty", path: "/my-duty", label: "我的授课" },
{ key: "my-learning", path: "/my-learning", label: "我的课程" },
2025-02-27 10:23:36 +08:00
{ key: "my-path", path: "/my-path", label: "我的路径" },
2025-02-26 21:08:38 +08:00
];
}
}, [isAuthenticated]);
2025-02-25 22:04:17 +08:00
const selectedKey =
menuItems.find((item) => item.path === pathname)?.key || "";
return (
<Menu
mode="horizontal"
className="border-none font-medium"
2025-02-27 10:23:36 +08:00
disabledOverflow={true}
2025-02-25 22:04:17 +08:00
selectedKeys={[selectedKey]}
onClick={({ key }) => {
const selectedItem = menuItems.find((item) => item.key === key);
if (selectedItem) navigate(selectedItem.path);
window.scrollTo({
top: 0,
behavior: "smooth",
});
}}>
{menuItems.map(({ key, label }) => (
<Menu.Item
key={key}
className="text-gray-600 hover:text-blue-600">
{label}
</Menu.Item>
))}
</Menu>
);
};