// Main dashboard page: metrics, refresh, config 'use client'; import { useEffect, useState } from 'react'; import { signOut, useSession } from 'next-auth/react'; import { SessionsLineChart, CategoriesBarChart } from '../../components/Charts'; import DashboardSettings from './settings'; import UserManagement from './users'; import { Company, MetricsResult } from '../../lib/types'; interface MetricsCardProps { label: string; value: string | number | null | undefined; } function MetricsCard({ label, value }: MetricsCardProps) { return (
{value ?? '-'} {label}
); } export default function DashboardPage() { const { data: session } = useSession() || { data: null }; const [metrics, setMetrics] = useState(null); const [company, setCompany] = useState(null); // Loading state used in the fetchData function const [, setLoading] = useState(false); const [csvUrl, setCsvUrl] = useState(''); const [refreshing, setRefreshing] = useState(false); const isAdmin = session?.user?.role === 'admin'; const isAuditor = session?.user?.role === 'auditor'; useEffect(() => { // Fetch metrics, company, and CSV URL on mount const fetchData = async () => { setLoading(true); const res = await fetch('/api/dashboard/metrics'); const data = await res.json(); setMetrics(data.metrics); setCompany(data.company); setCsvUrl(data.csvUrl); setLoading(false); }; fetchData(); }, []); async function handleRefresh() { if (isAuditor) return; // Prevent auditors from refreshing setRefreshing(true); await fetch('/api/admin/refresh-sessions', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ companyId: company?.id }), }); setRefreshing(false); window.location.reload(); } async function handleSaveConfig() { if (isAuditor) return; // Prevent auditors from changing config await fetch('/api/dashboard/config', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ csvUrl }), }); window.location.reload(); } return (

Analytics Dashboard

{/* Admin-only settings and user management */} {company && isAdmin && ( <> )}
setCsvUrl(e.target.value)} placeholder="CSV feed URL (with basic auth if set in backend)" readOnly={isAuditor} /> {!isAuditor && ( <> )}

Sessions Per Day

{metrics?.days && Object.keys(metrics.days).length > 0 ? : No data}

Top Categories

{metrics?.categories && Object.keys(metrics.categories).length > 0 ? : No data}

Languages

{metrics?.languages ? Object.entries(metrics.languages).map(([lang, n]) => (
{lang} {String(n)}
)) : No data}
); }