"use client"; import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import { Monitor, Clock, LogOut, Upload, Calendar, Search, Server, AlertCircle, FileText } 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 [searchTerm, setSearchTerm] = useState(''); const fetchHosts = async () => { try { setLoading(true); const response = await fetch('/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 handleLogout = async () => { try { await fetch('/api/auth/logout', { method: 'POST' }); // 刷新页面以触发重新认证(Basic Auth 或重定向) window.location.reload(); } catch (error) { console.error('Logout failed', error); } }; 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(); }, []); const filteredHosts = hosts.filter(host => host.hostname.toLowerCase().includes(searchTerm.toLowerCase()) ); const activeHostsCount = hosts.filter(h => isRecent(h.lastUpdate)).length; return (
{/* Navigation Bar */}
{/* Stats & Search */}

总主机数

{hosts.length}

在线主机

{activeHostsCount}

setSearchTerm(e.target.value)} />
{/* Content */} {loading ? (

正在加载主机列表...

) : error ? (

加载失败

{error}

) : filteredHosts.length === 0 ? (

没有找到主机

{searchTerm ? '尝试调整搜索关键词' : '暂无主机连接记录'}

) : (
{filteredHosts.map((host) => { const online = isRecent(host.lastUpdate); return (
navigateToHost(host.hostname)} className="group bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700 overflow-hidden hover:shadow-md hover:border-blue-300 dark:hover:border-blue-700 transition-all duration-200 cursor-pointer relative" >
{online ? '在线' : '离线'}

{decodeURI(host.hostname)}

{formatDate(host.lastUpdate)}
查看详情
); })}
)}
); }