Skip to content

Commit c302aa2

Browse files
committed
fix(kanban): a windowed swimlane board counts each whole swimlane, and each whole column when it queries itself
A board drawn in swimlanes over a window of its query asks one grouped read on the lane field and the swimlane field together. Each swimlane row's count, and on a board that queries itself each column's count, is the sum of its groups. A board holding every record asks nothing. Claude-Session: https://claude.ai/code/session_01L9qibwxBy3QieJMqJbJtTk Co-authored-by: Claude <noreply@anthropic.com>
1 parent f294277 commit c302aa2

5 files changed

Lines changed: 261 additions & 29 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@object-ui/plugin-kanban': patch
3+
---
4+
5+
fix(kanban): a windowed swimlane board counts each whole swimlane, and each whole column when it queries itself
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
A kanban drawn in swimlanes that holds only a window of its query (a page a list view hands it, or its own fetch that filled its `limit`) wrote each swimlane row's count from the cards it held, marked `+`, and a board that queried itself wrote its column counts the same way. Such a board now asks one grouped read on the lane field and the swimlane field together, each group's record count under the board's filter and search, and writes every swimlane row's whole count from it; a board that queries itself also writes every column's whole count from the same read. A board that holds every record of its query asks nothing more. A source with no grouped door, or a read that is refused or cut, keeps the counts marked `+`, as before. Nothing to migrate.

