Skip to content

refactor(web): share display formatters and HTTP status colours - #547

Open
sisantaChhatoi wants to merge 1 commit into
BaaRai:mainfrom
sisantaChhatoi:refactor/shared-formatters
Open

sisantaChhatoi wants to merge 1 commit into
BaaRai:mainfrom
sisantaChhatoi:refactor/shared-formatters

Conversation

@sisantaChhatoi

Copy link
Copy Markdown

Part 1 of the series described in #546.

Problem

Display helpers were copied across the frontend, and the copies drifted apart:

  • fmtBytes / fmtSize / formatArchiveBytes: 10 copies with 5 different behaviours (some stopped at MB, so 2 GiB showed as "2048.0 MB"; empty values showed as "—" or "0 B")
  • fmtTokens: 7 copies with 3 behaviours ("25k" vs "25.0k"; one had no "M", so 2.5M showed as "2500.0k")
  • fmtTime / fmtDateTime: 11 copies of the same month-day-time format
  • fmtDuration (ms): 2 identical copies, plus 1 unused copy in the chat page
  • statusTone (HTTP status → text colour): 4 identical copies, one of them exported from http-code-block

Change

  • src/lib/format.ts: formatBytes (with an empty option), formatTokens, formatDurationMs, formatShortDateTime (seconds / year / empty options)
  • src/lib/status.ts: httpStatusTone, next to the other status-to-colour mappings
  • 21 files use them instead of local copies; the unused copies are removed

Kept local on purpose, because their formats are genuinely different: the ISO timestamps in logs, the full-locale date on the finding detail page, the translated seconds-based task duration, and the unix-seconds task time. The dashboard's statusColor is also unchanged (different shades).

Behaviour change

Each value now renders the same way everywhere:

  • sizes of 1 GiB and up show as GB (e.g. "2.0 GB" instead of "2048.0 MB")
  • the dashboard and intercept pages show "25k" instead of "25.0k"
  • LLM records show "2.5M" instead of "2500.0k"

Tested

  • npx tsc --noEmit
  • npm run test:polling, test:replay, test:upload, test:traffic-stream, test:tamper-hooks, test:i18n, test:bash, and node --test src/lib/*.test.mjs
  • next build
  • spot-checked the outputs: 0 → "0 B", 1536 → "1.5 KB", 2 GiB → "2.0 GB", 1234 → "1.2k", 25000 → "25k", 185000 ms → "3m05s"

🤖 Generated with Claude Code

The same helpers were copied across the frontend: fmtBytes / fmtSize /
formatArchiveBytes (10 copies, 5 behaviours), fmtTokens (7 copies, 3
behaviours), fmtTime / fmtDateTime (11 copies), fmtDuration and statusTone
(4 identical copies). They now live in one place:

- src/lib/format.ts: formatBytes, formatTokens, formatDurationMs,
  formatShortDateTime (with seconds / year / empty options)
- src/lib/status.ts: httpStatusTone

Intentionally distinct formats stay local (ISO log timestamps, full-locale
finding date, the translated seconds-based task duration, unix task times).

Visible changes from using one canonical output each:
- sizes of 1 GiB and up show as GB instead of thousands of MB
- dashboard / intercept token counts show "25k" instead of "25.0k"
- LLM records show "2.5M" instead of "2500.0k"

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sisantaChhatoi
sisantaChhatoi force-pushed the refactor/shared-formatters branch from 131e41c to a183871 Compare November 25, 2026 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant