Open source CapCut alternative.
TypeScript
feat(site): email a user from the analytics grid
David Leca478d14056b
diff --git a/site/src/app/su/analytics/product/page.tsx b/site/src/app/su/analytics/product/page.tsxindex 49fe61c6..a6d1f776 100644--- a/site/src/app/su/analytics/product/page.tsx+++ b/site/src/app/su/analytics/product/page.tsx@@ -1,7 +1,7 @@"use client";-import { ArrowUpRight } from "lucide-react";-import { useMemo, useRef } from "react";+import { ArrowUpRight, Mail } from "lucide-react";+import { useMemo, useRef, useState } from "react";import {Area,AreaChart,@@ -33,10 +33,13 @@ import { REFERRAL_SOURCES } from "@/lib/onboarding/sequence";import { useLocalPref } from "@/cut/lib/uiState";import { cn } from "@/lib/utils";import { DragBlock, useReorder } from "@/app/su/analytics/Reorder";+import { OutreachComposeDialog } from "@/app/su/outreach/ComposeDialog";import { SuStandIn } from "@/app/su/SuStandIn";import { useRowWindow } from "@/app/su/analytics/rowWindow";+import { Button } from "@/components/ui/button";import { useAnalyticsRollup } from "@/queries/analytics";import { ApiError } from "@/queries/apiClient";+import { useOutreachAction, type OutreachRow } from "@/queries/outreach";// Everything here renders the nightly rollup (analytics/rollup.json via// /api/analytics/rollup) — stale until the next job run by design. "Active" is@@ -697,6 +700,15 @@ function ActivityGrid({const users = useMemo(() => rankUsers(rollup, workBits, sort), [rollup, workBits, sort]);const body = useRef<HTMLTableSectionElement>(null);const rows = useRowWindow(users.length, body);+ // A row's email button puts the account on the outreach list and opens the+ // same note the Outreach tab writes, so a busy user gets a word or a deal+ // from here.+ const outreach = useOutreachAction();+ const [sendTarget, setSendTarget] = useState<OutreachRow | null>(null);+ const adding =+ outreach.isPending && outreach.variables?.action === "add" ? outreach.variables.email : null;+ const emailUser = (email: string) =>+ outreach.mutate({ action: "add", email }, { onSuccess: (result) => setSendTarget(result.row) });// Newest day sits in the leftmost column so the current dots are in view// before any horizontal scroll; each column keeps its index into the@@ -777,8 +789,20 @@ function ActivityGrid({{users.slice(rows.start, rows.end).map((user, offset) => {const index = rows.start + offset;return (- <tr key={user.id} data-row="">- <td className="sticky left-0 z-10 bg-card py-1 pr-4 whitespace-nowrap">+ <tr key={user.id} data-row="" className="group">+ <td className="sticky left-0 z-10 bg-card py-1 pr-12 whitespace-nowrap">+ {/* Pinned in the cell's right padding, so showing it moves+ nothing. */}+ <Button+ aria-label={`Email ${user.name}`}+ className="absolute top-1/2 right-3 -translate-y-1/2 opacity-0 group-hover:opacity-100 focus-visible:opacity-100"+ disabled={adding === user.email}+ onClick={() => emailUser(user.email)}+ size="icon-xs"+ variant="ghost"+ >+ <Mail />+ </Button><span className="flex items-center gap-1.5"><span className="block max-w-56 truncate text-sm" title={user.name}>{user.email}@@ -851,6 +875,10 @@ function ActivityGrid({data</span></p>+ {outreach.isError ? (+ <p className="mt-2 text-sm text-destructive">Couldn't put that account on the outreach list.</p>+ ) : null}+ <OutreachComposeDialog onClose={() => setSendTarget(null)} target={sendTarget} /></div>);}diff --git a/site/src/app/su/outreach/ComposeDialog.tsx b/site/src/app/su/outreach/ComposeDialog.tsxnew file mode 100644index 00000000..17c8bc07--- /dev/null+++ b/site/src/app/su/outreach/ComposeDialog.tsx@@ -0,0 +1,665 @@+"use client";++import { XIcon } from "lucide-react";+import { useEffect, useRef, useState } from "react";++import {+ CreditOfferFields,+ useCreditOfferDefaults,+} from "@/app/su/CreditOfferFields";+import {+ useLastOutreachStart,+ useOutreachDrafts,+} from "@/app/su/outreach/drafts";+import { UserHistoryPanel } from "@/app/su/outreach/UserHistory";+import { Button } from "@/components/ui/button";+import {+ Dialog,+ DialogClose,+ DialogContent,+ DialogDescription,+ DialogFooter,+ DialogHeader,+ DialogTitle,+} from "@/components/ui/dialog";+import { Input } from "@/components/ui/input";+import { Label } from "@/components/ui/label";+import { Switch } from "@/components/ui/switch";+import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";+import { Textarea } from "@/components/ui/textarea";+import {+ creditOfferTermsIfValid,+ OFFER_CLAIM_NAMES,+ type CreditOfferTerms,+} from "@/lib/credits/offerTerms";+import {+ isOfferPlaceholder,+ OUTREACH_PLACEHOLDERS,+} from "@/lib/marketing/placeholders";+import { cn } from "@/lib/utils";+import { ApiError } from "@/queries/apiClient";+import {+ useBusyOutreachIds,+ useOutreachAction,+ type OutreachRow,+} from "@/queries/outreach";+import {+ useDeleteOutreachTemplate,+ useOutreachTemplates,+ useSaveOutreachTemplate,+} from "@/queries/outreachTemplates";+import { usePromotions, type PromotionSummary } from "@/queries/promotions";++// The note to one person on the outreach list: the words, the send toggles+// and the credit offer, started from a template, a recent send or a+// promotion. The Outreach tab opens it from its rows and Analytics from a+// user's row; both hand it the row to write to and hear when it is done.++const BLANK = "blank";++// A place a note can start from: a saved template, something this browser+// already sent, or a promotion. The first two are starting points; whatever+// ends up in the dialog — words, send toggles and the credit offer alike —+// is what goes out. A promotion goes out as it is saved, the same email its+// segment send mails, so it is read here and not edited.+type StartPoint = {+ id: string;+ title: string;+ meta: string;+ subject: string;+ body: string;+ unsubscribeLink: boolean;+ trackReplies: boolean;+ creditOffer: CreditOfferTerms | null;+ at: number;+ remove?: () => void;+ busy?: boolean;+ promotion?: PromotionSummary;+};++const promotionStartId = (id: string) => `promo:${id}`;++export function ago(iso: string | null): string {+ if (!iso) return "—";+ const mins = Math.floor((Date.now() - new Date(iso).getTime()) / 60000);+ if (mins < 60) return `${Math.max(mins, 0)}m ago`;+ if (mins < 60 * 24) return `${Math.floor(mins / 60)}h ago`;+ return `${Math.floor(mins / (60 * 24))}d ago`;+}++export function OutreachComposeDialog({+ target,+ onClose,+}: {+ /** The row the note goes to; null keeps the dialog closed. */+ target: OutreachRow | null;+ onClose: () => void;+}) {+ const [source, setSource] = useState(BLANK);+ const [subject, setSubject] = useState("");+ const [body, setBody] = useState("");+ const [templateName, setTemplateName] = useState("");+ const [naming, setNaming] = useState(false);+ const bodyField = useRef<HTMLTextAreaElement>(null);+ const act = useOutreachAction();+ const busy = useBusyOutreachIds();+ const templates = useOutreachTemplates();+ const promotions = usePromotions();+ const saveTemplate = useSaveOutreachTemplate();+ const deleteTemplate = useDeleteOutreachTemplate();+ const { drafts, forget, remember } = useOutreachDrafts();+ const [lastStart, setLastStart] = useLastOutreachStart();+ const [unsubscribeLink, setUnsubscribeLink] = useState(true);+ const [trackReplies, setTrackReplies] = useState(true);+ const [creditOffer, setCreditOffer] = useState<CreditOfferTerms | null>(null);+ const [testNotice, setTestNotice] = useState<string | null>(null);+ const offerDefaults = useCreditOfferDefaults();+ // The row on screen, read at the end of a send that may have outlived it.+ const shown = useRef(target);+ useEffect(() => {+ shown.current = target;+ }, [target]);++ const saved = templates.data?.templates ?? [];++ const dropSource = (id: string) => {+ if (source === id) setSource(BLANK);+ };++ // Templates are shared and outlive the browser; recent sends are this+ // browser's own. One list, newest first, each entry saying which it is.+ const starts: StartPoint[] = [+ ...saved.map((template) => ({+ at: new Date(template.updatedAt).getTime(),+ body: template.body,+ busy: deleteTemplate.isPending,+ id: `tpl:${template.id}`,+ meta: "Template",+ remove: () =>+ deleteTemplate.mutate(template.id, {+ onSuccess: () => dropSource(`tpl:${template.id}`),+ }),+ subject: template.subject,+ title: template.name,+ trackReplies: template.trackReplies,+ unsubscribeLink: template.unsubscribeLink,+ creditOffer: creditOfferTermsIfValid(template.promotion),+ })),+ ...drafts.map((draft) => ({+ at: new Date(draft.savedAt).getTime(),+ body: draft.body,+ creditOffer: draft.creditOffer,+ id: `draft:${draft.id}`,+ meta: `Sent ${ago(draft.savedAt)}`,+ remove: () => {+ forget(draft.id);+ dropSource(`draft:${draft.id}`);+ },+ subject: draft.subject,+ title: draft.subject,+ trackReplies: draft.trackReplies,+ unsubscribeLink: draft.unsubscribeLink,+ })),+ ...(promotions.data?.promotions ?? []).map((promotion) => ({+ at: new Date(promotion.updatedAt).getTime(),+ body: promotion.body,+ creditOffer: promotion.creditOffer,+ id: promotionStartId(promotion.id),+ meta: `Promotion · ${promotion.counts.sent} sent`,+ promotion,+ subject: promotion.subject,+ title: promotion.name || "Untitled",+ trackReplies: false,+ unsubscribeLink: true,+ })),+ ].sort((a, b) => b.at - a.at);++ const sources: StartPoint[] = [+ {+ at: 0,+ body: "",+ id: BLANK,+ meta: "",+ subject: "",+ title: "Blank",+ trackReplies: true,+ unsubscribeLink: true,+ creditOffer: null,+ },+ ...starts,+ ];++ const pickSource = (id: string) => {+ const picked = sources.find((s) => s.id === id);+ if (!picked) return;+ setSource(id);+ setSubject(picked.subject);+ setBody(picked.body);+ setUnsubscribeLink(picked.unsubscribeLink);+ setTrackReplies(picked.trackReplies);+ setCreditOffer(picked.creditOffer);+ setNaming(false);+ };++ // Each opening starts from whatever the last note went out from, so a run+ // through a list is one click from ready; with nothing sent yet, the newest+ // starting point, and with nothing saved at all, a blank note. The seed+ // lands in the render that first sees the row, and clears with it, so the+ // same row opened again starts fresh.+ const [seededFor, setSeededFor] = useState<string | null>(null);+ if (target && seededFor !== target.id) {+ setSeededFor(target.id);+ const start =+ sources.find((option) => option.id === lastStart) ??+ starts[0] ??+ sources[0];+ setSource(start.id);+ setSubject(start.subject);+ setBody(start.body);+ setUnsubscribeLink(start.unsubscribeLink);+ setTrackReplies(start.trackReplies);+ setCreditOffer(start.creditOffer);+ setNaming(false);+ setTestNotice(null);+ } else if (!target && seededFor !== null) {+ setSeededFor(null);+ }++ // A promotion is checked whole by the server when it is sent, so the+ // button is live and a blank field comes back as the reason.+ const selectedPromotion = sources.find((s) => s.id === source)?.promotion;+ const sendable =+ selectedPromotion !== undefined ||+ (subject.trim() !== "" && body.trim() !== "");+ const selectedTemplate = source.startsWith("tpl:")+ ? saved.find((template) => `tpl:${template.id}` === source)+ : undefined;++ const submitTemplate = () => {+ const name = templateName.trim();+ if (name === "" || !sendable) return;+ saveTemplate.mutate(+ {+ body,+ name,+ subject,+ trackReplies,+ unsubscribeLink,+ promotion: creditOffer,+ },+ {+ onSuccess: (result) => {+ // The note now lives in a template, so the sent copy of the same+ // words stops being a second entry saying the same thing.+ const same = drafts.find(+ (draft) => draft.subject === subject && draft.body === body,+ );+ if (same) {+ forget(same.id);+ if (lastStart === `draft:${same.id}`) {+ setLastStart(`tpl:${result.template.id}`);+ }+ }+ setSource(`tpl:${result.template.id}`);+ setNaming(false);+ },+ },+ );+ };++ // A placeholder is easier to click than to spell, and it lands where the+ // cursor already is.+ const insertPlaceholder = (name: string) => {+ const token = `{{${name}}}`;+ const field = bodyField.current;+ const start = field?.selectionStart ?? body.length;+ const end = field?.selectionEnd ?? start;+ setBody(`${body.slice(0, start)}${token}${body.slice(end)}`);+ requestAnimationFrame(() => {+ field?.focus();+ field?.setSelectionRange(start + token.length, start + token.length);+ });+ };++ // The note as it stands goes to the operator's own inbox, filled with the+ // row's values, so the words can be read as mail before they are sent.+ const submitTest = () => {+ if (!target || !sendable || selectedPromotion) return;+ setTestNotice(null);+ act.mutate(+ {+ action: "test",+ body,+ creditOffer,+ outreachId: target.id,+ subject,+ unsubscribeLink,+ },+ {+ onSuccess: (result) => setTestNotice(`Test sent to ${result.sentTo}.`),+ },+ );+ };++ // A send in flight leaves the dialog free for the next row, so it closes+ // only if that row is still the one on screen.+ const closeIfShown = (id: string) => {+ if (shown.current?.id === id) onClose();+ };++ const submitSend = () => {+ if (!target || !sendable) return;+ const id = target.id;+ setTestNotice(null);+ if (selectedPromotion) {+ act.mutate(+ {+ action: "promote",+ outreachId: id,+ promotionId: selectedPromotion.id,+ },+ {+ onSuccess: () => {+ setLastStart(source);+ closeIfShown(id);+ },+ },+ );+ return;+ }+ // Sending a start point exactly as it is stays that start point; a change+ // to its words or its toggles becomes an entry of its own.+ const from = sources.find((option) => option.id === source);+ const edited =+ from?.subject !== subject ||+ from.body !== body ||+ from.unsubscribeLink !== unsubscribeLink ||+ from.trackReplies !== trackReplies ||+ JSON.stringify(from.creditOffer) !== JSON.stringify(creditOffer);+ act.mutate(+ {+ action: "send",+ body,+ creditOffer,+ outreachId: id,+ subject,+ trackReplies,+ unsubscribeLink,+ },+ {+ onSuccess: () => {+ setLastStart(+ edited+ ? `draft:${remember({ body, creditOffer, subject, trackReplies, unsubscribeLink })}`+ : source,+ );+ closeIfShown(id);+ },+ },+ );+ };++ const sending = target !== null && busy.has(target.id);+ // A failed send keeps its dialog open, so the reason belongs in there with+ // the words that still need fixing; the server names a bad placeholder.+ const sendFailed =+ act.isError &&+ (act.variables?.action === "send" ||+ act.variables?.action === "test" ||+ act.variables?.action === "promote");+ const sendIssue =+ sendFailed && act.error instanceof ApiError+ ? act.error.issues[0]?.message+ : undefined;+ // The offer's placeholders are offered only while the note carries one.+ const placeholders = OUTREACH_PLACEHOLDERS.filter(+ (name) => creditOffer !== null || !isOfferPlaceholder(name),+ );++ return (+ <Dialog+ onOpenChange={(open) => {+ if (!open) onClose();+ }}+ open={target !== null}+ >+ <DialogContent className="grid max-h-[85vh] grid-rows-[auto_minmax(0,1fr)_auto] sm:max-w-5xl">+ <DialogHeader>+ <DialogTitle>Email {target?.name}</DialogTitle>+ <DialogDescription>+ Goes to {target?.email}.{" "}+ {selectedPromotion+ ? "The promotion goes as its segment send would send it, counts in the promotion, and a later segment send skips this person."+ : trackReplies+ ? "Replies come back to your inbox and mark this row replied."+ : "Replies come straight back to your own address; the Mark replied button files the row."}+ </DialogDescription>+ </DialogHeader>++ {/* The note and the person's past share the dialog; the tabs reset+ with each row so a new person opens on the note. */}+ <Tabs+ className="min-h-0 gap-3"+ defaultValue="note"+ key={target?.id ?? "none"}+ >+ <TabsList variant="line">+ <TabsTrigger value="note">Note</TabsTrigger>+ <TabsTrigger value="history">History</TabsTrigger>+ </TabsList>+ <TabsContent className="min-h-0" value="history">+ {target ? <UserHistoryPanel target={target} /> : null}+ </TabsContent>+ <TabsContent+ className="grid min-h-0 min-w-0 gap-4 md:grid-cols-[18rem_minmax(0,1fr)]"+ value="note"+ >+ <div className="flex min-h-0 flex-col gap-2 md:border-r md:pr-4">+ <span className="text-xs font-medium text-muted-foreground">+ Start from+ </span>+ <div className="min-h-0 flex-1 space-y-0.5 overflow-y-auto max-md:max-h-40">+ {sources.map((option) => (+ <div className="group/start relative" key={option.id}>+ <button+ className={cn(+ "w-full rounded-lg px-2 py-1.5 pr-8 text-left transition-colors",+ source === option.id+ ? "bg-accent text-accent-foreground"+ : "hover:bg-accent/50",+ )}+ onClick={() => pickSource(option.id)}+ type="button"+ >+ <span className="block truncate text-sm">+ {option.title}+ </span>+ {option.meta ? (+ <span className="block truncate text-xs text-muted-foreground">+ {option.meta}+ </span>+ ) : null}+ </button>+ {option.remove ? (+ <Button+ aria-label={`Delete ${option.title}`}+ className="absolute top-1.5 right-1 opacity-0 group-hover/start:opacity-100 focus-visible:opacity-100"+ disabled={option.busy}+ onClick={option.remove}+ size="icon-xs"+ variant="ghost"+ >+ <XIcon />+ </Button>+ ) : null}+ </div>+ ))}+ </div>+ {naming ? (+ <div className="space-y-2">+ <Input+ aria-label="Template name"+ autoFocus+ maxLength={80}+ onChange={(event) => setTemplateName(event.target.value)}+ onKeyDown={(event) => {+ if (event.key === "Enter") submitTemplate();+ }}+ placeholder="Template name"+ value={templateName}+ />+ <div className="flex items-center gap-2">+ <Button+ className="flex-1"+ disabled={+ saveTemplate.isPending || templateName.trim() === ""+ }+ onClick={submitTemplate}+ size="sm"+ >+ {saveTemplate.isPending ? "Saving…" : "Save"}+ </Button>+ <Button+ onClick={() => setNaming(false)}+ size="sm"+ variant="ghost"+ >+ Cancel+ </Button>+ </div>+ </div>+ ) : (+ <Button+ disabled={!sendable || selectedPromotion !== undefined}+ onClick={() => {+ setTemplateName(selectedTemplate?.name ?? "");+ setNaming(true);+ }}+ size="sm"+ variant="outline"+ >+ Save as template+ </Button>+ )}+ {saveTemplate.isError ? (+ <p className="text-xs text-destructive">+ Couldn't save that template. Try a different name.+ </p>+ ) : null}+ {deleteTemplate.isError ? (+ <p className="text-xs text-destructive">+ Couldn't delete that template.+ </p>+ ) : null}+ </div>++ {selectedPromotion ? (+ <PromotionPreview+ promotion={selectedPromotion}+ issue={sendFailed ? sendIssue : undefined}+ />+ ) : (+ <div className="flex min-h-0 min-w-0 flex-col gap-2">+ <Label htmlFor="outreach-subject">Subject</Label>+ <Input+ id="outreach-subject"+ maxLength={200}+ onChange={(event) => setSubject(event.target.value)}+ value={subject}+ />+ <Label className="mt-1" htmlFor="outreach-body">+ Message+ </Label>+ <Textarea+ className="min-h-40 flex-1 resize-none field-sizing-fixed"+ id="outreach-body"+ maxLength={5000}+ onChange={(event) => setBody(event.target.value)}+ ref={bodyField}+ value={body}+ />+ <div className="flex flex-wrap items-center gap-1.5">+ <span className="text-xs text-muted-foreground">+ Blank line starts a paragraph. Insert:+ </span>+ {placeholders.map((name) => (+ <Button+ key={name}+ onClick={() => insertPlaceholder(name)}+ size="xs"+ variant="outline"+ >+ {`{{${name}}}`}+ </Button>+ ))}+ </div>+ <CreditOfferFields+ idPrefix="outreach-offer"+ value={creditOffer}+ defaults={offerDefaults}+ onChange={setCreditOffer}+ hint="The note carries a link for AI credits; {{claimUrl}} is the link and {{claimBy}} the last day to claim."+ />+ {sendFailed ? (+ <p className="text-sm text-destructive">+ {sendIssue ??+ "That didn’t go through. The account may have unsubscribed since the last scan, or the text may name a placeholder that doesn’t exist."}+ </p>+ ) : testNotice ? (+ <p className="text-sm text-muted-foreground">{testNotice}</p>+ ) : null}+ </div>+ )}+ </TabsContent>+ </Tabs>++ <DialogFooter>+ {/* The footer column is reversed below sm, so ordering the toggles+ last keeps them painted above the send buttons there. */}+ {selectedPromotion ? null : (+ <div className="flex flex-wrap items-center gap-x-4 gap-y-2 max-sm:order-last sm:mr-auto">+ <Button+ disabled={sending || !sendable}+ onClick={submitTest}+ variant="outline"+ >+ Send test to me+ </Button>+ <Label className="gap-2 font-normal text-muted-foreground">+ <Switch+ checked={unsubscribeLink}+ onCheckedChange={setUnsubscribeLink}+ />+ Add unsubscribe link+ </Label>+ <Label className="gap-2 font-normal text-muted-foreground">+ <Switch+ checked={trackReplies}+ onCheckedChange={setTrackReplies}+ />+ Track replies+ </Label>+ </div>+ )}+ <DialogClose render={<Button variant="outline" />}>+ Cancel+ </DialogClose>+ <Button disabled={sending || !sendable} onClick={submitSend}>+ {sending+ ? "Sending…"+ : selectedPromotion+ ? "Send promotion"+ : "Send"}+ </Button>+ </DialogFooter>+ </DialogContent>+ </Dialog>+ );+}++// A promotion as it will go: the subject, the words, the button and the+// offer, read from the saved row. Its words change on the Promotions tab.+function PromotionPreview({+ promotion,+ issue,+}: {+ promotion: PromotionSummary;+ issue: string | undefined;+}) {+ const offer = promotion.creditOffer;+ return (+ <div className="flex min-h-0 min-w-0 flex-col gap-2">+ <span className="text-xs font-medium text-muted-foreground">Subject</span>+ <p className="text-sm">+ {promotion.subject || (+ <span className="text-muted-foreground">No subject yet</span>+ )}+ </p>+ <span className="mt-1 text-xs font-medium text-muted-foreground">+ Message+ </span>+ <div className="min-h-0 flex-1 overflow-y-auto rounded-lg border px-3 py-2 text-sm whitespace-pre-wrap">+ {promotion.body || (+ <span className="text-muted-foreground">No message yet</span>+ )}+ </div>+ {promotion.ctaLabel ? (+ <p className="text-xs text-muted-foreground">+ Button: {promotion.ctaLabel} → {promotion.ctaUrl}+ </p>+ ) : null}+ {offer ? (+ <p className="text-xs text-muted-foreground">+ Credit offer: ${offer.dollars}, landed by{" "}+ {OFFER_CLAIM_NAMES[offer.claim].toLowerCase()},{" "}+ {offer.claimWindowDays} days to claim, lives {offer.expiresAfterDays}{" "}+ days.+ </p>+ ) : null}+ <p className="text-xs text-muted-foreground">+ Edit the words on the Promotions tab.+ </p>+ {issue ? <p className="text-sm text-destructive">{issue}</p> : null}+ </div>+ );+}diff --git a/site/src/app/su/outreach/page.tsx b/site/src/app/su/outreach/page.tsxindex 4beef222..1c71ed65 100644--- a/site/src/app/su/outreach/page.tsx+++ b/site/src/app/su/outreach/page.tsx@@ -1,30 +1,12 @@"use client";-import { XIcon } from "lucide-react";-import { useEffect, useRef, useState } from "react";+import { useEffect, useState } from "react";-import { CreditOfferFields, useCreditOfferDefaults } from "@/app/su/CreditOfferFields";-import {- useLastOutreachStart,- useOutreachDrafts,-} from "@/app/su/outreach/drafts";+import { ago, OutreachComposeDialog } from "@/app/su/outreach/ComposeDialog";import { Badge } from "@/components/ui/badge";import { Button } from "@/components/ui/button";-import {- Dialog,- DialogClose,- DialogContent,- DialogDescription,- DialogFooter,- DialogHeader,- DialogTitle,-} from "@/components/ui/dialog";import { Input } from "@/components/ui/input";-import { Label } from "@/components/ui/label";-import { Switch } from "@/components/ui/switch";-import { Textarea } from "@/components/ui/textarea";import { formatBytes } from "@/lib/bytes";-import { creditOfferTermsIfValid, OFFER_CLAIM_NAMES, type CreditOfferTerms } from "@/lib/credits/offerTerms";import { cn } from "@/lib/utils";import {OUTREACH_REASON_LABELS,@@ -33,9 +15,6 @@ import {type OutreachReason,type OutreachStatus,} from "@/lib/marketing/campaigns";-import { isOfferPlaceholder, OUTREACH_PLACEHOLDERS } from "@/lib/marketing/placeholders";-import { ApiError } from "@/queries/apiClient";-import { usePromotions, type PromotionSummary } from "@/queries/promotions";import {useBusyOutreachIds,useOutreach,@@ -44,11 +23,6 @@ import {useOutreachSearch,type OutreachRow,} from "@/queries/outreach";-import {- useDeleteOutreachTemplate,- useOutreachTemplates,- useSaveOutreachTemplate,-} from "@/queries/outreachTemplates";const FILTERS: { status: OutreachStatus; label: string }[] = [{ status: "todo", label: "To email" },@@ -57,38 +31,6 @@ const FILTERS: { status: OutreachStatus; label: string }[] = [{ status: "ignored", label: "Ignored" },];-const BLANK = "blank";--// A place a note can start from: a saved template, something this browser-// already sent, or a promotion. The first two are starting points; whatever-// ends up in the dialog — words, send toggles and the credit offer alike —-// is what goes out. A promotion goes out as it is saved, the same email its-// segment send mails, so it is read here and not edited.-type StartPoint = {- id: string;- title: string;- meta: string;- subject: string;- body: string;- unsubscribeLink: boolean;- trackReplies: boolean;- creditOffer: CreditOfferTerms | null;- at: number;- remove?: () => void;- busy?: boolean;- promotion?: PromotionSummary;-};--const promotionStartId = (id: string) => `promo:${id}`;--function ago(iso: string | null): string {- if (!iso) return "—";- const mins = Math.floor((Date.now() - new Date(iso).getTime()) / 60000);- if (mins < 60) return `${Math.max(mins, 0)}m ago`;- if (mins < 60 * 24) return `${Math.floor(mins / 60)}h ago`;- return `${Math.floor(mins / (60 * 24))}d ago`;-}-// The walls the scan found lead the row in the warning tone, so a person// who was declined or is leaving Pro stands out at a glance. Out of credits// is carried by the balance badge that follows, and the two use reasons by@@ -142,12 +84,6 @@ export default function SuOutreachPage() {// a tab pins it to that group until the next search begins.const [scoped, setScoped] = useState(false);const [sendTarget, setSendTarget] = useState<OutreachRow | null>(null);- const [source, setSource] = useState(BLANK);- const [subject, setSubject] = useState("");- const [body, setBody] = useState("");- const [templateName, setTemplateName] = useState("");- const [naming, setNaming] = useState(false);- const bodyField = useRef<HTMLTextAreaElement>(null);// The needle trails the field by a beat, so a search fires once per pause// in typing; clearing the field (handled in the input's onChange) brings// the plain list back at once.@@ -166,227 +102,10 @@ export default function SuOutreachPage() {const counts = countsData?.counts;const act = useOutreachAction();const busy = useBusyOutreachIds();- const templates = useOutreachTemplates();- const promotions = usePromotions();- const saveTemplate = useSaveOutreachTemplate();- const deleteTemplate = useDeleteOutreachTemplate();- const { drafts, forget, remember } = useOutreachDrafts();- const [lastStart, setLastStart] = useLastOutreachStart();- const [unsubscribeLink, setUnsubscribeLink] = useState(true);- const [trackReplies, setTrackReplies] = useState(true);- const [creditOffer, setCreditOffer] = useState<CreditOfferTerms | null>(null);- const [testNotice, setTestNotice] = useState<string | null>(null);- const offerDefaults = useCreditOfferDefaults();-- const saved = templates.data?.templates ?? [];-- const dropSource = (id: string) => {- if (source === id) setSource(BLANK);- };-- // Templates are shared and outlive the browser; recent sends are this- // browser's own. One list, newest first, each entry saying which it is.- const starts: StartPoint[] = [- ...saved.map((template) => ({- at: new Date(template.updatedAt).getTime(),- body: template.body,- busy: deleteTemplate.isPending,- id: `tpl:${template.id}`,- meta: "Template",- remove: () =>- deleteTemplate.mutate(template.id, {- onSuccess: () => dropSource(`tpl:${template.id}`),- }),- subject: template.subject,- title: template.name,- trackReplies: template.trackReplies,- unsubscribeLink: template.unsubscribeLink,- creditOffer: creditOfferTermsIfValid(template.promotion),- })),- ...drafts.map((draft) => ({- at: new Date(draft.savedAt).getTime(),- body: draft.body,- creditOffer: draft.creditOffer,- id: `draft:${draft.id}`,- meta: `Sent ${ago(draft.savedAt)}`,- remove: () => {- forget(draft.id);- dropSource(`draft:${draft.id}`);- },- subject: draft.subject,- title: draft.subject,- trackReplies: draft.trackReplies,- unsubscribeLink: draft.unsubscribeLink,- })),- ...(promotions.data?.promotions ?? []).map((promotion) => ({- at: new Date(promotion.updatedAt).getTime(),- body: promotion.body,- creditOffer: promotion.creditOffer,- id: promotionStartId(promotion.id),- meta: `Promotion · ${promotion.counts.sent} sent`,- promotion,- subject: promotion.subject,- title: promotion.name || "Untitled",- trackReplies: false,- unsubscribeLink: true,- })),- ].sort((a, b) => b.at - a.at);-- const sources: StartPoint[] = [- {- at: 0,- body: "",- id: BLANK,- meta: "",- subject: "",- title: "Blank",- trackReplies: true,- unsubscribeLink: true,- creditOffer: null,- },- ...starts,- ];-- // A promotion is checked whole by the server when it is sent, so the- // button is live and a blank field comes back as the reason.- const selectedPromotion = sources.find((s) => s.id === source)?.promotion;- const sendable = selectedPromotion !== undefined || (subject.trim() !== "" && body.trim() !== "");- const selectedTemplate = source.startsWith("tpl:")- ? saved.find((template) => `tpl:${template.id}` === source)- : undefined;-- const pickSource = (id: string) => {- const picked = sources.find((s) => s.id === id);- if (!picked) return;- setSource(id);- setSubject(picked.subject);- setBody(picked.body);- setUnsubscribeLink(picked.unsubscribeLink);- setTrackReplies(picked.trackReplies);- setCreditOffer(picked.creditOffer);- setNaming(false);- };-- const openSend = (row: OutreachRow) => {- // Open on whatever the last note went out from, so a run through the list- // is one click from ready; with nothing sent yet, the newest starting- // point, and with nothing saved at all, a blank note.- const start =- sources.find((option) => option.id === lastStart) ?? starts[0] ?? sources[0];- setSource(start.id);- setSubject(start.subject);- setBody(start.body);- setUnsubscribeLink(start.unsubscribeLink);- setTrackReplies(start.trackReplies);- setCreditOffer(start.creditOffer);- setNaming(false);- setSendTarget(row);- };// An address that is not on the list is put there, then opened for a note.const addByEmail = () =>- act.mutate({ action: "add", email: needle }, { onSuccess: (result) => openSend(result.row) });-- const submitTemplate = () => {- const name = templateName.trim();- if (name === "" || !sendable) return;- saveTemplate.mutate(- { body, name, subject, trackReplies, unsubscribeLink, promotion: creditOffer },- {- onSuccess: (result) => {- // The note now lives in a template, so the sent copy of the same- // words stops being a second entry saying the same thing.- const same = drafts.find(- (draft) => draft.subject === subject && draft.body === body,- );- if (same) {- forget(same.id);- if (lastStart === `draft:${same.id}`) {- setLastStart(`tpl:${result.template.id}`);- }- }- setSource(`tpl:${result.template.id}`);- setNaming(false);- },- },- );- };-- // A placeholder is easier to click than to spell, and it lands where the- // cursor already is.- const insertPlaceholder = (name: string) => {- const token = `{{${name}}}`;- const field = bodyField.current;- const start = field?.selectionStart ?? body.length;- const end = field?.selectionEnd ?? start;- setBody(`${body.slice(0, start)}${token}${body.slice(end)}`);- requestAnimationFrame(() => {- field?.focus();- field?.setSelectionRange(start + token.length, start + token.length);- });- };-- // The note as it stands goes to the operator's own inbox, filled with the- // row's values, so the words can be read as mail before they are sent.- const submitTest = () => {- const target = sendTarget;- if (!target || !sendable || selectedPromotion) return;- setTestNotice(null);- act.mutate(- { action: "test", body, creditOffer, outreachId: target.id, subject, unsubscribeLink },- { onSuccess: (result) => setTestNotice(`Test sent to ${result.sentTo}.`) },- );- };-- const submitSend = () => {- const target = sendTarget;- if (!target || !sendable) return;- setTestNotice(null);- if (selectedPromotion) {- act.mutate(- { action: "promote", outreachId: target.id, promotionId: selectedPromotion.id },- {- onSuccess: () => {- setLastStart(source);- setSendTarget((current) => (current?.id === target.id ? null : current));- },- },- );- return;- }- // Sending a start point exactly as it is stays that start point; a change- // to its words or its toggles becomes an entry of its own.- const from = sources.find((option) => option.id === source);- const edited =- from?.subject !== subject ||- from.body !== body ||- from.unsubscribeLink !== unsubscribeLink ||- from.trackReplies !== trackReplies ||- JSON.stringify(from.creditOffer) !== JSON.stringify(creditOffer);- act.mutate(- {- action: "send",- body,- creditOffer,- outreachId: target.id,- subject,- trackReplies,- unsubscribeLink,- },- {- onSuccess: () => {- setLastStart(- edited- ? `draft:${remember({ body, creditOffer, subject, trackReplies, unsubscribeLink })}`- : source,- );- // A send in flight leaves the dialog free for the next row, so only- // close it if that row is still the one on screen.- setSendTarget((current) => (current?.id === target.id ? null : current));- },- },- );- };+ act.mutate({ action: "add", email: needle }, { onSuccess: (result) => setSendTarget(result.row) });const searching = needle !== "";const allTabs = searching && !scoped;@@ -403,17 +122,8 @@ export default function SuOutreachPage() {: FILTERS.reduce((sum, filter) => sum + search.data.counts[filter.status], 0): 0;const truncated = searching && matchTotal > rows.length;- const sending = sendTarget !== null && busy.has(sendTarget.id);- // A failed send keeps its dialog open, so the reason belongs in there with- // the words that still need fixing; the server names a bad placeholder.- const sendFailed =- act.isError &&- (act.variables?.action === "send" || act.variables?.action === "test" || act.variables?.action === "promote");- const sendIssue = sendFailed && act.error instanceof ApiError ? act.error.issues[0]?.message : undefined;const addFailed = act.isError && act.variables?.action === "add";const adding = act.isPending && act.variables?.action === "add";- // The offer's placeholders are offered only while the note carries one.- const placeholders = OUTREACH_PLACEHOLDERS.filter((name) => creditOffer !== null || !isOfferPlaceholder(name));return (<div className="space-y-4 pb-9">@@ -543,7 +253,7 @@ export default function SuOutreachPage() {)}<Buttondisabled={busy.has(row.id)}- onClick={() => openSend(row)}+ onClick={() => setSendTarget(row)}size="sm">{row.sentCount > 0 ? "Email again" : "Email"}@@ -579,241 +289,13 @@ export default function SuOutreachPage() {</p>) : null}- {act.isError && !sendFailed && !addFailed ? (+ {act.isError && !addFailed ? (<p className="text-sm text-destructive">That didn't go through. Run a scan and try again.</p>) : null}- <Dialog- onOpenChange={(open) => {- if (!open) setTestNotice(null);- setSendTarget(open ? sendTarget : null);- }}- open={sendTarget !== null}- >- <DialogContent className="grid max-h-[85vh] grid-rows-[auto_minmax(0,1fr)_auto] sm:max-w-5xl">- <DialogHeader>- <DialogTitle>Email {sendTarget?.name}</DialogTitle>- <DialogDescription>- Goes to {sendTarget?.email}.{" "}- {selectedPromotion- ? "The promotion goes as its segment send would send it, counts in the promotion, and a later segment send skips this person."- : trackReplies- ? "Replies come back to your inbox and mark this row replied."- : "Replies come straight back to your own address; the Mark replied button files the row."}- </DialogDescription>- </DialogHeader>-- <div className="grid min-h-0 min-w-0 gap-4 md:grid-cols-[18rem_minmax(0,1fr)]">- <div className="flex min-h-0 flex-col gap-2 md:border-r md:pr-4">- <span className="text-xs font-medium text-muted-foreground">- Start from- </span>- <div className="min-h-0 flex-1 space-y-0.5 overflow-y-auto max-md:max-h-40">- {sources.map((option) => (- <div className="group/start relative" key={option.id}>- <button- className={cn(- "w-full rounded-lg px-2 py-1.5 pr-8 text-left transition-colors",- source === option.id- ? "bg-accent text-accent-foreground"- : "hover:bg-accent/50",- )}- onClick={() => pickSource(option.id)}- type="button"- >- <span className="block truncate text-sm">{option.title}</span>- {option.meta ? (- <span className="block truncate text-xs text-muted-foreground">- {option.meta}- </span>- ) : null}- </button>- {option.remove ? (- <Button- aria-label={`Delete ${option.title}`}- className="absolute top-1.5 right-1 opacity-0 group-hover/start:opacity-100 focus-visible:opacity-100"- disabled={option.busy}- onClick={option.remove}- size="icon-xs"- variant="ghost"- >- <XIcon />- </Button>- ) : null}- </div>- ))}- </div>- {naming ? (- <div className="space-y-2">- <Input- aria-label="Template name"- autoFocus- maxLength={80}- onChange={(event) => setTemplateName(event.target.value)}- onKeyDown={(event) => {- if (event.key === "Enter") submitTemplate();- }}- placeholder="Template name"- value={templateName}- />- <div className="flex items-center gap-2">- <Button- className="flex-1"- disabled={saveTemplate.isPending || templateName.trim() === ""}- onClick={submitTemplate}- size="sm"- >- {saveTemplate.isPending ? "Saving…" : "Save"}- </Button>- <Button- onClick={() => setNaming(false)}- size="sm"- variant="ghost"- >- Cancel- </Button>- </div>- </div>- ) : (- <Button- disabled={!sendable || selectedPromotion !== undefined}- onClick={() => {- setTemplateName(selectedTemplate?.name ?? "");- setNaming(true);- }}- size="sm"- variant="outline"- >- Save as template- </Button>- )}- {saveTemplate.isError ? (- <p className="text-xs text-destructive">- Couldn't save that template. Try a different name.- </p>- ) : null}- {deleteTemplate.isError ? (- <p className="text-xs text-destructive">- Couldn't delete that template.- </p>- ) : null}- </div>-- {selectedPromotion ? (- <PromotionPreview promotion={selectedPromotion} issue={sendFailed ? sendIssue : undefined} />- ) : (- <div className="flex min-h-0 min-w-0 flex-col gap-2">- <Label htmlFor="outreach-subject">Subject</Label>- <Input- id="outreach-subject"- maxLength={200}- onChange={(event) => setSubject(event.target.value)}- value={subject}- />- <Label className="mt-1" htmlFor="outreach-body">- Message- </Label>- <Textarea- className="min-h-40 flex-1 resize-none field-sizing-fixed"- id="outreach-body"- maxLength={5000}- onChange={(event) => setBody(event.target.value)}- ref={bodyField}- value={body}- />- <div className="flex flex-wrap items-center gap-1.5">- <span className="text-xs text-muted-foreground">- Blank line starts a paragraph. Insert:- </span>- {placeholders.map((name) => (- <Button- key={name}- onClick={() => insertPlaceholder(name)}- size="xs"- variant="outline"- >- {`{{${name}}}`}- </Button>- ))}- </div>- <CreditOfferFields- idPrefix="outreach-offer"- value={creditOffer}- defaults={offerDefaults}- onChange={setCreditOffer}- hint="The note carries a link for AI credits; {{claimUrl}} is the link and {{claimBy}} the last day to claim."- />- {sendFailed ? (- <p className="text-sm text-destructive">- {sendIssue ??- "That didn’t go through. The account may have unsubscribed since the last scan, or the text may name a placeholder that doesn’t exist."}- </p>- ) : testNotice ? (- <p className="text-sm text-muted-foreground">{testNotice}</p>- ) : null}- </div>- )}- </div>-- <DialogFooter>- {/* The footer column is reversed below sm, so ordering the toggles- last keeps them painted above the send buttons there. */}- {selectedPromotion ? null : (- <div className="flex flex-wrap items-center gap-x-4 gap-y-2 max-sm:order-last sm:mr-auto">- <Button disabled={sending || !sendable} onClick={submitTest} variant="outline">- Send test to me- </Button>- <Label className="gap-2 font-normal text-muted-foreground">- <Switch- checked={unsubscribeLink}- onCheckedChange={setUnsubscribeLink}- />- Add unsubscribe link- </Label>- <Label className="gap-2 font-normal text-muted-foreground">- <Switch checked={trackReplies} onCheckedChange={setTrackReplies} />- Track replies- </Label>- </div>- )}- <DialogClose render={<Button variant="outline" />}>Cancel</DialogClose>- <Button disabled={sending || !sendable} onClick={submitSend}>- {sending ? "Sending…" : selectedPromotion ? "Send promotion" : "Send"}- </Button>- </DialogFooter>- </DialogContent>- </Dialog>- </div>- );-}--// A promotion as it will go: the subject, the words, the button and the-// offer, read from the saved row. Its words change on the Promotions tab.-function PromotionPreview({ promotion, issue }: { promotion: PromotionSummary; issue: string | undefined }) {- const offer = promotion.creditOffer;- return (- <div className="flex min-h-0 min-w-0 flex-col gap-2">- <span className="text-xs font-medium text-muted-foreground">Subject</span>- <p className="text-sm">{promotion.subject || <span className="text-muted-foreground">No subject yet</span>}</p>- <span className="mt-1 text-xs font-medium text-muted-foreground">Message</span>- <div className="min-h-0 flex-1 overflow-y-auto rounded-lg border px-3 py-2 text-sm whitespace-pre-wrap">- {promotion.body || <span className="text-muted-foreground">No message yet</span>}- </div>- {promotion.ctaLabel ? (- <p className="text-xs text-muted-foreground">- Button: {promotion.ctaLabel} → {promotion.ctaUrl}- </p>- ) : null}- {offer ? (- <p className="text-xs text-muted-foreground">- Credit offer: ${offer.dollars}, landed by {OFFER_CLAIM_NAMES[offer.claim].toLowerCase()}, {offer.claimWindowDays} days to claim,- lives {offer.expiresAfterDays} days.- </p>- ) : null}- <p className="text-xs text-muted-foreground">Edit the words on the Promotions tab.</p>- {issue ? <p className="text-sm text-destructive">{issue}</p> : null}+ <OutreachComposeDialog onClose={() => setSendTarget(null)} target={sendTarget} /></div>);}