import { useState } from 'react'; import { useQuery } from '@tanstack/react-query'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Input } from '@/components/ui/input'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Loader2, Search, ChevronDown, ChevronRight } from 'lucide-react'; import { format } from 'date-fns'; import { supabase } from '@/lib/supabaseClient'; interface EdgeFunctionLog { id: string; timestamp: number; event_type: string; event_message: string; function_id: string; level: string; } const FUNCTION_NAMES = [ 'detect-location', 'process-selective-approval', 'process-selective-rejection', ]; export function EdgeFunctionLogs() { const [selectedFunction, setSelectedFunction] = useState('all'); const [searchTerm, setSearchTerm] = useState(''); const [timeRange, setTimeRange] = useState<'1h' | '24h' | '7d'>('24h'); const [expandedLog, setExpandedLog] = useState(null); const { data: logs, isLoading } = useQuery({ queryKey: ['edge-function-logs', selectedFunction, timeRange], queryFn: async () => { // Query Supabase edge function logs // Note: This uses the analytics endpoint which requires specific permissions const hoursAgo = timeRange === '1h' ? 1 : timeRange === '24h' ? 24 : 168; const startTime = Date.now() - (hoursAgo * 60 * 60 * 1000); // For now, return the logs from context as an example // In production, this would call the Supabase Management API const allLogs: EdgeFunctionLog[] = []; return allLogs; }, refetchInterval: 30000, // Refresh every 30 seconds }); const filteredLogs = logs?.filter(log => { if (searchTerm && !log.event_message.toLowerCase().includes(searchTerm.toLowerCase())) { return false; } return true; }) || []; const getLevelColor = (level: string): "default" | "destructive" | "secondary" => { switch (level.toLowerCase()) { case 'error': return 'destructive'; case 'warn': return 'destructive'; case 'info': return 'default'; default: return 'secondary'; } }; const toggleExpand = (logId: string) => { setExpandedLog(expandedLog === logId ? null : logId); }; return (
setSearchTerm(e.target.value)} className="pl-10" />
{isLoading ? (
) : filteredLogs.length === 0 ? (

No edge function logs found. Logs will appear here when edge functions are invoked.

) : (
{filteredLogs.map((log) => ( toggleExpand(log.id)} >
{expandedLog === log.id ? ( ) : ( )} {log.level} {format(log.timestamp, 'HH:mm:ss.SSS')} {log.event_type}
{log.event_message}
{expandedLog === log.id && (
Full Message:

{log.event_message}

Timestamp:

{format(log.timestamp, 'PPpp')}

)}
))}
)}
); }