Passed
Pull Request — master (#5)
by Jesús
01:51
created

main.ts ➔ init   A

Complexity

Conditions 2

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 23
rs 9.376
c 0
b 0
f 0
cc 2
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 setupLearnModal(): void {
18
  const learnBtn = document.getElementById('learn-more-btn');
19
  const modal = document.getElementById('learn-modal');
20
  const modalClose = document.getElementById('modal-close');
21
  const modalOverlay = modal?.querySelector('.modal-overlay');
22
23
  if (!learnBtn || !modal || !modalClose) return;
24
25
  const openModal = () => {
26
    modal.removeAttribute('hidden');
27
    modal.setAttribute('aria-hidden', 'false');
28
    document.body.style.overflow = 'hidden'; // Prevent background scrolling
29
  };
30
31
  const closeModal = () => {
32
    modal.setAttribute('aria-hidden', 'true');
33
    setTimeout(() => {
34
      modal.setAttribute('hidden', '');
35
      document.body.style.overflow = ''; // Restore scrolling
36
    }, 300); // Match CSS transition duration
37
  };
38
39
  learnBtn.addEventListener('click', openModal);
40
  modalClose.addEventListener('click', closeModal);
41
  modalOverlay?.addEventListener('click', closeModal);
42
43
  // Close modal with Escape key
44
  document.addEventListener('keydown', (e) => {
45
    if (e.key === 'Escape' && modal.getAttribute('aria-hidden') === 'false') {
46
      e.preventDefault();
47
      closeModal();
48
    }
49
  });
50
}
51
52
async function init(): Promise<void> {
53
  initTheme();
54
  const savedLanguage = initLanguage();
55
56
  const uiLang = wordlistToUILang[savedLanguage] || 'en';
57
  setTranslations(getTranslation(uiLang));
58
59
  await loadWordlist(savedLanguage);
60
  createGrid();
61
  setupWordInput();
62
  updateUITranslations();
63
64
  // Inject git commit hash into footer
65
  const gitHashElement = document.getElementById('git-hash');
66
  if (gitHashElement) {
67
    gitHashElement.textContent = import.meta.env.VITE_GIT_HASH || 'dev';
68
  }
69
70
  elements.resetButton.addEventListener('click', handleReset);
71
  elements.themeToggle.addEventListener('click', toggleTheme);
72
  setupLanguageToggle();
73
  setupLearnModal();
74
}
75
76
init();
77