"use client"; import { useState, useMemo } from "react"; import Link from "next/link"; import { Pencil, Trash2, RefreshCw, Tv2, CalendarDays, Download, ChevronUp, ChevronDown, } from "lucide-react"; import { Button } from "@/components/ui/button"; import { useActiveSchedule } from "@/hooks/use-channels"; import type { ChannelResponse } from "@/lib/types"; import { ConfirmDialog } from "./confirm-dialog"; interface ChannelCardProps { channel: ChannelResponse; isGenerating: boolean; isFirst: boolean; isLast: boolean; onEdit: () => void; onDelete: () => void; onGenerateSchedule: () => void; onViewSchedule: () => void; onExport: () => void; onMoveUp: () => void; onMoveDown: () => void; } function useScheduleStatus(channelId: string) { const { data: schedule } = useActiveSchedule(channelId); // eslint-disable-next-line react-hooks/purity -- Date.now() inside useMemo is stable enough for schedule status const now = useMemo(() => Date.now(), []); if (!schedule) return { status: "none" as const, label: null }; const expiresAt = new Date(schedule.valid_until); const hoursLeft = (expiresAt.getTime() - now) / (1000 * 60 * 60); if (hoursLeft < 0) { return { status: "expired" as const, label: "Schedule expired" }; } if (hoursLeft < 6) { const h = Math.ceil(hoursLeft); return { status: "expiring" as const, label: `Expires in ${h}h` }; } const fmt = expiresAt.toLocaleDateString(undefined, { weekday: "short", hour: "2-digit", minute: "2-digit", hour12: false, }); return { status: "ok" as const, label: `Until ${fmt}` }; } export function ChannelCard({ channel, isGenerating, isFirst, isLast, onEdit, onDelete, onGenerateSchedule, onViewSchedule, onExport, onMoveUp, onMoveDown, }: ChannelCardProps) { const [confirmOpen, setConfirmOpen] = useState(false); const blockCount = Object.values(channel.schedule_config.day_blocks).reduce( (sum, blocks) => sum + blocks.length, 0 ); const { status, label } = useScheduleStatus(channel.id); const scheduleColor = status === "expired" ? "text-red-400" : status === "expiring" ? "text-amber-400" : status === "ok" ? "text-zinc-500" : "text-zinc-600"; return (
{channel.description}
)}