"use client"; import { useState } from "react"; import { trpc } from "~/lib/trpc/client.js"; import { BroadcastModal } from "./BroadcastModal.js"; import { CreateTaskModal } from "./CreateTaskModal.js"; function formatDate(date: string | Date): string { const d = typeof date === "string" ? new Date(date) : date; return d.toLocaleDateString("en-GB", { day: "2-digit", month: "short", year: "numeric", hour: "2-digit", minute: "2-digit", }); } const TARGET_LABELS: Record = { all: "All Users", role: "By Role", project: "By Project", orgUnit: "By Org Unit", user: "Specific User", }; export function BroadcastManagementClient() { const [showModal, setShowModal] = useState(false); const [showTaskModal, setShowTaskModal] = useState(false); const { data: broadcasts = [], isLoading } = trpc.notification.listBroadcasts.useQuery( { limit: 50 }, { staleTime: 30_000 }, ); const utils = trpc.useUtils(); function handleSuccess() { void utils.notification.listBroadcasts.invalidate(); } return (
{/* Header */}

Broadcast Management

{/* Loading */} {isLoading && (
{[...Array(3)].map((_, i) => (
))}
)} {/* Table */} {!isLoading && ( <> {broadcasts.length === 0 ? (
No broadcasts sent yet.
) : (
{broadcasts.map((b) => ( ))}
Title Target Recipients Sender Date

{b.title}

{b.body && (

{b.body}

)}
{TARGET_LABELS[b.targetType] ?? b.targetType} {b.targetValue && ( ({b.targetValue}) )} {b.recipientCount ?? 0} {b.sender?.name ?? b.sender?.email ?? "-"} {b.sentAt ? formatDate(b.sentAt) : "Scheduled"}
)} )} {/* Broadcast Modal */} {showModal && ( setShowModal(false)} onSuccess={handleSuccess} /> )} {/* Create Task Modal */} {showTaskModal && ( setShowTaskModal(false)} onSuccess={handleSuccess} /> )}
); }