@remit/ui 0.0.169 → 0.0.170

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remit/ui",
3
- "version": "0.0.169",
3
+ "version": "0.0.170",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src"
@@ -12,6 +12,7 @@ import type {
12
12
  IntelligenceData,
13
13
  SenderTrustLevel,
14
14
  } from "./intelligence-panel.js";
15
+ import type { ResultCount } from "./list-result-header.js";
15
16
  import type { ListState } from "./message-list-state.js";
16
17
 
17
18
  /** Pane-count thresholds, aligned to Tailwind `lg`/`xl`. The whole shell reflows
@@ -314,6 +315,27 @@ export interface ThreadSection {
314
315
  /** Section label; omit for a flat list. */
315
316
  label?: string;
316
317
  threads: ThreadRowData[];
318
+ /**
319
+ * How much mail the section's category holds, as the server counted it —
320
+ * independent of how many rows were fetched. Absent, or `unknown`, renders no
321
+ * number: a loaded-row length presented as a category total is the defect this
322
+ * replaces (#312).
323
+ */
324
+ total?: ResultCount;
325
+ /**
326
+ * The section's request came back full, so the category holds more than these
327
+ * rows whether or not anyone counted it. It is the only thing that keeps a way
328
+ * out of a section whose total was withheld.
329
+ */
330
+ atCap?: boolean;
331
+ /** The section's own request is still in flight, so it has no rows yet. */
332
+ loading?: boolean;
333
+ /**
334
+ * The section's own request failed. Each section is its own query, so one
335
+ * category's failure states itself where that category would have been and
336
+ * leaves the rest of the brief standing.
337
+ */
338
+ error?: boolean;
317
339
  }
318
340
 
