MainMenu.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import { Menu, type MenuProps } from "antd";
  2. import {
  3. SearchOutlined,
  4. HomeOutlined,
  5. FieldTimeOutlined,
  6. FolderOutlined,
  7. FileOutlined,
  8. } from "@ant-design/icons";
  9. import { useNavigate, useMatches, type UIMatch } from "react-router";
  10. import {
  11. ChannelIcon,
  12. CourseOutLinedIcon,
  13. DocumentIcon,
  14. RobotIcon,
  15. TaskIcon,
  16. TipitakaIcon,
  17. } from "../../assets/icon";
  18. import React, { useState } from "react";
  19. import { useAppSelector } from "../../hooks";
  20. import { currentUser } from "../../reducers/current-user";
  21. import { useRecent } from "../../hooks/useRecent.ts";
  22. import RecentModal from "../recent/RecentModal.tsx";
  23. /* ================= 类型 ================= */
  24. interface MenuItem {
  25. key: string;
  26. label?: React.ReactNode;
  27. icon?: React.ReactNode;
  28. type?: "divider";
  29. children?: MenuItem[];
  30. /** ⭐ 用于高亮匹配 */
  31. activeId?: string | string[];
  32. }
  33. export interface RouteHandle {
  34. id?: string;
  35. crumb?: string | ((match: UIMatch) => string);
  36. }
  37. /* ================= 当前路由ID ================= */
  38. function useCurrentRouteId(): string | undefined {
  39. const matches = useMatches() as UIMatch<number, RouteHandle>[];
  40. return [...matches].reverse().find((match) => match.handle?.id)?.handle?.id;
  41. }
  42. /* ================= 匹配算法 ================= */
  43. function matchActive(routeId: string | undefined, active?: string | string[]) {
  44. if (!routeId || !active) return false;
  45. const list = Array.isArray(active) ? active : [active];
  46. return list.some((id) => routeId === id || routeId.startsWith(id + "."));
  47. }
  48. /* ================= 找当前选中key ================= */
  49. function findSelectedKey(
  50. items: MenuItem[],
  51. routeId?: string
  52. ): string | undefined {
  53. for (const item of items) {
  54. if (matchActive(routeId, item.activeId)) return item.key;
  55. if (item.children) {
  56. const k = findSelectedKey(item.children, routeId);
  57. if (k) return k;
  58. }
  59. }
  60. }
  61. /* ================= 找展开父级keys ================= */
  62. function findOpenKeys(
  63. items: MenuItem[],
  64. routeId?: string,
  65. parents: string[] = []
  66. ): string[] {
  67. for (const item of items) {
  68. if (matchActive(routeId, item.activeId)) {
  69. return parents;
  70. }
  71. if (item.children) {
  72. const found = findOpenKeys(item.children, routeId, [
  73. ...parents,
  74. item.key,
  75. ]);
  76. if (found.length) return found;
  77. }
  78. }
  79. return [];
  80. }
  81. /* ================= 组件 ================= */
  82. interface Props {
  83. onSearch?: () => void;
  84. }
  85. const Widget = ({ onSearch }: Props) => {
  86. const navigate = useNavigate();
  87. const routeId = useCurrentRouteId();
  88. const currUser = useAppSelector(currentUser);
  89. const { data } = useRecent(currUser?.id, 5, 0);
  90. const [recentOpen, setRecentOpen] = useState(false);
  91. const recentList: MenuItem[] = data
  92. ? data?.data.rows.map((item, id) => {
  93. return {
  94. key: `recent-${id}`,
  95. label: item.title,
  96. };
  97. })
  98. : [];
  99. /* ================= 菜单配置 ================= */
  100. const items: MenuItem[] = [
  101. {
  102. key: "search",
  103. icon: <SearchOutlined />,
  104. label: "搜索",
  105. },
  106. {
  107. key: "/workspace",
  108. icon: <HomeOutlined />,
  109. label: "主页",
  110. activeId: "workspace.home",
  111. },
  112. {
  113. key: "/workspace/ai",
  114. icon: <RobotIcon />,
  115. label: "AI",
  116. activeId: "workspace.ai",
  117. },
  118. {
  119. key: "/workspace/tipitaka",
  120. icon: <TipitakaIcon />,
  121. label: "巴利三藏",
  122. activeId: "workspace.tipitaka",
  123. },
  124. { type: "divider", key: "d1" },
  125. {
  126. key: "/workspace/recent",
  127. icon: <FieldTimeOutlined />,
  128. label: "最近打开",
  129. children: [
  130. ...recentList,
  131. {
  132. key: "/workspace/recent/list",
  133. label: "更多……",
  134. },
  135. ],
  136. },
  137. {
  138. key: "/workspace/doc",
  139. icon: <DocumentIcon />,
  140. label: "文档",
  141. children: [
  142. {
  143. key: "/workspace/article",
  144. label: "文章",
  145. activeId: "workspace.article",
  146. icon: <FileOutlined />,
  147. },
  148. {
  149. key: "/workspace/anthology",
  150. label: "文集",
  151. activeId: "workspace.anthology",
  152. icon: <FolderOutlined />,
  153. },
  154. ],
  155. },
  156. {
  157. key: "/workspace/channel",
  158. icon: <ChannelIcon />,
  159. label: "频道",
  160. activeId: "workspace.channel",
  161. },
  162. {
  163. key: "/workspace/term",
  164. icon: <ChannelIcon />,
  165. label: "Term",
  166. activeId: "workspace.term",
  167. },
  168. {
  169. key: "/workspace/course",
  170. icon: <CourseOutLinedIcon />,
  171. label: "Course",
  172. },
  173. {
  174. key: "/workspace/task",
  175. icon: <TaskIcon />,
  176. label: "Task",
  177. activeId: "workspace.task",
  178. children: [
  179. {
  180. key: "/workspace/task/pending",
  181. label: "Pending",
  182. activeId: "workspace.task.pending",
  183. },
  184. {
  185. key: "/workspace/task/to-do-list",
  186. label: "To-Do List",
  187. activeId: "workspace.task.todo",
  188. },
  189. {
  190. key: "/workspace/task/hell",
  191. label: "Task Hell",
  192. activeId: "workspace.task.hell",
  193. },
  194. ],
  195. },
  196. ];
  197. console.log("nav", routeId);
  198. /** 当前选中 */
  199. const selectedKey = findSelectedKey(items, routeId);
  200. /** 自动展开父级 */
  201. const openKeys = findOpenKeys(items, routeId);
  202. /** 点击 */
  203. const handleClick: MenuProps["onClick"] = ({ key }) => {
  204. if (key === "search") {
  205. onSearch?.();
  206. return;
  207. } else if (key === "/workspace/recent/list") {
  208. setRecentOpen(true);
  209. return;
  210. }
  211. navigate(key);
  212. };
  213. return (
  214. <>
  215. <Menu
  216. mode="inline"
  217. selectedKeys={selectedKey ? [selectedKey] : []}
  218. defaultOpenKeys={openKeys}
  219. items={items as MenuProps["items"]}
  220. onClick={handleClick}
  221. style={{ borderRight: 0 }}
  222. />
  223. <RecentModal
  224. open={recentOpen}
  225. onOpenChange={() => setRecentOpen(false)}
  226. onSelect={(e, row) => {
  227. if (e.ctrlKey || e.metaKey) {
  228. window.open("");
  229. } else {
  230. navigate(`/workspace/${row.type}/${row.articleId}`);
  231. }
  232. }}
  233. />
  234. </>
  235. );
  236. };
  237. export default Widget;