|
13 | 13 | // Flag to prevent double-triggering recovery on multiple blur events |
14 | 14 | let recoveryInProgress = $state(false); |
15 | 15 |
|
| 16 | + // Timeout ID for debounced submission |
| 17 | + let submitTimeoutId = $state<number | undefined>(undefined); |
| 18 | +
|
16 | 19 | // TODO: Use word validation instead of presence. |
17 | 20 | const submitEnabled = $derived( |
18 | 21 | words.every((word) => word.value.trim().length > 0), |
|
21 | 24 | const handleKeyDownInput = (event: KeyboardEvent, currentIndex: number) => { |
22 | 25 | const isLastIndex = currentIndex === words.length - 1; |
23 | 26 | if (event.key === "Enter" && isLastIndex && submitEnabled) { |
| 27 | + // Clear any pending timeout and submit immediately |
| 28 | + if (submitTimeoutId !== undefined) { |
| 29 | + clearTimeout(submitTimeoutId); |
| 30 | + submitTimeoutId = undefined; |
| 31 | + } |
24 | 32 | handleRecoverWithPhrase(); |
25 | 33 | return; |
26 | 34 | } |
|
36 | 44 | } |
37 | 45 | } |
38 | 46 | } |
| 47 | +
|
| 48 | + // Clear existing timeout and start new one for auto-submit |
| 49 | + if (submitTimeoutId !== undefined) { |
| 50 | + clearTimeout(submitTimeoutId); |
| 51 | + } |
| 52 | +
|
| 53 | + // Set 1 second timeout to auto-submit if all words are filled |
| 54 | + submitTimeoutId = window.setTimeout(() => { |
| 55 | + if (submitEnabled && !recoveryInProgress) { |
| 56 | + handleRecoverWithPhrase(); |
| 57 | + } |
| 58 | + }, 1000); |
39 | 59 | }; |
40 | 60 |
|
41 | 61 | const handleRecoverWithPhrase = async () => { |
|
92 | 112 | `recovery-phrase-${focusIndex}`, |
93 | 113 | ); |
94 | 114 | // Don't set focus somewhere if all words are filled. |
95 | | - if (nonNullish(nextElement) && focusIndex < words.length - 1) { |
| 115 | + if (nonNullish(nextElement) && focusIndex === words.length - 1) { |
96 | 116 | nextElement.focus(); |
97 | | - } else { |
98 | | - // This will trigger the recovery flow if all words are filled. |
99 | | - (event.currentTarget as HTMLElement)?.blur(); |
100 | 117 | } |
101 | 118 | }; |
102 | 119 |
|
103 | | - const handleBlur = () => { |
104 | | - // Auto-submit when all 24 words are complete |
105 | | - if (submitEnabled && !recoveryInProgress) { |
106 | | - handleRecoverWithPhrase(); |
107 | | - } |
108 | | - }; |
| 120 | + // Cleanup timeout on component unmount |
| 121 | + $effect(() => { |
| 122 | + return () => { |
| 123 | + if (submitTimeoutId !== undefined) { |
| 124 | + clearTimeout(submitTimeoutId); |
| 125 | + } |
| 126 | + }; |
| 127 | + }); |
109 | 128 | </script> |
110 | 129 |
|
111 | 130 | <div |
|
132 | 151 | bind:value={word.value} |
133 | 152 | onkeydown={(e) => handleKeyDownInput(e, i)} |
134 | 153 | onpaste={(e) => handlePaste(e, i)} |
135 | | - onblur={handleBlur} |
136 | 154 | class="peer text-text-primary ring-border-secondary focus:ring-border-brand h-8 w-full rounded-full border-none bg-transparent pl-10 text-base ring outline-none ring-inset focus:ring-2" |
137 | 155 | /> |
138 | 156 | <!-- Left slot --> |
|
0 commit comments