1
- import { useState , useRef , useCallback } from "react" ;
1
+ import { useState , useRef } from "react" ;
2
2
import { Guess , State , GameEvent } from "@types" ;
3
3
4
4
export function useGuesses (
@@ -21,109 +21,104 @@ export function useGuesses(
21
21
} ) )
22
22
)
23
23
) ;
24
+
24
25
const gameEventTimerId = useRef < number | null > ( null ) ;
25
26
26
- const onKeyPress = useCallback (
27
- ( keyValue : string ) => {
28
- if ( ! isValidKey ( keyValue ) ) return ;
27
+ function handleKeyPress ( keyValue : string ) {
28
+ if ( ! isValidKey ( keyValue ) ) return ;
29
29
30
- if ( ! isSpecialKey ( keyValue ) ) {
31
- currentAttempt . length < solutionLength &&
32
- setCurrentAttempt (
33
- ( prevCurrentGuess ) =>
34
- prevCurrentGuess + keyValue . toLocaleLowerCase ( )
35
- ) ;
36
- }
30
+ if ( ! isSpecialKey ( keyValue ) ) {
31
+ currentAttempt . length < solutionLength &&
32
+ setCurrentAttempt (
33
+ ( prevCurrentGuess ) => prevCurrentGuess + keyValue . toLocaleLowerCase ( )
34
+ ) ;
35
+ }
37
36
38
- if ( isDelete ( keyValue ) ) {
39
- currentAttempt . length > 0 &&
40
- setCurrentAttempt ( ( prevCurrentGuess ) =>
41
- prevCurrentGuess . slice ( 0 , - 1 )
42
- ) ;
43
- }
37
+ if ( isDelete ( keyValue ) ) {
38
+ currentAttempt . length > 0 &&
39
+ setCurrentAttempt ( ( prevCurrentGuess ) => prevCurrentGuess . slice ( 0 , - 1 ) ) ;
40
+ }
44
41
45
- if ( isSubmit ( keyValue ) ) {
46
- const canSubmitAttempt =
47
- history . length < max_turns &&
48
- ! history . includes ( currentAttempt ) &&
49
- currentAttempt . length === solutionLength &&
50
- wordList . includes ( currentAttempt ) ;
51
-
52
- if ( ! canSubmitAttempt ) {
53
- let msg = "" ;
54
- let type = "error" ;
55
-
56
- if ( history . length >= max_turns ) {
57
- msg = solution ;
58
- type = "game-over" ;
59
- } else if ( history . includes ( currentAttempt ) ) {
60
- msg = "Already tried that!" ;
61
- } else if ( currentAttempt . length !== solutionLength ) {
62
- msg = "Not enough letters!" ;
63
- } else if ( ! wordList . includes ( currentAttempt ) ) {
64
- msg = "Not in word list" ;
65
- }
66
-
67
- onEvent ( {
68
- type,
69
- msg,
70
- } as GameEvent ) ;
71
-
72
- gameEventTimerId . current && clearTimeout ( gameEventTimerId . current ) ;
73
- gameEventTimerId . current = setTimeout ( ( ) => onEvent ( null ) , 1200 ) ;
74
-
75
- return ;
42
+ if ( isSubmit ( keyValue ) ) {
43
+ const canSubmitAttempt =
44
+ history . length < max_turns &&
45
+ ! history . includes ( currentAttempt ) &&
46
+ currentAttempt . length === solutionLength &&
47
+ wordList . includes ( currentAttempt ) ;
48
+
49
+ if ( ! canSubmitAttempt ) {
50
+ let msg = "" ;
51
+ let type = "error" ;
52
+
53
+ if ( history . length >= max_turns ) {
54
+ msg = solution ;
55
+ type = "game-over" ;
56
+ } else if ( history . includes ( currentAttempt ) ) {
57
+ msg = "Already tried that!" ;
58
+ } else if ( currentAttempt . length !== solutionLength ) {
59
+ msg = "Not enough letters!" ;
60
+ } else if ( ! wordList . includes ( currentAttempt ) ) {
61
+ msg = "Not in word list" ;
76
62
}
77
63
78
- const currentGuess = toGuess ( currentAttempt , solution ) ;
64
+ onEvent ( {
65
+ type,
66
+ msg,
67
+ } as GameEvent ) ;
79
68
80
- setGuesses ( ( prevGuesses ) => {
81
- const newGuesses = [ ... prevGuesses ] as Guess [ ] [ ] ;
82
- newGuesses [ history . length ] = currentGuess ;
83
- return newGuesses ;
84
- } ) ;
69
+ gameEventTimerId . current && clearTimeout ( gameEventTimerId . current ) ;
70
+ gameEventTimerId . current = setTimeout ( ( ) => onEvent ( null ) , 1200 ) ;
71
+
72
+ return ;
73
+ }
85
74
86
- setKeyStates ( ( prevKeyStates ) => {
87
- const newKeyStates = { ...prevKeyStates } ;
88
- currentGuess . forEach ( ( { letter, state } ) => {
89
- const _letter = letter ! . toLocaleUpperCase ( ) ;
90
- newKeyStates [ _letter ] = getFinalKeyState (
91
- newKeyStates [ _letter ] ,
92
- state
93
- ) ;
94
- } ) ;
95
-
96
- return newKeyStates ;
75
+ const currentGuess = toGuess ( currentAttempt , solution ) ;
76
+
77
+ setGuesses ( ( prevGuesses ) => {
78
+ const newGuesses = [ ...prevGuesses ] as Guess [ ] [ ] ;
79
+ newGuesses [ history . length ] = currentGuess ;
80
+ return newGuesses ;
81
+ } ) ;
82
+
83
+ setKeyStates ( ( prevKeyStates ) => {
84
+ const newKeyStates = { ...prevKeyStates } ;
85
+ currentGuess . forEach ( ( { letter, state } ) => {
86
+ const _letter = letter ! . toLocaleUpperCase ( ) ;
87
+ newKeyStates [ _letter ] = getFinalKeyState (
88
+ newKeyStates [ _letter ] ,
89
+ state
90
+ ) ;
97
91
} ) ;
98
92
99
- setHistory ( ( prevHistory ) => [ ...prevHistory , currentAttempt ] ) ;
93
+ return newKeyStates ;
94
+ } ) ;
100
95
101
- history . length + 1 >= max_turns &&
102
- onEvent ( {
103
- type : "game-over" ,
104
- msg : solution ,
105
- } ) ;
96
+ setHistory ( ( prevHistory ) => [ ...prevHistory , currentAttempt ] ) ;
106
97
107
- currentAttempt === solution &&
108
- onEvent ( {
109
- type : "win" ,
110
- msg : [ "Impressive!" , "Great!" ] . at (
111
- Math . floor ( Math . random ( ) * 2 )
112
- ) as string ,
113
- } ) ;
98
+ history . length + 1 >= max_turns &&
99
+ onEvent ( {
100
+ type : "game-over" ,
101
+ msg : solution ,
102
+ } ) ;
114
103
115
- setCurrentAttempt ( "" ) ;
116
- }
117
- } ,
118
- [ currentAttempt , history , max_turns , solutionLength ]
119
- ) ;
104
+ currentAttempt === solution &&
105
+ onEvent ( {
106
+ type : "win" ,
107
+ msg : [ "Impressive!" , "Great!" ] . at (
108
+ Math . floor ( Math . random ( ) * 2 )
109
+ ) as string ,
110
+ } ) ;
111
+
112
+ setCurrentAttempt ( "" ) ;
113
+ }
114
+ }
120
115
121
116
return {
122
117
currentAttempt,
123
118
keyStates,
124
119
guesses,
125
120
history,
126
- onKeyPress ,
121
+ handleKeyPress ,
127
122
} ;
128
123
}
129
124
0 commit comments