"use client"; import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import { XCircle } from 'lucide-react'; import { format, differenceInMinutes } from 'date-fns'; interface Host { hostname: string; lastUpdate: string; } export default function Home() { const router = useRouter(); const [hosts, setHosts] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const fetchHosts = async () => { try { setLoading(true); const response = await fetch('/api/hosts'); if (!response.ok) throw new Error('获取主机列表失败'); const data = await response.json(); setHosts(data); } catch (err) { setError(err instanceof Error ? err.message : '未知错误'); } finally { setLoading(false); } }; const formatDate = (date: string) => { return format(new Date(date), 'yyyy-MM-dd HH:mm:ss'); }; const isRecent = (date: string) => { const diffMinutes = differenceInMinutes(new Date(), new Date(date)); return diffMinutes <= 60; // 1小时内 }; const navigateToHost = (hostname: string) => { router.push(`/hosts/${hostname}`); }; useEffect(() => { fetchHosts(); }, []); return (

屏幕截图监控系统

{/* 主机列表卡片网格 */}
{hosts.map((host) => (
navigateToHost(host.hostname)} >

{host.hostname}

最后更新: {formatDate(host.lastUpdate)}

))}
{/* 加载状态 */} {loading && (
)} {/* 错误提示 */} {error && (

加载失败

{error}

)}
); }