From f7db78c08f3e545c5efb24ad5da48087e8d5f726 Mon Sep 17 00:00:00 2001 From: Laura Date: Tue, 4 Nov 2025 22:47:27 +0100 Subject: [PATCH] improve time rendering --- static/js/chat.js | 4 ++-- static/js/lib.js | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/static/js/chat.js b/static/js/chat.js index a48753e..9e7e6d3 100644 --- a/static/js/chat.js +++ b/static/js/chat.js @@ -261,7 +261,7 @@ this.#_time = make("div", "time"); if (this.#time) { - this.#_time.textContent = `${this.#time.toFixed(1)}s`; + this.#_time.textContent = formatMilliseconds(this.#time * 1000); } _body.appendChild(this.#_time); @@ -868,7 +868,7 @@ this.#time = time; if (this.#time) { - this.#_time.textContent = `${this.#time.toFixed(1)}s`; + this.#_time.textContent = formatMilliseconds(this.#time * 1000); } } diff --git a/static/js/lib.js b/static/js/lib.js index 989f06f..ec94bf0 100644 --- a/static/js/lib.js +++ b/static/js/lib.js @@ -46,16 +46,25 @@ function escapeHtml(text) { return text.replace(/&/g, "&").replace(/"/g, """).replace(//g, ">"); } +const fracZerosRgx = /(?:(\.\d*?[1-9])0+|\.0+)$/; + +function round(num, digits) { + return num.toFixed(digits).replace(fracZerosRgx, "$1") || "0"; +} + function formatMilliseconds(ms) { if (ms < 1000) { return `${ms}ms`; } - if (ms < 10000) { - return `${(ms / 1000).toFixed(1)}s`; + if (ms < 60000) { + return `${round(ms / 1000, 1)}s`; } - return `${Math.round(ms / 1000)}s`; + const minutes = Math.floor(ms / 60000), + seconds = ms - minutes * 60000; + + return `${minutes}m ${round(seconds / 1000, 1)}s`; } function formatTimestamp(ts) {