import { getEntries, getSettings, getTodayEntries, getWeekEntries, formatDuration, formatDate } from '../storage.js' import type { TimeEntry } from '../types.js' import { generateReportPDF } from '../pdf.js' let currentRange: 'today' | 'week' | 'all' = 'today' let currentEntries: TimeEntry[] = [] const els = { totalHours: document.getElementById('total-hours') as HTMLSpanElement, entryCount: document.getElementById('entry-count') as HTMLSpanElement, estimatedValue: document.getElementById('estimated-value') as HTMLSpanElement, entriesContainer: document.getElementById('entries') as HTMLDivElement, downloadBtn: document.getElementById('download-pdf') as HTMLButtonElement, } async function init(): Promise { await loadEntries('today') document.querySelectorAll('.filter-btn').forEach((btn) => { btn.addEventListener('click', async () => { document.querySelectorAll('.filter-btn').forEach((b) => b.classList.remove('active')) btn.classList.add('active') const range = btn.getAttribute('data-range') as 'today' | 'week' | 'all' await loadEntries(range) }) }) els.downloadBtn.addEventListener('click', async () => { const settings = await getSettings() const blob = generateReportPDF(currentEntries, settings, currentRange) downloadBlob(blob, `ticksimple-report-${currentRange}-${new Date().toISOString().slice(0, 10)}.pdf`) }) } async function loadEntries(range: 'today' | 'week' | 'all'): Promise { currentRange = range const allEntries = await getEntries() const settings = await getSettings() switch (range) { case 'today': currentEntries = getTodayEntries(allEntries) break case 'week': currentEntries = getWeekEntries(allEntries) break case 'all': currentEntries = allEntries.filter((e) => e.endTime !== null).sort((a, b) => b.startTime - a.startTime) break } const totalMs = currentEntries.reduce((sum, e) => sum + e.duration, 0) const totalHours = totalMs / 3600000 const estimatedValue = totalHours * settings.hourlyRate els.totalHours.textContent = `${totalHours.toFixed(1)}h` els.entryCount.textContent = String(currentEntries.length) els.estimatedValue.textContent = `${settings.currency}${estimatedValue.toFixed(0)}` if (currentEntries.length === 0) { els.entriesContainer.innerHTML = '

No entries for this period.

' return } els.entriesContainer.innerHTML = currentEntries.map((entry) => `
${formatDuration(entry.duration)}
`).join('') } function escapeHtml(text: string): string { const div = document.createElement('div') div.textContent = text return div.innerHTML } function downloadBlob(blob: Blob, filename: string): void { const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = filename a.click() URL.revokeObjectURL(url) } init().catch(console.error)