docs: add comments to the src/hooks directory

This commit is contained in:
zhaoying
2026-02-02 16:25:17 +08:00
parent 940c594066
commit 6194222289
3 changed files with 130 additions and 58 deletions

View File

@@ -1,7 +1,30 @@
/*
* @Author: ZhaoYing
* @Date: 2026-02-02 16:24:49
* @Last Modified by: ZhaoYing
* @Last Modified time: 2026-02-02 16:24:49
*/
/**
* useNavigationBreadcrumbs Hook
*
* Automatically updates breadcrumbs based on current route:
* - Matches current path against menu structure
* - Supports dynamic routes with parameters
* - Handles nested menu hierarchies
* - Updates breadcrumbs on route changes
*
* @hook
*/
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import { useMenu } from '@/store/menu';
/**
* Hook to automatically update breadcrumbs based on navigation.
*
* @param source - Menu source type ('space' or 'manage')
*/
export const useNavigationBreadcrumbs = (source: 'space' | 'manage' = 'manage') => {
const location = useLocation();
const { allMenus, updateBreadcrumbs } = useMenu();
@@ -10,26 +33,26 @@ export const useNavigationBreadcrumbs = (source: 'space' | 'manage' = 'manage')
const currentPath = location.pathname;
const menus = allMenus[source] || [];
// 查找匹配的菜单项并构建keyPath
/** Find matching menu item and build key path */
const findMenuKeyPath = (menuList: any[], parentKeys: string[] = []): string[] | null => {
let bestMatch: { path: string; parentId?: string; score: number } | null = null;
for (const menu of menuList) {
// 检查子菜单
/** Check submenus */
if (menu.subs && menu.subs.length > 0) {
const menuPath = menu.path ? (menu.path[0] !== '/' ? '/' + menu.path : menu.path) : '';
for (const sub of menu.subs) {
if (sub.path) {
const subPath = sub.path[0] !== '/' ? '/' + sub.path : sub.path;
// 精确匹配优先
/** Exact match has priority */
if (subPath === currentPath) {
return [sub.path, `${menu.id}`];
}
console.log('menuPath', menuPath)
// 动态路由匹配
/** Dynamic route matching */
if (subPath.includes(':')) {
// 检查是否在父菜单下
/** Check if under parent menu */
if (menuPath && currentPath.startsWith(menuPath + '/')) {
const relativePath = currentPath.replace(menuPath, '');
const pathSegments = subPath.split('/');
@@ -42,7 +65,7 @@ export const useNavigationBreadcrumbs = (source: 'space' | 'manage' = 'manage')
}
}
}
// 直接匹配子菜单路径
/** Direct match submenu path */
const pathSegments = subPath.split('/');
const currentSegments = currentPath.split('/');
if (pathSegments.length === currentSegments.length) {
@@ -57,14 +80,14 @@ export const useNavigationBreadcrumbs = (source: 'space' | 'manage' = 'manage')
}
}
// 检查主菜单
/** Check main menu */
if (menu.path) {
const menuPath = menu.path[0] !== '/' ? '/' + menu.path : menu.path;
// 精确匹配优先
/** Exact match has priority */
if (menuPath === currentPath) {
return [menu.path, ...parentKeys].reverse();
}
// 动态路由匹配
/** Dynamic route matching */
if (menuPath.includes(':')) {
const pathSegments = menuPath.split('/');
const currentSegments = currentPath.split('/');