"use client"; import { useState } from "react"; import { getStopPendingCustomers, type StopCustomer } from "@/actions/stops/get-stop-customers"; import { sendStopBlast } from "@/actions/communications/stop-blast"; import { AdminButton } from "@/components/admin/design-system"; const quickMessages = [ { label: "Truck running late", value: "Heads up — the truck is running about 15 minutes behind schedule. Thanks for your patience!" }, { label: "Stop moved", value: "Attention — today's stop location has changed to a new address. Please check the updated details." }, { label: "Weather delay", value: "Due to weather conditions, today's stop may be delayed. We'll send updates as the day progresses." }, { label: "Sold out", value: "This stop has sold out of several items. Check our website or next stop for availability." }, { label: "Preorder cutoff reminder", value: "Friendly reminder — the preorder cutoff for our next stop is tonight at midnight. Order online to guarantee your pickup!" }, { label: "Pickup reminder", value: "Reminder — you have an order ready for pickup today. See you soon!" }, ]; export default function StopMessagingForm({ stopId, brandId, }: { stopId: string; brandId: string; }) { const [customers, setCustomers] = useState([]); const [loaded, setLoaded] = useState(false); const [loading, setLoading] = useState(false); const [channel, setChannel] = useState<"sms" | "email" | "both">("sms"); const [message, setMessage] = useState(""); const [customMessage, setCustomMessage] = useState(""); const [sending, setSending] = useState(false); const [sent, setSent] = useState(0); const [error, setError] = useState(null); async function loadCustomers() { setLoading(true); setError(null); const result = await getStopPendingCustomers(stopId); if (!result.success) { setError(result.error); setLoading(false); return; } setCustomers(result.customers); setLoaded(true); setLoading(false); } function applyQuickMessage(msg: string) { setMessage(msg); setCustomMessage(msg); } async function handleSend() { if (!message.trim()) return; setSending(true); setError(null); const blast = await sendStopBlast({ stopId, brandId, channel, body: message, audience: "pending", }); if (!blast.success) { setError(blast.error); setSending(false); return; } setSent(blast.messages_logged); setSending(false); } const recipients = customers.filter((c) => { if (channel === "sms") return c.customer_phone; if (channel === "email") return c.customer_email; return c.customer_phone || c.customer_email; }); return (

Send Message

Notify all customers with pending pickups at this stop.

{!loaded ? ( ) : ( <> {customers.length === 0 ? (
No pending orders for this stop yet.
) : (
{customers.length} pending order{customers.length !== 1 ? "s" : ""} found {recipients.length !== customers.length && ( — {recipients.length} with contact info )}
)} {/* Channel */}
{(["sms", "email", "both"] as const).map((ch) => ( ))}
{/* Quick messages */}
{quickMessages.map((qm) => ( ))}
{/* Message preview */}