"use client"
/**
* EmailRecipientsReview — the composer's first page: who gets this email.
*
* Select Role (cards) → warning for people who cannot be emailed → Options (send mode, copy
* me, stats) → Recipient list with attention filters, search, select all / clear, inline
* "add email", and pages of 10. Rendered inside the composer rail's body.
*/
import * as React from "react"
import { cn } from "../../lib/utils"
import { Badge, badgeVariants } from "../ui/badge"
import { LocalBanner } from "../ui/banner"
import { Button } from "../ui/button"
import { Checkbox } from "../ui/checkbox"
import { Input } from "../ui/input"
import { SelectionTileGrid } from "../ui/selection-tile-grid"
import { ToggleSwitch } from "../ui/toggle-switch"
import { emailProblem, type EmailAudience, type EmailRecipient } from "./email-composer-types"
type FilterId = "all" | "missing-email" | "blocked" | "multi" | "already"
const PAGE_SIZE = 10
export interface EmailRecipientsReviewProps {
audiences: EmailAudience[]
/** Empty until the sender picks a group. */
audienceId: string
onAudience: (id: string) => void
/** People in every audience, so cards can show counts. */
allPeople: EmailRecipient[]
/** Singular: "schedule". */
recordNoun: string
included: (person: EmailRecipient) => boolean
isBlocked: (person: EmailRecipient) => boolean
onToggle: (id: string, on: boolean) => void
onSetMany: (ids: string[], on: boolean) => void
onFixEmail: (id: string, email: string) => void
separate: boolean
onSeparate: (value: boolean) => void
copyMe: boolean
onCopyMe: (value: boolean) => void
meEmail: string
recipientCount: number
emailCount: number
}
function attentionOf(person: EmailRecipient): FilterId[] {
const kinds: FilterId[] = []
if (emailProblem(person)) kinds.push("missing-email")
if (person.blockedReason) kinds.push("blocked")
if ((person.records ?? 0) > 1) kinds.push("multi")
if (person.lastEmailed) kinds.push("already")
return kinds
}
function OptionSwitch({
id,
checked,
onChange,
label,
count,
}: {
id: string
checked: boolean
onChange: (value: boolean) => void
label: string
/** How many people the option affects, shown after the label. */
count?: number
}) {
return (
{label}
{count != null ? ({count}) : null}
)
}
/** One of the three counts under Options. */
function Stat({ label, value }: { label: string; value: number }) {
return (
)
}
function Section({ title, children, divided }: { title: string; children: React.ReactNode; divided?: boolean }) {
return (
)
}
export function EmailRecipientsReview(p: EmailRecipientsReviewProps) {
const [filter, setFilter] = React.useState("all")
const [query, setQuery] = React.useState("")
const [page, setPage] = React.useState(0)
const [draft, setDraft] = React.useState>({})
const audience = p.audiences.find(a => a.id === p.audienceId)
const people = React.useMemo(() => p.allPeople.filter(r => r.audience === p.audienceId), [p.allPeople, p.audienceId])
// One pass over the people: each person's attention kinds, and the totals.
const attention = React.useMemo(() => new Map(people.map(person => [person.id, new Set(attentionOf(person))])), [people])
const counts = React.useMemo(() => {
const tally: Record = { all: people.length, "missing-email": 0, blocked: 0, multi: 0, already: 0 }
attention.forEach(kinds => kinds.forEach(kind => (tally[kind] += 1)))
return tally
}, [people.length, attention])
const needsAttention = [...attention.values()].filter(kinds => kinds.size > 0).length
const blockedPeople = people.filter(person => person.blockedReason)
const shown = people.filter(person => {
const text = `${person.name} ${person.email}`.toLowerCase()
if (query.trim() && !text.includes(query.trim().toLowerCase())) return false
return filter === "all" ? true : Boolean(attention.get(person.id)?.has(filter))
})
const selectable = shown.filter(person => !p.isBlocked(person))
const pageCount = Math.max(1, Math.ceil(shown.length / PAGE_SIZE))
const safePage = Math.min(page, pageCount - 1)
const start = shown.length === 0 ? 0 : safePage * PAGE_SIZE
const rows = shown.slice(start, start + PAGE_SIZE)
const choose = (id: string) => {
p.onAudience(id)
setFilter("all")
setQuery("")
setPage(0)
}
const filters: { id: FilterId; label: string }[] = [
{ id: "all", label: "All" },
{ id: "missing-email", label: "Missing email" },
{ id: "blocked", label: "Can’t be emailed yet" },
{ id: "multi", label: `On more than one ${p.recordNoun}` },
{ id: "already", label: "Already emailed" },
]
const showSendMode = counts.multi > 0
return (
{/* Same size and layout as the Settings appearance selector. */}
({
value: item.id,
label: `${item.label} (${p.allPeople.filter(r => r.audience === item.id).length})`,
icon: item.icon ?? "fa-user",
}))}
labelPlacement="below"
columns={4}
value={p.audienceId}
onValueChange={choose}
interaction="radio"
idPrefix="email-audience"
itemVariant="outline"
itemMotion="pop"
/>
{blockedPeople.length > 0 ? (
{blockedPeople.length} of {people.length} won't get this email: {blockedPeople[0].blockedReason}.
) : null}
{showSendMode ? (
p.onSeparate(value === "separate")}
interaction="radio"
idPrefix="email-send-mode"
itemVariant="outline"
itemMotion="pop"
/>
) : null}
{p.audienceId ? (
) : null}
{!p.audienceId ? (
Choose a group above to see who will get this email.
) : (
{filters
.filter(item => item.id === "all" || counts[item.id] > 0)
.map(item => (
))}
{
setQuery(event.target.value)
setPage(0)
}}
placeholder="Search name or email"
aria-label="Search recipients"
className="ps-8"
/>
Showing {shown.length === 0 ? 0 : start + 1}–{start + rows.length} of {people.length}
{shown.length === 0 ? - No one matches that filter.
: null}
{rows.map(person => {
const problem = emailProblem(person)
const blocked = p.isBlocked(person)
const on = p.included(person)
return (
-
p.onToggle(person.id, value === true)}
aria-label={`Include ${person.name}`}
className="mt-1"
/>
{person.name}
{person.records ? (
{person.records} {person.records === 1 ? p.recordNoun : `${p.recordNoun}s`}
) : null}
{problem || person.email}
{person.blockedReason ?
{person.blockedReason}
: null}
{(person.records ?? 0) > 1 ? (
{p.separate ? `Gets a separate email for each ${p.recordNoun}.` : `Gets one email for all their ${p.recordNoun}s.`}
) : null}
{problem === "No email address" ? (
) : null}
{audience?.noun ?? audience?.label}
)
})}
{shown.length > PAGE_SIZE ? (
) : null}
)}
)
}