1- import { h } from 'preact'
21import { useEffect , useRef , useState , useCallback } from 'preact/hooks'
3- import { route } from 'preact-router'
4- import { useLocation } from 'preact-iso'
2+ import { route , getCurrentUrl } from 'preact-router'
53import { ChevronLeft , ChevronRight , X , Home , Users , Settings , FileText } from 'lucide-preact'
64import {
75 tabs ,
@@ -18,6 +16,21 @@ import {
1816} from '../../stores/tabs'
1917import { Button } from '../ui/Button'
2018
19+ // 自定义 hook 获取当前路径
20+ function useCurrentPath ( ) {
21+ const [ path , setPath ] = useState ( getCurrentUrl ( ) )
22+
23+ useEffect ( ( ) => {
24+ const handleRoute = ( ) => {
25+ setPath ( getCurrentUrl ( ) )
26+ }
27+ window . addEventListener ( 'popstate' , handleRoute )
28+ return ( ) => window . removeEventListener ( 'popstate' , handleRoute )
29+ } , [ ] )
30+
31+ return path
32+ }
33+
2134// 路径到标题的映射
2235const pathTitles : Record < string , string > = {
2336 '/' : '首页' ,
@@ -64,7 +77,7 @@ interface ContextMenuState {
6477}
6578
6679export function TabBar ( ) {
67- const location = useLocation ( )
80+ const currentPath = useCurrentPath ( )
6881 const tabsContainerRef = useRef < HTMLDivElement > ( null )
6982 const [ canScrollLeft , setCanScrollLeft ] = useState ( false )
7083 const [ canScrollRight , setCanScrollRight ] = useState ( false )
@@ -75,8 +88,6 @@ export function TabBar() {
7588 tab : null ,
7689 } )
7790
78- const currentPath = location . path
79-
8091 // 检查滚动状态
8192 const checkScroll = useCallback ( ( ) => {
8293 const container = tabsContainerRef . current
0 commit comments