319
341
  export interface ThreadMessageData {
@@ -1,9 +1,20 @@
1
+ /**
2
+ * The section header states its category's size, not its page's length (#312).
3
+ *
4
+ * The number used to be `section.threads.length` — how many of one shared 50-row
5
+ * window happened to be Marketing, rendered where a category total belongs. The
6
+ * cases below pin the two halves apart: a total that does not move when the rows
7
+ * do, and the shapes the component must refuse — a real total above no rows, and
8
+ * a number invented for a section nobody counted.
9
+ */
10
+
1
11
  import assert from "node:assert/strict";
2
12
  import { describe, it } from "node:test";
3
13
  import { createElement } from "react";
4
14
  import { renderToString } from "react-dom/server";
5
15
  import type { ThreadRowData, ThreadSection } from "./app-shell-types.js";
6
16
  import { BriefSection, SECTION_ROW_CAP } from "./brief-section.js";
17
+ import type { ResultCount } from "./list-result-header.js";
7
18
  import { ComfortableRow } from "./message-row.js";
8
19
 
9
20
  function makeRow(i: number): ThreadRowData {
@@ -20,25 +31,40 @@ function makeRow(i: number): ThreadRowData {
20
31
  };
21
32
  }
22
33
 
23
- function section(count: number): ThreadSection {
34
+ interface Props {
35
+ count: number;
36
+ total?: ResultCount;
37
+ loading?: boolean;
38
+ failed?: boolean;
39
+ label?: string;
40
+ initialExpanded?: boolean;
41
+ initialCollapsed?: boolean;
42
+ showAll?: boolean;
43
+ retry?: boolean;
44
+ atCap?: boolean;
45
+ }
46
+
47
+ function section(props: Props): ThreadSection {
24
48
  return {
25
49
  id: "personal",
26
- label: "Personal",
27
- threads: Array.from({ length: count }, (_, i) => makeRow(i + 1)),
50
+ label: props.label ?? "Personal",
51
+ threads: Array.from({ length: props.count }, (_, i) => makeRow(i + 1)),
52
+ total: props.total,
53
+ atCap: props.atCap,
54
+ loading: props.loading,
55
+ error: props.failed,
28
56
  };
29
57
  }
30
58
 
31
- function render(props: {
32
- count: number;
33
- initialExpanded?: boolean;
34
- initialCollapsed?: boolean;
35
- }): string {
59
+ function render(props: Props): string {
36
60
  return renderToString(
37
61
  createElement(BriefSection, {
38
- section: section(props.count),
62
+ section: section(props),
39
63
  Row: ComfortableRow,
40
64
  initialExpanded: props.initialExpanded,
41
65
  initialCollapsed: props.initialCollapsed,
66
+ onShowAll: props.showAll ? () => undefined : undefined,
67
+ onRetry: props.retry ? () => undefined : undefined,
42
68
  onSelectThread: () => undefined,
43
69
  }),
44
70
  );
@@ -48,20 +74,79 @@ function rowCount(html: string): number {
48
74
  return (html.match(/Subject \d+/g) ?? []).length;
49
75
  }
50
76
 
77
+ const exact = (value: number): ResultCount => ({ kind: "exact", value });
78
+
51
79
  describe("BriefSection", () => {
52
- it("renders the section label and a row count", () => {
80
+ it("renders the section label and the server's total for its category", () => {
81
+ const html = render({ count: 3, total: exact(4753) });
82
+ assert.match(html, /Personal/);
83
+ assert.match(html, />4,753</);
84
+ });
85
+
86
+ it("states the same total however many rows are loaded", () => {
87
+ const ten = render({ count: SECTION_ROW_CAP, total: exact(3942) });
88
+ const three = render({ count: 3, total: exact(3942) });
89
+ assert.match(ten, />3,942</);
90
+ assert.match(three, />3,942</);
91
+ assert.doesNotMatch(ten, />10</, "the header fell back to the page length");
92
+ });
93
+
94
+ it("renders no number for a section nobody counted", () => {
53
95
  const html = render({ count: 3 });
54
96
  assert.match(html, /Personal/);
55
- assert.match(html, />3</);
97
+ assert.doesNotMatch(html, />3</, "an uncounted section reported a size");
98
+ });
99
+
100
+ it("renders no number when the server withheld the count", () => {
101
+ const html = render({ count: 3, total: { kind: "unknown" } });
102
+ assert.doesNotMatch(html, />3</);
56
103
  });
57
104
 
58
- it("shows every row and no expander below the cap", () => {
105
+ it("shows every row and no control below the cap", () => {
59
106
  const html = render({ count: SECTION_ROW_CAP });
60
107
  assert.strictEqual(rowCount(html), SECTION_ROW_CAP);
61
108
  assert.doesNotMatch(html, /Show \d+ more/);
62
109
  assert.doesNotMatch(html, /Show less/);
63
110
  });
64
111
 
112
+ it("offers the whole category when the total runs past the loaded rows", () => {
113
+ const html = render({
114
+ count: SECTION_ROW_CAP,
115
+ total: exact(3942),
116
+ showAll: true,
117
+ });
118
+ assert.strictEqual(rowCount(html), SECTION_ROW_CAP);
119
+ assert.match(html, />3,942</, "the header lost the category total");
120
+ assert.match(html, /Show all 3,942/);
121
+ });
122
+
123
+ it("offers nothing beyond the rows when they are the whole category", () => {
124
+ const html = render({ count: 3, total: exact(3), showAll: true });
125
+ assert.doesNotMatch(html, /Show all/);
126
+ });
127
+
128
+ // A withheld total must not take the way out with it. The rows beyond this
129
+ // page are still there — a muted sender or an account pill is a reason not to
130
+ // state a size, never a reason to strand the reader in the section (#312).
131
+ it("still offers the whole category when the total was withheld", () => {
132
+ const html = render({
133
+ count: SECTION_ROW_CAP,
134
+ atCap: true,
135
+ showAll: true,
136
+ });
137
+ assert.match(html, /Show all/);
138
+ assert.doesNotMatch(
139
+ html,
140
+ /Show all \d/,
141
+ "a section with no count put a number on the control",
142
+ );
143
+ });
144
+
145
+ it("offers nothing beyond a short page with no total", () => {
146
+ const html = render({ count: 3, showAll: true });
147
+ assert.doesNotMatch(html, /Show all/);
148
+ });
149
+
65
150
  it("caps at SECTION_ROW_CAP with a 'Show N more' control over the cap", () => {
66
151
  const html = render({ count: 18 });
67
152
  assert.strictEqual(rowCount(html), SECTION_ROW_CAP);
@@ -80,8 +165,12 @@ describe("BriefSection", () => {
80
165
  assert.strictEqual(rowCount(html), 3);
81
166
  });
82
167
 
83
- it("shows only the header and count when initially collapsed", () => {
84
- const html = render({ count: 18, initialCollapsed: true });
168
+ it("keeps the total when the reader collapses the section", () => {
169
+ const html = render({
170
+ count: 18,
171
+ total: exact(18),
172
+ initialCollapsed: true,
173
+ });
85
174
  assert.match(html, /Personal/);
86
175
  assert.match(html, />18</);
87
176
  assert.strictEqual(rowCount(html), 0);
@@ -95,4 +184,49 @@ describe("BriefSection", () => {
95
184
  const collapsedHtml = render({ count: 3, initialCollapsed: true });
96
185
  assert.match(collapsedHtml, /<button[^>]*aria-expanded="false"/);
97
186
  });
187
+
188
+ // The shape #312 rejected outright: `Marketing 3,942` above nothing at all,
189
+ // which reads as a broken list rather than as a filter that matched nothing.
190
+ it("never states a total above no rows", () => {
191
+ const html = render({ count: 0, total: exact(3942), label: "Marketing" });
192
+ assert.doesNotMatch(html, />3,942</);
193
+ assert.match(html, /No Marketing mail in this brief\./);
194
+ });
195
+
196
+ it("shows the loading treatment, and its total, while the rows are in flight", () => {
197
+ const html = render({ count: 0, total: exact(3942), loading: true });
198
+ assert.match(html, />3,942</);
199
+ assert.doesNotMatch(html, /No Personal mail in this brief\./);
200
+ assert.match(html, /animate-pulse/);
201
+ });
202
+
203
+ // Seven requests are seven answers. A section whose own request never got one
204
+ // says so where that category would have been, and offers the way to ask
205
+ // again — the six that came back are not blanked by it.
206
+ it("states its own failure and offers its own retry", () => {
207
+ const html = render({
208
+ count: 0,
209
+ total: exact(3942),
210
+ failed: true,
211
+ label: "Marketing",
212
+ retry: true,
213
+ });
214
+ // Server-rendered, so the apostrophe arrives as an entity.
215
+ assert.match(html, /Couldn&#x27;t load Marketing/);
216
+ assert.match(html, /Try again/);
217
+ assert.doesNotMatch(
218
+ html,
219
+ /3,942/,
220
+ "a section that loaded nothing still stated a size",
221
+ );
222
+ assert.doesNotMatch(html, /No Marketing mail in this brief\./);
223
+ });
224
+
225
+ // D6 / issue #45: unclassified mail is its own section with its own label,
226
+ // never folded into Personal.
227
+ it("renders the unclassified section under its own label", () => {
228
+ const html = render({ count: 2, label: "Unclassified", total: exact(2) });
229
+ assert.match(html, /Unclassified/);
230
+ assert.doesNotMatch(html, /Personal/);
231
+ });
98
232
  });
@@ -1,6 +1,6 @@
1
1
  import type { Meta, StoryObj } from "@storybook/react";
2
2
  import type { ThreadRowData, ThreadSection } from "./app-shell-types.js";
3
- import { BriefSection } from "./brief-section.js";
3
+ import { BriefSection, SECTION_ROW_CAP } from "./brief-section.js";
4
4
  import { ComfortableRow } from "./message-row.js";
5
5
 
6
6
  function makeRow(i: number): ThreadRowData {
@@ -17,16 +17,28 @@ function makeRow(i: number): ThreadRowData {
17
17
  };
18
18
  }
19
19
 
20
+ const rows = (n: number): ThreadRowData[] =>
21
+ Array.from({ length: n }, (_, i) => makeRow(i + 1));
22
+
20
23
  const shortSection: ThreadSection = {
21
24
  id: "transactional",
22
25
  label: "Transactional",
23
- threads: Array.from({ length: 3 }, (_, i) => makeRow(i + 1)),
26
+ threads: rows(3),
27
+ total: { kind: "exact", value: 3 },
24
28
  };
25
29
 
26
30
  const longSection: ThreadSection = {
27
31
  id: "newsletter",
28
32
  label: "Newsletter",
29
- threads: Array.from({ length: 18 }, (_, i) => makeRow(i + 1)),
33
+ threads: rows(18),
34
+ };
35
+
36
+ /** Ten rows of a category holding thousands — the live shape of a brief section. */
37
+ const cappedSection: ThreadSection = {
38
+ id: "marketing",
39
+ label: "Marketing",
40
+ threads: rows(SECTION_ROW_CAP),
41
+ total: { kind: "exact", value: 3942 },
30
42
  };
31
43
 
32
44
  const meta: Meta<typeof BriefSection> = {
@@ -47,12 +59,77 @@ export default meta;
47
59
 
48
60
  type Story = StoryObj<typeof BriefSection>;
49
61
 
50
- /** Fewer than the cap — no expander. */
62
+ /** Fewer than the cap — the total is the rows, and there is nowhere else to go. */
51
63
  export const Short: Story = {
52
64
  args: { section: shortSection },
53
65
  };
54
66
 
55
- /** Over the cap — shows the first 10 rows and a "Show N more" control. */
67
+ /**
68
+ * The header total is the category's, counted by the server, and it stays put
69
+ * however many rows the section holds. Ten rows under `Marketing 3,942`, with the
70
+ * way to the rest beneath them.
71
+ */
72
+ export const CategoryTotal: Story = {
73
+ args: { section: cappedSection, onShowAll: () => undefined },
74
+ };
75
+
76
+ /**
77
+ * A section nobody counted — an account pill or a `before:` term is narrowing the
78
+ * rows after they arrive, so the server's number is of a wider set than the list.
79
+ * It renders no number rather than one that is not the section's size.
80
+ */
81
+ export const NoTotal: Story = {
82
+ args: { section: { ...cappedSection, total: undefined } },
83
+ };
84
+
85
+ /**
86
+ * The rows are still in flight. Distinct from a section that came back empty:
87
+ * the total is already known, and the treatment says the rows are coming.
88
+ */
89
+ export const Loading: Story = {
90
+ args: {
91
+ section: { ...cappedSection, threads: [], loading: true },
92
+ },
93
+ };
94
+
95
+ /**
96
+ * This section's own request failed. Each section is its own query, so the
97
+ * failure states itself here and offers the way to ask again while the rest of
98
+ * the brief stands.
99
+ */
100
+ export const Failed: Story = {
101
+ args: {
102
+ section: { ...cappedSection, threads: [], error: true },
103
+ onRetry: () => undefined,
104
+ },
105
+ };
106
+
107
+ /**
108
+ * A chip narrowed the section to nothing. The section says so in its own words,
109
+ * and drops its number — a real total above zero rows reads as a broken list.
110
+ */
111
+ export const EmptyUnderFilter: Story = {
112
+ args: {
113
+ section: { id: "personal", label: "Personal", threads: [] },
114
+ },
115
+ };
116
+
117
+ /**
118
+ * `uncategorized` is its own section with its own label, last in the order,
119
+ * never folded into Personal (D6, issue #45).
120
+ */
121
+ export const Unclassified: Story = {
122
+ args: {
123
+ section: {
124
+ id: "uncategorized",
125
+ label: "Unclassified",
126
+ threads: rows(4).map((row) => ({ ...row, category: "uncategorized" })),
127
+ total: { kind: "exact", value: 4 },
128
+ },
129
+ },
130
+ };
131
+
132
+ /** Over the cap with no total — the local expander, for a complete fixture set. */
56
133
  export const CollapsedAtCap: Story = {
57
134
  args: { section: longSection },
58
135
  };
@@ -62,7 +139,7 @@ export const Expanded: Story = {
62
139
  args: { section: longSection, initialExpanded: true },
63
140
  };
64
141
 
65
- /** Section collapsed by its header — only the label + count show, every row hidden. */
142
+ /** Section collapsed by its header — only the label + total show, every row hidden. */
66
143
  export const SectionCollapsed: Story = {
67
- args: { section: longSection, initialCollapsed: true },
144
+ args: { section: cappedSection, initialCollapsed: true },
68
145
  };
@@ -1,12 +1,17 @@
1
- import { ChevronDown } from "lucide-react";
1
+ import { ChevronDown, ChevronRight } from "lucide-react";
2
2
  import { useState } from "react";
3
3
  import { cn } from "../lib/cn.js";
4
4
  import type { ThreadSection } from "./app-shell-types.js";
5
+ import type { ResultCount } from "./list-result-header.js";
5
6
  import type { BriefRowComponent } from "./message-row.js";
6
7
 
7
- /** Rows shown before the "Show N more" expander kicks in. */
8
+ /** Rows a section renders before the reader is sent to the filtered list. */
8
9
  export const SECTION_ROW_CAP = 10;
9
10
 
11
+ const UNKNOWN_TOTAL: ResultCount = { kind: "unknown" };
12
+
13
+ const formatTotal = (n: number): string => n.toLocaleString();
14
+
10
15
  export interface BriefSectionProps {
11
16
  section: ThreadSection;
12
17
  Row: BriefRowComponent;
@@ -15,22 +20,42 @@ export interface BriefSectionProps {
15
20
  initialExpanded?: boolean;
16
21
  /** Seed the section-collapse state — lets a story / SSR render the header-only view. */
17
22
  initialCollapsed?: boolean;
23
+ /**
24
+ * Opens the filtered list for this section's category. Given, the rows beyond
25
+ * the ones loaded are the server's rather than this component's, so the
26
+ * control below the rows hands the reader over instead of revealing more.
27
+ */
28
+ onShowAll?: () => void;
29
+ /** Ask this section's own request again, after it failed. */
30
+ onRetry?: () => void;
18
31
  onSelectThread?: (id: string) => void;
19
32
  }
20
33
 
21
34
  /**
22
- * One brief section: a sticky category label, the first {@link SECTION_ROW_CAP}
23
- * rows, and a "Show N more" control that reveals the rest in place (toggling
24
- * back to "Show less"). The control is always tappable — it never disables.
35
+ * One brief section: a sticky category label, the server's total for that
36
+ * category, the newest rows of it, and a way to the rest.
37
+ *
38
+ * The header number is `section.total` — how much mail the category holds,
39
+ * counted over the whole scope. It does not move when more rows arrive, because
40
+ * it was never derived from them. A section with no total renders no number:
41
+ * the length of what happens to be loaded, presented as a category size, is the
42
+ * reading this replaces (#312).
43
+ *
44
+ * The control below the rows is the way to the rest of the category, whether or
45
+ * not the header carries a number: a section that came back full holds more than
46
+ * it shows, and says so even where the count was withheld.
47
+ *
48
+ * A total is never rendered above zero rows. Nothing loaded and the section
49
+ * still fetching is the loading treatment; nothing loaded because the request
50
+ * failed says so and offers the retry; nothing loaded and the fetch done is a
51
+ * filter that matched nothing, and says so.
25
52
  *
26
53
  * The header is itself a toggle: tapping it collapses the whole section to just
27
- * the label + count (hiding every row and the "Show N more" control) and
28
- * expands it again. Sections start expanded so the default brief render is
29
- * unchanged.
54
+ * the label + total and expands it again. Sections start expanded so the default
55
+ * brief render is unchanged.
30
56
  *
31
- * Owns only its own expand/collapse state; the parent supplies the grouped
32
- * section and a `Row` renderer so the live brief and the Storybook prototype
33
- * stay in lockstep.
57
+ * Owns only its own expand/collapse state; the parent supplies the section and a
58
+ * `Row` renderer so the live brief and the Storybook prototype stay in lockstep.
34
59
  */
35
60
  export function BriefSection({
36
61
  section,
@@ -38,17 +63,45 @@ export function BriefSection({
38
63
  selectedThreadId,
39
64
  initialExpanded = false,
40
65
  initialCollapsed = false,
66
+ onShowAll,
67
+ onRetry,
41
68
  onSelectThread,
42
69
  }: BriefSectionProps) {
43
70
  const [expanded, setExpanded] = useState(initialExpanded);
44
71
  const [collapsed, setCollapsed] = useState(initialCollapsed);
45
72
 
46
- const overCap = section.threads.length > SECTION_ROW_CAP;
73
+ const loaded = section.threads;
74
+ const loading = section.loading === true;
75
+ const failed = section.error === true;
76
+ const overCap = loaded.length > SECTION_ROW_CAP;
47
77
  const capped = !expanded && overCap;
48
- const visible = capped
49
- ? section.threads.slice(0, SECTION_ROW_CAP)
50
- : section.threads;
51
- const hiddenCount = section.threads.length - visible.length;
78
+ const visible = capped ? loaded.slice(0, SECTION_ROW_CAP) : loaded;
79
+
80
+ const total = section.total ?? UNKNOWN_TOTAL;
81
+ // Nothing fetched and nothing coming: the section has no size to state, only
82
+ // a filter that matched nothing.
83
+ const headerTotal =
84
+ total.kind === "exact" && (loaded.length > 0 || loading) && !failed
85
+ ? formatTotal(total.value)
86
+ : undefined;
87
+ // Nothing to show more of: a section a chip emptied offers a way out of the
88
+ // chip, not a number the reader cannot see any of.
89
+ // The way out of the section, and the number on it where there is one. A total
90
+ // the caller withheld — a muted sender, an account pill, a term the request
91
+ // could not carry — must not take the control with it: the rows beyond this
92
+ // page are still there, and a section with no number and no control is a dead
93
+ // end (#312).
94
+ const holdsMore =
95
+ total.kind === "exact"
96
+ ? total.value > visible.length
97
+ : section.atCap === true;
98
+ const showAllLabel =
99
+ onShowAll !== undefined && visible.length > 0 && holdsMore
100
+ ? total.kind === "exact"
101
+ ? `Show all ${formatTotal(total.value)}`
102
+ : "Show all"
103
+ : undefined;
104
+ const hiddenHere = loaded.length - visible.length;
52
105
 
53
106
  return (
54
107
  <div>
@@ -62,9 +115,11 @@ export function BriefSection({
62
115
  <span className="flex-1 text-2xs font-semibold uppercase tracking-wider text-fg-subtle">
63
116
  {section.label}
64
117
  </span>
65
- <span className="text-2xs text-fg-subtle tabular-nums">
66
- {section.threads.length}
67
- </span>
118
+ {headerTotal !== undefined && (
119
+ <span className="text-2xs text-fg-subtle tabular-nums">
120
+ {headerTotal}
121
+ </span>
122
+ )}
68
123
  <ChevronDown
69
124
  className={cn(
70
125
  "size-3 shrink-0 text-fg-subtle transition-transform duration-200",
@@ -75,25 +130,69 @@ export function BriefSection({
75
130
  )}
76
131
  {!collapsed && (
77
132
  <>
78
- <div className="divide-y divide-line">
79
- {visible.map((thread) => (
80
- <Row
81
- key={thread.id}
82
- thread={thread}
83
- active={thread.id === selectedThreadId}
84
- onClick={() => onSelectThread?.(thread.id)}
85
- />
86
- ))}
87
- </div>
88
- {overCap && (
133
+ {loaded.length === 0 ? (
134
+ failed ? (
135
+ <div className="flex flex-col items-center gap-1 border-b border-line px-row-inset py-4 text-center text-2xs text-danger">
136
+ <span>{`Couldn't load ${section.label ?? "these messages"}`}</span>
137
+ {onRetry && (
138
+ <button
139
+ type="button"
140
+ onClick={onRetry}
141
+ className="font-medium text-accent underline outline-none focus-visible:ring-2 focus-visible:ring-ring"
142
+ >
143
+ Try again
144
+ </button>
145
+ )}
146
+ </div>
147
+ ) : loading ? (
148
+ <div className="animate-pulse divide-y divide-line">
149
+ {[0, 1, 2].map((row) => (
150
+ <div key={row} className="px-row-inset py-3">
151
+ <div className="h-3 w-2/3 rounded bg-surface-sunken" />
152
+ </div>
153
+ ))}
154
+ </div>
155
+ ) : (
156
+ <p className="border-b border-line px-row-inset py-4 text-center text-2xs text-fg-subtle">
157
+ {section.label
158
+ ? `No ${section.label} mail in this brief.`
159
+ : "No mail in this brief."}
160
+ </p>
161
+ )
162
+ ) : (
163
+ <div className="divide-y divide-line">
164
+ {visible.map((thread) => (
165
+ <Row
166
+ key={thread.id}
167
+ thread={thread}
168
+ active={thread.id === selectedThreadId}
169
+ onClick={() => onSelectThread?.(thread.id)}
170
+ />
171
+ ))}
172
+ </div>
173
+ )}
174
+ {showAllLabel !== undefined ? (
89
175
  <button
90
176
  type="button"
91
- onClick={() => setExpanded((v) => !v)}
177
+ onClick={onShowAll}
92
178
  className="flex w-full items-center justify-center border-b border-line px-row-inset py-1.5 text-2xs font-medium text-accent outline-none transition-colors hover:bg-surface focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset"
93
179
  >
94
- {expanded ? "Show less" : `Show ${hiddenCount} more`}
95
- {!expanded && <ChevronDown className="ml-1 size-3" />}
180
+ {showAllLabel}
181
+ <ChevronRight className="ml-1 size-3" />
96
182
  </button>
183
+ ) : (
184
+ overCap && (
185
+ <button
186
+ type="button"
187
+ onClick={() => setExpanded((v) => !v)}
188
+ className="flex w-full items-center justify-center border-b border-line px-row-inset py-1.5 text-2xs font-medium text-accent outline-none transition-colors hover:bg-surface focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset"
189
+ >
190
+ {expanded
191
+ ? "Show less"
192
+ : `Show ${formatTotal(hiddenHere)} more`}
193
+ {!expanded && <ChevronDown className="ml-1 size-3" />}
194
+ </button>
195
+ )
97
196
  )}
98
197
  </>
99
198
  )}
@@ -69,4 +69,71 @@ describe("BriefSections", () => {
69
69
  assert.match(html, /Weekly Brief/);
70
70
  assert.doesNotMatch(html, /Priya Nair/);
71
71
  });
72
+
73
+ // #312: a section the server answered for is a section, rows or not. Dropping
74
+ // it would leave the reader unable to tell a chip that matched nothing from a
75
+ // category the brief never asked about.
76
+ it("keeps a counted section a chip narrowed to nothing", () => {
77
+ const html = renderToString(
78
+ createElement(BriefSections, {
79
+ sections: [
80
+ {
81
+ id: "personal",
82
+ label: "Personal",
83
+ threads: [],
84
+ total: { kind: "exact", value: 4753 },
85
+ },
86
+ ],
87
+ Row: ComfortableRow,
88
+ briefCategory: "all",
89
+ onSelectThread: () => undefined,
90
+ onSelectBriefCategory: () => undefined,
91
+ }),
92
+ );
93
+ assert.match(html, /No Personal mail in this brief\./);
94
+ });
95
+
96
+ // A search is answered by one ordered list. Sectioning the matches would put
97
+ // an old newsletter above a newer match, which is what the sections do (#312).
98
+ it("renders no headers at all under `flat`, keeping the given row order", () => {
99
+ const html = renderToString(
100
+ createElement(BriefSections, {
101
+ sections,
102
+ Row: ComfortableRow,
103
+ briefCategory: "all",
104
+ flat: true,
105
+ onSelectThread: () => undefined,
106
+ onSelectBriefCategory: () => undefined,
107
+ }),
108
+ );
109
+ assert.doesNotMatch(html, />Personal</);
110
+ assert.doesNotMatch(html, />Newsletter</);
111
+ assert.ok(
112
+ html.indexOf("Priya Nair") < html.indexOf("Weekly Brief"),
113
+ "the flat list reordered its rows",
114
+ );
115
+ });
116
+
117
+ // Narrowed to one category the label repeats the chip, but the total does
118
+ // not: it is the only statement of how much mail that category holds.
119
+ it("keeps the header at a single-category scope once it carries a total", () => {
120
+ const html = renderToString(
121
+ createElement(BriefSections, {
122
+ sections: [
123
+ {
124
+ id: "newsletter",
125
+ label: "Newsletter",
126
+ threads: sections[1].threads,
127
+ total: { kind: "exact", value: 2295 },
128
+ },
129
+ ],
130
+ Row: ComfortableRow,
131
+ briefCategory: "newsletter",
132
+ onSelectThread: () => undefined,
133
+ onSelectBriefCategory: () => undefined,
134
+ }),
135
+ );
136
+ assert.match(html, /Newsletter/);
137
+ assert.match(html, />2,295</);
138
+ });
72
139
  });
@@ -84,6 +84,56 @@ const sections: ThreadSection[] = [
84
84
  },
85
85
  ];
86
86
 
87
+ /**
88
+ * The live shape: each section is its own category-scoped query, so its header
89
+ * carries that category's real size and its rows are the newest page of it.
90
+ */
91
+ const countedSections: ThreadSection[] = [
92
+ {
93
+ id: "personal",
94
+ label: "Personal",
95
+ threads: [
96
+ {
97
+ id: "p1",
98
+ accountId: "a1",
99
+ fromName: "Priya Nair",
100
+ fromEmail: "priya@example.com",
101
+ subject: "Design review tomorrow",
102
+ snippet: "Can we move it to 2pm? I have a conflict.",
103
+ timeLabel: "8:15",
104
+ isRead: false,
105
+ category: "personal",
106
+ },
107
+ ],
108
+ total: { kind: "exact", value: 4753 },
109
+ },
110
+ {
111
+ id: "newsletter",
112
+ label: "Newsletter",
113
+ threads: Array.from({ length: 10 }, (_, i) => newsletterRow(i + 1)),
114
+ total: { kind: "exact", value: 2295 },
115
+ },
116
+ {
117
+ id: "social",
118
+ label: "Social",
119
+ threads: [],
120
+ total: { kind: "exact", value: 88 },
121
+ loading: true,
122
+ },
123
+ {
124
+ id: "marketing",
125
+ label: "Marketing",
126
+ threads: [],
127
+ error: true,
128
+ },
129
+ {
130
+ id: "uncategorized",
131
+ label: "Unclassified",
132
+ threads: [],
133
+ total: { kind: "exact", value: 12 },
134
+ },
135
+ ];
136
+
87
137
  const meta: Meta<typeof BriefSections> = {
88
138
  title: "Screens/Kit/BriefSections",
89
139
  component: BriefSections,
@@ -130,9 +180,9 @@ export const AllScopeWithHeaders: Story = {
130
180
  };
131
181
 
132
182
  /**
133
- * (b) Single-category filter: narrowed to Newsletter, the list renders FLAT with
134
- * NO section header — the header would be redundant once a single category is
135
- * selected. This is the behavior the live brief now inherits from the kit.
183
+ * (b) Single-category filter over uncounted sections: narrowed to Newsletter, the
184
+ * list renders FLAT with NO section header — with nothing but the label to state,
185
+ * the header only repeats the chip.
136
186
  */
137
187
  export const SingleCategoryFlat: Story = {
138
188
  args: { briefCategory: "newsletter" },
@@ -143,6 +193,86 @@ export const SingleCategoryFlat: Story = {
143
193
  ),
144
194
  };
145
195
 
196
+ /**
197
+ * (b2) The live brief: each header carries its category's real size, a section
198
+ * whose rows have not arrived shows the loading treatment under its total, a
199
+ * section whose own request failed says so and offers its own retry, and a
200
+ * section a chip emptied says so too. "Show all" hands the reader to that
201
+ * category's own list rather than fetching more rows here.
202
+ */
203
+ export const ServerTotals: Story = {
204
+ args: {
205
+ sections: countedSections,
206
+ briefCategory: "all",
207
+ onShowAllSection: () => undefined,
208
+ onRetrySection: () => undefined,
209
+ },
210
+ render: (args) => (
211
+ <div className="flex h-screen w-96 flex-col border-r border-line">
212
+ <BriefSections {...args} />
213
+ </div>
214
+ ),
215
+ };
216
+
217
+ /**
218
+ * (b3) The "show all" destination: narrowed to one counted category, the header
219
+ * stays, because the total is the one thing the chip cannot state.
220
+ */
221
+ export const SingleCategoryCounted: Story = {
222
+ args: { sections: countedSections, briefCategory: "newsletter" },
223
+ render: (args) => (
224
+ <div className="flex h-screen w-96 flex-col border-r border-line">
225
+ <BriefSections {...args} />
226
+ </div>
227
+ ),
228
+ };
229
+
230
+ /**
231
+ * (b4) The brief answering a search: no sections at all, one list in the order
232
+ * the server returned it. A newsletter from last spring under a header would
233
+ * outrank a mail from this morning, which is the reading a search must not give.
234
+ */
235
+ export const Searching: Story = {
236
+ args: {
237
+ sections: [
238
+ {
239
+ id: "matches",
240
+ threads: [
241
+ {
242
+ id: "m1",
243
+ accountId: "a1",
244
+ fromName: "CI",
245
+ fromEmail: "ci@build.example",
246
+ subject: "Your build passed",
247
+ snippet: "All checks green on main.",
248
+ timeLabel: "8:02",
249
+ isRead: false,
250
+ category: "automated",
251
+ },
252
+ {
253
+ id: "m2",
254
+ accountId: "a1",
255
+ fromName: "Digest",
256
+ fromEmail: "digest@news.example",
257
+ subject: "Weekly digest for you",
258
+ snippet: "Stories you might have missed.",
259
+ timeLabel: "Mar 4",
260
+ isRead: true,
261
+ category: "newsletter",
262
+ },
263
+ ],
264
+ },
265
+ ],
266
+ briefCategory: "all",
267
+ flat: true,
268
+ },
269
+ render: (args) => (
270
+ <div className="flex h-screen w-96 flex-col border-r border-line">
271
+ <BriefSections {...args} />
272
+ </div>
273
+ ),
274
+ };
275
+
146
276
  const accountSources: FilterSheetSource[] = [
147
277
  { id: "all", label: "All", active: true },
148
278
  { id: "a1", label: "work", count: 3 },
@@ -150,6 +150,20 @@ interface BriefSectionsBaseProps
150
150
  */
151
151
  keyboard?: MessageListKeyboard;
152
152
  onSelectThread?: (id: string) => void;
153
+ /**
154
+ * Sends the reader to the filtered list for one section's category. Given, a
155
+ * section holding fewer rows than its total offers the way to the rest — the
156
+ * brief itself never grows past its per-section page.
157
+ */
158
+ onShowAllSection?: (sectionId: string) => void;
159
+ /** Ask one section's own request again, after it failed. */
160
+ onRetrySection?: (sectionId: string) => void;
161
+ /**
162
+ * Render one list rather than one section per category. A search is answered
163
+ * this way: the rows come back in one global order, and a header between them
164
+ * would put an old match from an earlier category above a newer one (#312).
165
+ */
166
+ flat?: boolean;
153
167
  /**
154
168
  * Drop the filter row and its panel, keeping the rows where they are. See
155
169
  * `FilterSheetProps`.
@@ -163,7 +177,8 @@ export type BriefSectionsProps = BriefSectionsBaseProps & BriefFilterControl;
163
177
 
164
178
  /**
165
179
  * The daily-brief list body: category pills (single-select) + attribute chips
166
- * (additive) + one capped section per category (see {@link BriefSection}). Owns
180
+ * (additive) + one capped section per category (see {@link BriefSection}), or —
181
+ * under `flat` — one plain list in the order the rows arrived. Owns
167
182
  * its own filter state; the category axis is controlled via
168
183
  * `briefCategory`/`onSelectBriefCategory`. Consumers pre-filter `sections`
169
184
  * (e.g. by search) and pass a `Row` renderer; the web client reuses this so the
@@ -176,6 +191,9 @@ export function BriefSections({
176
191
  Row,
177
192
  keyboard,
178
193
  onSelectThread,
194
+ onShowAllSection,
195
+ onRetrySection,
196
+ flat = false,
179
197
  onSelectBriefCategory,
180
198
  sources,
181
199
  sourcesNote,
@@ -216,16 +234,31 @@ export function BriefSections({
216
234
  (briefCategory === "all" || t.category === briefCategory) &&
217
235
  matchesBriefFilters(t, active);
218
236
 
219
- // One section per category only earns its keep at the "all" scope. Narrow to
220
- // a single category and the headers are redundant: render a plain flat list.
221
- const showSections = briefCategory === "all";
237
+ // One section per category earns its keep at the "all" scope, and wherever a
238
+ // header carries the server's total for its category: narrowed to one
239
+ // category the label restates the chip, but the total does not — it is the
240
+ // only statement of how much mail that category holds. A search overrules
241
+ // both: its answer is one list in one order.
242
+ const showSections =
243
+ !flat &&
244
+ (briefCategory === "all" ||
245
+ sections.some((section) => section.total !== undefined));
222
246
 
247
+ // A section the server answered for stays on screen with no rows: nothing
248
+ // matching a chip is a state the section states, and is not the same as a
249
+ // category the brief never asked about.
223
250
  const filtered = sections
224
251
  .map((section) => ({
225
252
  ...section,
226
253
  threads: section.threads.filter(matches),
227
254
  }))
228
- .filter((section) => section.threads.length > 0);
255
+ .filter(
256
+ (section) =>
257
+ section.threads.length > 0 ||
258
+ section.total !== undefined ||
259
+ section.loading === true ||
260
+ section.error === true,
261
+ );
229
262
 
230
263
  const flatRows = sections.flatMap((s) => s.threads).filter(matches);
231
264
 
@@ -258,6 +291,12 @@ export function BriefSections({
258
291
  Row={Row}
259
292
  selectedThreadId={selectedThreadId}
260
293
  onSelectThread={onSelectThread}
294
+ onShowAll={
295
+ onShowAllSection ? () => onShowAllSection(section.id) : undefined
296
+ }
297
+ onRetry={
298
+ onRetrySection ? () => onRetrySection(section.id) : undefined
299
+ }
261
300
  />
262
301
  ))
263
302
  ) : (