An inbox
Conversations, the open thread beside them with a reply box, the customer's panel
Copy the file below whole (src/screens/inbox.tsx, say) beside frame.tsx (docs/screen-frame.md) and swap the sample conversations for yours; keep the parts and their props. The queues stand in the sidebar under the pages (ScreenFrame's more), each with its count; the list in the frame's first container with a search and a status filter over its rows, a bordered Select chip ("Status: All 3") whose options count what each would show, the count at the option's end and in its name (the list pane is about 250 px: a SegmentedControl there cuts its words); a row opens its thread in the second container (ListDetail opens="panel"; on a phone, the theme's Sheet over the list); the reply box under the thread, with Reply and Internal note; the customer's panel beside the thread (aside), under it where the room is narrow, with the conversation's status, priority and assignee as bordered chips (Properties: SelectTrigger shape="chip", PersonSelect shape="chip") that change it in place, the queues' counts following. Light, dark, phone and desktop are the parts' own: add no class for them. Its empty list is the filtered state; rows loading, a failed load, a reply sending: docs/screen-states.md.
Show the code397 lines
// src/screens/inbox.tsx: the inbox, a thread open beside its list
import { useState } from 'react';
import { AppFramePanel } from '@/components/goop/patterns/app-frame';
import {
ListDetail,
ListDetailItem,
ListDetailList,
ListDetailPanel,
ListDetailTrigger,
} from '@/components/goop/patterns/list-detail';
import { Avatar } from '@/components/ui/goop/avatar';
import { ChatMessage } from '@/components/ui/goop/chat-message';
import { CommandShortcut } from '@/components/ui/goop/command';
import {
Composer,
ComposerActions,
ComposerEditor,
type ComposerMode,
ComposerSubmit,
ComposerToolbar,
} from '@/components/ui/goop/composer';
import { Empty, EmptyDescription, EmptyHeader, EmptyTitle } from '@/components/ui/goop/empty';
import { Identity } from '@/components/ui/goop/identity';
import { Item, ItemContent, ItemDescription, ItemEnd, ItemOverlay, ItemTitle } from '@/components/ui/goop/item';
import { MessageList } from '@/components/ui/goop/message-list';
import { PersonSelect } from '@/components/ui/goop/person-select';
import { type PriorityLevel, PriorityMark } from '@/components/ui/goop/priority-mark';
import { Properties, PropertyRow } from '@/components/ui/goop/properties';
import { SearchInput } from '@/components/ui/goop/search-input';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/goop/select';
import {
SidebarGroup,
SidebarGroupContent,
SidebarGroupLabel,
SidebarMenu,
SidebarMenuItem,
} from '@/components/ui/goop/sidebar-group';
import { SidebarMenuBadge, SidebarMenuButton } from '@/components/ui/goop/sidebar-menu-button';
import { ScreenFrame, ScreenTopBar } from './frame';
// ---- the sample data: swap it for yours
type Status = 'open' | 'waiting' | 'closed';
const STATUS_WORDS: Record<Status, string> = { open: 'Open', waiting: 'Waiting', closed: 'Closed' };
const STATUSES = Object.keys(STATUS_WORDS) as Status[];
const PRIORITY_WORDS: Record<PriorityLevel, string> = {
none: 'No priority',
urgent: 'Urgent',
high: 'High',
medium: 'Medium',
low: 'Low',
};
const PRIORITIES = Object.keys(PRIORITY_WORDS) as PriorityLevel[];
const TEAM = [
{ id: 'cormac', name: 'Cormac Foley' },
{ id: 'ngozi', name: 'Ngozi Ilori' },
{ id: 'teodora', name: 'Teodora Valk' },
];
interface Message {
id: string;
from: 'customer' | 'you';
text: string;
time: string;
note?: boolean;
}
interface Conversation {
id: string;
subject: string;
customer: string;
email: string;
status: Status;
priority: PriorityLevel;
/** A TEAM id, or null for nobody. */
assignee: string | null;
when: string;
unread: boolean;
messages: Message[];
}
const ME = 'cormac';
const CONVERSATIONS: Conversation[] = [
{
id: 'c-2041',
subject: 'Wrong date on our invoice',
customer: 'Rhys Pugh',
email: 'rhys.pugh@example.org',
status: 'open',
priority: 'high',
assignee: ME,
when: '09:31',
unread: false,
messages: [
{ id: 'm1', from: 'customer', text: 'Our invoice shows the wrong date. Can you resend it?', time: '09:12' },
{ id: 'm2', from: 'you', text: 'Sorry about that. It shows the day we sent it.', time: '09:20' },
{ id: 'm3', from: 'you', text: 'Siddharth can reissue it with the paid date.', time: '09:24', note: true },
],
},
{
id: 'c-2040',
subject: 'Change the billing address',
customer: 'Lupe Ortega',
email: 'lupe@example.org',
status: 'waiting',
priority: 'none',
assignee: null,
when: '08:47',
unread: true,
messages: [{ id: 'm1', from: 'customer', text: 'We moved last week. Is it too late to change it?', time: '08:47' }],
},
{
id: 'c-2038',
subject: 'Export last year’s visits',
customer: 'Amara Diallo',
email: 'amara@example.org',
status: 'closed',
priority: 'low',
assignee: 'ngozi',
when: 'Mon',
unread: false,
messages: [
{ id: 'm1', from: 'customer', text: 'Can we have last year’s visits as a spreadsheet?', time: 'Mon 10:02' },
{ id: 'm2', from: 'you', text: 'Here it is, one row a visit. Anything else?', time: 'Mon 11:15' },
],
},
];
const QUEUES = [
{ id: 'all', words: 'All', icon: 'inbox' },
{ id: 'mine', words: 'Assigned to me', icon: 'user' },
{ id: 'unassigned', words: 'Unassigned', icon: 'circle-dashed' },
] as const;
type Queue = (typeof QUEUES)[number]['id'];
const inQueue = (queue: Queue, one: Conversation) => queue === 'all' || one.assignee === (queue === 'mine' ? ME : null);
const MODES: ComposerMode[] = [
{ value: 'reply', label: 'Reply', placeholder: 'Write to the customer…' },
{
value: 'note',
label: 'Internal note',
variant: 'note',
placeholder: 'Only your team sees this…',
submitLabel: 'Add note',
},
];
const ANY = 'any';
export default function InboxScreen() {
const [conversations, setConversations] = useState(CONVERSATIONS);
const [queue, setQueue] = useState<Queue>('all');
const [query, setQuery] = useState('');
const [status, setStatus] = useState<string>(ANY);
const [openId, setOpenId] = useState<string | null>(null);
const words = query.trim().toLowerCase();
// what the queue and the search leave: each status option counts its own share of it
const found = conversations.filter(
(one) => inQueue(queue, one) && `${one.customer} ${one.subject}`.toLowerCase().includes(words),
);
const shown = found.filter((one) => status === ANY || one.status === status);
const counted = (n: number) => `${n} ${n === 1 ? 'conversation' : 'conversations'}`;
const countOf = (value: string) => (value === ANY ? found : found.filter((one) => one.status === value)).length;
const filterWords = (value: string) => (value === ANY ? 'All' : STATUS_WORDS[value as Status]);
const open = conversations.find((one) => one.id === openId);
const change = (id: string, next: Partial<Conversation>) =>
setConversations((all) => all.map((one) => (one.id === id ? { ...one, ...next } : one)));
// opening a conversation reads it; a reply waits on the customer, a note leaves the status as it was
const choose = (id: string | null) => {
setOpenId(id);
if (id) change(id, { unread: false });
};
const send = (one: Conversation, text: string, note: boolean) =>
change(one.id, {
messages: [...one.messages, { id: `m${one.messages.length + 1}`, from: 'you', text, time: 'Now', note }],
status: note ? one.status : 'waiting',
when: 'Now',
});
return (
<ScreenFrame
current="/inbox"
scroll="panel"
more={
<SidebarGroup>
<SidebarGroupLabel>Queues</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu>
{QUEUES.map((each) => (
<SidebarMenuItem key={each.id}>
<SidebarMenuButton
size="sm"
icon={each.icon}
isActive={each.id === queue}
onClick={() => setQueue(each.id)}
>
{each.words}
<SidebarMenuBadge>{conversations.filter((one) => inQueue(each.id, one)).length}</SidebarMenuBadge>
</SidebarMenuButton>
</SidebarMenuItem>
))}
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
}
>
<ListDetail opens="panel" idle="hint" value={openId} onValueChange={choose}>
<AppFramePanel size="list" header={<ScreenTopBar title="Inbox" />}>
<ListDetailList
filtersEdge="item"
unreadRows
filters={
<>
<SearchInput
label="Search conversations"
placeholder="Search"
value={query}
onChange={(event) => setQuery(event.currentTarget.value)}
/>
{/* the list pane is narrow: a chip keeps the words and the counts whole */}
<Select value={status} onValueChange={setStatus}>
<SelectTrigger
shape="chip"
aria-label={`Status: ${filterWords(status)}, ${counted(countOf(status))}`}
>
<SelectValue>{`Status: ${filterWords(status)} ${countOf(status)}`}</SelectValue>
</SelectTrigger>
<SelectContent aria-label="Status" search={false}>
{[ANY, ...STATUSES].map((value) => (
<SelectItem
key={value}
value={value}
aria-label={`${filterWords(value)}, ${counted(countOf(value))}`}
>
{filterWords(value)}
<CommandShortcut>{countOf(value)}</CommandShortcut>
</SelectItem>
))}
</SelectContent>
</Select>
</>
}
>
{shown.length === 0 ? (
<Empty>
<EmptyHeader>
<EmptyTitle>No conversation matches.</EmptyTitle>
<EmptyDescription>Try other words, another status or another queue.</EmptyDescription>
</EmptyHeader>
</Empty>
) : (
<ul aria-label="Conversations">
{shown.map((one) => (
<ListDetailItem key={one.id} value={one.id} asChild>
<Item variant="ghost" unread={one.unread} asChild>
<li>
<ItemOverlay asChild>
<ListDetailTrigger aria-labelledby={`${one.id}-name`} />
</ItemOverlay>
<ItemContent>
<ItemTitle id={`${one.id}-name`}>{one.customer}</ItemTitle>
<ItemDescription>{one.subject}</ItemDescription>
</ItemContent>
<ItemEnd>{one.when}</ItemEnd>
</li>
</Item>
</ListDetailItem>
))}
</ul>
)}
</ListDetailList>
</AppFramePanel>
<ListDetailPanel
title={open?.subject ?? ''}
closeLabel="Close conversation"
placeholder={
<Empty>
<EmptyHeader>
<EmptyTitle>Choose a conversation</EmptyTitle>
<EmptyDescription>Its messages open here, with the customer beside them.</EmptyDescription>
</EmptyHeader>
</Empty>
}
asideLabel="Customer"
aside={
open ? (
<div className="flex flex-col gap-5">
<Identity name={open.customer} description={open.email} size="lg" />
<Properties aria-label="Conversation">
<PropertyRow label="Status" icon="circle-dashed">
{(control) => (
<Select value={open.status} onValueChange={(next) => change(open.id, { status: next as Status })}>
<SelectTrigger {...control} shape="chip">
<SelectValue />
</SelectTrigger>
<SelectContent aria-label="Status" search={false}>
{STATUSES.map((value) => (
<SelectItem key={value} value={value}>
{STATUS_WORDS[value]}
</SelectItem>
))}
</SelectContent>
</Select>
)}
</PropertyRow>
<PropertyRow label="Priority" icon="antenna-bars-3">
{(control) => (
<Select
value={open.priority}
onValueChange={(next) => change(open.id, { priority: next as PriorityLevel })}
>
<SelectTrigger {...control} shape="chip">
<SelectValue />
</SelectTrigger>
<SelectContent aria-label="Priority" search={false}>
{PRIORITIES.map((value) => (
<SelectItem
key={value}
value={value}
mark={<PriorityMark priority={value} size="sm" aria-hidden="true" />}
>
{PRIORITY_WORDS[value]}
</SelectItem>
))}
</SelectContent>
</Select>
)}
</PropertyRow>
<PropertyRow label="Assignee" icon="user">
<PersonSelect
shape="chip"
people={TEAM}
optional
value={open.assignee ?? ''}
onValueChange={(next) => change(open.id, { assignee: next || null })}
/>
</PropertyRow>
</Properties>
</div>
) : null
}
>
{open ? <Thread key={open.id} conversation={open} onSend={(text, note) => send(open, text, note)} /> : null}
</ListDetailPanel>
</ListDetail>
</ScreenFrame>
);
}
/** The open conversation: its messages (a note in its warm box), then the reply box with its two modes. */
function Thread({
conversation,
onSend,
}: {
conversation: Conversation;
onSend: (text: string, note: boolean) => void;
}) {
const [mode, setMode] = useState('reply');
const [draft, setDraft] = useState('');
const { customer } = conversation;
return (
<>
<MessageList aria-label={`Conversation with ${customer}`} inset={false} className="grow">
{conversation.messages.map((message) => (
<ChatMessage
key={message.id}
from={message.from === 'you' ? 'self' : 'other'}
author={message.from === 'you' ? 'You' : customer}
avatar={
message.from === 'you' ? undefined : (
<Avatar variant="solid" name={customer} aria-hidden="true">
{customer.split(' ').map((word) => word[0])}
</Avatar>
)
}
time={message.time}
variant={message.note ? 'note' : 'default'}
>
{message.text}
</ChatMessage>
))}
</MessageList>
<Composer
variant="reply"
aria-label={`Reply to ${customer}`}
modes={MODES}
mode={mode}
onModeChange={setMode}
onSubmit={(event) => {
event.preventDefault();
if (draft.trim()) onSend(draft.trim(), mode === 'note');
setDraft('');
}}
className="shrink-0"
>
<ComposerEditor aria-label="Message" value={draft} onChange={(event) => setDraft(event.target.value)} />
<ComposerToolbar>
<ComposerActions>
<ComposerSubmit aria-label="Send" disabled={!draft.trim()} />
</ComposerActions>
</ComposerToolbar>
</Composer>
</>
);
}