‎ui/packages/plugin-kanban/src/KanbanBoardCore.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ export interface KanbanBoardCoreProps {
8383
* reason: only `ObjectKanban`, which asks the grouped lane read, supplies it.
8484
*/
8585
laneCounts?: KanbanBoardProps['laneCounts'];
86+
/**
87+
* Each swimlane row's whole record count on a windowed board — see
88+
* `KanbanBoardProps.swimlaneCounts` (objectstack#37844). A PROP for the
89+
* reason `laneCounts` is.
90+
*/
91+
swimlaneCounts?: KanbanBoardProps['swimlaneCounts'];
8692
/**
8793
* Draw the trailing "Uncategorized" lane even when no loaded card lands in
8894
* it: its records sit past the page (objectstack#37516). A PROP for the
@@ -144,6 +150,7 @@ export const KanbanBoardCore: React.FC<KanbanBoardCoreProps> = ({
144150
dropBlockedReason,
145151
onLaneRowsChange,
146152
laneCounts,
153+
swimlaneCounts,
147154
keepUncolumned,
148155
quickAdd,
149156
onQuickAdd,
@@ -184,6 +191,7 @@ export const KanbanBoardCore: React.FC<KanbanBoardCoreProps> = ({
184191
swimlaneField={schema.swimlaneField}
185192
countsAreWindowed={schema.countsAreWindowed}
186193
laneCounts={laneCounts}
194+
swimlaneCounts={swimlaneCounts}
187195
/>
188196
</Suspense>
189197
);

‎ui/packages/plugin-kanban/src/KanbanImpl.tsx‎

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,18 @@ export interface KanbanBoardProps {
290290
* (`swimlaneCountIsWhole`). A PROP, never a schema key.
291291
*/
292292
laneCounts?: Readonly<Record<string, number>> | null
293+
/**
294+
* objectstack#37844 — each swimlane row's whole record count, by the key
295+
* `laneKeyOf` gives the swimlane value (the no-value swimlane under `''`),
296+
* on a board drawn in swimlanes that holds a window of its query.
297+
* `ObjectKanban` reads it off one grouped read on the lane field and the
298+
* swimlane field (`readLaneCounts` folded by the swimlane key). Absent: the
299+
* row counts its cards, marked `+` where `swimlaneCountIsWhole` says the
300+
* board holds part of its query. `null`: the read has not answered or cannot
301+
* (no door, a refusal, a cut answer), so the row stays marked. A PROP, never
302+
* a schema key.
303+
*/
304+
swimlaneCounts?: Readonly<Record<string, number>> | null
293305
/**
294306
* Whether the cards may be dragged at all (default `true`). `ObjectKanban`
295307
* passes `false` when the caller may not make the move its `onCardMove`
@@ -887,6 +899,26 @@ function swimlaneCountIsWhole(
887899
return true
888900
}
889901

902+
/**
903+
* objectstack#37844 — a swimlane row's whole count from the swimlane read
904+
* (`KanbanBoardProps.swimlaneCounts`), or `null` where it gives none and the
905+
* row falls back to its cards (`swimlaneCountIsWhole`). The no-value row
906+
* (`UNCATEGORIZED_LANE`) reads the read's no-value group (`''`); a row keyed
907+
* `''` (a stored empty text) shares that group with it, so it reads none. A
908+
* row the whole answer names no group for holds no record of the query when it
909+
* draws no card, and is a floor when it draws some (a write landed between the
910+
* two reads).
911+
*/
912+
function swimlaneRowCount(
913+
swimlaneCounts: Readonly<Record<string, number>> | null | undefined,
914+
lane: string,
915+
cards: number,
916+
): { count: number; atLeast: boolean } | null {
917+
if (!swimlaneCounts) return null
918+
if (lane === '') return null
919+
return wholeLaneCount(swimlaneCounts, lane === UNCATEGORIZED_LANE ? '' : lane, cards)
920+
}
921+
890922
/**
891923
* What a lane header shows IN PLACE OF its count while the board's records
892924
* have not settled (objectstack#30431).
@@ -1408,15 +1440,15 @@ function moveOutcomeSentence(outcome: unknown, movedSentence: string): string |
14081440
return movedSentence
14091441
}
14101442

1411-
export default function KanbanBoard({ columns, onCardMove, onCardClick, className, quickAdd, onQuickAdd, coverImageField, conditionalFormatting, objectFields, swimlaneTargetDef, swimlaneField, countsAreWindowed, laneCounts, cardsMovable = true, rowOpensRecord = true, canDropCard, dropBlockedReason, onLaneRowsChange }: KanbanBoardProps) {
1443+
export default function KanbanBoard({ columns, onCardMove, onCardClick, className, quickAdd, onQuickAdd, coverImageField, conditionalFormatting, objectFields, swimlaneTargetDef, swimlaneField, countsAreWindowed, laneCounts, swimlaneCounts, cardsMovable = true, rowOpensRecord = true, canDropCard, dropBlockedReason, onLaneRowsChange }: KanbanBoardProps) {
14121444
const hasDnd = useHasDndProvider()
14131445

14141446
if (hasDnd) {
14151447
return (
14161448
<KanbanCardsMovable.Provider value={cardsMovable}>
14171449
<KanbanCardsOpenRecord.Provider value={rowOpensRecord}>
14181450
<DndBridge>
1419-
{(dnd) => <KanbanBoardInner columns={columns} onCardMove={onCardMove} onCardClick={onCardClick} className={className} dnd={dnd} quickAdd={quickAdd} onQuickAdd={onQuickAdd} coverImageField={coverImageField} conditionalFormatting={conditionalFormatting} objectFields={objectFields} swimlaneTargetDef={swimlaneTargetDef} swimlaneField={swimlaneField} countsAreWindowed={countsAreWindowed} laneCounts={laneCounts} canDropCard={canDropCard} dropBlockedReason={dropBlockedReason} onLaneRowsChange={onLaneRowsChange} />}
1451+
{(dnd) => <KanbanBoardInner columns={columns} onCardMove={onCardMove} onCardClick={onCardClick} className={className} dnd={dnd} quickAdd={quickAdd} onQuickAdd={onQuickAdd} coverImageField={coverImageField} conditionalFormatting={conditionalFormatting} objectFields={objectFields} swimlaneTargetDef={swimlaneTargetDef} swimlaneField={swimlaneField} countsAreWindowed={countsAreWindowed} laneCounts={laneCounts} swimlaneCounts={swimlaneCounts} canDropCard={canDropCard} dropBlockedReason={dropBlockedReason} onLaneRowsChange={onLaneRowsChange} />}
14201452
</DndBridge>
14211453
</KanbanCardsOpenRecord.Provider>
14221454
</KanbanCardsMovable.Provider>
@@ -1426,13 +1458,13 @@ export default function KanbanBoard({ columns, onCardMove, onCardClick, classNam
14261458
return (
14271459
<KanbanCardsMovable.Provider value={cardsMovable}>
14281460
<KanbanCardsOpenRecord.Provider value={rowOpensRecord}>
1429-
<KanbanBoardInner columns={columns} onCardMove={onCardMove} onCardClick={onCardClick} className={className} dnd={null} quickAdd={quickAdd} onQuickAdd={onQuickAdd} coverImageField={coverImageField} conditionalFormatting={conditionalFormatting} objectFields={objectFields} swimlaneTargetDef={swimlaneTargetDef} swimlaneField={swimlaneField} countsAreWindowed={countsAreWindowed} laneCounts={laneCounts} canDropCard={canDropCard} dropBlockedReason={dropBlockedReason} onLaneRowsChange={onLaneRowsChange} />
1461+
<KanbanBoardInner columns={columns} onCardMove={onCardMove} onCardClick={onCardClick} className={className} dnd={null} quickAdd={quickAdd} onQuickAdd={onQuickAdd} coverImageField={coverImageField} conditionalFormatting={conditionalFormatting} objectFields={objectFields} swimlaneTargetDef={swimlaneTargetDef} swimlaneField={swimlaneField} countsAreWindowed={countsAreWindowed} laneCounts={laneCounts} swimlaneCounts={swimlaneCounts} canDropCard={canDropCard} dropBlockedReason={dropBlockedReason} onLaneRowsChange={onLaneRowsChange} />
14301462
</KanbanCardsOpenRecord.Provider>
14311463
</KanbanCardsMovable.Provider>
14321464
)
14331465
}
14341466

1435-
function KanbanBoardInner({ columns, onCardMove, onCardClick, className, dnd, quickAdd, onQuickAdd, coverImageField: _coverImageField, conditionalFormatting, objectFields, swimlaneTargetDef, swimlaneField, countsAreWindowed, laneCounts, canDropCard, dropBlockedReason, onLaneRowsChange }: KanbanBoardProps & { dnd: ReturnType<typeof useDnd> | null }) {
1467+
function KanbanBoardInner({ columns, onCardMove, onCardClick, className, dnd, quickAdd, onQuickAdd, coverImageField: _coverImageField, conditionalFormatting, objectFields, swimlaneTargetDef, swimlaneField, countsAreWindowed, laneCounts, swimlaneCounts, canDropCard, dropBlockedReason, onLaneRowsChange }: KanbanBoardProps & { dnd: ReturnType<typeof useDnd> | null }) {
14361468
const { t } = useKanbanT()
14371469
// The prefix of each swimlane cell's drop-reason element id (#27099).
14381470
const dropReasonIdBase = React.useId()
@@ -2246,7 +2278,10 @@ function KanbanBoardInner({ columns, onCardMove, onCardClick, className, dnd, qu
22462278
<span className={cn("transition-transform text-xs", isCollapsed ? "" : "rotate-90")}>▶</span>
22472279
<span className=" text-xs font-medium text-muted-foreground uppercase tracking-wider">{laneLabel}</span>
22482280
<span className=" text-xs text-muted-foreground">
2249-
{recordsSettled ? `(${laneCountLabel(laneCardCount, !swimlaneCountIsWhole(laneCounts, boardColumns, countsAreWindowed))})` : <LaneCountPending />}
2281+
{recordsSettled
2282+
? `(${countText(swimlaneRowCount(swimlaneCounts, lane, laneCardCount)
2283+
?? { count: laneCardCount, atLeast: !swimlaneCountIsWhole(laneCounts, boardColumns, countsAreWindowed) })})`
2284+
: <LaneCountPending />}
22502285
</span>
22512286
</button>
22522287

‎ui/packages/plugin-kanban/src/ObjectKanban.swimlaneStatistic-37326.test.tsx‎

Lines changed: 112 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,29 @@ const deals = (stage: string, n: number): Row[] =>
8989
// 20 open deals (amounts 0 … 19, sum 190), 4 won (sum 6).
9090
const ROWS = [...deals('open', 20), ...deals('won', 4)];
9191

92-
/** A grouped aggregate over `rows` as the platform's door answers it. */
93-
function groupHeadersOver(rows: Row[]) {
92+
/**
93+
* A grouped aggregate over `rows` as the platform's door answers it: one group
94+
* per distinct value of every `groupBy` key (objectstack#37844 asks two).
95+
* `cut` answers the first two groups and `hasMore`, as the door cuts a grouped
96+
* read to its default page size.
97+
*/
98+
function groupHeadersOver(rows: Row[], cut = false) {
9499
const store = new ValueDataSource<Row>({ items: rows });
95100
return vi.fn(async (_object: string, query: any) => {
96101
const kept = extractRecords(
97102
await store.find(OBJECT, { ...(query.where ? { $filter: query.where } : {}), $top: 100000 } as any),
98103
) as Row[];
99-
const key = query.groupBy[0];
100-
const groups = new Map<unknown, Row[]>();
104+
const keys: string[] = query.groupBy;
105+
const groups = new Map<string, { values: unknown[]; members: Row[] }>();
101106
for (const row of kept) {
102-
const k = row[key] ?? null;
103-
groups.set(k, [...(groups.get(k) ?? []), row]);
107+
const values = keys.map((key) => row[key] ?? null);
108+
const id = JSON.stringify(values);
109+
const group = groups.get(id) ?? { values, members: [] };
110+
group.members.push(row);
111+
groups.set(id, group);
104112
}
105-
return [...groups].map(([k, members]) => {
106-
const out: Row = { [key]: k };
113+
const answer: Row[] & { hasMore?: boolean } = [...groups.values()].map(({ values: k, members }) => {
114+
const out: Row = Object.fromEntries(keys.map((key, i) => [key, k[i]]));
107115
for (const agg of query.aggregations) {
108116
const values = members.map((m) => m[agg.field]) as number[];
109117
out[agg.alias] = agg.function === 'count'
@@ -118,10 +126,14 @@ function groupHeadersOver(rows: Row[]) {
118126
}
119127
return out;
120128
});
129+
if (!cut) return answer;
130+
const partial: Row[] & { hasMore?: boolean } = answer.slice(0, 2);
131+
partial.hasMore = true;
132+
return partial;
121133
});
122134
}
123135

124-
function adapter(rows: Row[], door: 'answers' | 'refuses' = 'answers') {
136+
function adapter(rows: Row[], door: 'answers' | 'refuses' | 'cuts' | 'none' = 'answers') {
125137
const store = new ValueDataSource<Row>({ items: rows });
126138
return {
127139
find: vi.fn(async (object: string, params: any) => store.find(object, params)),
@@ -130,19 +142,23 @@ function adapter(rows: Row[], door: 'answers' | 'refuses' = 'answers') {
130142
update: vi.fn(),
131143
delete: vi.fn(),
132144
getObjectSchema: vi.fn(async () => DEF),
133-
queryGroupHeaders: door === 'answers'
134-
? groupHeadersOver(rows)
135-
: vi.fn(async () => { throw Object.assign(new Error('forbidden'), { status: 403 }); }),
145+
...(door === 'none'
146+
? {}
147+
: {
148+
queryGroupHeaders: door === 'refuses'
149+
? vi.fn(async () => { throw Object.assign(new Error('forbidden'), { status: 403 }); })
150+
: groupHeadersOver(rows, door === 'cuts'),
151+
}),
136152
} as any;
137153
}
138154

139155
const SWIMLANES = { swimlaneField: 'region' };
140156

141-
function mount(source: any, summary: unknown, handed?: { rows: Row[] }) {
157+
function mount(source: any, summary: unknown, handed?: { rows: Row[] }, extra: Record<string, unknown> = {}) {
142158
return render(
143159
<SchemaRendererProvider dataSource={source}>
144160
<ObjectKanban
145-
schema={{ type: 'object-kanban', objectName: OBJECT, groupBy: 'stage', cardFields: ['amount'], summary, ...SWIMLANES } as any}
161+
schema={{ type: 'object-kanban', objectName: OBJECT, groupBy: 'stage', cardFields: ['amount'], summary, ...SWIMLANES, ...extra } as any}
146162
dataSource={source}
147163
{...(handed ? { data: handed.rows, laneStatisticQuery: { search: '', reloadKey: '0' } } : {})}
148164
/>
@@ -189,22 +205,30 @@ describe('a swimlane board draws the view\'s lane statistic (objectstack#37326)'
189205
expect(footRow(container)!.textContent).toContain('Count');
190206
});
191207

192-
it('(c) on a page handed in with its query, a column counts the whole lane and a swimlane row is marked a floor', async () => {
208+
it('(c) on a page handed in with its query, a column counts the whole lane and a swimlane row its whole swimlane (objectstack#37844)', async () => {
193209
const source = adapter(ROWS);
194-
// The page: 3 open deals, 1 won; the query holds 20 and 4.
210+
// The page: 3 open deals, 1 won; the query holds 20 and 4, 12 north and 12 south.
195211
const { container } = mount(source, { type: 'sum', field: 'amount' }, { rows: [...deals('open', 3), ...deals('won', 1)] });
196212
await waitFor(() => expect(columnCounts(container)).toEqual(['20', '4']));
197213
expect(footFigures(container)).toEqual(['190', '6']);
198-
// north holds open-000, open-002, won-000 on the page; south open-001.
199-
expect(swimlaneCounts(container)).toEqual(['3+', '1+']);
200-
expect(source.queryGroupHeaders).toHaveBeenCalledTimes(1);
214+
// The page holds 3 north and 1 south; the swimlane read counts 12 and 12.
215+
await waitFor(() => expect(swimlaneCounts(container)).toEqual(['12', '12']));
216+
// The lane read, then ONE read on both keys, counting each group.
217+
expect(source.queryGroupHeaders).toHaveBeenCalledTimes(2);
218+
expect(source.queryGroupHeaders.mock.calls[1][1]).toEqual({
219+
groupBy: ['stage', 'region'],
220+
aggregations: [{ function: 'count', alias: 'count' }],
221+
});
201222
expect(source.find).not.toHaveBeenCalled();
202223
});
203224

204-
it('(d) CONTROL: handed every record of its query, every count is bare', async () => {
205-
const { container } = mount(adapter(ROWS), undefined, { rows: ROWS });
225+
it('(d) CONTROL: handed every record of its query, every count is bare, and no swimlane read is asked', async () => {
226+
const source = adapter(ROWS);
227+
const { container } = mount(source, undefined, { rows: ROWS });
206228
await waitFor(() => expect(columnCounts(container)).toEqual(['20', '4']));
207229
expect(swimlaneCounts(container)).toEqual(['12', '12']);
230+
expect(source.queryGroupHeaders).toHaveBeenCalledTimes(1);
231+
expect(source.queryGroupHeaders.mock.calls[0][1].groupBy).toEqual(['stage']);
208232
});
209233

210234
it('(e) a refused read marks the page\'s column counts `+`, and draws no figure', async () => {
@@ -216,10 +240,13 @@ describe('a swimlane board draws the view\'s lane statistic (objectstack#37326)'
216240
expect(footFigures(container)).toEqual([null, null]);
217241
});
218242

219-
it('(f) CONTROL: with no statistic the board draws no foot row', async () => {
220-
const { container } = mount(adapter(ROWS), undefined);
243+
it('(f) CONTROL: with no statistic the board draws no foot row, and a board holding every record asks no read', async () => {
244+
const source = adapter(ROWS);
245+
const { container } = mount(source, undefined);
221246
await waitFor(() => expect(swimlaneCounts(container)).toEqual(['12', '12']));
222247
expect(footRow(container)).toBeNull();
248+
expect(columnCounts(container)).toEqual(['20', '4']);
249+
expect(source.queryGroupHeaders).not.toHaveBeenCalled();
223250
});
224251

225252
// 20 open deals (amounts 0 … 19), 4 won (0 … 3).
@@ -237,6 +264,68 @@ describe('a swimlane board draws the view\'s lane statistic (objectstack#37326)'
237264
});
238265
});
239266

267+
/**
268+
* objectstack#37844 — a board drawn in swimlanes that queries its own cards
269+
* through a window (`limit` 10 over the query's 24 records: 10 open, all
270+
* north and south alternating, 5 + 5) reads every swimlane row's count and
271+
* every column's count whole from ONE grouped read on both keys.
272+
*/
273+
describe('a windowed swimlane board counts each whole swimlane and column from one two-key read (objectstack#37844)', () => {
274+
const WINDOW = { limit: 10 };
275+
276+
it('a board that queries itself through a full window counts 12 and 12 swimlanes, 20 and 4 columns', async () => {
277+
const source = adapter(ROWS);
278+
const { container } = mount(source, undefined, undefined, WINDOW);
279+
await waitFor(() => expect(swimlaneCounts(container)).toEqual(['12', '12']));
280+
expect(columnCounts(container)).toEqual(['20', '4']);
281+
// The window: 10 cards, all open.
282+
expect(container.querySelectorAll('[role="listitem"]')).toHaveLength(10);
283+
expect(source.find).toHaveBeenCalledTimes(1);
284+
expect(source.queryGroupHeaders).toHaveBeenCalledTimes(1);
285+
expect(source.queryGroupHeaders.mock.calls[0][1]).toEqual({
286+
groupBy: ['stage', 'region'],
287+
aggregations: [{ function: 'count', alias: 'count' }],
288+
});
289+
});
290+
291+
it('reads under the board\'s own filter', async () => {
292+
const source = adapter(ROWS);
293+
// amount >= 2: open 2 … 19 (18: 9 north, 9 south), won 2, 3 (north, south).
294+
const { container } = mount(source, undefined, undefined, { ...WINDOW, filter: [['amount', '>=', 2]] });
295+
await waitFor(() => expect(swimlaneCounts(container)).toEqual(['10', '10']));
296+
expect(columnCounts(container)).toEqual(['18', '2']);
297+
expect(source.queryGroupHeaders.mock.calls[0][1].where).toBeDefined();
298+
});
299+
300+
it.each([
301+
['has no grouped door', 'none'],
302+
['refuses the read', 'refuses'],
303+
['cuts the read (hasMore)', 'cuts'],
304+
] as const)('CONTROL: when the source %s, the window\'s counts stay marked `+`', async (_, door) => {
305+
const source = adapter(ROWS, door);
306+
const { container } = mount(source, undefined, undefined, WINDOW);
307+
if (door !== 'none') await waitFor(() => expect(source.queryGroupHeaders).toHaveBeenCalledTimes(1));
308+
await waitFor(() => expect(swimlaneCounts(container)).toEqual(['5+', '5+']));
309+
expect(columnCounts(container)).toEqual(['10+', '0+']);
310+
});
311+
312+
it('CONTROL: a window the query does not fill asks no read and writes bare counts', async () => {
313+
const source = adapter(ROWS);
314+
const { container } = mount(source, undefined, undefined, { limit: 50 });
315+
await waitFor(() => expect(swimlaneCounts(container)).toEqual(['12', '12']));
316+
expect(columnCounts(container)).toEqual(['20', '4']);
317+
expect(source.queryGroupHeaders).not.toHaveBeenCalled();
318+
});
319+
320+
it('CONTROL: a page handed in whose lane read is cut asks no swimlane read; its rows stay marked `+`', async () => {
321+
const source = adapter(ROWS, 'cuts');
322+
const { container } = mount(source, undefined, { rows: [...deals('open', 3), ...deals('won', 1)] });
323+
await waitFor(() => expect(source.queryGroupHeaders).toHaveBeenCalledTimes(1));
324+
await waitFor(() => expect(swimlaneCounts(container)).toEqual(['3+', '1+']));
325+
expect(source.queryGroupHeaders).toHaveBeenCalledTimes(1);
326+
});
327+
});
328+
240329
describe('a swimlane board handed one page draws no column past the page (objectstack#37718)', () => {
241330
const ACCOUNT_DEF = {
242331
...DEF,

0 commit comments

Comments
 (0)