|
5 | 5 | * LICENSE file in the root directory of this source tree. |
6 | 6 | * ========================================================================== */ |
7 | 7 |
|
8 | | -import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux"; |
9 | 8 | import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment"; |
| 9 | +import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux"; |
10 | 10 |
|
11 | 11 | import type { RootState, AppDispatch } from "./store"; |
12 | 12 |
|
| 13 | +const defaultState: RootState = { |
| 14 | + accept: { value: undefined }, |
| 15 | + contentType: { value: undefined }, |
| 16 | + response: { value: undefined }, |
| 17 | + server: { value: undefined, options: [] }, |
| 18 | + body: { type: "empty" }, |
| 19 | + params: {}, |
| 20 | + auth: { selected: undefined, options: {}, data: {} }, |
| 21 | +} as RootState; |
| 22 | + |
13 | 23 | export const useTypedDispatch = (): any => { |
| 24 | + const dispatch = useDispatch<AppDispatch>(); |
| 25 | + |
14 | 26 | if (!ExecutionEnvironment.canUseDOM) { |
15 | 27 | return () => {}; // Return a no-op function during SSR |
16 | 28 | } |
17 | | - return useDispatch<AppDispatch>(); |
| 29 | + return dispatch; |
18 | 30 | }; |
19 | 31 |
|
20 | 32 | export const useTypedSelector: TypedUseSelectorHook<RootState> = (selector) => { |
21 | | - if (!ExecutionEnvironment.canUseDOM) { |
22 | | - // Return default values during SSR to prevent null store access |
23 | | - return selector({ |
24 | | - accept: { value: undefined }, |
25 | | - contentType: { value: undefined }, |
26 | | - response: { value: undefined }, |
27 | | - server: { value: undefined, options: [] }, |
28 | | - body: { type: "empty" }, |
29 | | - params: {}, |
30 | | - auth: { selected: undefined, options: {}, data: {} }, |
31 | | - } as RootState); |
32 | | - } |
33 | | - return useSelector(selector); |
| 33 | + const result = useSelector((state: RootState) => { |
| 34 | + if (!ExecutionEnvironment.canUseDOM) { |
| 35 | + // Return default values during SSR to prevent null store access |
| 36 | + return selector(defaultState); |
| 37 | + } |
| 38 | + return selector(state); |
| 39 | + }); |
| 40 | + |
| 41 | + return result; |
34 | 42 | }; |
0 commit comments