/
usePracticeShortTyping.tsx
155 lines (133 loc) · 4.28 KB
/
usePracticeShortTyping.tsx
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
import { useRouter } from 'next/router';
import { useCallback } from 'react';
import { useMemo, useRef, useState } from 'react';
import type { ShortTypingType } from '@/apis/typing';
import useCurrentTyping from '@/components/typing/short/_hook/useCurrentTyping';
import useToggle from '@/hooks/useToggle';
import type {
CurrentTypingActionType,
CurrentTypingInfoType,
EndGameValueType,
PrevNextTypingInfoType,
} from '@/types/shortTyping';
import type { TypingHistoryType } from '@/types/typing';
import { getTypingHistoryAverage } from '@/utils/typing';
const usePracticeShortTyping = (originalTypings: ShortTypingType[]) => {
const [currentIdx, setCurrentIdx] = useState(0);
const router = useRouter();
const {
originalTyping,
userTyping,
time,
typingCount,
typingAccuracy,
typingWpm,
typingSpeed,
handlePracticeSubmit,
handleTyping,
timePlay,
timePause,
resetTypingData,
} = useCurrentTyping(
originalTypings[currentIdx] ?? {
typingId: 0,
content: '',
},
);
const [isResultModalOpen, handleResultModalToggle] = useToggle();
const [isExitModalOpen, handleExitModalToggle] = useToggle();
const history = useRef<TypingHistoryType[]>([]);
const prevUserTyping = history.current[history.current.length - 1]?.content ?? '';
const typingAvgResult = getTypingHistoryAverage(history.current);
const saveTypingHistory = useCallback(
(content: string) => {
history.current = [
...history.current,
{ typingSpeed, typingAccuracy, typingWpm, typingTime: time, content, endTime: new Date() },
];
},
[time, typingAccuracy, typingSpeed, typingWpm],
);
const prevOriginalTyping = useMemo(
() => (currentIdx > 0 ? originalTypings[currentIdx - 1]?.content : ''),
[currentIdx, originalTypings],
);
const nextOriginalTyping = useMemo(
() => (currentIdx < originalTypings.length - 1 ? originalTypings[currentIdx + 1].content : ''),
[currentIdx, originalTypings],
);
const handleSubmit = useCallback(
async (input: string) => {
await handlePracticeSubmit(input);
saveTypingHistory(input);
},
[handlePracticeSubmit, saveTypingHistory],
);
const handleEndTyping = useCallback(
async (input: string) => {
// NOTE: 짧은글에서는 데이터를 전송하는 것을 기다리지 않고, 바로 다음 문장으로 넘어가는 것이 좋을 것 같다.
await handleSubmit(input);
if (currentIdx < originalTypings.length - 1) {
resetTypingData();
setCurrentIdx((prev) => prev + 1);
} else {
// 여기서 결과 모달을 띄워야하기 때문에, 모달 관련 로직을 여기서 처리하는게 맞을 것 같음
handleResultModalToggle();
}
},
[currentIdx, handleResultModalToggle, handleSubmit, originalTypings.length, resetTypingData],
);
const handleExitModalClose = useCallback(() => {
timePlay();
handleExitModalToggle();
}, [handleExitModalToggle, timePlay]);
const handleResultModalOpen = useCallback(() => {
handleExitModalToggle();
handleResultModalToggle();
}, [handleExitModalToggle, handleResultModalToggle]);
const handleExitModalOpen = useCallback(() => {
timePause();
handleExitModalToggle();
}, [handleExitModalToggle, timePause]);
const handleReplay = () => {
handleResultModalToggle();
router.reload();
};
const currentTypingInfos: CurrentTypingInfoType = {
originalTyping,
userTyping,
typingTime: time,
typingCount,
typingWpm,
typingAccuracy,
typingSpeed,
index: currentIdx,
};
const prevNextTypingInfo: PrevNextTypingInfoType = {
prevUserTyping,
prevOriginalTyping,
nextOriginalTyping,
};
const endGameValue: EndGameValueType = {
result: typingAvgResult,
endTime: history.current[history.current.length - 1]?.endTime ?? new Date(),
isExitModalOpen,
isResultModalOpen,
handleExitModalOpen,
handleExitModalClose,
handleResultModalOpen,
handleReplay,
};
const currentTypingActions: CurrentTypingActionType = {
onEndTyping: handleEndTyping,
onTyping: handleTyping,
onExit: handleExitModalOpen,
};
return {
currentTypingInfos,
currentTypingActions,
endGameValue,
prevNextTypingInfo,
};
};
export default usePracticeShortTyping;