feat(coach): move an answer's model and tokens behind a details icon

The tokens and model no longer sit as right-aligned text under a Coach
answer. The row is one left-aligned group: copy, read aloud, try again, a
details icon, then the time. The details icon is a focusable button whose
tooltip names the model and the token count; it opens on hover, on
keyboard focus and on a tap, and a second tap, a tap elsewhere or Escape
closes it. Strings in all seven locales; the joined tokens-with-model
string is gone.
This commit is contained in:
Marc-André Bombeck committed 2026-10-05 17:29:28 +02:00
1 parent 3bede8230b
commit 0c3de5bdfb
10 files changed
+204 -103

No files matched your search

@@ -2,9 +2,9 @@
* The one action row under a Coach message, and the follow-up chips that
* live in the answer's column.
*
* - Assistant: copy, read aloud, try again, then the time, in one left
* group; the tokens and model in the right group of the same row. No
* feedback thumbs, no evidence disclosure.
* - Assistant: copy, read aloud, try again, details, then the time, in one
* left-aligned row; the model and tokens live behind the details icon,
* not as text on the row. No feedback thumbs, no evidence disclosure.
* - User: copy, remember, time in one row; remember is an icon.
* - The time reads "14:32" today, "yesterday 14:32", or the date further
* back.
@@ -50,7 +50,7 @@ vi.mock("../read-aloud", async (importOriginal) => {
});
import { ChatBubble, areChatBubblePropsEqual } from "../chat-bubble";
import { answerMetaText, messageTimeText } from "../message-actions";
import { answerInfoLines, messageTimeText } from "../message-actions";
function render(node: React.ReactNode, locale: "en" | "de" = "en") {
return renderToStaticMarkup(
@@ -100,26 +100,27 @@ const ANSWER = {
describe("assistant action row", () => {
const html = render(<ChatBubble {...ANSWER} />);
const row = slot(html, "coach-answer-actions");
const left = slot(row, "coach-answer-actions-left");
it("lists copy, read aloud, try again and the time on the left, in that order", () => {
it("lists copy, read aloud, try again, details and the time, in that order", () => {
const order = [
"coach-copy-message",
"coach-read-aloud",
"coach-try-again",
"coach-answer-info",
"coach-message-time",
].map((name) => left.indexOf(`data-slot="${name}"`));
].map((name) => row.indexOf(`data-slot="${name}"`));
expect(order.every((at) => at > -1)).toBe(true);
expect([...order].sort((a, b) => a - b)).toEqual(order);
});
it("puts the tokens and the model on the right of the same row", () => {
const meta = slot(row, "coach-answer-meta");
expect(meta).toContain("1,234 tokens · gpt-4o-mini");
expect(meta).toContain('title="gpt-4o-mini"');
expect(meta).toContain("truncate");
expect(left).not.toContain("coach-answer-meta");
expect(row).toContain("justify-between");
it("keeps the model and tokens off the row, behind a named details button", () => {
expect(row).not.toContain("1,234");
expect(row).not.toContain("gpt-4o-mini");
expect(row).not.toContain("justify-between");
expect(row).not.toContain("coach-answer-meta");
const info = slot(row, "coach-answer-info");
expect(info).toMatch(/^<button type="button"/);
expect(info).toContain('aria-label="Answer details"');
});
it("has no thumbs, no evidence disclosure and no separate token line", () => {
@@ -129,19 +130,18 @@ describe("assistant action row", () => {
});
it("shows the time as text, the full date for screen readers", () => {
const time = slot(left, "coach-message-time");
const time = slot(row, "coach-message-time");
expect(time).toMatch(/^<time[^>]*dateTime="/);
expect(time).toContain("Message sent");
expect(time).toMatch(/<span aria-hidden="true">[^<]*\d:\d{2}[^<]*<\/span>/);
});
it("keeps an empty right group when the answer has no count", () => {
it("offers no details button when neither the model nor the count is known", () => {
const bare = render(
<ChatBubble {...ANSWER} tokensUsed={null} model={null} />,
);
expect(slot(bare, "coach-answer-meta")).toMatch(
/<p data-slot="coach-answer-meta"[^>]*><\/p>/,
);
expect(bare).toContain("coach-answer-actions");
expect(bare).not.toContain("coach-answer-info");
});
it("is absent on a refusal and while the answer is in flight", () => {
@@ -276,17 +276,29 @@ describe("messageTimeText", () => {
});
});
describe("answerMetaText", () => {
describe("answerInfoLines", () => {
const { t } = getServerTranslator("en");
it("formats the count for the locale and adds the model", () => {
expect(answerMetaText(1234, "gpt-4o", "en-US", t)).toBe(
"1,234 tokens · gpt-4o",
);
expect(answerMetaText(1234, null, "de-DE", t)).toBe("1.234 tokens");
it("names the model, then the count formatted for the locale", () => {
expect(answerInfoLines(1234, "gpt-4o", "en-US", t)).toEqual([
"Model: gpt-4o",
"1,234 tokens",
]);
expect(answerInfoLines(1234, null, "de-DE", t)).toEqual(["1.234 tokens"]);
});
it("is nothing without a count", () => {
expect(answerMetaText(null, "gpt-4o", "en-US", t)).toBeNull();
it("keeps the model without a count, and is empty without either", () => {
expect(answerInfoLines(null, "gpt-4o", "en-US", t)).toEqual([
"Model: gpt-4o",
]);
expect(answerInfoLines(null, null, "en-US", t)).toEqual([]);
});
it("speaks the reader's language", () => {
const de = getServerTranslator("de").t;
expect(answerInfoLines(1234, "gpt-4o", "de-DE", de)).toEqual([
"Modell: gpt-4o",
"1.234 Tokens",
]);
});
});
@@ -3,10 +3,10 @@
/**
* The one action row under a Coach message, for both roles.
*
* Assistant: a single line. Left, the icon actions in reading order (copy,
* read aloud, try again) followed by the time the answer was written; right,
* the tokens and model the answer cost, as meta. The right group truncates
* before the row would wrap, so the row is one line at 390 px too.
* Assistant: a single line, left-aligned. The icon actions in reading order
* (copy, read aloud, try again, details) followed by the time the answer was
* written. The details icon holds the model and the tokens the answer cost
* in a tooltip that opens on hover, on keyboard focus and on a tap.
*
* User: copy, remember, time, flush with the bubble's right edge.
*
@@ -15,7 +15,7 @@
* `opacity-0` rather than `invisible` keeps every control focusable, and
* focus reveals the row.
*/
import { useCallback, useState } from "react";
import { useCallback, useRef, useState } from "react";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { toast } from "sonner";
import {
@@ -23,11 +23,18 @@ import {
BookmarkPlus,
Check,
Copy,
Info,
Loader2,
RotateCcw,
} from "lucide-react";
import { cn } from "@/lib/utils";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { useFormatters, useTranslations } from "@/lib/i18n/context";
import { resolveIntlLocale } from "@/lib/format-locale";
import type { Formatters } from "@/lib/format-locale";
@@ -85,27 +92,30 @@ export function messageTimeText(
}
/**
* The answer's cost as one meta string: tokens with the model when both are
* known, tokens alone otherwise, nothing without a count. The count is the
* server's (`done.usage` live, `tokensUsed` on reload), formatted for the
* reader's locale.
* What the details tooltip says about an answer: the model that wrote it and
* the tokens it cost, one line each, in that order, each only when known. The
* count is the server's (`done.usage` live, `tokensUsed` on reload),
* formatted for the reader's locale. Empty when neither is known, and then
* the row offers no details icon at all.
*/
export function answerMetaText(
export function answerInfoLines(
tokens: number | null | undefined,
model: string | null | undefined,
locale: string,
t: Translate,
): string | null {
if (tokens == null) return null;
let count: string;
try {
count = new Intl.NumberFormat(locale).format(tokens);
} catch {
count = String(tokens);
): string[] {
const lines: string[] = [];
if (model) lines.push(t("insights.coach.answer.infoModel", { model }));
if (tokens != null) {
let count: string;
try {
count = new Intl.NumberFormat(locale).format(tokens);
} catch {
count = String(tokens);
}
lines.push(t("insights.coach.tokensUsed", { count }));
}
return model
? t("insights.coach.tokensUsedWithModel", { count, model })
: t("insights.coach.tokensUsed", { count });
return lines;
}
function CopyMessageButton({
@@ -170,6 +180,58 @@ function TryAgainButton({ onRegenerate }: { onRegenerate: () => void }) {
);
}
/**
* The model and token count of an answer, behind an info icon. A tooltip,
* opened the three ways a reader can reach it: hovering the icon, focusing
* it from the keyboard, and tapping it on a touch screen, where there is no
* hover. A second tap, a tap elsewhere or Escape closes it.
*
* The tap is handled here because the tooltip primitive closes on every
* click: the click toggles from the state the tooltip had when the pointer
* went down, before the primitive's own pointer-down close ran.
*/
function AnswerInfoButton({ lines }: { lines: string[] }) {
const { t } = useTranslations();
const [open, setOpen] = useState(false);
const openAtPointerDown = useRef<boolean | null>(null);
const label = t("insights.coach.answer.info");
return (
<TooltipProvider delayDuration={150}>
<Tooltip open={open} onOpenChange={setOpen}>
<TooltipTrigger asChild>
<button
type="button"
data-slot="coach-answer-info"
aria-label={label}
className={COACH_ICON_BUTTON}
onPointerDown={() => {
openAtPointerDown.current = open;
}}
onClick={(event) => {
// Keep the primitive from closing it again on this click.
event.preventDefault();
const was = openAtPointerDown.current;
openAtPointerDown.current = null;
setOpen(was === null ? (current) => !current : !was);
}}
>
<Info className="size-4" aria-hidden="true" />
</button>
</TooltipTrigger>
<TooltipContent
side="top"
data-slot="coach-answer-info-content"
className="flex flex-col gap-0.5 tabular-nums"
>
{lines.map((line) => (
<span key={line}>{line}</span>
))}
</TooltipContent>
</Tooltip>
</TooltipProvider>
);
}
/**
* When the message was written, as text. The visible run is short ("14:32");
* the full date and time ride the `title` and the screen-reader text.
@@ -293,34 +355,23 @@ export function AssistantMessageActions({
model,
}: AssistantMessageActionsProps) {
const { t, locale } = useTranslations();
const meta = answerMetaText(tokens, model, resolveIntlLocale(locale), t);
const info = answerInfoLines(tokens, model, resolveIntlLocale(locale), t);
return (
<div
data-slot="coach-answer-actions"
className={cn(
"flex w-full min-w-0 items-center justify-between gap-2 self-stretch",
"flex w-full min-w-0 items-center gap-0.5 self-stretch",
REVEAL.assistant,
"transition-opacity duration-150 motion-reduce:transition-none",
)}
>
<div
data-slot="coach-answer-actions-left"
className="flex shrink-0 items-center gap-0.5"
>
<CopyMessageButton content={content} strip />
{!streaming && <ReadAloudButton content={content} />}
{!streaming && onRegenerate && (
<TryAgainButton onRegenerate={onRegenerate} />
)}
{createdAt && <MessageTime iso={createdAt} />}
</div>
<p
data-slot="coach-answer-meta"
title={model ?? undefined}
className="text-muted-foreground min-w-0 truncate text-right text-xs tabular-nums"
>
{meta}
</p>
<CopyMessageButton content={content} strip />
{!streaming && <ReadAloudButton content={content} />}
{!streaming && onRegenerate && (
<TryAgainButton onRegenerate={onRegenerate} />
)}
{!streaming && info.length > 0 && <AnswerInfoButton lines={info} />}
{createdAt && <MessageTime iso={createdAt} />}
</div>
);
}