diff --git a/apps/yaak-proxy/components/FlowListHeader.tsx b/apps/yaak-proxy/components/FlowListHeader.tsx index 869cc7805..7b4af4aa7 100644 --- a/apps/yaak-proxy/components/FlowListHeader.tsx +++ b/apps/yaak-proxy/components/FlowListHeader.tsx @@ -262,7 +262,7 @@ export const FlowListHeader = memo(function FlowListHeader({ sort={sort} onSort={onSort} onResizeStart={() => { - startWidth.current = c.current; + startWidth.current = shownWidth(gridRef.current, c); }} onResizeMove={(dx) => setPrefs((p) => resizeColumn(p, c.id, startWidth.current + dx))} onResetWidth={() => setPrefs((p) => resetWidth(p, c.id))} @@ -279,6 +279,20 @@ export const FlowListHeader = memo(function FlowListHeader({ ); }); +/** + * A column's content width as laid out, so a resize starts from what shows: a numeric column's + * can be wider than its width in pixels, at its default or its least width, at a larger + * interface size + */ +function shownWidth(grid: HTMLElement | null, c: Column): number { + if (c.fitChars == null) return c.current; + const cell = grid?.querySelector(`[data-column="${c.id}"]`); + if (cell == null) return c.current; + const style = getComputedStyle(cell); + const padding = Number.parseFloat(style.paddingLeft) + Number.parseFloat(style.paddingRight); + return cell.getBoundingClientRect().width - padding; +} + interface HeaderCellProps { column: Column; sort: FlowSortKey[]; diff --git a/apps/yaak-proxy/harness/FlowPanesHarness.tsx b/apps/yaak-proxy/harness/FlowPanesHarness.tsx index 75889c098..d5e427c19 100644 --- a/apps/yaak-proxy/harness/FlowPanesHarness.tsx +++ b/apps/yaak-proxy/harness/FlowPanesHarness.tsx @@ -11,8 +11,9 @@ import { type FlowFixture, fixtures } from "./fixtures"; /** * Dev only: captured flows in the detail card, over fixtures. Open * `/harness.html?flow=0&theme=dark`; `&list` puts the flow list beside the card (its halves - * stacked), `&below` puts it above (side by side), and `&tab=info` opens the response half on a - * tab, as though it were chosen by hand for flows of that shape. + * stacked), `&below` puts it above (side by side), `&tab=info` opens the response half on a + * tab, as though it were chosen by hand for flows of that shape, and `&numbers` gives the flows + * the widest sizes and times. */ export function FlowPanesHarness() { const params = new URLSearchParams(location.search); @@ -40,13 +41,28 @@ export function FlowPanesHarness() { ); } +const NUMBERS = [ + { responseSize: 999_949, requestSize: 1000, elapsedMs: 3_599_000, elapsedHeadersMs: 999 }, + { responseSize: 124_700, requestSize: 999_949, elapsedMs: 999, elapsedHeadersMs: 3_599_000 }, + { responseSize: 20_200, requestSize: 812, elapsedMs: 9_949, elapsedHeadersMs: 59_499 }, + { responseSize: 999_949_000, requestSize: 41_800, elapsedMs: 59_999_000, elapsedHeadersMs: 600 }, + { responseSize: 1000, requestSize: 7_600_000, elapsedMs: 234, elapsedHeadersMs: 5_900 }, + { responseSize: 9_000, requestSize: 999_949_000_000, elapsedMs: 400_000, elapsedHeadersMs: 12 }, +]; + function FixtureCard({ fixture }: { fixture: FlowFixture }) { const params = new URLSearchParams(location.search); const store = useStore(); const setWindow = useSetAtom(flowWindowAtom); const [ready, setReady] = useState(false); useLayoutEffect(() => { - const flows = fixtures.map((f, i) => ({ ...f.flow, number: 1401 + i })); + // `&numbers`: sizes and times of every unit, the widest of each among them + const numbers = params.has("numbers"); + const flows = fixtures.map((f, i) => ({ + ...f.flow, + number: 1401 + i, + ...(numbers && f.flow.elapsedMs != null && NUMBERS[i % NUMBERS.length]), + })); const tab = params.get("tab"); if (tab != null) { browserTabMemory()?.setItem(learnedTabKey("response", flowShape(fixture.flow)), tab); diff --git a/apps/yaak-proxy/lib/columns.test.ts b/apps/yaak-proxy/lib/columns.test.ts index 210c31a98..a49e22401 100644 --- a/apps/yaak-proxy/lib/columns.test.ts +++ b/apps/yaak-proxy/lib/columns.test.ts @@ -1,11 +1,17 @@ +import { formatSize } from "@yaakapp-internal/lib/formatSize"; import type { Flow, FlowSortKey } from "@yaakapp-internal/proxy-lib"; +// The formatter alone: the package index reaches for the window as it loads +import { formatMillis } from "../../../packages/ui/src/components/tags/HttpResponseDurationTag"; import { describe, expect, test } from "vite-plus/test"; import customSortFixture from "../../../crates-proxy/yaak-proxy-lib/tests/fixtures/custom_column_sort.json"; import { addCustomColumn, addSortKey, allColumns, + BASE_FONT_SIZE, + CHAR_REM, cellFilterTerm, + columnDef, columnShifts, type ColumnPrefs, customPath, @@ -35,6 +41,7 @@ import { toggleSortKey, urlParts, } from "./columns"; +import { DEFAULT_SETTINGS_VALUES } from "./settings"; function flow(number: number, extra: Partial = {}): Flow { return { @@ -109,10 +116,67 @@ describe("shownColumns", () => { prefs = resizeColumn(prefs, "path", 500); const columns = shownColumns(prefs); const pad = (w: number) => `calc(${w}px + 0.75rem)`; + // Size and Time at their defaults, which grow with the interface size, and never narrower + // than their widest values + const size = "calc(max(4.8rem, 4rem) + 0.75rem)"; + const time = `calc(max(${64 / 15}rem, 4rem) + 0.75rem)`; expect(gridTemplate(columns)).toBe( - ["24px", ...[46, 20, 50, 240, 500, 120, 46, 50, 72, 64].map(pad)].join(" "), + ["24px", ...[46, 20, 50, 240, 500, 120, 46, 50].map(pad), size, time].join(" "), ); - expect(gridWidth(columns)).toBe("calc(1232px + 10 * 0.75rem)"); + expect(gridWidth(columns)).toBe( + `calc(1096px + max(4.8rem, 4rem) + max(${64 / 15}rem, 4rem) + 10 * 0.75rem)`, + ); + // Resized, a numeric column keeps the pixels it was given + const resized = shownColumns(resizeColumn(prefs, "size", 90)); + expect(gridTemplate(resized)).toContain("calc(max(90px, 4rem) + 0.75rem)"); + }); +}); + +describe("numeric column widths", () => { + // Every format the cells show: each unit's widest, and around each rounding edge + const sizes = [0, 9, 999, 1000, 1001, 1049, 99_949, 99_951, 999_949, 999_999, 1_000_001]; + const giga = 999_949_000_000; + const durations = [0, 999, 1000, 9_949, 9_999, 59_499, 59_999, 60_000, 3_599_499, 59_999_999]; + const numeric = ["size", "requestSize", "time", "latency"] as const; + + test("the formats' widest values are as many characters as each numeric column holds", () => { + const widest = (values: string[]) => Math.max(...values.map((v) => v.length)); + const sizeChars = widest([...sizes, giga].map(formatSize)); + const timeChars = widest(durations.map(formatMillis)); + expect(sizeChars).toBe(8); + expect(timeChars).toBe(8); + for (const id of numeric) { + const def = columnDef(id); + const chars = id === "size" || id === "requestSize" ? sizeChars : timeChars; + expect(def.fitChars).toBeGreaterThanOrEqual(chars); + // In pixels at the default interface size, as the row's font sets them + const fit = (def.fitChars ?? 0) * CHAR_REM * BASE_FONT_SIZE; + expect(def.minWidth).toBeGreaterThanOrEqual(fit); + expect(def.width).toBeGreaterThanOrEqual(fit); + } + }); + + test("the room for a character fits JetBrains Mono at text-xs", () => { + // A monospace advance of 0.6em, at 0.8rem + expect(CHAR_REM).toBeGreaterThanOrEqual(0.6 * 0.8); + expect(BASE_FONT_SIZE).toBe(DEFAULT_SETTINGS_VALUES.interfaceFontSize); + }); + + test("defaults fit each label sorted, with the sort's arrow and place", () => { + // Measured in the harness at the default interface size, sorted by four columns + const header = { size: 46, requestSize: 96, time: 50, latency: 67 }; + for (const id of numeric) expect(columnDef(id).width).toBeGreaterThanOrEqual(header[id]); + }); + + test("a stored width narrower than the least is raised, and a wider one kept", () => { + const parsed = parseColumnPrefs({ widths: { size: 44, time: 50, requestSize: 120, host: 20 } }); + expect(parsed.widths).toEqual({ size: 60, time: 60, requestSize: 120, host: 60 }); + expect(shownColumns(parsed).find((c) => c.id === "size")?.current).toBe(60); + expect(resizeColumn(defaultColumnPrefs, "latency", 10).widths).toEqual({ latency: 60 }); + // Reset, the defaults are back + const reset = shownColumns(resetWidths(parsed)).find((c) => c.id === "size"); + expect(reset).toMatchObject({ current: 72, resized: false }); + expect(resetWidth(parsed, "time").widths).toEqual({ size: 60, requestSize: 120, host: 60 }); }); }); diff --git a/apps/yaak-proxy/lib/columns.ts b/apps/yaak-proxy/lib/columns.ts index ffb150a8a..8f1f50f6b 100644 --- a/apps/yaak-proxy/lib/columns.ts +++ b/apps/yaak-proxy/lib/columns.ts @@ -23,6 +23,11 @@ export interface ColumnDef { defaultVisible: boolean; /** The header or query parameter a custom column shows: `header.x-request-id` */ path?: string; + /** + * A numeric column's widest value, in characters of the row's monospace font: it is never + * narrower than that at any interface size, and its default width grows with the interface size + */ + fitChars?: number; } const builtin = ( @@ -32,7 +37,26 @@ const builtin = ( minWidth: number, align: "left" | "right", defaultVisible: boolean, -): ColumnDef => ({ id, label, width, minWidth, align, defaultVisible }); + fitChars?: number, +): ColumnDef => ({ id, label, width, minWidth, align, defaultVisible, fitChars }); + +/** + * The widest size and duration a numeric cell shows, in characters: `999.9 KB` and `999m 59s`, + * from `formatSize` and `formatMillis` + */ +export const NUMERIC_FIT_CHARS = 8; + +/** + * Room for one character of a numeric cell, `font-mono text-xs`: 0.8rem, and a monospace advance + * of 0.6em, JetBrains Mono's and most others', with a little over for wider editor fonts + */ +export const CHAR_REM = 0.5; + +/** The default interface font size, `DEFAULT_SETTINGS_VALUES.interfaceFontSize`, widths are at */ +export const BASE_FONT_SIZE = 15; + +/** A numeric column's least width, in pixels at the default interface size: its widest value */ +const NUMERIC_MIN = NUMERIC_FIT_CHARS * CHAR_REM * BASE_FONT_SIZE; export const COLUMNS: ColumnDef[] = [ builtin("number", "#", 46, 30, "left", true), @@ -43,10 +67,11 @@ export const COLUMNS: ColumnDef[] = [ builtin("query", "Query", 120, 40, "left", true), builtin("status", "Status", 46, 34, "left", true), builtin("type", "Type", 50, 34, "left", true), - builtin("requestSize", "Request Size", 80, 44, "right", false), - builtin("size", "Size", 72, 44, "right", true), - builtin("time", "Time", 64, 44, "right", true), - builtin("latency", "Latency", 64, 44, "right", false), + // Each default fits its label sorted, with the sort's arrow and place, and its widest value + builtin("requestSize", "Request Size", 96, NUMERIC_MIN, "right", false, NUMERIC_FIT_CHARS), + builtin("size", "Size", 72, NUMERIC_MIN, "right", true, NUMERIC_FIT_CHARS), + builtin("time", "Time", 64, NUMERIC_MIN, "right", true, NUMERIC_FIT_CHARS), + builtin("latency", "Latency", 68, NUMERIC_MIN, "right", false, NUMERIC_FIT_CHARS), builtin("started", "Started", 92, 60, "left", false), builtin("remote", "Remote Address", 150, 60, "left", false), builtin("version", "HTTP Version", 70, 44, "left", false), @@ -172,6 +197,8 @@ export const defaultColumnPrefs: ColumnPrefs = { export interface Column extends ColumnDef { /** Its width now, in pixels of content */ current: number; + /** Whether the person set its width, rather than leaving it at its default */ + resized: boolean; } /** @@ -198,7 +225,11 @@ export function shownColumns(prefs: ColumnPrefs, unavailable: ColumnId[] = []): const all = allColumns(prefs).filter((c) => !unavailable.includes(c.id)); const shown = all.filter((c) => isVisible(prefs, c)); const columns = shown.length > 0 ? shown : all.filter((c) => c.id === "path"); - return columns.map((c) => ({ ...c, current: columnWidth(prefs, c.id) })); + return columns.map((c) => ({ + ...c, + current: columnWidth(prefs, c.id), + resized: prefs.widths[c.id] != null, + })); } /** Columns the platform has nothing for: App, where it cannot tell which app sent a flow */ @@ -211,16 +242,28 @@ export function columnWidth(prefs: ColumnPrefs, id: ColumnId): number { return Math.max(def.minWidth, Math.round(prefs.widths[id] ?? def.width)); } +/** + * A column's content width, as CSS. A numeric column's default grows with the interface size, + * and at any width it holds its widest value, which grows with the interface size too. + */ +function columnTrack(c: Column): string { + if (c.fitChars == null) return `${c.current}px`; + const width = c.resized ? `${c.current}px` : `${c.current / BASE_FONT_SIZE}rem`; + return `max(${width}, ${c.fitChars * CHAR_REM}rem)`; +} + /** The grid template for the header and every row: the mark, then each column and its padding */ export function gridTemplate(columns: Column[]): string { - const cells = columns.map((c) => `calc(${c.current}px + ${CELL_PADDING})`); + const cells = columns.map((c) => `calc(${columnTrack(c)} + ${CELL_PADDING})`); return [`${MARK_WIDTH}px`, ...cells].join(" "); } /** The width of a row's grid, as CSS: the list scrolls sideways when it is wider than the list */ export function gridWidth(columns: Column[]): string { - const px = columns.reduce((sum, c) => sum + c.current, MARK_WIDTH); - return `calc(${px}px + ${columns.length} * ${CELL_PADDING})`; + const fixed = columns.filter((c) => c.fitChars == null); + const px = fixed.reduce((sum, c) => sum + c.current, MARK_WIDTH); + const numeric = columns.filter((c) => c.fitChars != null).map((c) => ` + ${columnTrack(c)}`); + return `calc(${px}px${numeric.join("")} + ${columns.length} * ${CELL_PADDING})`; } /** Whether the picker may uncheck `id`: any column but the last one shown */ @@ -406,7 +449,10 @@ export function parseColumnPrefs(value: unknown): ColumnPrefs { const v = (visible as Record | undefined)?.[def.id]; if (typeof v === "boolean") prefs.visible[def.id] = v; const w = (widths as Record | undefined)?.[def.id]; - if (typeof w === "number" && Number.isFinite(w) && w > 0) prefs.widths[def.id] = w; + // Narrower than the column's least width, as a numeric column's was before it grew, rises to it + if (typeof w === "number" && Number.isFinite(w) && w > 0) { + prefs.widths[def.id] = Math.max(def.minWidth, w); + } } // Columns no longer there are dropped; those not listed follow the rest in their default order if (Array.isArray(order)) {