| Total Complexity | 9 |
| Complexity/F | 1.8 |
| Lines of Code | 55 |
| Function Count | 5 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import { elements, resetBoxes, setStateFromIndex, state } from '../../bip39'; |
||
| 2 | import { updateDisplay } from '../../display'; |
||
| 3 | import { showToast } from '../../display'; |
||
| 4 | import { currentTranslations } from '../../language'; |
||
| 5 | import { getWordIndex, isWordInWordlist } from '../domain/wordInputHelpers'; |
||
| 6 | |||
| 7 | let errorClearTimeout: NodeJS.Timeout | null = null; |
||
| 8 | |||
| 9 | export function validateWordInput(): void { |
||
| 10 | const value = elements.wordInput.value.trim().toLowerCase(); |
||
| 11 | |||
| 12 | if (!value) { |
||
| 13 | clearInputError(); |
||
| 14 | return; |
||
| 15 | } |
||
| 16 | |||
| 17 | const wordExists = isWordInWordlist(value, state.wordlist); |
||
| 18 | |||
| 19 | if (wordExists) { |
||
| 20 | handleValidWord(value); |
||
| 21 | } else { |
||
| 22 | handleInvalidWord(); |
||
| 23 | } |
||
| 24 | } |
||
| 25 | |||
| 26 | function clearInputError(): void { |
||
| 27 | elements.wordInput.classList.remove('error'); |
||
| 28 | } |
||
| 29 | |||
| 30 | function handleValidWord(value: string): void { |
||
| 31 | clearInputError(); |
||
| 32 | const wordIndex = getWordIndex(value, state.wordlist); |
||
| 33 | if (wordIndex !== -1) { |
||
| 34 | setStateFromIndex(wordIndex); |
||
| 35 | updateDisplay(); |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | function handleInvalidWord(): void { |
||
| 40 | elements.wordInput.classList.add('error'); |
||
| 41 | resetBoxes(); |
||
| 42 | updateDisplay(); |
||
| 43 | showToast('invalid-word-toast', currentTranslations.invalidWordMessage); |
||
| 44 | scheduleErrorAutoClear(); |
||
| 45 | } |
||
| 46 | |||
| 47 | function scheduleErrorAutoClear(): void { |
||
| 48 | if (errorClearTimeout) { |
||
| 49 | clearTimeout(errorClearTimeout); |
||
| 50 | } |
||
| 51 | errorClearTimeout = setTimeout(() => { |
||
| 52 | elements.wordInput.classList.remove('error'); |
||
| 53 | }, 3500); |
||
| 54 | } |
||
| 55 |