// Copyright (c) 2026 Gordon Bolton. MIT License. import { getActiveTab, saveActiveTab } from '../utils/storage.js'; import { loadHistory } from './history.js'; import { renderRequests } from './requests.js'; export function initTabs() { const downloadsTab = document.querySelector('[data-tab="downloads"]'); const requestsTab = document.querySelector('[data-tab="requests"]'); const historyTab = document.querySelector('[data-tab="history"]'); if (!downloadsTab || !historyTab) return; // Load saved tab const savedTab = getActiveTab(); if (savedTab === 'requests') { activateTab('requests'); } else if (savedTab === 'history') { activateTab('history'); } else { activateTab('downloads'); } downloadsTab.addEventListener('click', () => activateTab('downloads')); if (requestsTab) { requestsTab.addEventListener('click', () => activateTab('requests')); } historyTab.addEventListener('click', () => activateTab('history')); } export function activateTab(tab) { const downloadsTab = document.querySelector('[data-tab="downloads"]'); const requestsTab = document.querySelector('[data-tab="requests"]'); const historyTab = document.querySelector('[data-tab="history"]'); const downloadsSection = document.getElementById('tab-downloads'); const requestsSection = document.getElementById('tab-requests'); const historySection = document.getElementById('tab-history'); // Remove active class from all tabs if (downloadsTab) downloadsTab.classList.remove('active'); if (requestsTab) requestsTab.classList.remove('active'); if (historyTab) historyTab.classList.remove('active'); // Hide all sections if (downloadsSection) downloadsSection.classList.add('hidden'); if (requestsSection) requestsSection.classList.add('hidden'); if (historySection) historySection.classList.add('hidden'); if (tab === 'downloads') { if (downloadsTab) downloadsTab.classList.add('active'); if (downloadsSection) downloadsSection.classList.remove('hidden'); saveActiveTab('downloads'); } else if (tab === 'requests') { if (requestsTab) requestsTab.classList.add('active'); if (requestsSection) requestsSection.classList.remove('hidden'); saveActiveTab('requests'); renderRequests(); } else if (tab === 'history') { if (historyTab) historyTab.classList.add('active'); if (historySection) historySection.classList.remove('hidden'); saveActiveTab('history'); loadHistory(); } } export function goHome() { activateTab('downloads'); }