import { useCallback, useEffect, useState } from 'react'; import { useQueryClient } from '@tanstack/react-query'; import { Dashboard } from './pages/Dashboard'; import { Checklist } from './pages/Checklist'; import { Documents } from './pages/Documents'; import { Scanner } from './pages/Scanner'; import { IncidentCenter } from './pages/IncidentCenter'; import { Settings } from './pages/Settings'; import { Education } from './pages/Education'; import { Navigation } from './components/Navigation'; import { ComplianceProvider, useCompliance } from './context/ComplianceContext'; import { useConfig, isDevMode, PLAN_OVERRIDE_KEY, dispatchPlanChange, externalApiFetch } from './hooks/useApi'; import { useDashboardStats, usePlanSync } from './hooks/useAccount'; import { FINDINGS_KEY, fetchPluginFindings } from './hooks/useFindings'; export type Page = 'dashboard' | 'checklist' | 'documents' | 'scanner' | 'incidents' | 'education' | 'settings'; const PAGES: Page[] = ['dashboard', 'checklist', 'documents', 'scanner', 'incidents', 'education', 'settings']; /** Session key used to open a template when navigating to Documents. */ export const OPEN_TEMPLATE_KEY = 'resicoma_open_template'; function pageFromHash(): Page { const value = window.location.hash.replace(/^#\/?/, '').split(/[/?]/)[0]; return (PAGES as string[]).includes(value) ? (value as Page) : 'dashboard'; } /** Keep the WordPress admin submenu highlight in step with the current section. */ function syncWpMenu(page: Page) { const menu = document.getElementById('toplevel_page_resicoma'); if (!menu) return; const target = page === 'dashboard' ? 'page=resicoma' : `page=resicoma#/${page}`; menu.querySelectorAll('.wp-submenu li').forEach((li) => { const link = li.querySelector('a'); if (!link) return; const match = (link.getAttribute('href') || '').endsWith(target); li.classList.toggle('current', match); link.classList.toggle('current', match); if (match) link.setAttribute('aria-current', 'page'); else link.removeAttribute('aria-current'); }); } export function navigateTo(page: Page, options?: { template?: string }) { if (options?.template) { try { window.sessionStorage.setItem(OPEN_TEMPLATE_KEY, options.template); } catch { // Session storage can be unavailable; the Documents page then opens on the list. } } const hash = page === 'dashboard' ? '#/' : `#/${page}`; if (window.location.hash === hash) { window.dispatchEvent(new HashChangeEvent('hashchange')); } else { window.location.hash = hash; } } export function App() { return ( ); } function Shell() { const config = useConfig(); const queryClient = useQueryClient(); const { completedItems, totalItems, loading: checklistLoading } = useCompliance(); const [currentPage, setCurrentPage] = useState(pageFromHash); const [, setNavTick] = useState(0); const stats = useDashboardStats(); usePlanSync(); useEffect(() => { const onHashChange = () => { setCurrentPage(pageFromHash()); setNavTick((n) => n + 1); }; window.addEventListener('hashchange', onHashChange); return () => window.removeEventListener('hashchange', onHashChange); }, []); useEffect(() => { syncWpMenu(currentPage); window.scrollTo(0, 0); }, [currentPage]); // Warm the caches for the service-backed pages so they open quickly. useEffect(() => { if (!config.connected) return; void queryClient.prefetchQuery({ queryKey: ['monitored-plugins'], queryFn: async () => (await externalApiFetch<{ data: unknown[] }>('/api/plugins')).data ?? [], staleTime: 60_000, }); void queryClient.prefetchQuery({ queryKey: FINDINGS_KEY, queryFn: fetchPluginFindings, staleTime: 60_000 }); void queryClient.prefetchQuery({ queryKey: ['incidents'], queryFn: async () => (await externalApiFetch<{ data: unknown[] }>('/api/dashboard?action=incidents_list')).data ?? [], staleTime: 60_000, }); }, [config.connected, queryClient]); const navigate = useCallback((page: Page) => navigateTo(page), []); const counts = stats.data ? { incidents: stats.data.overdue_incidents || 0, scanner: stats.data.critical_vulnerabilities || 0, } : undefined; const planOverride = isDevMode() ? readOverride() : null; return (
{planOverride && (

Development plan override: {planOverride}

)}
{currentPage === 'dashboard' && } {currentPage === 'checklist' && } {currentPage === 'documents' && } {currentPage === 'scanner' && } {currentPage === 'incidents' && } {currentPage === 'education' && } {currentPage === 'settings' && }
); } function readOverride(): string | null { try { return window.localStorage.getItem(PLAN_OVERRIDE_KEY); } catch { return null; } }