import { useState } from "react"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog"; import { Clock, AlertTriangle, Skull, Trash2 } from "lucide-react"; import { SigviewRecord as Signature } from "@/lib/pbtypes"; import { getSignatureMeta } from "@/hooks/useSignatureCategories"; import { SignatureEditModal } from "@/components/SignatureEditModal"; interface SignatureListItemProps { signature: Signature; onDelete?: (signatureId: string) => Promise; onUpdate?: (updatedSignature: Partial) => Promise; } export const SignatureListItem = ({ signature, onDelete, onUpdate }: SignatureListItemProps) => { const [isEditModalOpen, setIsEditModalOpen] = useState(false); const isOld = () => { if (!signature.updated) return false; const updatedTime = new Date(signature.updated); if (isNaN(updatedTime.getTime())) return false; // Handle invalid date const now = new Date(); const diffHours = (now.getTime() - updatedTime.getTime()) / (1000 * 60 * 60); return diffHours > 3; }; const formatDate = (dateStr: string | undefined) => { if (!dateStr) return "Unknown"; const date = new Date(dateStr); if (isNaN(date.getTime())) return "Invalid date"; // Handle invalid date const now = new Date(); const diffMs = now.getTime() - date.getTime(); // Handle cases where the time difference is very small or negative if (diffMs < 0) return "Just now"; const diffMinutes = Math.max(0, Math.floor(diffMs / (1000 * 60))); const diffHours = Math.max(0, Math.floor(diffMs / (1000 * 60 * 60))); if (diffMinutes === 0) { return "Just now"; } else if (diffMinutes < 60) { return `${diffMinutes}m ago`; } else if (diffHours < 96) { return `${diffHours}h ago`; } else { return date.toLocaleString(); } }; const meta = getSignatureMeta(signature.type || ""); const isGasSite = signature.type?.toLowerCase().includes('gas'); const oldEntry = isOld(); const handleDelete = async () => { if (onDelete) { try { await onDelete(signature.id); } catch (error) { console.error('Failed to delete signature:', error); } } }; const handleUpdate = async (updatedSignature: Partial) => { if (onUpdate) { try { await onUpdate(updatedSignature); } catch (error) { console.error('Failed to update signature:', error); throw error; } } }; return ( <>
setIsEditModalOpen(true)} >
{/* Type Badge - Most Important */} {signature.dangerous ? : meta.icon} {signature.type || "Unknown Type"} {/* Signature Name and ID */}
{signature.identifier}

{signature.signame || "Unnamed Signature"} {signature.dangerous && ( DANGEROUS )}

{signature.note && ( {signature.note} )}
{/* Dates */}
{signature.updated && (
{oldEntry && } Updated: {formatDate(signature.updated)}
)} {signature.created && (
Created: {formatDate(signature.created)}
)}
{/* Delete Button */} {onDelete && ( Delete Signature Are you sure you want to delete signature {signature.identifier}?
This action cannot be undone.
Cancel Delete
)}
{/* Edit Modal */} {onUpdate && ( setIsEditModalOpen(false)} onSave={handleUpdate} /> )} ); };