import { useCallback, useEffect, useMemo, useState } from "react" import { Link } from "react-router-dom" import { CalendarClock, ChevronLeft, ChevronRight, Megaphone, Plus, RefreshCw, XCircle, } from "lucide-react" import { toast } from "sonner" import { cancelScheduledNotification, getScheduledNotifications, } from "../../api/notifications.api" import { AdminFiltersPanel } from "../../components/filters/AdminFiltersPanel" import { Badge } from "../../components/ui/badge" import { Button } from "../../components/ui/button" import { Card, CardContent } from "../../components/ui/card" import { Select } from "../../components/ui/select" import { SpinnerIcon } from "../../components/ui/spinner-icon" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "../../components/ui/table" import { channelLabel, formatScheduledAtLabel, scheduledStatusBadgeVariant, } from "../../lib/notificationBulk" import { countActiveFilters } from "../../lib/adminFilterUtils" import { cn } from "../../lib/utils" import { DEFAULT_TABLE_PAGE_SIZE } from "../../lib/tablePagination" import type { NotificationChannel, ScheduledNotification, ScheduledNotificationStatus, } from "../../types/notification.types" const STATUS_OPTIONS: Array<{ value: "" | ScheduledNotificationStatus; label: string }> = [ { value: "", label: "All statuses" }, { value: "pending", label: "Pending" }, { value: "processing", label: "Processing" }, { value: "sent", label: "Sent" }, { value: "failed", label: "Failed" }, { value: "cancelled", label: "Cancelled" }, ] const CHANNEL_OPTIONS: Array<{ value: "" | NotificationChannel; label: string }> = [ { value: "", label: "All channels" }, { value: "sms", label: "SMS" }, { value: "email", label: "Email" }, { value: "push", label: "Push" }, { value: "in_app", label: "In-app" }, ] export function ScheduledNotificationsPage() { const [loading, setLoading] = useState(true) const [error, setError] = useState(false) const [rows, setRows] = useState([]) const [totalCount, setTotalCount] = useState(0) const [page, setPage] = useState(1) const [limit] = useState(DEFAULT_TABLE_PAGE_SIZE) const [statusFilter, setStatusFilter] = useState<"" | ScheduledNotificationStatus>("") const [channelFilter, setChannelFilter] = useState<"" | NotificationChannel>("") const [cancellingId, setCancellingId] = useState(null) const totalPages = useMemo( () => Math.max(1, Math.ceil(totalCount / limit)), [totalCount, limit], ) const load = useCallback(async () => { setLoading(true) setError(false) try { const res = await getScheduledNotifications({ page, limit, ...(statusFilter ? { status: statusFilter } : {}), ...(channelFilter ? { channel: channelFilter } : {}), }) setRows(res.data.scheduled_notifications) setTotalCount(res.data.total_count) } catch { setError(true) setRows([]) setTotalCount(0) toast.error("Failed to load scheduled notifications") } finally { setLoading(false) } }, [page, limit, statusFilter, channelFilter]) useEffect(() => { void load() }, [load]) useEffect(() => { setPage(1) }, [statusFilter, channelFilter]) const handleCancel = async (job: ScheduledNotification) => { if (job.status !== "pending" && job.status !== "processing") return try { setCancellingId(job.id) await cancelScheduledNotification(job.id) toast.success("Scheduled notification cancelled", { description: `Job #${job.id} was cancelled.`, }) await load() } catch { toast.error("Failed to cancel scheduled notification") } finally { setCancellingId(null) } } const targetingSummary = (job: ScheduledNotification) => { if (job.target_role) return `Role: ${job.target_role}` if (job.target_user_ids?.length) { return `${job.target_user_ids.length} user(s)` } if (job.target_raw?.phones?.length) { return `${job.target_raw.phones.length} phone(s)` } if (job.target_raw?.emails?.length) { return `${job.target_raw.emails.length} email(s)` } return "—" } const activeFilterCount = countActiveFilters([ { value: statusFilter }, { value: channelFilter }, ]) const clearFilters = () => { setStatusFilter("") setChannelFilter("") } return (

Notifications

Scheduled notifications

View pending and completed bulk notification jobs. Cancel jobs before they are sent.

{loading && (
Loading scheduled jobs…
)} {!loading && error && (
Could not load scheduled notifications.
)} {!loading && !error && rows.length === 0 && (

No scheduled jobs found

Schedule a notification from the composer to see it here.

)} {!loading && !error && rows.length > 0 && ( <>
ID Channel Title / message Audience Scheduled Status Actions {rows.map((job) => ( #{job.id} {channelLabel(job.channel)}

{job.title || "—"}

{job.message}

{targetingSummary(job)} {formatScheduledAtLabel(job.scheduled_at)} {job.status} {job.last_error && job.status === "failed" && (

{job.last_error}

)}
{(job.status === "pending" || job.status === "processing") && ( )}
))}
{totalPages > 1 && (

Page {page} of {totalPages}

)} )}
) }