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

109 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-02-28 15:45:19 +08:00
import { Input, Button } from "antd";
import { PlusOutlined, SearchOutlined, UserOutlined } from "@ant-design/icons";
2025-02-21 13:14:47 +08:00
import { useAuth } from "@web/src/providers/auth-provider";
2025-02-28 15:45:19 +08:00
import { useNavigate, useParams } from "react-router-dom";
2025-02-25 08:25:54 +08:00
import { UserMenu } from "./UserMenu/UserMenu";
2025-02-21 13:14:47 +08:00
import { NavigationMenu } from "./NavigationMenu";
2025-02-25 16:04:55 +08:00
import { useMainContext } from "./MainProvider";
2025-02-06 16:32:31 +08:00
export function MainHeader() {
2025-02-21 13:14:47 +08:00
const { isAuthenticated, user } = useAuth();
2025-02-26 10:19:29 +08:00
const { id } = useParams();
2025-02-21 13:14:47 +08:00
const navigate = useNavigate();
2025-02-25 16:04:55 +08:00
const { searchValue, setSearchValue } = useMainContext();
2025-02-21 13:14:47 +08:00
return (
2025-02-27 21:45:40 +08:00
<div className="select-none w-full flex items-center justify-between bg-white shadow-md border-b border-gray-100 fixed z-30 py-2 px-4 md:px-6">
{/* 左侧区域 - 设置为不收缩 */}
<div className="flex items-center justify-start space-x-4 flex-shrink-0">
<img src="/logo.svg" className="h-12 w-12" />
2025-02-27 21:57:05 +08:00
<div
onClick={() => navigate("/")}
2025-02-27 21:45:40 +08:00
className="text-2xl font-bold bg-gradient-to-r from-primary-600 via-primary-500 to-primary-400 bg-clip-text text-transparent hover:scale-105 transition-transform cursor-pointer whitespace-nowrap">
2025-02-27 21:57:05 +08:00
</div>
2025-02-27 21:45:40 +08:00
<NavigationMenu />
2025-02-27 09:05:27 +08:00
</div>
2025-02-27 21:45:40 +08:00
{/* 右侧区域 - 可以灵活收缩 */}
<div className="flex justify-end gap-2 md:gap-4 flex-shrink">
<div className="flex items-center gap-2 md:gap-4">
2025-02-27 22:58:42 +08:00
<Input
size="large"
prefix={
<SearchOutlined className="text-gray-400 group-hover:text-blue-500 transition-colors" />
}
placeholder="搜索课程"
className="w-full md:w-96 rounded-full"
value={searchValue}
onClick={(e) => {
2025-02-28 09:23:36 +08:00
if (
!window.location.pathname.startsWith("/search")
) {
2025-02-27 22:58:42 +08:00
navigate(`/search`);
window.scrollTo({
top: 0,
behavior: "smooth",
});
}
}}
onChange={(e) => setSearchValue(e.target.value)}
onPressEnter={(e) => {
2025-02-28 09:23:36 +08:00
if (
!window.location.pathname.startsWith("/search")
) {
2025-02-27 22:58:42 +08:00
navigate(`/search`);
window.scrollTo({
top: 0,
behavior: "smooth",
});
}
}}
/>
2025-02-21 13:14:47 +08:00
{isAuthenticated && (
<>
<Button
2025-02-27 22:58:42 +08:00
size="large"
shape="round"
icon={<PlusOutlined></PlusOutlined>}
2025-02-26 10:19:29 +08:00
onClick={() => {
const url = id
? `/course/${id}/editor`
: "/course/editor";
navigate(url);
}}
2025-02-28 09:23:36 +08:00
type="primary">
2025-02-26 10:19:29 +08:00
{id ? "编辑课程" : "创建课程"}
2025-02-21 13:14:47 +08:00
</Button>
</>
)}
2025-02-27 09:05:27 +08:00
{isAuthenticated && (
<Button
2025-02-27 22:58:42 +08:00
size="large"
shape="round"
2025-02-27 09:05:27 +08:00
onClick={() => {
window.location.href = "/path/editor";
}}
2025-02-28 09:23:36 +08:00
ghost
type="primary"
2025-02-27 09:05:27 +08:00
icon={<PlusOutlined></PlusOutlined>}>
2025-02-27 22:37:09 +08:00
2025-02-21 13:14:47 +08:00
</Button>
2025-02-27 09:05:27 +08:00
)}
2025-02-21 13:14:47 +08:00
{isAuthenticated ? (
2025-02-25 08:25:54 +08:00
<UserMenu />
2025-02-21 13:14:47 +08:00
) : (
<Button
2025-02-27 22:58:42 +08:00
type="primary"
size="large"
shape="round"
2025-02-21 13:14:47 +08:00
onClick={() => navigate("/login")}
icon={<UserOutlined />}>
</Button>
)}
</div>
</div>
2025-02-26 23:18:14 +08:00
</div>
2025-02-21 13:14:47 +08:00
);
}