import { useState, useMemo } from "react"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { Card, CardContent, CardHeader } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Badge } from "@/components/ui/badge"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Separator } from "@/components/ui/separator"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogTitle, } from "@/components/ui/dialog"; import { Search, CheckCheck, Send, BellRing, Mail, MessageSquare, History, Target, ArrowRight, Loader2, Calendar, } from "lucide-react"; import { notificationService } from "@/services/notification.service"; import type { SendPushNotificationRequest, SendSmsNotificationRequest, SendEmailNotificationRequest, } from "@/services/notification.service"; import { toast } from "sonner"; import { format } from "date-fns"; import { cn } from "@/lib/utils"; type Channel = "PUSH" | "SMS" | "EMAIL"; export default function NotificationsPage() { const queryClient = useQueryClient(); const [searchQuery, setSearchQuery] = useState(""); const [activeChannel, setActiveChannel] = useState("PUSH"); const [isSendModalOpen, setIsSendModalOpen] = useState(false); // Combined form state const [pushForm, setPushForm] = useState({ title: "", body: "", recipientId: "", url: "", icon: "/assets/icon.png", }); const [smsForm, setSmsForm] = useState({ body: "", recipientPhone: "", }); const [emailForm, setEmailForm] = useState({ subject: "", body: "", recipientEmail: "", }); const { data: notifications, isLoading } = useQuery({ queryKey: ["notifications"], queryFn: () => notificationService.getNotifications(), }); const { data: unreadCount } = useQuery({ queryKey: ["notifications", "unread-count"], queryFn: () => notificationService.getUnreadCount(), }); const pushMutation = useMutation({ mutationFn: (data: SendPushNotificationRequest) => notificationService.sendPushNotification(data), onSuccess: () => { toast.success("Network transmission: Push packet delivered to gateway"); setIsSendModalOpen(false); queryClient.invalidateQueries({ queryKey: ["notifications"] }); }, }); const smsMutation = useMutation({ mutationFn: (data: SendSmsNotificationRequest) => notificationService.sendSmsNotification(data), onSuccess: () => { toast.success("Cellular uplink: SMS payload queued for broadcast"); setIsSendModalOpen(false); queryClient.invalidateQueries({ queryKey: ["notifications"] }); }, }); const emailMutation = useMutation({ mutationFn: (data: SendEmailNotificationRequest) => notificationService.sendEmailNotification(data), onSuccess: () => { toast.success("SMTP Handshake: Email broadcast initiated"); setIsSendModalOpen(false); queryClient.invalidateQueries({ queryKey: ["notifications"] }); }, }); const filteredNotifications = useMemo(() => { if (!notifications) return []; return notifications.filter((n) => { if (!searchQuery) return true; const q = searchQuery.toLowerCase(); return ( n.title?.toLowerCase().includes(q) || n.body.toLowerCase().includes(q) ); }); }, [notifications, searchQuery]); const handleSend = () => { if (activeChannel === "PUSH") pushMutation.mutate(pushForm); else if (activeChannel === "SMS") smsMutation.mutate(smsForm); else if (activeChannel === "EMAIL") emailMutation.mutate(emailForm); }; const isPending = pushMutation.isPending || smsMutation.isPending || emailMutation.isPending; return (
{/* Header Section */}
Messaging Hub

Command Center

Dispatch multi-channel broadcasts and monitor real-time network telemetry across the Yaltopia mesh.

{/* Stats / Quick Info */}
System Status Active
{unreadCount ?? 0}

Active notifications in current window.

{notifications?.length ?? 0}
Push
SMS
Mail

Operator Directives

{[ { icon: Target, label: "Audience Segmentation", desc: "Filter by role", }, { icon: Calendar, label: "Scheduled Dispatch", desc: "Queue for later", }, { icon: History, label: "Audit Integrity", desc: "Full log access", }, ].map((item, idx) => (

{item.label}

{item.desc}

))}
{/* History Feed */}

Transmission Log

setSearchQuery(e.target.value)} />
{isLoading ? ( Array.from({ length: 4 }).map((_, i) => (
)) ) : filteredNotifications.length ? ( filteredNotifications.map((n) => (
{n.title} Push

{n.body}

{format( new Date(n.createdAt), "HH:mm · MMM d, yyyy", )} {n.isSent ? "Delivered" : "Queued"}
)) ) : (
Zero Telemetry No transmissions detected in the current signal range.
)}
{/* Dispatch Dialog */}
{/* Decorative element */}
Signal Transmission
Dispatch{" "} Broadcast Authoritative platform-wide signal broadcast. Choose delivery channels and construct the payload with precision.
{/* Channel Selector */}
{[ { id: "PUSH", icon: BellRing, label: "Push Notification" }, { id: "SMS", icon: MessageSquare, label: "SMS Gateway" }, { id: "EMAIL", icon: Mail, label: "Email Relay" }, ].map((c) => ( ))}
{/* Dynamic Form Area */}
{activeChannel === "PUSH" && (
setPushForm({ ...pushForm, title: e.target.value }) } placeholder="Critical System Patch Available" className="h-12 rounded-xl bg-slate-50 border-slate-200/60 font-bold" />