1+ 'use client' ;
2+
3+ import { useState , useEffect } from 'react' ;
4+
5+ // 定义全局可见性状态的键
6+ type VisibilityKey =
7+ | 'endpoints.showApiKeyAll' // 主控管理:全局显示API Key
8+ | 'endpoints.showUrlAll' // 主控管理:全局显示URL
9+ | 'tunnels.showFullAddress' ; // 实例管理:显示完整地址
10+
11+ // 默认值配置
12+ const DEFAULT_VALUES : Record < VisibilityKey , boolean > = {
13+ 'endpoints.showApiKeyAll' : false ,
14+ 'endpoints.showUrlAll' : false ,
15+ 'tunnels.showFullAddress' : true ,
16+ } ;
17+
18+ /**
19+ * 全局可见性状态管理Hook
20+ * 支持localStorage持久化
21+ */
22+ export const useGlobalVisibility = ( ) => {
23+ // 状态存储
24+ const [ visibilityState , setVisibilityState ] = useState < Record < VisibilityKey , boolean > > ( {
25+ 'endpoints.showApiKeyAll' : DEFAULT_VALUES [ 'endpoints.showApiKeyAll' ] ,
26+ 'endpoints.showUrlAll' : DEFAULT_VALUES [ 'endpoints.showUrlAll' ] ,
27+ 'tunnels.showFullAddress' : DEFAULT_VALUES [ 'tunnels.showFullAddress' ] ,
28+ } ) ;
29+
30+ // 初始化时从localStorage读取状态
31+ useEffect ( ( ) => {
32+ if ( typeof window === 'undefined' ) return ;
33+
34+ const savedState : Partial < Record < VisibilityKey , boolean > > = { } ;
35+
36+ // 读取所有保存的状态
37+ Object . keys ( DEFAULT_VALUES ) . forEach ( ( key ) => {
38+ const visibilityKey = key as VisibilityKey ;
39+ const saved = localStorage . getItem ( `globalVisibility.${ visibilityKey } ` ) ;
40+ if ( saved !== null ) {
41+ savedState [ visibilityKey ] = saved === 'true' ;
42+ } else {
43+ savedState [ visibilityKey ] = DEFAULT_VALUES [ visibilityKey ] ;
44+ }
45+ } ) ;
46+
47+ setVisibilityState ( prev => ( { ...prev , ...savedState } ) ) ;
48+ } , [ ] ) ;
49+
50+ // 更新单个状态
51+ const setVisibility = ( key : VisibilityKey , value : boolean ) => {
52+ setVisibilityState ( prev => ( {
53+ ...prev ,
54+ [ key ] : value
55+ } ) ) ;
56+
57+ // 保存到localStorage
58+ if ( typeof window !== 'undefined' ) {
59+ localStorage . setItem ( `globalVisibility.${ key } ` , String ( value ) ) ;
60+ }
61+ } ;
62+
63+ // 切换单个状态
64+ const toggleVisibility = ( key : VisibilityKey ) => {
65+ const currentValue = visibilityState [ key ] ;
66+ const newValue = ! currentValue ;
67+ setVisibility ( key , newValue ) ;
68+ return newValue ;
69+ } ;
70+
71+ // 获取单个状态
72+ const getVisibility = ( key : VisibilityKey ) : boolean => {
73+ return visibilityState [ key ] ;
74+ } ;
75+
76+ // 重置所有状态为默认值
77+ const resetAllVisibility = ( ) => {
78+ setVisibilityState ( DEFAULT_VALUES ) ;
79+
80+ // 清除localStorage中的所有状态
81+ if ( typeof window !== 'undefined' ) {
82+ Object . keys ( DEFAULT_VALUES ) . forEach ( ( key ) => {
83+ localStorage . removeItem ( `globalVisibility.${ key } ` ) ;
84+ } ) ;
85+ }
86+ } ;
87+
88+ return {
89+ // 状态获取
90+ getVisibility,
91+
92+ // 状态更新
93+ setVisibility,
94+ toggleVisibility,
95+
96+ // 批量操作
97+ resetAllVisibility,
98+
99+ // 便捷访问器(主控管理相关)
100+ endpoints : {
101+ showApiKeyAll : visibilityState [ 'endpoints.showApiKeyAll' ] ,
102+ showUrlAll : visibilityState [ 'endpoints.showUrlAll' ] ,
103+ setShowApiKeyAll : ( value : boolean ) => setVisibility ( 'endpoints.showApiKeyAll' , value ) ,
104+ setShowUrlAll : ( value : boolean ) => setVisibility ( 'endpoints.showUrlAll' , value ) ,
105+ toggleShowApiKeyAll : ( ) => toggleVisibility ( 'endpoints.showApiKeyAll' ) ,
106+ toggleShowUrlAll : ( ) => toggleVisibility ( 'endpoints.showUrlAll' ) ,
107+ } ,
108+
109+ // 便捷访问器(实例管理相关)
110+ tunnels : {
111+ showFullAddress : visibilityState [ 'tunnels.showFullAddress' ] ,
112+ setShowFullAddress : ( value : boolean ) => setVisibility ( 'tunnels.showFullAddress' , value ) ,
113+ toggleShowFullAddress : ( ) => toggleVisibility ( 'tunnels.showFullAddress' ) ,
114+ } ,
115+ } ;
116+ } ;
0 commit comments