🪢 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): Topics trace list loads rows as you scroll (#19289)
Trang Nguyen29223ab21ad9
diff --git a/web/src/features/topics/TopicGrid.tsx b/web/src/features/topics/TopicGrid.tsxindex a730cbc615..7fe096f4d6 100644--- a/web/src/features/topics/TopicGrid.tsx+++ b/web/src/features/topics/TopicGrid.tsx@@ -1,6 +1,7 @@import {useMemo,useState,+ type CSSProperties,type MouseEvent,type PointerEvent,type ReactNode,@@ -30,6 +31,8 @@ type TopicGridProps = {onSelectTopic: (topicId: string | null) => void;onSelectTrace: (traceId: string) => void;previewOnSelect?: boolean;+ /** Matches the trace list header in the side-by-side layout so both dividers line up. */+ barHeight?: number;};type GridHoverCardProps = {@@ -276,6 +279,7 @@ export function TopicGrid({selectedIndex,onSelectTopic,onSelectTrace,+ barHeight,previewOnSelect = false,}: TopicGridProps) {const [zoom, setZoom] = useState(0);@@ -405,7 +409,17 @@ export function TopicGrid({return (<>- <div className="flex h-12 shrink-0 items-center justify-end gap-2 px-4">+ <div+ className={cn(+ "flex h-12 shrink-0 items-center justify-end gap-2 px-4",+ barHeight && "lg:h-(--bar-height)",+ )}+ style={+ barHeight+ ? ({ "--bar-height": `${barHeight}px` } as CSSProperties)+ : undefined+ }+ ><div className="flex items-center gap-0.5"><IconButtonicon={Minus}diff --git a/web/src/features/topics/TopicGridView.tsx b/web/src/features/topics/TopicGridView.tsxindex 493041b6c4..920fb21c1e 100644--- a/web/src/features/topics/TopicGridView.tsx+++ b/web/src/features/topics/TopicGridView.tsx@@ -1,8 +1,10 @@+/* eslint-disable @typescript-eslint/no-use-before-define */import { type RouterOutputs } from "@/src/utils/api";import { useMemo, useState } from "react";import { type DataTablePeekViewProps } from "@/src/components/table/peek";+import { useElementSize } from "@/src/hooks/useElementSize";import { TopicsEmptyState } from "./TopicsEmptyState";-import { type Facet, allTopicsLabel } from "./facet-topics";+import { type Facet, allTopicsLabel, orderedFacetTopics } from "./facet-topics";import { prepareTopicGrid } from "./prepare-topic-grid";import { TopicGrid } from "./TopicGrid";import { TopicTraceList } from "./TopicTraceList";@@ -21,6 +23,11 @@ type TopicGridViewProps = {canGenerateTopics: boolean;};+type TopicGridContentProps = Omit<+ TopicGridViewProps,+ "onGenerateTopics" | "canGenerateTopics"+>;+function gridMessage(mapResult: TopicGridViewProps["mapResult"]) {if (mapResult.error) return `Grid could not load: ${mapResult.error.message}`;if (!mapResult.data) return "Loading grid…";@@ -33,39 +40,53 @@ function gridMessage(mapResult: TopicGridViewProps["mapResult"]) {}export function TopicGridView({+ onGenerateTopics,+ canGenerateTopics,+ ...props+}: TopicGridViewProps) {+ if (orderedFacetTopics(props.facet).length === 0) {+ return (+ <TopicsEmptyState+ onGenerateTopics={onGenerateTopics}+ canGenerateTopics={canGenerateTopics}+ />+ );+ }+ return <TopicGridContent {...props} />;+}++/** Mounts once topics exist, so the trace list header is there to measure from the first render. */+function TopicGridContent({projectId,facet,mapResult,onOpenTrace,peekView,tracePanelRef,- onGenerateTopics,- canGenerateTopics,-}: TopicGridViewProps) {+}: TopicGridContentProps) {const [selectedTopicId, setSelectedTopicId] = useState<string | null>(null);+ const [headerRef, headerSize] = useElementSize<HTMLDivElement>();const { topics, points, perDot, rowsById } = useMemo(() => prepareTopicGrid(facet, mapResult.data),[facet, mapResult.data],);const selectedTopic =topics.find((topic) => topic.id === selectedTopicId) ?? null;- if (topics.length === 0) {- return (- <TopicsEmptyState- onGenerateTopics={onGenerateTopics}- canGenerateTopics={canGenerateTopics}- />- );- }+ const selectedId = selectedTopic?.id ?? null;+ // Stable per topic so the list keeps its loaded chunks across re-renders.+ const listRows = useMemo(+ () =>+ selectedId+ ? facet.rows.filter((row) => row.topicId === selectedId)+ : facet.rows,+ [facet.rows, selectedId],+ );const selectedIndex = selectedTopic? topics.findIndex((topic) => topic.id === selectedTopic.id): null;const message = facet.map? gridMessage(mapResult): "No map published for this facet.";- const listRows = selectedTopic- ? facet.rows.filter((row) => row.topicId === selectedTopic.id)- : facet.rows;return (<div className="flex h-full min-h-0 min-w-0 flex-col lg:flex-row">@@ -84,6 +105,7 @@ export function TopicGridView({selectedIndex={selectedIndex}onSelectTopic={setSelectedTopicId}onSelectTrace={onOpenTrace}+ barHeight={headerSize?.height}/>)}</section>@@ -97,6 +119,7 @@ export function TopicGridView({rows={listRows}peekView={peekView}tracePanelRef={tracePanelRef}+ headerRef={headerRef}divider="side"/></div>diff --git a/web/src/features/topics/TopicTraceList.tsx b/web/src/features/topics/TopicTraceList.tsxindex 824d985b81..9eb33850b7 100644--- a/web/src/features/topics/TopicTraceList.tsx+++ b/web/src/features/topics/TopicTraceList.tsx@@ -1,18 +1,19 @@-/* eslint-disable react/no-unstable-nested-components, @typescript-eslint/no-use-before-define */-import { useState } from "react";-import { type OnChangeFn, type PaginationState } from "@tanstack/react-table";+/* eslint-disable react/no-unstable-nested-components */+import { type RefObject, type UIEvent, useState } from "react";import { ExternalLink } from "@langfuse/icons";import { DataTable } from "@/src/components/table/data-table";import { type LangfuseColumnDef } from "@/src/components/table/types";import { type DataTablePeekViewProps } from "@/src/components/table/peek";import { Button } from "@/src/components/design-system/Button/Button";-import { formatCompactRelativeTime } from "@/src/utils/dates";import { cn } from "@/src/utils/tailwind";import { useReadPath } from "@/src/features/events/hooks/useReadPath";import { type FacetRow, topicTracesHref } from "./facet-topics";/** With the table's own 4px (8px on the first column), cells end up 16px from each edge. */const CELL = "block px-3";+/** Rows drawn at once; more load as the list nears its end. */+const ROW_CHUNK = 100;+const LOAD_MORE_THRESHOLD_PX = 400;const FIRST_CELL = "block pr-3 pl-2";export function TopicTraceList({@@ -23,20 +24,19 @@ export function TopicTraceList({rows,peekView,tracePanelRef,+ headerRef,divider,}: TopicTraceListProps) {- const [pagination, setPagination] = useState<PaginationState>({- pageIndex: 0,- pageSize: 20,- });- const pageIndex = Math.min(- pagination.pageIndex,- Math.max(0, Math.ceil(rows.length / pagination.pageSize) - 1),- );- const onPaginationChange: OnChangeFn<PaginationState> = (update) => {- setPagination((current) =>- typeof update === "function" ? update(current) : update,- );+ const [shown, setShown] = useState({ rows, count: ROW_CHUNK });+ // A new topic or facet starts again from the first chunk.+ const visibleCount = shown.rows === rows ? shown.count : ROW_CHUNK;+ const onScrollCapture = (event: UIEvent<HTMLElement>) => {+ const target = event.target as HTMLElement;+ const nearBottom =+ target.scrollTop + target.clientHeight >=+ target.scrollHeight - LOAD_MORE_THRESHOLD_PX;+ if (nearBottom && visibleCount < rows.length)+ setShown({ rows, count: visibleCount + ROW_CHUNK });};const { isV4 } = useReadPath();const tracingHref = topicTracesHref(projectId, rows, isV4);@@ -44,8 +44,8 @@ export function TopicTraceList({{accessorKey: "traceId",header: () => <span className={FIRST_CELL}>Trace ID</span>,- size: 128,- minSize: 96,+ size: 208,+ minSize: 128,cell: ({ row }) => (<spanclassName={cn(@@ -73,22 +73,12 @@ export function TopicTraceList({);},},- {- accessorKey: "unitStartTime",- header: () => <span className={CELL}>Started</span>,- size: 96,- minSize: 88,- cell: ({ row }) => (- <span className={cn("text-muted-foreground block tabular-nums", CELL)}>- {compactAge(row.original.unitStartTime)}- </span>- ),- },];return (<sectionaria-label="Topic traces"ref={tracePanelRef}+ onScrollCapture={onScrollCapture}className={cn("flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden",divider === "side" && "border-t lg:border-t-0 lg:border-l",@@ -96,40 +86,45 @@ export function TopicTraceList({!divider && "rounded-md border",)}>- <div className="flex h-12 shrink-0 items-center justify-between gap-2 px-4">- <h3 className="flex min-w-0 items-baseline gap-2 text-base leading-none font-bold">- {swatchColor ? (+ <div ref={headerRef} className="flex shrink-0 flex-col gap-1 px-4 py-3">+ <div className="flex h-7 items-center justify-between gap-4">+ <p className="text-muted-foreground flex min-w-0 items-center gap-2">+ {swatchColor && (+ <span+ aria-hidden+ className="size-3 shrink-0 rounded-xs"+ style={{ backgroundColor: swatchColor }}+ />+ )}<span- aria-hidden- className="size-2.5 shrink-0 self-center rounded-xs"- style={{ backgroundColor: swatchColor }}+ className="truncate font-mono text-xs"+ title={`${rows.length.toLocaleString()} traces`}+ >+ {`${rows.length.toLocaleString()} traces`}+ </span>+ </p>+ {tracingHref && (+ <Button+ href={tracingHref}+ text="Open in Tracing"+ variant="ghost"+ size="sm"+ icon={ExternalLink}/>- ) : null}- <span className="truncate" title={title}>- {title}- </span>- <span className="text-muted-foreground shrink-0 font-mono text-xs font-normal">- {rows.length.toLocaleString()} traces- </span>+ )}+ </div>+ <h3 className="truncate text-lg font-bold" title={title}>+ {title}</h3>- {tracingHref ? (- <Button- href={tracingHref}- text="Open in Tracing"- variant="secondary"- size="sm"- icon={ExternalLink}- />- ) : null}+ {description && (+ <p+ className="text-muted-foreground line-clamp-2 text-sm"+ title={description}+ >+ {description}+ </p>+ )}</div>- {description ? (- <p- className="bg-muted text-muted-foreground mx-4 mb-3 line-clamp-3 rounded-md px-3 py-2 text-sm"- title={description}- >- {description}- </p>- ) : null}<DataTabletableName="topics-topic-traces"columns={columns}@@ -137,18 +132,9 @@ export function TopicTraceList({isLoading: false,isError: false,data: rows- .slice(- pageIndex * pagination.pageSize,- (pageIndex + 1) * pagination.pageSize,- )+ .slice(0, visibleCount).map((row) => ({ ...row, id: row.traceId })),}}- pagination={{- totalCount: rows.length,- state: { ...pagination, pageIndex },- onChange: onPaginationChange,- options: [20, 50, 100],- }}className="min-h-0 overscroll-contain"cellPadding="relaxed"noResultsMessage="No trace summaries."@@ -166,9 +152,7 @@ type TopicTraceListProps = {rows: FacetRow[];peekView: DataTablePeekViewProps;tracePanelRef?: (element: HTMLElement | null) => void;+ /** Lets a neighbouring panel match this header's height. */+ headerRef?: RefObject<HTMLDivElement | null>;divider?: "top" | "side";};--function compactAge(timestamp: string) {- return formatCompactRelativeTime(new Date(timestamp)).replace(/ ago$/, "");-}diff --git a/web/src/features/topics/TopicTreemapView.tsx b/web/src/features/topics/TopicTreemapView.tsxindex 785fccaf30..9dda67053f 100644--- a/web/src/features/topics/TopicTreemapView.tsx+++ b/web/src/features/topics/TopicTreemapView.tsx@@ -1,4 +1,4 @@-import { useState } from "react";+import { useMemo, useState } from "react";import { type DataTablePeekViewProps } from "@/src/components/table/peek";import { TopicsEmptyState } from "./TopicsEmptyState";import { type Facet } from "./facet-topics";@@ -25,7 +25,18 @@ export function TopicTreemapView({canGenerateTopics,}: TopicTreemapViewProps) {const [selectedTopicId, setSelectedTopicId] = useState<string | null>(null);- if (facet.topics.length === 0) {+ const { topics, total } = prepareTopicDisplay(facet, "treemap");+ const selected =+ topics.find((topic) => topic.id === selectedTopicId) ?? topics[0];+ const selectedRowTopicId =+ selected && selected.id !== UNASSIGNED_TOPIC_ID ? selected.id : null;+ // Stable per topic so the list keeps its loaded chunks across re-renders.+ const rows = useMemo(+ () =>+ facet.rows.filter((row) => (row.topicId ?? null) === selectedRowTopicId),+ [facet.rows, selectedRowTopicId],+ );+ if (facet.topics.length === 0 || !selected) {return (<TopicsEmptyStateonGenerateTopics={onGenerateTopics}@@ -33,14 +44,6 @@ export function TopicTreemapView({/>);}- const { topics, total } = prepareTopicDisplay(facet, "treemap");- const selected =- topics.find((topic) => topic.id === selectedTopicId) ?? topics[0];- const selectedRowTopicId =- selected.id === UNASSIGNED_TOPIC_ID ? null : selected.id;- const rows = facet.rows.filter(- (row) => (row.topicId ?? null) === selectedRowTopicId,- );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">