'use client' import { useState } from 'react' import { useRouter, useParams } from 'next/navigation' import { useQuery } from '@tanstack/react-query' import { monitorAPI } from '@/lib/api' import { DashboardLayout } from '@/components/layout/dashboard-layout' import { Button } from '@/components/ui/button' import { Card, CardContent } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { SEORankingCard } from '@/components/seo-ranking-card' import { toast } from 'sonner' export default function MonitorHistoryPage() { const router = useRouter() const params = useParams() const id = params?.id as string const [isChecking, setIsChecking] = useState(false) const [isCheckingSeo, setIsCheckingSeo] = useState(false) const { data: monitorData, refetch: refetchMonitor } = useQuery({ queryKey: ['monitor', id], queryFn: async () => { const response = await monitorAPI.get(id) return response.monitor }, }) const { data: historyData, isLoading, refetch: refetchHistory } = useQuery({ queryKey: ['history', id], queryFn: async () => { const response = await monitorAPI.history(id) return response.snapshots }, }) const handleCheckNow = async (type: 'content' | 'seo' = 'content') => { if (type === 'seo') { if (isCheckingSeo) return setIsCheckingSeo(true) } else { if (isChecking) return setIsChecking(true) } try { const result = await monitorAPI.check(id, type) if (type === 'seo') { toast.success('SEO Ranking check completed') } else { if (result.snapshot?.errorMessage) { toast.error(`Check failed: ${result.snapshot.errorMessage}`) } else { toast.success(result.snapshot?.changed ? 'Changes detected!' : 'No changes detected') } } refetchMonitor() refetchHistory() } catch (err: any) { console.error('Failed to trigger check:', err) toast.error(`Failed to check ${type === 'seo' ? 'SEO' : 'monitor'}`) } finally { if (type === 'seo') { setIsCheckingSeo(false) } else { setIsChecking(false) } } } if (isLoading) { return ( Loading history... ) } const snapshots = historyData || [] const monitor = monitorData return ( {/* Page Header */} router.push('/monitors')} className="gap-2"> Back {monitor?.name || 'Monitor History'} {monitor && ( {monitor.url} )} {monitor && ( handleCheckNow('content')} disabled={isChecking || isCheckingSeo} className="gap-2" > {isChecking ? ( ) : ( )} Check Now {monitor.seoKeywords && monitor.seoKeywords.length > 0 && ( handleCheckNow('seo')} disabled={isChecking || isCheckingSeo} className="gap-2 border-purple-200 text-purple-700 hover:bg-purple-50" > {isCheckingSeo ? ( ) : ( )} SEO Check )} { try { await monitorAPI.exportAuditTrail(id, 'json'); } catch (e) { console.error('Export failed:', e); } }} className="gap-2" > JSON { try { await monitorAPI.exportAuditTrail(id, 'csv'); } catch (e) { console.error('Export failed:', e); } }} className="gap-2" > CSV )} {/* SEO Rankings */} {monitor && monitor.seoKeywords && monitor.seoKeywords.length > 0 && ( SEO Keyword Performance )} {/* History List */} Check History {snapshots.length === 0 ? ( No history yet The first check will happen soon ) : ( {snapshots.map((snapshot: any) => { // Determine border color based on HTTP status const getBorderColor = () => { if (snapshot.httpStatus >= 400 || snapshot.errorMessage) { return 'border-l-4 border-l-red-500' // Error (4xx, 5xx) } if (snapshot.httpStatus >= 200 && snapshot.httpStatus < 300) { if (snapshot.changed) { return 'border-l-4 border-l-green-500' // Success with change } return 'border-l-4 border-l-blue-400' // Success no change (neutral) } return 'border-l-4 border-l-blue-400' // Default neutral } return ( {snapshot.changed ? ( Changed ) : ( No Change )} {snapshot.errorMessage && ( Error )} {new Date(snapshot.createdAt).toLocaleString(undefined, { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', })} HTTP {snapshot.httpStatus} {snapshot.responseTime}ms {snapshot.changePercentage && ( {Number(snapshot.changePercentage).toFixed(2)}% changed )} {snapshot.errorMessage && ( {snapshot.errorMessage} )} {snapshot.summary && ( Summary {snapshot.summary} )} router.push(`/monitors/${id}/snapshot/${snapshot.id}`)} > {snapshot.errorMessage ? 'View Error' : 'View Details'} ) })} )} ) }
Loading history...
{monitor.url}
The first check will happen soon
{snapshot.errorMessage}
Summary
{snapshot.summary}