Passed
Push — master ( cb41c7...97cf22 )
by Jesús
01:56
created

src/main.ts   A

Complexity

Total Complexity 14
Complexity/F 2

Size

Lines of Code 90
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 74
mnd 7
bc 7
fnc 7
dl 0
loc 90
rs 10
bpm 1
cpm 2
noi 0
c 0
b 0
f 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A main.ts ➔ handleReset 0 5 1
A main.ts ➔ closeLearnModal 0 7 1
A main.ts ➔ setupLearnModal 0 11 5
A main.ts ➔ setupModalEventListeners 0 10 2
A main.ts ➔ setupModalKeyboardHandler 0 6 2
A main.ts ➔ init 0 23 2
A main.ts ➔ openLearnModal 0 5 1
1
import { getTranslation, wordlistToUILang } from './i18n';
2
import { resetBoxes } from './core/state';
3
import { loadWordlist } from './services/wordlist';
4
import { initTheme, toggleTheme } from './services/theme';
5
import { initLanguage, setupLanguageToggle, setTranslations, updateUITranslations } from './services/language';
6
import { createGrid } from './components/grid';
7
import { updateDisplay } from './components/display';
8
import { setupWordInput, clearWordInput } from './components/wordInput';
9
import { elements } from './core/dom';
10
11
function handleReset(): void {
12
  resetBoxes();
13
  clearWordInput();
14
  updateDisplay();
15
}
16
17
function openLearnModal(modal: HTMLElement): void {
18
  modal.removeAttribute('hidden');
19
  modal.setAttribute('aria-hidden', 'false');
20
  document.body.style.overflow = 'hidden'; // Prevent background scrolling
21
}
22
23
function closeLearnModal(modal: HTMLElement): void {
24
  modal.setAttribute('aria-hidden', 'true');
25
  setTimeout(() => {
26
    modal.setAttribute('hidden', '');
27
    document.body.style.overflow = ''; // Restore scrolling
28
  }, 300); // Match CSS transition duration
29
}
30
31
function setupModalEventListeners(
32
  learnBtn: HTMLElement,
33
  modal: HTMLElement,
34
  modalClose: HTMLElement,
35
  modalOverlay: Element | null
36
): void {
37
  learnBtn.addEventListener('click', () => openLearnModal(modal));
38
  modalClose.addEventListener('click', () => closeLearnModal(modal));
39
  modalOverlay?.addEventListener('click', () => closeLearnModal(modal));
40
}
41
42
function setupModalKeyboardHandler(modal: HTMLElement): void {
43
  document.addEventListener('keydown', (e) => {
44
    if (e.key === 'Escape' && modal.getAttribute('aria-hidden') === 'false') {
45
      e.preventDefault();
46
      closeLearnModal(modal);
47
    }
48
  });
49
}
50
51
function setupLearnModal(): void {
52
  const learnBtn = document.getElementById('learn-more-btn');
53
  const modal = document.getElementById('learn-modal');
54
  const modalClose = document.getElementById('modal-close');
55
  const modalOverlay = modal?.querySelector('.modal-overlay') ?? null;
56
57
  if (!learnBtn || !modal || !modalClose) return;
58
59
  setupModalEventListeners(learnBtn, modal, modalClose, modalOverlay);
60
  setupModalKeyboardHandler(modal);
61
}
62
63
async function init(): Promise<void> {
64
  initTheme();
65
  const savedLanguage = initLanguage();
66
67
  const uiLang = wordlistToUILang[savedLanguage] || 'en';
68
  setTranslations(getTranslation(uiLang));
69
70
  updateUITranslations();
71
  await loadWordlist(savedLanguage);
72
  createGrid();
73
  setupWordInput();
74
75
  // Inject git commit hash into footer
76
  const gitHashElement = document.getElementById('git-hash');
77
  if (gitHashElement) {
78
    gitHashElement.textContent = import.meta.env.VITE_GIT_HASH || 'dev';
79
  }
80
81
  elements.resetButton.addEventListener('click', handleReset);
82
  elements.themeToggle.addEventListener('click', toggleTheme);
83
  setupLanguageToggle();
84
  setupLearnModal();
85
}
86
87
init().catch((error) => {
88
  console.error('Failed to initialize application:', error);
89
});
90