import { useState, useEffect, useRef } from 'react'; import { motion } from 'framer-motion'; import { RefreshCw, Trash2, ScrollText, Pause, Play } from 'lucide-react'; import { useAuth } from '../context/AuthContext'; import { cn } from '../lib/utils'; export default function Logs() { const { token: adminToken } = useAuth(); const [logs, setLogs] = useState([]); const [isLoading, setIsLoading] = useState(true); const [autoRefresh, setAutoRefresh] = useState(false); const logsEndRef = useRef(null); const fetchLogs = async () => { setIsLoading(true); try { const res = await fetch('/admin/logs', { headers: { 'X-Admin-Token': adminToken } }); const text = await res.text(); // Parse logs: Assuming they are line separated JSON or text let logLines = []; try { const json = JSON.parse(text); if (Array.isArray(json)) logLines = json; } catch { logLines = text.split('\n').filter(Boolean); } setLogs(logLines); } catch (error) { console.error('Failed to fetch logs', error); } finally { setIsLoading(false); } }; useEffect(() => { fetchLogs(); }, [adminToken]); useEffect(() => { let interval; if (autoRefresh) { interval = setInterval(fetchLogs, 5000); } return () => clearInterval(interval); }, [autoRefresh, adminToken]); const clearLogs = async () => { if (!confirm('确定要清空日志吗?')) return; try { await fetch('/admin/logs', { method: 'DELETE', headers: { 'X-Admin-Token': adminToken } }); setLogs([]); } catch (error) { console.error('Failed to clear logs', error); } }; return (
查看实时系统运行日志