'use client' import { useEffect, useState } from 'react' import { useRouter } from 'next/navigation' import { useQuery } from '@tanstack/react-query' import { monitorAPI } from '@/lib/api' import { isAuthenticated, clearAuth } from '@/lib/auth' export default function DashboardPage() { const router = useRouter() const [showAddForm, setShowAddForm] = useState(false) const [newMonitor, setNewMonitor] = useState({ url: '', name: '', frequency: 60, }) useEffect(() => { if (!isAuthenticated()) { router.push('/login') } }, [router]) const { data, isLoading, refetch } = useQuery({ queryKey: ['monitors'], queryFn: async () => { const response = await monitorAPI.list() return response.monitors }, }) const handleLogout = () => { clearAuth() router.push('/login') } const handleAddMonitor = async (e: React.FormEvent) => { e.preventDefault() try { await monitorAPI.create(newMonitor) setNewMonitor({ url: '', name: '', frequency: 60 }) setShowAddForm(false) refetch() } catch (err) { console.error('Failed to create monitor:', err) } } const handleCheckNow = async (id: string) => { try { await monitorAPI.check(id) alert('Check triggered! Results will appear shortly.') setTimeout(() => refetch(), 2000) } catch (err) { console.error('Failed to trigger check:', err) } } const handleDelete = async (id: string) => { if (!confirm('Are you sure you want to delete this monitor?')) return try { await monitorAPI.delete(id) refetch() } catch (err) { console.error('Failed to delete monitor:', err) } } if (isLoading) { return (

Loading...

) } const monitors = data || [] return (
{/* Header */}

Website Monitor

{/* Main Content */}

Your Monitors

{monitors.length} monitor{monitors.length !== 1 ? 's' : ''} active

{/* Add Monitor Form */} {showAddForm && (

Add New Monitor

setNewMonitor({ ...newMonitor, url: e.target.value }) } placeholder="https://example.com" required className="mt-1 block w-full rounded-md border px-3 py-2" />
setNewMonitor({ ...newMonitor, name: e.target.value }) } placeholder="My Monitor" className="mt-1 block w-full rounded-md border px-3 py-2" />
)} {/* Monitors List */} {monitors.length === 0 ? (

No monitors yet

) : (
{monitors.map((monitor: any) => (

{monitor.name}

{monitor.url}

Every {monitor.frequency} min Status: {monitor.status} {monitor.last_checked_at && ( Last checked:{' '} {new Date(monitor.last_checked_at).toLocaleString()} )}
))}
)}
) }