🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(web): design-system LabelList with score-chip style labels (#18463)
Trang Nguyenb6cf2cd609da
diff --git a/web/src/components/TruncatedLabels.tsx b/web/src/components/TruncatedLabels.tsxdeleted file mode 100644index 3e0a24818c..0000000000--- a/web/src/components/TruncatedLabels.tsx+++ /dev/null@@ -1,104 +0,0 @@-/* eslint-disable @repo/no-style-props */-import React from "react";-import { Button } from "@/src/components/ui/button";-import { HoverCard } from "@/src/components/design-system/HoverCard/HoverCard";-import { StatusBadge } from "@/src/components/ui/StatusBadge/StatusBadge";-import { PRODUCTION_LABEL, LATEST_PROMPT_LABEL } from "@langfuse/shared";-import { cn } from "@/src/utils/tailwind";--interface TruncatedLabelsProps {- labels: string[];- maxVisibleLabels?: number;- className?: string;- showSimpleBadges?: boolean;-}--export function TruncatedLabels({- labels,- maxVisibleLabels = 5,- className,- showSimpleBadges = false,-}: TruncatedLabelsProps) {- // Enhanced sorting: prioritize latest and production labels- const sortedLabels = [...labels].sort((a, b) => {- // Production label comes first- if (a === PRODUCTION_LABEL) return -1;- if (b === PRODUCTION_LABEL) return 1;-- // Latest label comes second- if (a === LATEST_PROMPT_LABEL) return -1;- if (b === LATEST_PROMPT_LABEL) return 1;-- // Then alphabetically- return a.localeCompare(b);- });-- // Split labels into visible and hidden- const visibleLabels = sortedLabels.slice(0, maxVisibleLabels);- const hiddenLabels = sortedLabels.slice(maxVisibleLabels);- const hasHiddenLabels = hiddenLabels.length > 0;-- return (- <div className={cn("flex flex-wrap gap-1", className)}>- {visibleLabels.map((label) =>- showSimpleBadges ? (- <div- key={label}- className="bg-secondary text-secondary-foreground max-h-fit min-h-6 w-fit content-center rounded-sm px-1 text-left text-xs font-bold"- >- {label}- </div>- ) : (- <StatusBadge- type={label}- key={label}- isLive={label === PRODUCTION_LABEL}- preserveCase- />- ),- )}- {hasHiddenLabels && (- <HoverCard- placement="bottom-start"- content={- <div className="w-80 p-3">- <div className="space-y-2">- <h4 className="text-sm font-bold">All Labels</h4>- <div className="flex flex-wrap gap-1">- {sortedLabels.map((label) =>- showSimpleBadges ? (- <div- key={label}- className="bg-secondary text-secondary-foreground max-h-fit min-h-6 w-fit content-center rounded-sm px-1 text-left text-xs font-bold"- >- {label}- </div>- ) : (- <StatusBadge- type={label}- key={label}- isLive={label === PRODUCTION_LABEL}- preserveCase- />- ),- )}- </div>- </div>- </div>- }- >- {({ getTriggerProps }) => (- <Button- variant="outline"- size="sm"- className="text-muted-foreground hover:text-foreground h-6 cursor-pointer text-xs"- {...getTriggerProps()}- >- +{hiddenLabels.length} more- </Button>- )}- </HoverCard>- )}- </div>- );-}diff --git a/web/src/components/design-system/LabelList/LabelList.stories.tsx b/web/src/components/design-system/LabelList/LabelList.stories.tsxnew file mode 100644index 0000000000..9ac1a7842b--- /dev/null+++ b/web/src/components/design-system/LabelList/LabelList.stories.tsx@@ -0,0 +1,58 @@+import preview from "../../../../.storybook/preview";+import { LabelList } from "./LabelList";++const meta = preview.meta({+ component: LabelList,+});++const manyLabels = [+ { name: "production", isProduction: true },+ { name: "latest" },+ { name: "canary" },+ { name: "experiment-a" },+ { name: "experiment-b" },+ { name: "qa" },+ { name: "staging" },+];++const fewLabels = manyLabels.slice(0, 3);++export const Default = meta.story({+ args: {+ labels: fewLabels,+ },+});++export const WithOverflow = meta.story({+ args: {+ labels: manyLabels,+ maxVisible: 3,+ },+});++export const SingleLine = meta.story({+ args: {+ labels: manyLabels,+ shouldWrap: false,+ },+ render: (args) => (+ <div className="w-60 resize-x overflow-hidden">+ <LabelList {...args} />+ </div>+ ),+});++export const Inline = meta.story({+ parameters: {+ controls: {+ disable: true,+ },+ },+ render: () => (+ <div className="flex flex-wrap items-center gap-1">+ <span className="text-xs">Version 4</span>+ <LabelList labels={fewLabels} layout="inline" />+ <span className="text-muted-foreground text-xs">by jane</span>+ </div>+ ),+});diff --git a/web/src/components/design-system/LabelList/LabelList.tsx b/web/src/components/design-system/LabelList/LabelList.tsxnew file mode 100644index 0000000000..0c5d096850--- /dev/null+++ b/web/src/components/design-system/LabelList/LabelList.tsx@@ -0,0 +1,225 @@+import { type ReactNode, useLayoutEffect, useRef, useState } from "react";+import { flushSync } from "react-dom";+import { cva } from "class-variance-authority";++import { BadgeShell } from "../Badge/Badge";+import { HoverCard } from "../HoverCard/HoverCard";++const labelListVariants = cva("", {+ variants: {+ layout: {+ contained: "flex gap-1",+ /** Labels flow as direct children of the host's flex-wrap row. */+ inline: "contents",+ },+ wrap: {+ true: "flex-wrap",+ false: "relative min-w-0 flex-nowrap items-center",+ },+ },+ defaultVariants: {+ layout: "contained",+ wrap: true,+ },+});++const DEFAULT_MAX_VISIBLE = 5;+/** Matches `gap-1` between chips. */+const CHIP_GAP = 4;++export function LabelList(props: LabelListProps) {+ const { labels, layout = "contained" } = props;+ const isSingleLine = layout === "contained" && props.shouldWrap === false;+ const { listRef, probeRef, fittingCount } = useFittingCount(+ labels,+ isSingleLine,+ );+ const visibleCount = isSingleLine+ ? fittingCount+ : (props.maxVisible ?? DEFAULT_MAX_VISIBLE);+ const visibleLabels = labels.slice(0, visibleCount);+ const hiddenCount = labels.length - visibleLabels.length;+ const renderChip = (label: LabelListItem) => (+ <LabelChip+ key={label.name}+ name={label.name}+ isProduction={label.isProduction ?? false}+ />+ );+ const overflow =+ hiddenCount > 0 ? (+ <LabelOverflow+ chips={labels.map(renderChip)}+ totalCount={labels.length}+ hiddenCount={hiddenCount}+ />+ ) : null;++ if (!isSingleLine) {+ return (+ <div className={labelListVariants({ layout, wrap: true })}>+ {visibleLabels.map(renderChip)}+ {overflow}+ </div>+ );+ }++ return (+ <div ref={listRef} className={labelListVariants({ wrap: false })}>+ <div className="flex min-w-0 gap-1 overflow-hidden">+ {visibleLabels.map(renderChip)}+ </div>+ {overflow}+ {/* Natural chip widths, measured to decide how many fit. */}+ <div+ ref={probeRef}+ aria-hidden+ className="pointer-events-none invisible absolute flex w-max gap-1"+ >+ {labels.map(renderChip)}+ <BadgeShell color="filled" font="mono" size="md">+ +{labels.length}+ </BadgeShell>+ </div>+ </div>+ );+}++export type LabelListItem = {+ name: string;+ /** Shows the production marker. */+ isProduction?: boolean;+};++type LabelListProps = {+ /** Rendered in the given order. */+ labels: LabelListItem[];+} & (+ | {+ layout?: "contained";+ shouldWrap?: true;+ /** Labels beyond this count collapse into a `+N` hover card. */+ maxVisible?: number;+ }+ | {+ layout?: "contained";+ /** One line: as many labels as fit, the rest collapse into `+N`. */+ shouldWrap: false;+ maxVisible?: never;+ }+ | {+ layout: "inline";+ shouldWrap?: never;+ maxVisible?: number;+ }+);++function useFittingCount(labels: LabelListItem[], isEnabled: boolean) {+ const listRef = useRef<HTMLDivElement>(null);+ const probeRef = useRef<HTMLDivElement>(null);+ const [fittingCount, setFittingCount] = useState(labels.length);+ const labelsKey = labels+ .map((label) => `${label.name}:${label.isProduction ?? false}`)+ .join("\u0000");++ useLayoutEffect(() => {+ const list = listRef.current;+ const probe = probeRef.current;+ if (!isEnabled || !list || !probe) return;++ const update = () => {+ const widths = [...probe.children].map(+ (child) => (child as HTMLElement).offsetWidth,+ );+ const overflowWidth = widths.pop() ?? 0;+ // Sub-pixel rounding at the exact boundary: allow a pixel of slack.+ const available = list.clientWidth + 1;+ const allWidth = sumWithGaps(widths);+ if (allWidth <= available) {+ setFittingCount(widths.length);+ return;+ }+ let count = 0;+ while (+ count < widths.length &&+ sumWithGaps(widths.slice(0, count + 1)) + CHIP_GAP + overflowWidth <=+ available+ ) {+ count++;+ }+ setFittingCount(Math.max(1, count));+ };++ update();+ if (typeof ResizeObserver === "undefined") return;+ // Resize callbacks run before paint; a synchronous commit keeps it that way.+ const observer = new ResizeObserver(() => flushSync(update));+ observer.observe(list);+ return () => observer.disconnect();+ }, [isEnabled, labelsKey]);++ return { listRef, probeRef, fittingCount };+}++function sumWithGaps(widths: number[]): number {+ return widths.reduce(+ (sum, width, index) => sum + width + (index > 0 ? CHIP_GAP : 0),+ 0,+ );+}++function LabelOverflow({+ chips,+ totalCount,+ hiddenCount,+}: {+ chips: ReactNode;+ totalCount: number;+ hiddenCount: number;+}) {+ return (+ <HoverCard+ placement="bottom-start"+ content={+ <div className="w-80 p-3">+ <div className="space-y-2">+ <h4 className="text-sm font-bold">All Labels</h4>+ <div className="flex flex-wrap gap-1">{chips}</div>+ </div>+ </div>+ }+ >+ {({ getTriggerProps }) => (+ <BadgeShell asChild color="filled" font="mono" size="md">+ <button+ type="button"+ className="text-muted-foreground cursor-pointer self-center"+ aria-label={`Show all ${totalCount} labels`}+ {...getTriggerProps()}+ >+ +{hiddenCount}+ </button>+ </BadgeShell>+ )}+ </HoverCard>+ );+}++function LabelChip({+ name,+ isProduction,+}: {+ name: string;+ isProduction: boolean;+}) {+ return (+ <BadgeShell color="filled" font="mono" size="md">+ {isProduction && (+ <span className="bg-dark-green size-1.5 shrink-0 rounded-full" />+ )}+ <span className="truncate py-0.5" title={name}>+ {name}+ </span>+ </BadgeShell>+ );+}diff --git a/web/src/features/experiments/components/steps/PromptModelStep.tsx b/web/src/features/experiments/components/steps/PromptModelStep.tsxindex 79646a1a83..52d2b81210 100644--- a/web/src/features/experiments/components/steps/PromptModelStep.tsx+++ b/web/src/features/experiments/components/steps/PromptModelStep.tsx@@ -35,7 +35,8 @@ import { CardDescription } from "@/src/components/ui/card";import { cn } from "@/src/utils/tailwind";import { type PromptModelStepProps } from "@/src/features/experiments/types/stepProps";import { StepHeader } from "@/src/features/experiments/components/shared/StepHeader";-import { TruncatedLabels } from "@/src/components/TruncatedLabels";+import { toPromptLabelListItems } from "@/src/features/prompts/utils";+import { LabelList } from "@/src/components/design-system/LabelList/LabelList";export const PromptModelStep: React.FC<PromptModelStepProps> = ({projectId,@@ -235,11 +236,14 @@ export const PromptModelStep: React.FC<PromptModelStepProps> = ({Version {prompt.version}</span>{prompt.labels.length > 0 && (- <TruncatedLabels- labels={prompt.labels}- maxVisibleLabels={2}- className="min-w-0"- />+ <div className="min-w-0">+ <LabelList+ labels={toPromptLabelListItems(+ prompt.labels,+ )}+ maxVisible={2}+ />+ </div>)}</div><CheckIcondiff --git a/web/src/features/prompts/PromptMetricsPage.tsx b/web/src/features/prompts/PromptMetricsPage.tsxindex dcae2edcd0..31a33161e3 100644--- a/web/src/features/prompts/PromptMetricsPage.tsx+++ b/web/src/features/prompts/PromptMetricsPage.tsx@@ -21,7 +21,8 @@ import { Skeleton } from "@/src/components/ui/skeleton";import { type ScoreAggregate } from "@langfuse/shared";import Page from "@/src/components/layouts/page";import { DetailPageNav } from "@/src/features/navigate-detail-pages";-import { TruncatedLabels } from "@/src/components/TruncatedLabels";+import { toPromptLabelListItems } from "@/src/features/prompts/utils";+import { LabelList } from "@/src/components/design-system/LabelList/LabelList";import { getPromptTabs, PROMPT_TABS } from "@/src/features/navigation";import {useScoreColumns,@@ -194,17 +195,15 @@ export default function PromptMetricsPage({id: "labels",header: "Labels",isPinnedLeft: true,- size: 160,+ size: 240,cell: ({ row }) => {const values: string[] = row.getValue("labels");return (values &&values.length > 0 && (- <TruncatedLabels- labels={values}- maxVisibleLabels={3}- className="-mr-8 flex max-h-full flex-wrap gap-1"- showSimpleBadges={true}+ <LabelList+ labels={toPromptLabelListItems(values)}+ shouldWrap={false}/>));diff --git a/web/src/features/prompts/components/SetPromptVersionLabels/index.tsx b/web/src/features/prompts/components/SetPromptVersionLabels/index.tsxindex 1dbe84e6fd..0d246db902 100644--- a/web/src/features/prompts/components/SetPromptVersionLabels/index.tsx+++ b/web/src/features/prompts/components/SetPromptVersionLabels/index.tsx@@ -25,8 +25,11 @@ import {} from "@langfuse/shared";import { LabelCommandItem } from "./LabelCommandItem";import { usePostHogClientCapture } from "@/src/features/posthog-analytics";-import { isReservedPromptLabel } from "@/src/features/prompts/utils";-import { TruncatedLabels } from "@/src/components/TruncatedLabels";+import {+ isReservedPromptLabel,+ toPromptLabelListItems,+} from "@/src/features/prompts/utils";+import { LabelList } from "@/src/components/design-system/LabelList/LabelList";import { cn } from "@/src/utils/tailwind";export function SetPromptVersionLabels({@@ -180,9 +183,9 @@ export function SetPromptVersionLabels({>{title && title}{promptLabels.length > 0 && (- <TruncatedLabels- labels={promptLabels}- maxVisibleLabels={maxVisibleLabels}+ <LabelList+ labels={toPromptLabelListItems(promptLabels)}+ maxVisible={maxVisibleLabels}/>)}<Buttondiff --git a/web/src/features/prompts/utils.ts b/web/src/features/prompts/utils.tsindex fb85771f62..3bf9e1dd5a 100644--- a/web/src/features/prompts/utils.ts+++ b/web/src/features/prompts/utils.ts@@ -1,9 +1,22 @@import { LATEST_PROMPT_LABEL, PRODUCTION_LABEL } from "@langfuse/shared";+import { type LabelListItem } from "@/src/components/design-system/LabelList/LabelList";export const isReservedPromptLabel = (label: string) => {return [PRODUCTION_LABEL, LATEST_PROMPT_LABEL].includes(label);};+/** Production first, then latest, then the rest alphabetically. */+export const toPromptLabelListItems = (labels: string[]): LabelListItem[] =>+ [...labels]+ .sort((a, b) => {+ if (a === PRODUCTION_LABEL) return -1;+ if (b === PRODUCTION_LABEL) return 1;+ if (a === LATEST_PROMPT_LABEL) return -1;+ if (b === LATEST_PROMPT_LABEL) return 1;+ return a.localeCompare(b);+ })+ .map((name) => ({ name, isProduction: name === PRODUCTION_LABEL }));+/*** Href for a prompt's detail page.*diff --git a/web/src/features/skills/components/SkillMetadataSelect.tsx b/web/src/features/skills/components/SkillMetadataSelect.tsxindex f0583de275..39099037cd 100644--- a/web/src/features/skills/components/SkillMetadataSelect.tsx+++ b/web/src/features/skills/components/SkillMetadataSelect.tsx@@ -1,14 +1,8 @@import { useId, useMemo, useState } from "react";-import {- CircleCheckIcon,- CircleFadingArrowUp,- CircleIcon,- Plus,- TagIcon,- X,-} from "lucide-react";+import { CircleCheckIcon, CircleIcon, Plus, TagIcon, X } from "lucide-react";import { PromptLabelSchema, SKILL_LATEST_LABEL } from "@langfuse/shared";-import { TruncatedLabels } from "@/src/components/TruncatedLabels";+import { toPromptLabelListItems } from "@/src/features/prompts/utils";+import { LabelList } from "@/src/components/design-system/LabelList/LabelList";import { Badge } from "@/src/components/ui/badge";import { Button } from "@/src/components/ui/button";import { Input } from "@/src/components/ui/input";@@ -172,7 +166,7 @@ export function SkillLabelsSelect({disabled ? "cursor-not-allowed opacity-50" : "cursor-pointer",)}>- <TruncatedLabels labels={value} maxVisibleLabels={5} />+ <LabelList labels={toPromptLabelListItems(value)} maxVisible={5} /><Buttontype="button"variant="outline"@@ -186,7 +180,7 @@ export function SkillLabelsSelect({"opacity-0 group-focus-within/skill-version:opacity-100 group-hover/skill-version:opacity-100 [@media(hover:none)]:opacity-100",)}>- <CircleFadingArrowUp className="icon-base text-icon-foreground" />+ <Plus className="icon-base text-icon-foreground" /></Button></div></Trigger>