Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

Smart Results polish: files cut note, Intl totals, every bar label at 360 px (#235)

Oleksandr Ivanchenko61ea8caebd28
diff --git a/frontend/src/components/results/Chart.tsx b/frontend/src/components/results/Chart.tsx
index fbadd2e1..5ba31056 100644
--- a/frontend/src/components/results/Chart.tsx
+++ b/frontend/src/components/results/Chart.tsx
@@ -153,9 +153,9 @@ export const Chart: React.FC<RendererProps<ChartBlock>> = ({ block }) => {
const xPos = (i: number) => (isBar ? PAD.left + (i + 0.5) * xStep : PAD.left + (n > 1 ? i * xStep : PLOT_W / 2));
// Bars: every category is named when its band fits a shortened label (5 bars at 390 px do); lines thin out.
- const MIN_BAND_LABEL = 46;
+ const MIN_BAND_LABEL = 28;
const labelEvery = isBar ? Math.max(1, Math.ceil(MIN_BAND_LABEL / xStep)) : Math.max(1, Math.ceil(n / (narrow ? 4 : 10)));
- const labelChars = isBar ? Math.max(4, Math.floor((xStep * labelEvery - 6) / 6)) : 10;
+ const labelChars = isBar ? Math.max(3, Math.floor((xStep * labelEvery - 4) / 6)) : 10;
const activePoint = active ? block.points[active.pointIdx] : null;
const activeSeries = active ? block.series[active.seriesIdx] : null;
@@ -244,7 +244,14 @@ export const Chart: React.FC<RendererProps<ChartBlock>> = ({ block }) => {
fontSize={11}
fill="#a1a1aa"
>
- {p.x.length > labelChars ? `${p.x.slice(0, labelChars - 1)}…` : p.x}
+ {p.x.length > labelChars ? (
+ <>
+ <title>{p.x}</title>
+ {`${p.x.slice(0, labelChars - 1)}…`}
+ </>
+ ) : (
+ p.x
+ )}
</text>
);
})}
diff --git a/frontend/src/components/results/Files.tsx b/frontend/src/components/results/Files.tsx
index e0cba441..624450e0 100644
--- a/frontend/src/components/results/Files.tsx
+++ b/frontend/src/components/results/Files.tsx
@@ -8,7 +8,7 @@ function validCut(v: Record<string, unknown>, keys: ('total_rows' | 'total_cols'
const cut: PreviewCut = {};
for (const k of keys) {
if (v[k] === undefined || v[k] === null) continue;
- if (!isIndex(v[k])) return undefined;
+ if (!isIndex(v[k]) || !Number.isSafeInteger(v[k])) return undefined;
cut[k] = v[k] as number;
}
if (v.truncated !== undefined && v.truncated !== null) {
diff --git a/frontend/src/smartResults.ts b/frontend/src/smartResults.ts
index 7ff4e4a8..1c8e66ed 100644
--- a/frontend/src/smartResults.ts
+++ b/frontend/src/smartResults.ts
@@ -196,18 +196,26 @@ export interface FileItem {
* "Preview, file is longer" when only `truncated` is known. Null for a complete preview. The shown
* counts are the rows and lines actually drawn; the totals are the server's.
*/
+const TOTAL_FULL = new Intl.NumberFormat('en');
+const TOTAL_COMPACT = new Intl.NumberFormat('en', { notation: 'compact', maximumFractionDigits: 1 });
+/** A count from the server, written with Intl: "143", "12,345", "1.2M". Never exponent notation. */
+export function formatTotal(n: number): string {
+ return n >= 1_000_000 ? TOTAL_COMPACT.format(n) : TOTAL_FULL.format(n);
+}
+
export function previewNote(preview: FilePreview): string | null {
if (!preview || preview.kind === 'image') return null;
const parts: string[] = [];
if (preview.kind === 'table') {
- if (preview.total_rows !== undefined && preview.total_rows > preview.rows.length) parts.push(`${preview.rows.length} of ${preview.total_rows} rows`);
- if (preview.total_cols !== undefined && preview.total_cols > preview.columns.length) parts.push(`${preview.columns.length} of ${preview.total_cols} columns`);
+ if (preview.total_rows !== undefined && preview.total_rows > preview.rows.length) parts.push(`${preview.rows.length} of ${formatTotal(preview.total_rows)} rows`);
+ if (preview.total_cols !== undefined && preview.total_cols > preview.columns.length) parts.push(`${preview.columns.length} of ${formatTotal(preview.total_cols)} columns`);
} else if (preview.total_lines !== undefined) {
const shown = preview.excerpt.replace(/\n$/, '').split('\n').length;
- if (preview.total_lines > shown) parts.push(`${shown} of ${preview.total_lines} lines`);
+ if (preview.total_lines > shown) parts.push(`${shown} of ${formatTotal(preview.total_lines)} lines`);
}
if (parts.length) return `Showing ${parts.join(', ')}`;
- return preview.truncated || preview.total_rows !== undefined || preview.total_cols !== undefined || preview.total_lines !== undefined ? 'Preview, file is longer' : null;
+ // A total that is not larger than what is drawn (the server may send totals for every file) is not a cut.
+ return preview.truncated ? 'Preview, file is longer' : null;
}
/** 1–10 files; source is always slot. */
diff --git a/frontend/tests/resultsChart.test.mjs b/frontend/tests/resultsChart.test.mjs
index 685f11c6..debf76ec 100644
--- a/frontend/tests/resultsChart.test.mjs
+++ b/frontend/tests/resultsChart.test.mjs
@@ -152,19 +152,25 @@ test('a value too large to scale abstains instead of drawing NaN coordinates (SR
assert.equal(m.validChart({ ...bar, points: bar.points.map((p, i) => (i === 0 ? { ...p, values: p.values.map(() => 1e308) } : p)) }), null);
});
-test('every bar category is named at a 390 px card width, shortened to fit its band', async () => {
- const real = Object.getOwnPropertyDescriptor(dom.window.HTMLElement.prototype, 'clientWidth');
- Object.defineProperty(dom.window.HTMLElement.prototype, 'clientWidth', { configurable: true, get: () => 300 });
- // The chart measures its box through a ResizeObserver; jsdom has none, so a no-op one lets the first measure run.
- globalThis.ResizeObserver = class { observe() {} disconnect() {} };
- try {
- const host = await mount(barBlock());
- const labels = [...host.querySelectorAll('svg[role="img"] text')].filter((t) => t.getAttribute('y') === '240');
- assert.equal(labels.length, barBlock().points.length, 'all 5 categories are labelled, not 3');
- assert.ok(labels.every((t) => t.textContent.length <= 7), 'shortened to fit its band: ' + labels.map((t) => t.textContent).join('|'));
- } finally {
- delete globalThis.ResizeObserver;
- if (real) Object.defineProperty(dom.window.HTMLElement.prototype, 'clientWidth', real);
- else delete dom.window.HTMLElement.prototype.clientWidth;
- }
-});
+for (const width of [300, 270]) {
+ test(`every bar category is named at a ${width} px chart width (390 and 360 px phones), shortened to fit its band`, async () => {
+ const real = Object.getOwnPropertyDescriptor(dom.window.HTMLElement.prototype, 'clientWidth');
+ Object.defineProperty(dom.window.HTMLElement.prototype, 'clientWidth', { configurable: true, get: () => width });
+ // The chart measures its box through a ResizeObserver; jsdom has none, so a no-op one lets the first measure run.
+ globalThis.ResizeObserver = class { observe() {} disconnect() {} };
+ try {
+ const host = await mount(barBlock());
+ const labels = [...host.querySelectorAll('svg[role="img"] text')].filter((t) => t.getAttribute('y') === '240');
+ assert.equal(labels.length, barBlock().points.length, 'all 5 categories are labelled, not 3');
+ // A shortened label keeps its full name in a <title> (hover); the drawn text is the last node.
+ const drawn = labels.map((t) => t.lastChild.textContent);
+ assert.ok(drawn.every((t) => t.length <= 7), 'shortened to fit its band: ' + drawn.join('|'));
+ const cut = labels.filter((t) => t.querySelector('title'));
+ assert.ok(cut.length > 0 && cut.every((t) => t.querySelector('title').textContent.length > t.lastChild.textContent.length), 'a shortened label keeps its full name');
+ } finally {
+ delete globalThis.ResizeObserver;
+ if (real) Object.defineProperty(dom.window.HTMLElement.prototype, 'clientWidth', real);
+ else delete dom.window.HTMLElement.prototype.clientWidth;
+ }
+ });
+}
diff --git a/frontend/tests/smartResultsIris.test.mjs b/frontend/tests/smartResultsIris.test.mjs
index 854c3623..3dc57293 100644
--- a/frontend/tests/smartResultsIris.test.mjs
+++ b/frontend/tests/smartResultsIris.test.mjs
@@ -142,11 +142,31 @@ test('files: Copy data on a cut preview says it copies the preview only; complet
assert.match(m.blockCsv(block('files')), /copies the preview only/);
});
+test('previewNote: totals are written with Intl, never as exponents; unsafe integers make the block fall back', () => {
+ const t = (extra) => ({ kind: 'table', columns: ['a'], rows: [['1']], ...extra });
+ assert.equal(m.previewNote(t({ total_rows: 143 })), 'Showing 1 of 143 rows');
+ assert.equal(m.previewNote(t({ total_rows: 12345 })), 'Showing 1 of 12,345 rows');
+ assert.equal(m.previewNote(t({ total_rows: 1_200_000 })), 'Showing 1 of 1.2M rows');
+ assert.doesNotMatch(m.previewNote(t({ total_rows: 9_007_199_254_740_991 })), /e\+/i);
+ const b = block('files');
+ const mk = (extra) => ({ ...b, items: [{ ...b.items[0], preview: { kind: 'table', columns: ['a'], rows: [['x']], ...extra } }] });
+ assert.equal(m.validFiles(mk({ total_rows: 1e308 })), null, '1e308 is not a count');
+ assert.equal(m.validFiles(mk({ total_rows: 9_007_199_254_740_993 })), null, 'beyond the safe integers');
+ assert.ok(m.validFiles(mk({ total_rows: 9_007_199_254_740_991 })));
+});
+
test('previewNote: counts the rows and lines drawn, and says nothing when the totals do not exceed them', () => {
const t = (extra) => ({ kind: 'table', columns: ['a', 'b', 'c'], rows: [['1', '2', '3']], ...extra });
assert.equal(m.previewNote(t({ total_rows: 20, total_cols: 12 })), 'Showing 1 of 20 rows, 3 of 12 columns');
assert.equal(m.previewNote(t({ total_cols: 12 })), 'Showing 3 of 12 columns');
- assert.equal(m.previewNote(t({ total_rows: 1 })), 'Preview, file is longer', 'a total that is not larger than the rows drawn still marks a cut');
+ assert.equal(m.previewNote(t({ total_rows: 1 })), null, 'a total equal to the rows drawn is not a cut');
+ assert.equal(m.previewNote(t({ total_rows: 1, total_cols: 3 })), null, 'totals on a complete file say nothing');
+ assert.equal(m.previewNote(t({ total_rows: 0, total_cols: 1 })), null, 'a total below what is drawn says nothing');
+ assert.equal(m.previewNote(t({ total_rows: 20, total_cols: 3 })), 'Showing 1 of 20 rows', 'a rows-only cut does not claim columns were cut');
+ assert.equal(m.previewNote(t({ total_rows: 1, total_cols: 12 })), 'Showing 3 of 12 columns', 'a columns-only cut names columns only');
+ assert.equal(m.previewNote(t({ truncated: true })), 'Preview, file is longer');
+ assert.equal(m.previewNote(t({ total_rows: 1, truncated: true })), 'Preview, file is longer', 'truncated with no larger total');
+ assert.equal(m.previewNote({ kind: 'text', excerpt: 'a\nb', total_lines: 2 }), null);
assert.equal(m.previewNote(t({})), null);
assert.equal(m.previewNote({ kind: 'text', excerpt: 'a\nb\n', total_lines: 9 }), 'Showing 2 of 9 lines', 'a trailing newline is not a line');
assert.equal(m.previewNote({ kind: 'image' }), null);