Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): Topics treemap caps Unassigned and adds a side-by-side view (#19287)

Trang Nguyenc80198c5923e
diff --git a/web/src/features/topics/CurrentTopics.tsx b/web/src/features/topics/CurrentTopics.tsx
index b4addb3e02..34840f8a61 100644
--- a/web/src/features/topics/CurrentTopics.tsx
+++ b/web/src/features/topics/CurrentTopics.tsx
@@ -78,7 +78,9 @@ function CurrentFacet({
const isHandheld = useIsHandheld();
const mapResult = useCurrentTopicMap(
projectId,
- view === "treemap" ? undefined : facet.map?.runId,
+ view === "treemap" || view === "treemap-side"
+ ? undefined
+ : facet.map?.runId,
timeRange,
);
const peekNavigation = usePeekNavigation({
@@ -92,7 +94,7 @@ function CurrentFacet({
<MobileTopics
projectId={projectId}
facet={facet}
- view={view}
+ view={view === "treemap-side" ? "treemap" : view}
mapResult={mapResult}
traceFilters={traceFilters}
onGenerateTopics={onGenerateTopics}
@@ -100,7 +102,7 @@ function CurrentFacet({
onOpenTrace={peekNavigation.openPeek}
/>
);
- if (view === "treemap")
+ if (view === "treemap" || view === "treemap-side")
return (
<TopicTreemapView
key={facet.facetId}
@@ -109,6 +111,8 @@ function CurrentFacet({
onGenerateTopics={onGenerateTopics}
canGenerateTopics={canGenerateTopics}
peekView={peekView}
+ sideBySide={view === "treemap-side"}
+ tracePanelRef={tracePanelRef}
/>
);
if (view === "map")
diff --git a/web/src/features/topics/TopicTreemap.tsx b/web/src/features/topics/TopicTreemap.tsx
index 28c6a0d368..41a2800d4b 100644
--- a/web/src/features/topics/TopicTreemap.tsx
+++ b/web/src/features/topics/TopicTreemap.tsx
@@ -2,7 +2,10 @@ import { useState, type MouseEvent } from "react";
import { useElementSize } from "@/src/hooks/useElementSize";
import { CustomTooltip } from "@/src/components/design-system/CustomTooltip/CustomTooltip";
import { cn } from "@/src/utils/tailwind";
-import { type TopicDisplay } from "./prepare-topic-display";
+import {
+ type TopicDisplay,
+ UNASSIGNED_TOPIC_ID,
+} from "./prepare-topic-display";
import { layoutTreemap } from "./topic-treemap-layout";
import { TopicPreviewCard } from "./TopicPreviewCard";
@@ -25,6 +28,8 @@ type TopicTileProps = {
onSelect: (topicId: string) => void;
rect: { left: number; top: number; width: number; height: number };
tier: TileTier;
+ /** Drawn smaller than its share of traces. */
+ capped: boolean;
};
type TopicTileButtonProps = TopicTileProps & {
@@ -43,16 +48,35 @@ const TILE_PADDING: Record<TileTier, string> = {
tiny: "p-0",
};
-/** Vertical padding, name line height and count+share block height (px) for tiers that show a name. */
-const PADDING_Y_PX = 24;
-const NAME_LINE_PX = { large: 28, medium: 20 };
-const FOOTER_PX = { large: 54, medium: 46 };
-const NAME_CLAMP = ["line-clamp-1", "line-clamp-2", "line-clamp-3"];
+/** Padding (px), name line height, approximate bold character width and count line height per tier. */
+const PADDING_PX = { large: 24, medium: 24, small: 16, tiny: 0 };
+const NAME_LINE_PX = { large: 28, medium: 20, small: 16, tiny: 16 };
+const NAME_CHAR_PX = { large: 10, medium: 7.6, small: 6.8, tiny: 6.8 };
+const COUNT_PX = { large: 36, medium: 28, small: 0, tiny: 0 };
+/** Share caption: text-xs line height, approximate character width and the gap above it. */
+const SHARE_LINE_PX = 16;
+const SHARE_CHAR_PX = 6;
+const SHARE_GAP_PX = 2;
+const NAME_CLAMP = [
+ "line-clamp-1",
+ "line-clamp-2",
+ "line-clamp-3",
+ "line-clamp-4",
+ "line-clamp-5",
+ "line-clamp-6",
+];
+
+const NAME_SIZE: Record<TileTier, string> = {
+ large: "text-lg",
+ medium: "text-sm",
+ small: "text-xs",
+ tiny: "",
+};
const COUNT_SIZE: Record<TileTier, string> = {
large: "text-3xl",
medium: "text-xl",
- small: "text-sm",
+ small: "",
tiny: "",
};
@@ -63,6 +87,56 @@ function tileTier(width: number, height: number): TileTier {
return "tiny";
}
+/** The name comes first: the count (and share caption) shows only when the full name still fits above it. */
+function tileLabelLayout(
+ name: string,
+ rect: { width: number; height: number },
+ tier: TileTier,
+ shareLabel: string | null,
+) {
+ const contentWidth = rect.width - PADDING_PX[tier];
+ const lines = (reserved: number) =>
+ Math.floor(
+ (rect.height - PADDING_PX[tier] - reserved) / NAME_LINE_PX[tier],
+ );
+ const perLine = Math.max(1, Math.floor(contentWidth / NAME_CHAR_PX[tier]));
+ const needed = Math.ceil(name.length / perLine);
+ const shareLines = shareLabel
+ ? Math.ceil(
+ shareLabel.length /
+ Math.max(1, Math.floor(contentWidth / SHARE_CHAR_PX)),
+ )
+ : 0;
+ const footer =
+ COUNT_PX[tier] +
+ (shareLines > 0 ? SHARE_GAP_PX + shareLines * SHARE_LINE_PX : 0);
+ const withCount = lines(footer);
+ const countFits =
+ (tier === "large" || tier === "medium") && needed <= withCount;
+ const available = countFits ? withCount : lines(0);
+ return {
+ showCount: countFits,
+ nameLines: Math.min(
+ NAME_CLAMP.length,
+ Math.max(1, Math.min(needed, available)),
+ ),
+ };
+}
+
+/** Unassigned never takes more than ~10% of the area; its tile still shows the real count. */
+function layoutWeights(topics: TopicDisplay[]) {
+ const assigned = topics
+ .filter((topic) => topic.id !== UNASSIGNED_TOPIC_ID)
+ .reduce((sum, topic) => sum + topic.count, 0);
+ return topics.map((topic) => ({
+ topic,
+ count:
+ topic.id === UNASSIGNED_TOPIC_ID
+ ? Math.min(topic.count, Math.max(1, assigned / 9))
+ : topic.count,
+ }));
+}
+
function TopicTileButton({
topic,
colors,
@@ -76,19 +150,13 @@ function TopicTileButton({
function selectTopic() {
onSelect(topic.id);
}
- const showName = tier === "large" || tier === "medium";
- /** Name lines that fit above the count. */
- const nameLines = showName
- ? Math.min(
- 3,
- Math.max(
- 1,
- Math.floor(
- (rect.height - PADDING_Y_PX - FOOTER_PX[tier]) / NAME_LINE_PX[tier],
- ),
- ),
- )
- : 0;
+ const showShare = tier === "large" || topic.id === UNASSIGNED_TOPIC_ID;
+ const { showCount, nameLines } = tileLabelLayout(
+ topic.name,
+ rect,
+ tier,
+ showShare ? shareLabel : null,
+ );
return (
<button
{...triggerProps}
@@ -112,23 +180,25 @@ function TopicTileButton({
),
)}
>
- {showName && (
+ {tier !== "tiny" && (
<span
className={cn(
NAME_CLAMP[nameLines - 1],
- "font-bold",
- tier === "large" ? "text-lg" : "text-sm",
+ "mask-r-from-80% font-bold",
+ NAME_SIZE[tier],
)}
>
{topic.name}
</span>
)}
- {tier !== "tiny" && (
+ {showCount && (
<span className="flex flex-col gap-0.5">
<span className={cn("font-bold tabular-nums", COUNT_SIZE[tier])}>
{topic.count.toLocaleString()}
</span>
- {showName && <span className="text-xs opacity-80">{shareLabel}</span>}
+ {showShare && (
+ <span className="text-xs opacity-80">{shareLabel}</span>
+ )}
</span>
)}
</button>
@@ -136,9 +206,10 @@ function TopicTileButton({
}
function TopicTile(props: TopicTileProps) {
- const { topic, share, tier, previewOnSelect } = props;
- const shareLabel = `${Math.round(share * 100)}% of traces`;
- if (previewOnSelect || tier === "large" || tier === "medium") {
+ const { topic, share, tier, previewOnSelect, capped } = props;
+ const percentLabel = `${Math.round(share * 100)}% of traces`;
+ const shareLabel = capped ? `${percentLabel} · shown smaller` : percentLabel;
+ if ((previewOnSelect || tier === "large" || tier === "medium") && !capped) {
return <TopicTileButton {...props} shareLabel={shareLabel} />;
}
return (
@@ -148,8 +219,13 @@ function TopicTile(props: TopicTileProps) {
<span className="ph-no-capture flex flex-col gap-0.5">
<span className="font-bold">{topic.name}</span>
<span className="text-muted-foreground text-xs">
- {topic.count.toLocaleString()} traces · {shareLabel}
+ {topic.count.toLocaleString()} traces · {percentLabel}
</span>
+ {capped && (
+ <span className="text-muted-foreground text-xs">
+ Shown smaller than its actual share so topics stay readable.
+ </span>
+ )}
</span>
}
>
@@ -174,7 +250,16 @@ export function TopicTreemap({
const [previewedId, setPreviewedId] = useState<string | null>(null);
const [ref, size] = useElementSize<HTMLDivElement>();
const tiles = size
- ? layoutTreemap(topics, size.width, size.height, TILE_GAP)
+ ? layoutTreemap(
+ layoutWeights(topics),
+ size.width,
+ size.height,
+ TILE_GAP,
+ ).map((tile) => ({
+ ...tile,
+ item: tile.item.topic,
+ capped: tile.item.count < tile.item.topic.count,
+ }))
: [];
const previewedTopic = topics.find((topic) => topic.id === previewedId);
const previewedTile = tiles.find((tile) => tile.item.id === previewedId);
@@ -199,7 +284,7 @@ export function TopicTreemap({
className="relative h-full min-h-0 min-w-0 flex-1"
onClick={dismissPreview}
>
- {tiles.map(({ item, x, y, width, height }) => (
+ {tiles.map(({ item, capped, x, y, width, height }) => (
<TopicTile
key={item.id}
topic={item}
@@ -212,6 +297,7 @@ export function TopicTreemap({
rect={{ left: x, top: y, width, height }}
tier={tileTier(width, height)}
previewOnSelect={previewOnSelect}
+ capped={capped}
/>
))}
{previewedTopic && (
diff --git a/web/src/features/topics/TopicTreemapView.stories.tsx b/web/src/features/topics/TopicTreemapView.stories.tsx
index ae37182dd8..fe403a156b 100644
--- a/web/src/features/topics/TopicTreemapView.stories.tsx
+++ b/web/src/features/topics/TopicTreemapView.stories.tsx
@@ -90,6 +90,130 @@ const meta = preview.meta({
export const Default = meta.story({});
+/** Treemap on the left, traces on the right. */
+export const SideBySide = meta.story({ args: { sideBySide: true } });
+
+const manyUnassignedRows = [
+ ...assignedRows,
+ ...Array.from({ length: 320 }, (_, index) => ({
+ traceId: `unassigned-trace-${index}`,
+ summary: `Unrelated request ${index + 1} that fit no topic`,
+ topicId: null,
+ topicName: null,
+ outcome: "outlier" as const,
+ facetVersion: 1,
+ unitStartTime: new Date(Date.now() - index * 3 * 60_000).toISOString(),
+ })),
+];
+
+const MANY_TOPIC_NAMES = [
+ "Refund status",
+ "Order tracking",
+ "Change shipping address",
+ "Login help",
+ "Invoice copy",
+ "Cancel subscription",
+ "Update payment method",
+ "Missing items",
+ "Damaged delivery",
+ "Return label request",
+ "Promo code not applied",
+ "Delivery date change",
+ "Reset password",
+ "Two-factor setup",
+ "Account deletion",
+ "Plan upgrade",
+ "Billing cycle question",
+ "Gift card balance",
+ "Store opening hours",
+ "Product availability",
+ "Size exchange",
+ "Warranty claim",
+ "Price match request",
+ "Loyalty points",
+ "Order cancellation",
+ "Duplicate charge",
+ "Tax invoice",
+ "Bulk order quote",
+ "Pickup location",
+ "Driver contact",
+ "Late delivery",
+ "Wrong item sent",
+ "Email preferences",
+ "Language settings",
+ "API key question",
+ "Data export",
+ "Accessibility request",
+ "Feedback on app",
+ "Partnership inquiry",
+ "Job application",
+];
+const manyTopics = Array.from({ length: 40 }, (_, index) => ({
+ id: `many-${index}`,
+ versionId: `many-${index}-v1`,
+ runId: "run",
+ name: MANY_TOPIC_NAMES[index],
+ description: "",
+ count: Math.max(2, Math.round(120 / (index + 1))),
+}));
+const manyRows = manyTopics.flatMap((topic) =>
+ Array.from({ length: topic.count }, (_, index) => ({
+ traceId: `${topic.id}-trace-${index}`,
+ summary: `${topic.name} request ${index + 1}`,
+ topicId: topic.id,
+ topicName: topic.name,
+ outcome: "assigned" as const,
+ facetVersion: 1,
+ unitStartTime: new Date(Date.now() - index * 4 * 60_000).toISOString(),
+ })),
+);
+
+/** Forty clusters: small tiles drop the count, then the name, keeping both in the tooltip. */
+const manyTopic = { ...topics[0], count: 350 };
+const bigTopicRows = Array.from({ length: manyTopic.count }, (_, index) => ({
+ ...assignedRows[0],
+ traceId: `${manyTopic.id}-many-${index}`,
+ summary: `${manyTopic.name} question ${index + 1}`,
+}));
+
+/** One topic with 350 traces; its list adds 100 rows each time you scroll near the end. */
+export const ManyTraces = meta.story({
+ args: {
+ sideBySide: true,
+ facet: {
+ ...facet,
+ rows: [
+ ...bigTopicRows,
+ ...rows.filter((row) => row.topicId !== manyTopic.id),
+ ],
+ topics: [manyTopic, ...topics.slice(1)],
+ usableCount: bigTopicRows.length + rows.length - topics[0].count,
+ },
+ },
+});
+
+export const ManyClusters = meta.story({
+ args: {
+ facet: {
+ ...facet,
+ topics: manyTopics,
+ rows: manyRows,
+ usableCount: manyRows.length,
+ },
+ },
+});
+
+/** About half of the traces fit no topic; Unassigned stays at ~10% of the area. */
+export const LargeUnassigned = meta.story({
+ args: {
+ facet: {
+ ...facet,
+ rows: manyUnassignedRows,
+ usableCount: manyUnassignedRows.length,
+ },
+ },
+});
+
const issueTopics = [
{ name: "Incomplete runs with undelivered results", count: 40 },
{ name: "Research halted before handback report", count: 35 },
diff --git a/web/src/features/topics/TopicTreemapView.tsx b/web/src/features/topics/TopicTreemapView.tsx
index 9dda67053f..e708359233 100644
--- a/web/src/features/topics/TopicTreemapView.tsx
+++ b/web/src/features/topics/TopicTreemapView.tsx
@@ -1,5 +1,6 @@
import { useMemo, useState } from "react";
import { type DataTablePeekViewProps } from "@/src/components/table/peek";
+import { cn } from "@/src/utils/tailwind";
import { TopicsEmptyState } from "./TopicsEmptyState";
import { type Facet } from "./facet-topics";
import {
@@ -15,6 +16,10 @@ type TopicTreemapViewProps = {
peekView: DataTablePeekViewProps;
onGenerateTopics: () => void;
canGenerateTopics: boolean;
+ /** Treemap left, traces right on wide screens. */
+ sideBySide?: boolean;
+ /** Applied to the trace list only when side by side, so the peek matches its width. */
+ tracePanelRef?: (element: HTMLElement | null) => void;
};
export function TopicTreemapView({
@@ -23,6 +28,8 @@ export function TopicTreemapView({
peekView,
onGenerateTopics,
canGenerateTopics,
+ sideBySide = false,
+ tracePanelRef,
}: TopicTreemapViewProps) {
const [selectedTopicId, setSelectedTopicId] = useState<string | null>(null);
const { topics, total } = prepareTopicDisplay(facet, "treemap");
@@ -45,8 +52,18 @@ export function TopicTreemapView({
);
}
return (
- <div className="flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden">
- <div className="m-4 flex min-h-48 shrink-0 basis-1/2">
+ <div
+ className={cn(
+ "flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden",
+ sideBySide && "lg:flex-row",
+ )}
+ >
+ <div
+ className={cn(
+ "m-4 flex min-h-48 shrink-0 basis-1/2",
+ sideBySide && "lg:shrink lg:basis-3/5",
+ )}
+ >
<TopicTreemap
total={total}
topics={topics}
@@ -64,7 +81,8 @@ export function TopicTreemapView({
swatchColor={selected.color}
rows={rows}
peekView={peekView}
- divider="top"
+ tracePanelRef={sideBySide ? tracePanelRef : undefined}
+ divider={sideBySide ? "side" : "top"}
/>
</div>
);
diff --git a/web/src/features/topics/TopicsResultsViewSelect.tsx b/web/src/features/topics/TopicsResultsViewSelect.tsx
index 9524fc63a2..93df4a2a18 100644
--- a/web/src/features/topics/TopicsResultsViewSelect.tsx
+++ b/web/src/features/topics/TopicsResultsViewSelect.tsx
@@ -5,6 +5,7 @@ import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePos
export const TOPICS_RESULTS_VIEWS = [
{ value: "treemap", label: "Treemap" },
+ { value: "treemap-side", label: "Treemap, side by side" },
{ value: "grid", label: "Grid" },
{ value: "map", label: "Map" },
] as const;