'use client'; import { useState } from 'react'; import { Loader2, CheckCircle2, XCircle } from 'lucide-react'; import { toast } from 'sonner'; import { Button } from '@/components/ui/button'; import { apiFetch } from '@/lib/api/client'; interface TestResponse { ok: boolean; visitors?: number; error?: string; } /** * Hits POST /api/v1/admin/umami/test which calls Umami's `/api/websites/:id/ * active` to verify auth + websiteId in one request. On success, shows the * live visitor count as proof we got real data back. */ export function UmamiTestButton() { const [pending, setPending] = useState(false); const [result, setResult] = useState(null); async function runTest() { setPending(true); setResult(null); try { const res = await apiFetch<{ data: TestResponse }>('/api/v1/admin/umami/test', { method: 'POST', }); setResult(res.data); if (res.data.ok) { toast.success(`Umami reachable - ${res.data.visitors ?? 0} active visitor(s) right now`); } else { toast.error(res.data.error ?? 'Umami test failed'); } } catch (err) { const message = err instanceof Error ? err.message : 'Test failed'; setResult({ ok: false, error: message }); toast.error(message); } finally { setPending(false); } } return (
{result && (result.ok ? ( Connected ({result.visitors ?? 0} active) ) : ( {result.error ?? 'Failed'} ))}
); }