import React from "react"; import Link from "next/link"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Pencil, Trash2, ToggleLeft, ToggleRight } from "lucide-react"; import { useSonContext } from "@/contexts/SonContext"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog"; import { SonListSkeleton } from "./SonListSkeleton"; import { useToast } from "@/components/ui/use-toast"; import { Son } from "@/lib/types"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/components/ui/tooltip"; function formatDuration(duration: string): string { console.log("duration", duration); const match = duration.match(/^(\d+)([smhdw])$/); if (!match) return duration; const [, value, unit] = match; const num = parseInt(value, 10); switch (unit) { case "s": return `${num} second${num !== 1 ? "s" : ""}`; case "m": return `${num} minute${num !== 1 ? "s" : ""}`; case "h": return `${num} hour${num !== 1 ? "s" : ""}`; case "d": return `${num} day${num !== 1 ? "s" : ""}`; case "w": return `${num} week${num !== 1 ? "s" : ""}`; default: return duration; } } export function SonList() { const { sons, loading, error, deleteSon, updateSon } = useSonContext(); const { toast } = useToast(); if (loading) { return ; } if (error) { return
Error: {error.message}
; } if (sons.length === 0) { return
No sons found. Create your first son!
; } const handleToggleEnabled = async (son: Son) => { try { await updateSon(son.id, { ...son, enabled: !son.enabled }); toast({ title: "Son Updated", description: `${son.name} has been ${ !son.enabled ? "enabled" : "disabled" }.`, variant: "default", }); } catch (error) { toast({ title: "Error", description: "Failed to update Son status.", variant: "destructive", }); } }; return ( Your Sons Name Trigger Status Delay Actions {sons.map((son) => ( {son.name} {son.trigger}

{son.enabled ? "Enabled" : "Disabled"}

{formatDuration(son.delay)}
Are you sure? This action cannot be undone. This will permanently delete the Son. Cancel deleteSon(son.id)}> Delete
))}
); }