'use client'; import React, { useState, useEffect } from 'react'; import { FunctionLog } from '@/lib/vfs/types'; import { Loader2, AlertCircle, RefreshCw, Trash2, CheckCircle2, XCircle, Clock, ArrowRight } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { cn } from '@/lib/utils'; interface LogsViewerProps { deploymentId: string; workspaceId?: string; } interface EnrichedLog extends FunctionLog { functionName?: string; } export function LogsViewer({ deploymentId, workspaceId }: LogsViewerProps) { const apiBase = workspaceId ? `/api/w/${workspaceId}` : '/api'; const [logs, setLogs] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { loadLogs(); }, [deploymentId]); const loadLogs = async () => { try { setLoading(true); setError(null); const res = await fetch(`${apiBase}/admin/deployments/${deploymentId}/database/logs?limit=200`); if (!res.ok) { const data = await res.json(); throw new Error(data.error || 'Failed to load logs'); } const data = await res.json(); setLogs(data.logs); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load logs'); } finally { setLoading(false); } }; const clearLogs = async () => { if (!confirm('Clear all function execution logs? This cannot be undone.')) return; try { const res = await fetch(`${apiBase}/admin/deployments/${deploymentId}/database/logs`, { method: 'DELETE', }); if (!res.ok) throw new Error('Failed to clear logs'); await loadLogs(); } catch (err) { console.error('Failed to clear logs:', err); } }; const formatDate = (date: Date | string) => { const d = typeof date === 'string' ? new Date(date) : date; return d.toLocaleString(); }; if (loading) { return (
); } if (error) { return (

{error}

); } return (

Execution Logs

{logs.length === 0 ? (

No execution logs yet

Logs will appear here when functions are invoked

) : ( {logs.map(log => ( ))}
Status Function Method Path Duration Time
{log.statusCode >= 200 && log.statusCode < 300 ? ( ) : log.statusCode >= 400 ? ( ) : ( )} {log.functionName || log.functionId.slice(0, 8)} {log.method} {log.path} {log.durationMs}ms {formatDate(log.timestamp)}
)}
); }