-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinput-mode-context.tsx
More file actions
56 lines (49 loc) · 1.21 KB
/
input-mode-context.tsx
File metadata and controls
56 lines (49 loc) · 1.21 KB
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
import {
createContext,
Dispatch,
ReactNode,
useContext,
useMemo,
useReducer,
} from 'react';
import { createInputModeReducer } from '@widgets/community/hooks/create-input-mode-reducer';
import {
InputBoxMode,
ReducerAction,
} from '@widgets/community/types/input-box-type';
interface InputModeContextValue {
mode: InputBoxMode;
dispatch: Dispatch<ReducerAction>;
}
const InputModeContext = createContext<InputModeContextValue | null>(null);
export const InputModeContextProvider = ({
postId,
children,
}: {
postId: string;
children: ReactNode;
}) => {
const reducer: React.Reducer<InputBoxMode, ReducerAction> = (
prev,
action,
) => {
return createInputModeReducer({ postId, _prev: prev, action });
};
const [mode, dispatch] = useReducer(reducer, {
type: 'reset',
action: 'reset',
} as const);
const value = useMemo(() => ({ mode, dispatch }), [mode]);
return (
<InputModeContext.Provider value={value}>
{children}
</InputModeContext.Provider>
);
};
export const useChangeInputMode = () => {
const mode = useContext(InputModeContext);
if (!mode) {
throw new Error('InputModeContextProvider 안에서 사용하세요.');
}
return mode;
};