'use client'; import { useState } from 'react'; import { useProcesses, useProcessLogs, useMainLog, useClearProcessLogs, useClearMainLog } from '@/lib/hooks/useSupervisor'; import { LogViewer } from '@/components/logs/LogViewer'; import { LogControls } from '@/components/logs/LogControls'; import { LogSearch } from '@/components/logs/LogSearch'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { AlertCircle } from 'lucide-react'; export default function LogsPage() { const [selectedProcess, setSelectedProcess] = useState('main'); const [logType, setLogType] = useState<'stdout' | 'stderr'>('stdout'); const [searchTerm, setSearchTerm] = useState(''); const [isPlaying, setIsPlaying] = useState(true); const [autoScroll, setAutoScroll] = useState(true); const { data: processes, isError: processesError } = useProcesses({ refetchInterval: isPlaying ? 3000 : undefined, }); // Fetch logs based on selection const { data: processLogs, isLoading: processLogsLoading, refetch: refetchProcessLogs } = useProcessLogs( selectedProcess as string, logType, { enabled: selectedProcess !== 'main' && isPlaying, refetchInterval: isPlaying ? 2000 : undefined, } ); const { data: mainLogData, isLoading: mainLogLoading, refetch: refetchMainLog } = useMainLog({ enabled: selectedProcess === 'main' && isPlaying, refetchInterval: isPlaying ? 2000 : undefined, }); const clearProcessLogsMutation = useClearProcessLogs(); const clearMainLogMutation = useClearMainLog(); const currentLogs = selectedProcess === 'main' ? mainLogData?.logs || '' : processLogs?.bytes || ''; const isLoading = selectedProcess === 'main' ? mainLogLoading : processLogsLoading; const handleRefresh = () => { if (selectedProcess === 'main') { refetchMainLog(); } else { refetchProcessLogs(); } }; const handleClear = () => { if (!confirm('Are you sure you want to clear these logs?')) return; if (selectedProcess === 'main') { clearMainLogMutation.mutate(); } else { clearProcessLogsMutation.mutate(selectedProcess); } }; const handleDownload = () => { const blob = new Blob([currentLogs], { type: 'text/plain' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `${selectedProcess === 'main' ? 'supervisord' : selectedProcess}-${logType}-${Date.now()}.log`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); }; if (processesError) { return (

Process Logs

Failed to load processes

Could not connect to Supervisor. Please check your configuration.

); } return (

Process Logs

Real-time log viewing and management

{/* Controls */} Log Controls {/* Process Selector */}
{/* Log Type Selector (only for processes, not main) */} {selectedProcess !== 'main' && (
)}
{/* Search and Controls */}
setIsPlaying(!isPlaying)} autoScroll={autoScroll} onToggleAutoScroll={() => setAutoScroll(!autoScroll)} onRefresh={handleRefresh} onClear={handleClear} onDownload={handleDownload} isClearing={clearProcessLogsMutation.isPending || clearMainLogMutation.isPending} />
{/* Log Viewer */}
); }