Announcement composer
Write, target, preview and send a message, then see it in the history.
The composer is one controlled value: who sends it, on which channel, to whom, with what subject and body. The screen owns that value, computes the audience size and reacts to Send.
The whole recipe
Compose, count the audience, send, and see the message appear in the history. The composer is controlled (you own the ComposerValue), reports a live recipient count you compute, and senderModes-style rules decide which audiences a sender may use. Sending here only adds a row; in an app onSend calls useAction and the history refetches. A history row opens the sent message in MessageDrawer, whose "Send another to the same people" starts a new message.
Code
import { useState } from 'react';
import { AnnouncementComposer, Button, Card, DataTable, MessageDrawer, PageGrid, ToastRegion, messageColumns, type ComposerValue, type MessageRow, type ToastMessage } from '@ergasti/bricks';
import { brands, joinRequests, messages } from './pr';
const EMPTY: ComposerValue = { sender: null, channel: 'app', audience: { kind: 'all', picked: [] }, subject: '', body: '' };
function Screen() {
const [value, setValue] = useState<ComposerValue>({ ...EMPTY, subject: 'New: TRNDR tiers are coming' });
const [sent, setSent] = useState<MessageRow[]>(messages);
const [toast, setToast] = useState<ToastMessage | null>(null);
const [sending, setSending] = useState(false);
const [opened, setOpened] = useState<MessageRow | null>(null);
const recipients = value.audience.kind === 'all' ? 1240 : value.audience.kind === 'brand' ? 86 : value.audience.kind === 'tier' ? 127 : value.audience.picked.length;
const brand = brands[(value.sender as keyof typeof brands) ?? 'glow'];
const send = (draft: ComposerValue) => {
setSending(true);
window.setTimeout(() => {
const row: MessageRow = { id: Date.now(), subject: draft.subject || '(no subject)', body: draft.body, sender: draft.sender === null ? null : brand, audience: { kind: 'all' }, creators: recipients ?? 0, openPercent: 0, sent: 'Just now', by: 'You' };
setSent((all) => [row, ...all]);
setValue(EMPTY);
setSending(false);
setToast({ message: `Sent to ${recipients?.toLocaleString('en-US')} creators.` });
}, 700);
};
return (
<PageGrid>
<Card>
<AnnouncementComposer
value={value}
onChange={setValue}
senders={[brands.glow, brands.mira, brands.kemet]}
channels={[{ channel: 'app' }, { channel: 'sms' }, { channel: 'whatsapp', unavailable: 'WhatsApp is not set up yet.' }]}
brands={[{ ...brands.glow, creators: 86 }, { ...brands.mira, creators: 23 }, { ...brands.kemet, creators: 57 }]}
tiers={[{ tier: 'rising', creators: 812 }, { tier: 'established', creators: 301 }, { tier: 'elite', creators: 127 }]}
creators={joinRequests.map((j) => ({ id: j.id, name: j.creator.name, seed: j.creator.seed, tier: j.creator.tier }))}
recipients={recipients}
sending={sending}
onSend={send}
onCancel={() => setValue(EMPTY)}
/>
</Card>
<DataTable label="Message history" columns={messageColumns('en')} rows={sent} rowKey={(m) => m.id} onRowClick={setOpened} footer={<Button size="sm" onClick={() => setSent(messages)}>Reset history</Button>} />
<MessageDrawer open={opened !== null} onClose={() => setOpened(null)} message={opened} onSendAnother={() => { setValue({ ...EMPTY, subject: opened?.subject ?? '' }); setOpened(null); }} />
<ToastRegion toast={toast} onDismiss={() => setToast(null)} />
</PageGrid>
);
}How it fits together
- Own a
ComposerValue(sender,channel,audience,subject,body) and passvalueandonChange. Reset it to an empty value after a send. - Offer what the sender may use.
senders,channels(a channel withunavailableis disabled and shows why), and the audience data:brands,tiers,groups,creators. TRNDR (sendernull) may reach any audience; a brand sender only gets the modessenderModes(sender, offered)allows (not "everyone" or "group"), so a brand reaches only its own creators. - Count recipients yourself. The composer takes
recipients(a number, ornullwhile you work it out) and writes it on the send button ("Send to 1,240 creators");countingshows it is being counted. Ask your API when the audience changes. The send button does nothing while the subject or body is empty or the count is 0. - Channel rules are built in.
MessageFieldsshows counters: for SMS the count is in segments with the encoding (smsCount: GSM-7 fits 160 characters in one message and 153 per part when it splits; any other character, Arabic included, switches the whole message to UCS-2: 70, then 67 per part).MessagePreviewshows what the creator will see. - Send with
onSend(value); showsendingwhile the request runs anderrorif it fails. Confirm with a toast and refresh the history table (messageColumns). - Open a sent message. A history row opens MessageDrawer with that row: the message, its Creators and Opened figures, who sent it to whom, and "Send another to the same people" (
onSendAnother), which starts a new message. - As a modal. The PR portal writes new messages in a 480px Modal: pass
layout="modal", which draws one column and leaves the live preview out (previewbrings it back).channelsis optional; with 0 or 1 channels there is no channel picker.
Related: AnnouncementComposer, AudiencePicker, MessageFields, smsCount, messageColumns.