File size: 5,912 Bytes
d145f59
d8e9b2c
d145f59
 
 
d8e9b2c
d145f59
 
 
 
 
 
 
 
d8e9b2c
d145f59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8e9b2c
d145f59
d8e9b2c
d145f59
 
 
 
 
 
 
 
 
 
 
 
 
 
bce5831
d145f59
 
 
 
 
 
d8e9b2c
d145f59
 
 
 
 
d8e9b2c
d145f59
 
 
 
 
 
 
df5a20a
d145f59
 
 
 
 
 
 
 
 
 
d8e9b2c
d145f59
 
d8e9b2c
d145f59
 
 
 
 
 
 
 
df5a20a
d145f59
 
 
 
 
 
 
 
 
 
 
df5a20a
bce5831
d145f59
 
 
 
 
d8e9b2c
d145f59
d8e9b2c
d145f59
 
d8e9b2c
d145f59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8e9b2c
 
d145f59
d8e9b2c
 
 
 
d145f59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// contexts/Style/index.js

import React, { useReducer, useEffect, useMemo, createContext } from 'react';
import { useLocation } from 'react-router-dom';
import { isMobile as getIsMobile } from '../../helpers';

// Action Types
const ACTION_TYPES = {
  TOGGLE_SIDER: 'TOGGLE_SIDER',
  SET_SIDER: 'SET_SIDER',
  SET_MOBILE: 'SET_MOBILE',
  SET_SIDER_COLLAPSED: 'SET_SIDER_COLLAPSED',
  BATCH_UPDATE: 'BATCH_UPDATE',
};

// Constants
const STORAGE_KEYS = {
  SIDEBAR_COLLAPSED: 'default_collapse_sidebar',
};

const ROUTE_PATTERNS = {
  CONSOLE: '/console',
};

/**
 * 判断路径是否为控制台路由
 * @param {string} pathname - 路由路径
 * @returns {boolean} 是否为控制台路由
 */
const isConsoleRoute = (pathname) => {
  return pathname === ROUTE_PATTERNS.CONSOLE ||
    pathname.startsWith(ROUTE_PATTERNS.CONSOLE + '/');
};

/**
 * 获取初始状态
 * @param {string} pathname - 当前路由路径
 * @returns {Object} 初始状态对象
 */
const getInitialState = (pathname) => {
  const isMobile = getIsMobile();
  const isConsole = isConsoleRoute(pathname);
  const isCollapsed = localStorage.getItem(STORAGE_KEYS.SIDEBAR_COLLAPSED) === 'true';

  return {
    isMobile,
    showSider: isConsole && !isMobile,
    siderCollapsed: isCollapsed,
    isManualSiderControl: false,
  };
};

/**
 * Style reducer
 * @param {Object} state - 当前状态
 * @param {Object} action - action 对象
 * @returns {Object} 新状态
 */
const styleReducer = (state, action) => {
  switch (action.type) {
    case ACTION_TYPES.TOGGLE_SIDER:
      return {
        ...state,
        showSider: !state.showSider,
        isManualSiderControl: true,
      };

    case ACTION_TYPES.SET_SIDER:
      return {
        ...state,
        showSider: action.payload,
        isManualSiderControl: action.isManualControl ?? false,
      };

    case ACTION_TYPES.SET_MOBILE:
      return {
        ...state,
        isMobile: action.payload,
      };

    case ACTION_TYPES.SET_SIDER_COLLAPSED:
      // 自动保存到 localStorage
      localStorage.setItem(STORAGE_KEYS.SIDEBAR_COLLAPSED, action.payload.toString());
      return {
        ...state,
        siderCollapsed: action.payload,
      };

    case ACTION_TYPES.BATCH_UPDATE:
      return {
        ...state,
        ...action.payload,
      };

    default:
      return state;
  }
};

// Context (内部使用,不导出)
const StyleContext = createContext(null);

/**
 * 自定义 Hook - 处理窗口大小变化
 * @param {Function} dispatch - dispatch 函数
 * @param {Object} state - 当前状态
 * @param {string} pathname - 当前路径
 */
const useWindowResize = (dispatch, state, pathname) => {
  useEffect(() => {
    const handleResize = () => {
      const isMobile = getIsMobile();
      dispatch({ type: ACTION_TYPES.SET_MOBILE, payload: isMobile });

      // 只有在非手动控制的情况下,才根据屏幕大小自动调整侧边栏
      if (!state.isManualSiderControl && isConsoleRoute(pathname)) {
        dispatch({
          type: ACTION_TYPES.SET_SIDER,
          payload: !isMobile,
          isManualControl: false
        });
      }
    };

    let timeoutId;
    const debouncedResize = () => {
      clearTimeout(timeoutId);
      timeoutId = setTimeout(handleResize, 150);
    };

    window.addEventListener('resize', debouncedResize);
    return () => {
      window.removeEventListener('resize', debouncedResize);
      clearTimeout(timeoutId);
    };
  }, [dispatch, state.isManualSiderControl, pathname]);
};

/**
 * 自定义 Hook - 处理路由变化
 * @param {Function} dispatch - dispatch 函数
 * @param {string} pathname - 当前路径
 */
const useRouteChange = (dispatch, pathname) => {
  useEffect(() => {
    const isMobile = getIsMobile();
    const isConsole = isConsoleRoute(pathname);

    dispatch({
      type: ACTION_TYPES.BATCH_UPDATE,
      payload: {
        showSider: isConsole && !isMobile,
        isManualSiderControl: false,
      },
    });
  }, [pathname, dispatch]);
};

/**
 * 自定义 Hook - 处理移动设备侧边栏自动收起
 * @param {Object} state - 当前状态
 * @param {Function} dispatch - dispatch 函数
 */
const useMobileSiderAutoHide = (state, dispatch) => {
  useEffect(() => {
    // 移动设备上,如果不是手动控制且侧边栏是打开的,则自动关闭
    if (state.isMobile && state.showSider && !state.isManualSiderControl) {
      dispatch({ type: ACTION_TYPES.SET_SIDER, payload: false });
    }
  }, [state.isMobile, state.showSider, state.isManualSiderControl, dispatch]);
};

/**
 * Style Provider 组件
 */
export const StyleProvider = ({ children }) => {
  const location = useLocation();
  const pathname = location.pathname;

  const [state, dispatch] = useReducer(
    styleReducer,
    pathname,
    getInitialState
  );

  useWindowResize(dispatch, state, pathname);
  useRouteChange(dispatch, pathname);
  useMobileSiderAutoHide(state, dispatch);

  const contextValue = useMemo(
    () => ({ state, dispatch }),
    [state]
  );

  return (
    <StyleContext.Provider value={contextValue}>
      {children}
    </StyleContext.Provider>
  );
};

/**
 * 自定义 Hook - 使用 StyleContext
 * @returns {{state: Object, dispatch: Function}} context value
 */
export const useStyle = () => {
  const context = React.useContext(StyleContext);
  if (!context) {
    throw new Error('useStyle must be used within StyleProvider');
  }
  return context;
};

// 导出 action creators 以便外部使用
export const styleActions = {
  toggleSider: () => ({ type: ACTION_TYPES.TOGGLE_SIDER }),
  setSider: (show, isManualControl = false) => ({
    type: ACTION_TYPES.SET_SIDER,
    payload: show,
    isManualControl
  }),
  setMobile: (isMobile) => ({ type: ACTION_TYPES.SET_MOBILE, payload: isMobile }),
  setSiderCollapsed: (collapsed) => ({
    type: ACTION_TYPES.SET_SIDER_COLLAPSED,
    payload: collapsed
  }),
};