@@ -285,55 +285,22 @@ export default function Worldle() {
285285 setStarted ( true ) ;
286286 } ;
287287
288- useEffect ( ( ) => {
289- if ( started ) containerRef . current ?. focus ( ) ;
290- } , [ started , guesses , gameState ] ) ;
291-
292- if ( rawDataset . length <= 0 ) {
293- return < Navigate replace to = "/games" /> ;
294- }
295-
296- if ( ! started || ! answer ) {
297- return (
298- < OptionsScreen
299- dataset = { rawDataset }
300- onStart = { handleStart }
301- />
302- ) ;
303- }
288+ const answerName = answer ?. name ?. common || "" ;
289+ const template = useMemo ( ( ) => buildTemplate ( answerName ) , [ answerName ] ) ;
290+ const letterSlots = useMemo ( ( ) => template . reduce ( ( count , ch ) => count + ( ch === null ? 1 : 0 ) , 0 ) , [ template ] ) ;
304291
305- const answerName = answer . name . common ;
306- const template = buildTemplate ( answerName ) ;
307- const letterSlots = template . reduce ( ( count , ch ) => count + ( ch === null ? 1 : 0 ) , 0 ) ;
308-
309- // Dynamic cell size: fit within container, min 28px, max 48px
310- const totalChars = answerName . length ;
311- const cellSize = Math . max ( 28 , Math . min ( 48 , Math . floor ( 320 / totalChars ) ) ) ;
312- const spaceSize = Math . max ( 8 , cellSize * 0.35 ) ;
313- const fontSize = cellSize * 0.4 ;
314-
315- const buildGuess = ( letters ) => {
316- let li = 0 ;
317- return template . map ( ( ch ) => ( ch !== null ? ch : letters [ li ++ ] || "" ) ) . join ( "" ) ;
318- } ;
319-
320- const isFilled = letterInput . length === letterSlots ;
321-
322- const showMessage = ( msg ) => {
323- setMessage ( msg ) ;
324- setTimeout ( ( ) => setMessage ( "" ) , 1500 ) ;
325- } ;
326-
327- const submitGuess = ( ) => {
328- if ( gameState !== "playing" ) return ;
329- if ( ! isFilled ) {
292+ const submitGuess = useCallback ( ( ) => {
293+ if ( gameState !== "playing" || ! answer ) return ;
294+ if ( letterInput . length !== letterSlots ) {
330295 setShake ( true ) ;
331- showMessage ( `Fill all ${ letterSlots } letters` ) ;
296+ setMessage ( `Fill all ${ letterSlots } letters` ) ;
297+ setTimeout ( ( ) => setMessage ( "" ) , 1500 ) ;
332298 setTimeout ( ( ) => setShake ( false ) , 500 ) ;
333299 return ;
334300 }
335301
336- const full = buildGuess ( letterInput ) ;
302+ let li = 0 ;
303+ const full = template . map ( ( ch ) => ( ch !== null ? ch : letterInput [ li ++ ] || "" ) ) . join ( "" ) ;
337304 const newGuesses = [ ...guesses , full ] ;
338305 setGuesses ( newGuesses ) ;
339306 setLetterInput ( [ ] ) ;
@@ -346,7 +313,7 @@ export default function Worldle() {
346313 setGameState ( "lost" ) ;
347314 setStats ( ( prev ) => ( { correct : prev . correct , wrong : prev . wrong + 1 } ) ) ;
348315 }
349- } ;
316+ } , [ gameState , answer , letterInput , letterSlots , template , guesses , answerName ] ) ;
350317
351318 const processKey = useCallback ( ( key ) => {
352319 if ( gameState !== "playing" ) return ;
@@ -364,6 +331,35 @@ export default function Worldle() {
364331 }
365332 } , [ letterInput , letterSlots , gameState , submitGuess ] ) ;
366333
334+ useEffect ( ( ) => {
335+ if ( started ) containerRef . current ?. focus ( ) ;
336+ } , [ started , guesses , gameState ] ) ;
337+
338+ if ( rawDataset . length <= 0 ) {
339+ return < Navigate replace to = "/games" /> ;
340+ }
341+
342+ if ( ! started || ! answer ) {
343+ return (
344+ < OptionsScreen
345+ dataset = { rawDataset }
346+ onStart = { handleStart }
347+ />
348+ ) ;
349+ }
350+
351+ const totalChars = answerName . length ;
352+ const cellSize = Math . max ( 28 , Math . min ( 48 , Math . floor ( 320 / totalChars ) ) ) ;
353+ const spaceSize = Math . max ( 8 , cellSize * 0.35 ) ;
354+ const fontSize = cellSize * 0.4 ;
355+
356+ const buildGuess = ( letters ) => {
357+ let li = 0 ;
358+ return template . map ( ( ch ) => ( ch !== null ? ch : letters [ li ++ ] || "" ) ) . join ( "" ) ;
359+ } ;
360+
361+ const isFilled = letterInput . length === letterSlots ;
362+
367363 const handleKeyDown = ( e ) => {
368364 if ( gameState !== "playing" ) return ;
369365 if ( e . key === "Enter" ) e . preventDefault ( ) ;
0 commit comments