"use client"; import React, { useCallback, useEffect, useMemo, useState } from "react"; import { Mail, Megaphone, Pencil, Plus } from "lucide-react"; import { api, errorText } from "@/lib/api"; import { fmtDate } from "@/lib/format"; import { matches } from "@/lib/paging"; import type { InstallSummary, Notice, NoticeLevel } from "@/lib/types"; import { Badge, FormError, Loading, Modal, ModalButtons, PageMessage, PageTitle, SearchInput, useSubmit } from "@/components/ui"; type Msg = { type: "ok" | "err"; text: string } | null; const LEVELS: { key: NoticeLevel; label: string; hint: string }[] = [ { key: "info", label: "Information", hint: "Blue banner" }, { key: "maintenance", label: "Planned maintenance", hint: "Amber banner" }, { key: "warning", label: "Important", hint: "Red banner" }, ]; const LEVEL_LABEL = Object.fromEntries(LEVELS.map(l => [l.key, l.label])) as Record; // works in the browser's time; the API takes ISO. const toInput = (d: string | null) => { if (!d) return ""; const x = new Date(d); return new Date(x.getTime() - x.getTimezoneOffset() * 60000).toISOString().slice(0, 16); }; const fromInput = (v: string) => (v ? new Date(v).toISOString() : null); type Form = { title: string; message: string; level: NoticeLevel; allInstalls: boolean; installIds: string[]; showBanner: boolean; sendEmail: boolean; startsAt: string; endsAt: string }; const empty = (): Form => ({ title: "", message: "", level: "info", allInstalls: true, installIds: [], showBanner: true, sendEmail: false, startsAt: "", endsAt: "" }); function state(n: Notice, now = Date.now()) { if (!n.showBanner) return { label: "Email only", value: "info" as const }; if (n.endsAt && new Date(n.endsAt).getTime() <= now) return { label: "Ended", value: "info" as const }; if (n.startsAt && new Date(n.startsAt).getTime() > now) return { label: "Scheduled", value: "warning" as const }; return { label: "Showing", value: "ok" as const }; } /** Pick every install or some of them. */ function Audience({ installs, form, setForm }: { installs: InstallSummary[]; form: Form; setForm: (f: Form) => void }) { const [query, setQuery] = useState(""); const shown = useMemo(() => installs.filter(i => matches(query, i.name, i.url, i.customer?.name)), [installs, query]); const toggle = (id: string) => setForm({ ...form, installIds: form.installIds.includes(id) ? form.installIds.filter(x => x !== id) : [...form.installIds, id] }); return (
Who gets it
{!form.allInstalls && (
{shown.length === 0 &&

No installs match.

} {shown.map(i => ( ))}
)}
); } const toForm = (n: Notice): Form => ({ title: n.title, message: n.message, level: n.level, allInstalls: n.allInstalls, installIds: n.installIds, showBanner: n.showBanner, sendEmail: false, startsAt: toInput(n.startsAt), endsAt: toInput(n.endsAt) }); /** Hub 2.16.0: a new notice, or changes to one, in a popup. */ function NoticeModal({ notice, installs, onClose, onSaved }: { notice: Notice | null; installs: InstallSummary[]; onClose: () => void; onSaved: (text: string) => void }) { const initial = useMemo(() => (notice ? toForm(notice) : empty()), [notice]); const [form, setForm] = useState(initial); const { busy, error, run } = useSubmit(); const editing = !!notice; const dirty = JSON.stringify(form) !== JSON.stringify(initial); const save = (e: React.FormEvent) => { e.preventDefault(); const f = form; if (!editing && f.sendEmail && !confirm(`Email "${f.title}" to ${f.allInstalls ? "every church" : `${f.installIds.length} church${f.installIds.length === 1 ? "" : "es"}`} now?`)) return; run(async () => { const body = { title: f.title, message: f.message, level: f.level, allInstalls: f.allInstalls, installIds: f.allInstalls ? [] : f.installIds, showBanner: f.showBanner, startsAt: fromInput(f.startsAt), endsAt: fromInput(f.endsAt), ...(editing ? {} : { sendEmail: f.sendEmail }) }; const n = await api(editing ? `/api/admin/notices/${notice.id}` : "/api/admin/notices", { method: editing ? "PUT" : "POST", body }); const parts = [editing ? "Saved." : "Created."]; if (!editing && f.sendEmail) parts.push(`Emailed ${n.emailedTo} address${n.emailedTo === 1 ? "" : "es"}.`); if (n.showBanner) parts.push("Sites show the banner after their next heartbeat (within 15 minutes)."); onSaved(parts.join(" ")); }); }; return (
setForm({ ...form, title: e.target.value })} required />