import React, { useState } from "react"; import { useSonLogs } from "@/hooks/useSonLogs"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Button } from "@/components/ui/button"; import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card"; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Pagination, PaginationContent, PaginationItem, PaginationLink, PaginationNext, PaginationPrevious, PaginationEllipsis, } from "@/components/ui/pagination"; import { ActionExecutionLog } from "@/lib/types"; import { useToast } from "@/components/ui/use-toast"; import Link from "next/link"; import { WebhookDetailsDialog } from "@/components/WebhookDetailsDialog"; import { Badge } from "@/components/ui/badge"; const StatusBadge: React.FC<{ status: string }> = ({ status }) => { let color = "bg-gray-500"; switch (status.toLowerCase()) { case "success": color = "bg-green-500"; break; case "failure": color = "bg-red-500"; break; case "warning": color = "bg-yellow-500"; break; case "pending": color = "bg-blue-500"; break; } return {status}; }; const SonLogsPage: React.FC = () => { const { logs, loading, error, pagination, fetchLogs, fetchActionLogs } = useSonLogs(); const [selectedExecution, setSelectedExecution] = useState( null ); const [actionLogs, setActionLogs] = useState([]); const [selectedWebhookLogId, setSelectedWebhookLogId] = useState< string | null >(null); const { toast } = useToast(); const totalPages = Math.ceil(pagination.total / pagination.limit); const currentPage = Math.floor(pagination.offset / pagination.limit) + 1; const handlePageChange = (page: number) => { const newOffset = (page - 1) * pagination.limit; fetchLogs(newOffset); }; const openActionLogs = async (executionId: string) => { try { setSelectedExecution(executionId); const actionLogsData = await fetchActionLogs(executionId); setActionLogs(actionLogsData); } catch (error) { toast({ title: "Error", description: "Failed to fetch action logs", variant: "destructive", }); } }; if (loading) { return
Loading...
; } if (error) { return
Error: {error.message}
; } return (
Son Execution Logs Son Name Status Executed At Webhook Actions {logs.map((log) => ( {log.sonName} {new Date(log.executed_at).toLocaleString()} ))}
{currentPage > 1 && ( handlePageChange(currentPage - 1)} /> )} {[...Array(totalPages)].map((_, index) => { const page = index + 1; if ( page === 1 || page === totalPages || (page >= currentPage - 1 && page <= currentPage + 1) ) { return ( handlePageChange(page)} > {page} ); } else if ( page === currentPage - 2 || page === currentPage + 2 ) { return ; } return null; })} {currentPage < totalPages && ( handlePageChange(currentPage + 1)} /> )}
setSelectedExecution(null)} > Action Logs for Execution {selectedExecution} Action Type Status Executed At Error Message {actionLogs.map((log: ActionExecutionLog) => ( {log.action_type} {new Date(log.executed_at).toLocaleString()} {log.error_message || "N/A"} ))}
{selectedWebhookLogId && ( setSelectedWebhookLogId(null)} /> )}
); }; export default SonLogsPage;