All files / islands / NavigationState.tsx

100.00% Branches 0/0
2.38% Lines 1/42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
x2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

























































import { useEffect, useState } from "preact/hooks";

// 导航状态管理
export function useNavigationState() {
  const [currentPath, setCurrentPath] = useState("");

  useEffect(() => {
    // 获取当前路径
    const updatePath = () => {
      if (typeof globalThis.location !== "undefined") {
        setCurrentPath(globalThis.location.pathname);
      }
    };

    updatePath();

    // 监听路径变化
    const handlePopState = () => {
      updatePath();
    };

    if (typeof globalThis.window !== "undefined") {
      globalThis.window.addEventListener("popstate", handlePopState);
    }

    return () => {
      if (typeof globalThis.window !== "undefined") {
        globalThis.window.removeEventListener("popstate", handlePopState);
      }
    };
  }, []);

  // 检查路径是否激活
  const isPathActive = (path: string) => {
    if (path === "/") {
      return currentPath === "/";
    }
    return currentPath.startsWith(path);
  };

  // 检查状态码页面是否激活
  const isStatusActive = () => {
    return currentPath.startsWith("/status") ||
      currentPath === "/nonexistent-page";
  };

  return {
    currentPath,
    isPathActive,
    isStatusActive,
  };
}

// 导航状态提供者组件(如果需要全局状态)
export default function NavigationStateProvider(
  { children }: { children: preact.ComponentChildren },
) {
  return <div>{children}</div>;
}