import { useQuery } from "@tanstack/react-query"; import { DnsHistory } from "@shared/schema"; import { formatDistanceToNow } from "date-fns"; import { Loader2 } from "lucide-react"; import { Card, CardContent, CardTitle } from "@/components/ui/card"; interface RecentActivityProps { domainId?: number; } export function RecentActivity({ domainId }: RecentActivityProps) { // Fetch history data for the specified domain or all domains if not specified const queryKey = domainId ? ["/api/dns-history/domain", domainId] : ["/api/dns-history"]; const { data: historyEntries = [], isLoading } = useQuery({ queryKey, enabled: domainId !== undefined, }); // Limit to last 5 entries const recentEntries = historyEntries.slice(0, 5); const getActivityIcon = (action: string) => { switch (action) { case "create": return "border-success"; case "update": return "border-primary"; case "delete": return "border-destructive"; default: return "border-muted-foreground"; } }; const getActivityTitle = (entry: DnsHistory) => { if (!entry.previousValue || !entry.newValue) return "Unknown action"; try { const prev = JSON.parse(entry.previousValue); const curr = JSON.parse(entry.newValue); switch (entry.action) { case "create": return `${curr.name} ${curr.type} record created`; case "update": return `${curr.name} ${curr.type} record updated`; case "delete": return `${prev.name} ${prev.type} record deleted`; default: return "Unknown action"; } } catch (e) { return "Error parsing history"; } }; const getActivityDetails = (entry: DnsHistory) => { if (!entry.newValue) return ""; try { const data = JSON.parse(entry.newValue); switch (entry.action) { case "create": case "update": return `Value: ${data.content}`; default: return ""; } } catch (e) { return ""; } }; if (isLoading) { return (
); } return ( Recent Activity {recentEntries.length === 0 ? (
No recent activity found
) : (
{recentEntries.map((entry) => (

{getActivityTitle(entry)}

{getActivityDetails(entry)}

{formatDistanceToNow(new Date(entry.timestamp), { addSuffix: true })}

))}
)}
); }