| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- import { Menu, type MenuProps } from "antd";
- import {
- SearchOutlined,
- HomeOutlined,
- FieldTimeOutlined,
- FolderOutlined,
- FileOutlined,
- } from "@ant-design/icons";
- import { useNavigate, useMatches, type UIMatch } from "react-router";
- import {
- ChannelIcon,
- CourseOutLinedIcon,
- DocumentIcon,
- RobotIcon,
- TaskIcon,
- TipitakaIcon,
- } from "../../assets/icon";
- import React, { useState } from "react";
- import { useAppSelector } from "../../hooks";
- import { currentUser } from "../../reducers/current-user";
- import { useRecent } from "../../hooks/useRecent.ts";
- import RecentModal from "../recent/RecentModal.tsx";
- /* ================= 类型 ================= */
- interface MenuItem {
- key: string;
- label?: React.ReactNode;
- icon?: React.ReactNode;
- type?: "divider";
- children?: MenuItem[];
- /** ⭐ 用于高亮匹配 */
- activeId?: string | string[];
- }
- export interface RouteHandle {
- id?: string;
- crumb?: string | ((match: UIMatch) => string);
- }
- /* ================= 当前路由ID ================= */
- function useCurrentRouteId(): string | undefined {
- const matches = useMatches() as UIMatch<number, RouteHandle>[];
- return [...matches].reverse().find((match) => match.handle?.id)?.handle?.id;
- }
- /* ================= 匹配算法 ================= */
- function matchActive(routeId: string | undefined, active?: string | string[]) {
- if (!routeId || !active) return false;
- const list = Array.isArray(active) ? active : [active];
- return list.some((id) => routeId === id || routeId.startsWith(id + "."));
- }
- /* ================= 找当前选中key ================= */
- function findSelectedKey(
- items: MenuItem[],
- routeId?: string
- ): string | undefined {
- for (const item of items) {
- if (matchActive(routeId, item.activeId)) return item.key;
- if (item.children) {
- const k = findSelectedKey(item.children, routeId);
- if (k) return k;
- }
- }
- }
- /* ================= 找展开父级keys ================= */
- function findOpenKeys(
- items: MenuItem[],
- routeId?: string,
- parents: string[] = []
- ): string[] {
- for (const item of items) {
- if (matchActive(routeId, item.activeId)) {
- return parents;
- }
- if (item.children) {
- const found = findOpenKeys(item.children, routeId, [
- ...parents,
- item.key,
- ]);
- if (found.length) return found;
- }
- }
- return [];
- }
- /* ================= 组件 ================= */
- interface Props {
- onSearch?: () => void;
- }
- const Widget = ({ onSearch }: Props) => {
- const navigate = useNavigate();
- const routeId = useCurrentRouteId();
- const currUser = useAppSelector(currentUser);
- const { data } = useRecent(currUser?.id, 5, 0);
- const [recentOpen, setRecentOpen] = useState(false);
- const recentList: MenuItem[] = data
- ? data?.data.rows.map((item, id) => {
- return {
- key: `recent-${id}`,
- label: item.title,
- };
- })
- : [];
- /* ================= 菜单配置 ================= */
- const items: MenuItem[] = [
- {
- key: "search",
- icon: <SearchOutlined />,
- label: "搜索",
- },
- {
- key: "/workspace",
- icon: <HomeOutlined />,
- label: "主页",
- activeId: "workspace.home",
- },
- {
- key: "/workspace/ai",
- icon: <RobotIcon />,
- label: "AI",
- activeId: "workspace.ai",
- },
- {
- key: "/workspace/tipitaka",
- icon: <TipitakaIcon />,
- label: "巴利三藏",
- activeId: "workspace.tipitaka",
- },
- { type: "divider", key: "d1" },
- {
- key: "/workspace/recent",
- icon: <FieldTimeOutlined />,
- label: "最近打开",
- children: [
- ...recentList,
- {
- key: "/workspace/recent/list",
- label: "更多……",
- },
- ],
- },
- {
- key: "/workspace/doc",
- icon: <DocumentIcon />,
- label: "文档",
- children: [
- {
- key: "/workspace/article",
- label: "文章",
- activeId: "workspace.article",
- icon: <FileOutlined />,
- },
- {
- key: "/workspace/anthology",
- label: "文集",
- activeId: "workspace.anthology",
- icon: <FolderOutlined />,
- },
- ],
- },
- {
- key: "/workspace/channel",
- icon: <ChannelIcon />,
- label: "频道",
- activeId: "workspace.channel",
- },
- {
- key: "/workspace/term",
- icon: <ChannelIcon />,
- label: "Term",
- activeId: "workspace.term",
- },
- {
- key: "/workspace/course",
- icon: <CourseOutLinedIcon />,
- label: "Course",
- },
- {
- key: "/workspace/task",
- icon: <TaskIcon />,
- label: "Task",
- activeId: "workspace.task",
- children: [
- {
- key: "/workspace/task/pending",
- label: "Pending",
- activeId: "workspace.task.pending",
- },
- {
- key: "/workspace/task/to-do-list",
- label: "To-Do List",
- activeId: "workspace.task.todo",
- },
- {
- key: "/workspace/task/hell",
- label: "Task Hell",
- activeId: "workspace.task.hell",
- },
- ],
- },
- ];
- console.log("nav", routeId);
- /** 当前选中 */
- const selectedKey = findSelectedKey(items, routeId);
- /** 自动展开父级 */
- const openKeys = findOpenKeys(items, routeId);
- /** 点击 */
- const handleClick: MenuProps["onClick"] = ({ key }) => {
- if (key === "search") {
- onSearch?.();
- return;
- } else if (key === "/workspace/recent/list") {
- setRecentOpen(true);
- return;
- }
- navigate(key);
- };
- return (
- <>
- <Menu
- mode="inline"
- selectedKeys={selectedKey ? [selectedKey] : []}
- defaultOpenKeys={openKeys}
- items={items as MenuProps["items"]}
- onClick={handleClick}
- style={{ borderRight: 0 }}
- />
- <RecentModal
- open={recentOpen}
- onOpenChange={() => setRecentOpen(false)}
- onSelect={(e, row) => {
- if (e.ctrlKey || e.metaKey) {
- window.open("");
- } else {
- navigate(`/workspace/${row.type}/${row.articleId}`);
- }
- }}
- />
- </>
- );
- };
- export default Widget;
|