'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 */}

{monitor?.name || 'Monitor History'}

{monitor && (

{monitor.url}

)}
{monitor && (
{monitor.seoKeywords && monitor.seoKeywords.length > 0 && ( )}
)}
{/* 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}

)}
) })}
)}
) }