Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | 2x 2x 2x 94x 94x 94x 1x 3x 376x 1x 1x 3x 3x 10x 133x 4x 133x 133x 133x 133x 133x 10x 22x 846x | import { CircleSlash, Inbox, SearchX, TriangleAlert } from "lucide-react";
import { cn } from "../../lib/utils";
import { Pagination } from "../ui/pagination";
import { Separator } from "../ui/separator";
import { Skeleton } from "../ui/skeleton";
import { STATUS_CODE_TYPE, STATUS_INK } from "../ui/status-ink";
import { ContactTypeTag } from "./ContactTypeTag";
import type { WaInboxRow } from "../../lib/api/whatsapp";
import {
elapsedMinutes,
elapsedSince,
formatAgeLong,
formatAgeShort,
type SlaLevel,
slaLevel,
} from "../../lib/whatsapp/triage";
/**
* The triage list: a ruled, dense, tabular list of who is waiting, oldest
* first, with the age leading every row.
*
* WHY THE AGE CHIP LEADS
* The operator scans that one column top-to-bottom hunting the number that is
* too big. Put it after the name and the scan becomes a read. The chip is
* fixed-width, tabular and right-aligned so the column has an edge; its
* spoken form is rendered `sr-only` so a screen reader hears "waiting 4 hours
* 12 minutes" rather than "4 h 12 m".
*
* THE AGE IS NOT "LAST ACTIVITY"
* The chip measures `now − lastCustomerMessageAt` — how long the CONTACT has
* been waiting. The list used to print `formatRelativeTime(lastMessageAt)`,
* which is a different number (when anything last happened, including our own
* reply), and rendering both would get filed as a bug. One number, and it is
* the one the SLA strip is about.
*
* WHAT IS NOT HERE, AND WHY
* 1. The approved mockup shows a third line per row quoting the contact's last
* message. `GET /conversations` returns conversation rows only — there is no
* message preview on the wire, and adding one is a read-model change this
* screen does not need to be useful. The affordance is dropped rather than
* faked; the line is spent on the pro's owner and city instead, which the
* row already carries.
* 2. The mockup shows TWO sections in one list — a truncated "Waiting on us"
* above an "Answered" — and this is ONE list with a mode. Two sections
* means two concurrent queries and therefore two totals that can disagree
* mid-poll, on the one screen whose whole job is to make the backlog
* count trustworthy; and "answered" has no complementary server filter
* (`answeredByUs` is "ever answered", not "not waiting"), so the second
* section's membership would have to be guessed in the browser. The mode
* switch carries the same information with one query and one `total`.
*/
const CAPTION = "text-foreground-muted";
/** The chip's ink, by how breached the wait is. Never index red. */
const AGE_INK: Record<SlaLevel, string> = {
ok: "text-foreground-muted",
warn: STATUS_INK.warning,
breach: STATUS_INK.error,
};
const CONTACT_TYPE_CHIPS = [
{ value: "", label: "Everyone" },
{ value: "pro", label: "Pros" },
{ value: "customer", label: "Homeowners" },
{ value: "unknown", label: "Unmatched" },
] as const;
export type ConversationListProps = {
conversations: WaInboxRow[];
selectedId: number | null;
onSelect: (row: WaInboxRow) => void;
/** On: the waiting backlog, server-ordered oldest first. Off: everything, newest first. */
waitingOnly: boolean;
onWaitingOnlyChange: (value: boolean) => void;
contactTypeFilter: string;
onContactTypeFilterChange: (value: string) => void;
hasUnread: boolean;
onHasUnreadChange: (value: boolean) => void;
hasCustomerReply: boolean;
onHasCustomerReplyChange: (value: boolean) => void;
answeredByUs: boolean;
onAnsweredByUsChange: (value: boolean) => void;
total: number;
offset: number;
pageSize: number;
onOffsetChange: (value: number) => void;
isLoading: boolean;
isError: boolean;
/** True when any filter beyond the mode is on — it changes the empty state's meaning. */
hasActiveFilters: boolean;
now: Date;
};
export function ConversationList({
conversations,
selectedId,
onSelect,
waitingOnly,
onWaitingOnlyChange,
contactTypeFilter,
onContactTypeFilterChange,
hasUnread,
onHasUnreadChange,
hasCustomerReply,
onHasCustomerReplyChange,
answeredByUs,
onAnsweredByUsChange,
total,
offset,
pageSize,
onOffsetChange,
isLoading,
isError,
hasActiveFilters,
now,
}: ConversationListProps) {
const page = Math.floor(offset / pageSize) + 1;
const totalPages = Math.max(1, Math.ceil(total / pageSize));
return (
<div className="flex h-full flex-col border-r border-border-default">
<div className="flex flex-wrap items-center gap-1.5 px-3 py-2">
<FilterChip
label="Waiting on us"
title="Nothing outbound since their last inbound, oldest first"
active={waitingOnly}
onToggle={() => onWaitingOnlyChange(!waitingOnly)}
/>
<FilterChip
label="All"
title="Every conversation, most recently active first"
active={!waitingOnly}
onToggle={() => onWaitingOnlyChange(false)}
/>
<Separator orientation="vertical" className="mx-0.5 h-5" />
{CONTACT_TYPE_CHIPS.map((chip) => (
<FilterChip
key={chip.value || "all"}
label={chip.label}
title={`Only ${chip.label.toLowerCase()}`}
active={contactTypeFilter === chip.value}
onToggle={() => onContactTypeFilterChange(chip.value)}
/>
))}
<Separator orientation="vertical" className="mx-0.5 h-5" />
{/*
* "Unread" is "nobody has OPENED it" — the count is zeroed the
* moment a thread is selected, which is exactly why it cannot
* stand in for "waiting on us". Both are offered, with their own
* words, rather than one pretending to be the other.
*/}
<FilterChip
label="Unread"
title="Nobody has opened it yet. Not the same as unanswered."
active={hasUnread}
onToggle={() => onHasUnreadChange(!hasUnread)}
/>
<FilterChip
label="Replied"
title="Only conversations where the contact has written to us"
active={hasCustomerReply}
onToggle={() => onHasCustomerReplyChange(!hasCustomerReply)}
/>
<FilterChip
label="Answered"
title="Only conversations a person on our side has replied to. Automated sends do not count."
active={answeredByUs}
onToggle={() => onAnsweredByUsChange(!answeredByUs)}
/>
</div>
<div className="flex items-center gap-2 border-y border-border-default bg-background-subtle px-3.5 py-1.5">
<span className={cn(STATUS_CODE_TYPE, CAPTION, "grow")}>
{waitingOnly ? "Waiting on us · oldest first" : "All · newest first"}
</span>
<span className={cn(STATUS_CODE_TYPE, CAPTION)}>{total}</span>
</div>
<div className="min-h-0 flex-1 overflow-y-auto">
{isError ? (
<ListMessage
icon={TriangleAlert}
tone={STATUS_INK.error}
title="Could not load the inbox"
body="The clock is still running on everyone waiting — this panel just cannot see them. Retry, or reload the page."
/>
) : isLoading ? (
<div className="space-y-3 p-4">
{[1, 2, 3, 4, 5].map((i) => (
<Skeleton key={i} className="h-14 w-full" />
))}
</div>
) : conversations.length === 0 ? (
// "Nothing to do" and "nothing matches what you asked for" are
// different facts. One message for both reads as a bug the
// moment a filter is on.
hasActiveFilters ? (
<ListMessage
icon={SearchX}
title="Nothing matches these filters"
body="Clear a filter to widen the list. The figures above still describe the whole inbox."
/>
) : waitingOnly ? (
<ListMessage
icon={Inbox}
title="Inbox zero"
body="Nobody is waiting on a reply. Switch to All to browse every conversation."
/>
) : (
<ListMessage
icon={CircleSlash}
title="No conversations yet"
body="Conversations appear here as soon as somebody messages the business number."
/>
)
) : (
<ul>
{conversations.map((conv) => (
<ConversationRow
// Pros sharing a number share a conversation, so the id
// alone can repeat on the Pros tab.
key={`${conv.id}-${conv.proId}`}
row={conv}
isCurrent={conv.id !== null && selectedId === conv.id}
onSelect={onSelect}
now={now}
/>
))}
</ul>
)}
</div>
{/* Hidden when everything already fits on one page. */}
{total > pageSize && (
<Pagination
page={page}
totalPages={totalPages}
total={total}
showPageNumbers={false}
density="micro"
label="Conversation pages"
onPageChange={(next) => onOffsetChange((next - 1) * pageSize)}
className="border-t border-border-default px-3 py-2"
/>
)}
</div>
);
}
function ConversationRow({
row,
isCurrent,
onSelect,
now,
}: {
row: WaInboxRow;
isCurrent: boolean;
onSelect: (row: WaInboxRow) => void;
now: Date;
}) {
const waitingMs = elapsedSince(row.lastCustomerMessageAt, now);
const level: SlaLevel =
waitingMs === null ? "ok" : slaLevel(elapsedMinutes(waitingMs));
const name =
row.pro?.businessName || row.contactName || "Unknown contact";
const secondary = [row.pro?.ownerName, row.pro?.city]
.filter(Boolean)
.join(" · ");
return (
<li className="border-b border-border-default last:border-0">
<button
type="button"
onClick={() => onSelect(row)}
aria-current={isCurrent ? "true" : undefined}
className={cn(
"flex w-full items-start gap-2.5 px-3.5 py-2.5 text-left transition-colors",
// Rows TINT; they never lift. The −3px lift means
// "pick-up-able" and these are not draggable.
"hover:bg-foreground-default/7",
isCurrent &&
// The Index Red Rule: the current row gets a 3px inset bar
// and nothing else. An inset shadow, not a border, so
// marking a row shifts no pixel of its content.
"bg-foreground-default/7 shadow-[inset_3px_0_0_var(--secondary-600)]",
)}
>
<span
className={cn(
"w-14 shrink-0 pt-0.5 text-right",
STATUS_CODE_TYPE,
AGE_INK[level],
)}
>
{waitingMs === null ? (
// Never messaged us, so there is no wait to print. An em dash
// holds the column rather than collapsing it.
<span aria-hidden="true">—</span>
) : (
<>
{level === "breach" && (
<TriangleAlert
aria-hidden="true"
className="mr-0.5 inline h-3 w-3 align-[-0.1em]"
/>
)}
<span aria-hidden="true">{formatAgeShort(waitingMs)}</span>
<span className="sr-only">
Waiting {formatAgeLong(waitingMs)}
</span>
</>
)}
</span>
<span className="flex min-w-0 grow flex-col gap-0.5">
<span className="truncate text-sm font-semibold tabular-nums text-foreground-default">
{row.phoneNumber}
</span>
<span className="truncate text-sm text-foreground-muted">{name}</span>
{/*
* On the Pros tab a pro with no conversation at all appears with
* `id: null`, and that is a DIFFERENT fact from "they have not
* written": one means we have never opened a thread, the other
* means they have never answered one. The age chip only speaks to
* the second, so the first needs its own word.
*/}
{row.id === null ? (
<span className={cn(STATUS_CODE_TYPE, "text-foreground-subtle")}>
Never messaged
</span>
) : (
secondary && (
<span className="truncate text-xs text-foreground-subtle">
{secondary}
</span>
)
)}
</span>
<span className="flex shrink-0 flex-col items-end gap-1">
<ContactTypeTag contactType={row.contactType} />
{row.unreadCount > 0 && (
<span className={cn(STATUS_CODE_TYPE, CAPTION)}>
{row.unreadCount}
<span className="sr-only"> unopened</span>
</span>
)}
</span>
</button>
</li>
);
}
function ListMessage({
icon: Icon,
tone,
title,
body,
}: {
icon: typeof Inbox;
tone?: string;
title: string;
body: string;
}) {
return (
<div className="flex flex-col items-center gap-2 px-6 py-10 text-center">
<Icon
aria-hidden="true"
className={cn("h-7 w-7", tone ?? "text-foreground-subtle")}
/>
<p className="text-sm font-semibold text-foreground-default">{title}</p>
<p className="text-xs text-foreground-muted">{body}</p>
</div>
);
}
function FilterChip({
label,
title,
active,
onToggle,
}: {
label: string;
title: string;
active: boolean;
onToggle: () => void;
}) {
return (
<button
type="button"
onClick={onToggle}
title={title}
aria-pressed={active}
className={cn(
"h-6 rounded-sm border px-1.5 transition-colors pointer-coarse:min-h-11",
STATUS_CODE_TYPE,
active
? "border-foreground-default text-foreground-default"
: "border-border-default text-foreground-muted hover:border-border-strong hover:text-foreground-default",
)}
>
{label}
</button>
);
}
|