Skip to content

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

feat(ui): improve comments in annotation drawer within own panel (#8601)

Marc Klingen1046a9f65e55
diff --git a/web/src/features/annotation-queues/components/AnnotationQueueItemPage.tsx b/web/src/features/annotation-queues/components/AnnotationQueueItemPage.tsx
index 6b7801b86c..c54a7eab6e 100644
--- a/web/src/features/annotation-queues/components/AnnotationQueueItemPage.tsx
+++ b/web/src/features/annotation-queues/components/AnnotationQueueItemPage.tsx
@@ -30,6 +30,7 @@ export const AnnotationQueueItemPage: React.FC<{
>(null);
const [seenItemIds, setSeenItemIds] = useState<string[]>([]);
const [progressIndex, setProgressIndex] = useState(0);
+ const [hasCommentDraft, setHasCommentDraft] = useState(false);
const hasAccess = useHasProjectAccess({
projectId,
@@ -162,6 +163,12 @@ export const AnnotationQueueItemPage: React.FC<{
};
const handleNavigateNext = async () => {
+ if (hasCommentDraft) {
+ const proceed = confirm(
+ "You have an unsaved comment. Do you want to go to the next item and discard this draft?",
+ );
+ if (!proceed) return;
+ }
if (progressIndex >= seenItemIds.length - 1) {
const nextItem = await fetchAndLockNextMutation.mutateAsync({
queueId: annotationQueueId,
@@ -175,6 +182,13 @@ export const AnnotationQueueItemPage: React.FC<{
const handleComplete = async () => {
if (!relevantItem) return;
+ const willNavigate = !isSingleItem && progressIndex + 1 < totalItems;
+ if (hasCommentDraft && willNavigate) {
+ const proceed = confirm(
+ "You have an unsaved comment. Do you want to complete and move to the next item, discarding the draft?",
+ );
+ if (!proceed) return;
+ }
await completeMutation.mutateAsync({
itemId: relevantItem.id,
projectId,
@@ -205,6 +219,7 @@ export const AnnotationQueueItemPage: React.FC<{
view={view}
configs={configs}
projectId={projectId}
+ onHasCommentDraftChange={setHasCommentDraft}
/>
);
case AnnotationQueueObjectType.SESSION:
@@ -214,6 +229,7 @@ export const AnnotationQueueItemPage: React.FC<{
data={objectData.data}
configs={configs}
projectId={projectId}
+ onHasCommentDraftChange={setHasCommentDraft}
/>
);
default:
diff --git a/web/src/features/annotation-queues/components/processors/SessionAnnotationProcessor.tsx b/web/src/features/annotation-queues/components/processors/SessionAnnotationProcessor.tsx
index 5dc6860ff0..c465f841d1 100644
--- a/web/src/features/annotation-queues/components/processors/SessionAnnotationProcessor.tsx
+++ b/web/src/features/annotation-queues/components/processors/SessionAnnotationProcessor.tsx
@@ -22,6 +22,7 @@ interface SessionAnnotationProcessorProps {
data: any; // // Session data with scores
configs: ValidatedScoreConfig[];
projectId: string;
+ onHasCommentDraftChange?: (hasDraft: boolean) => void;
}
// some projects have thousands of traces in a session, paginate to avoid rendering all at once
@@ -29,7 +30,7 @@ const PAGE_SIZE = 10;
export const SessionAnnotationProcessor: React.FC<
SessionAnnotationProcessorProps
-> = ({ item, data, configs, projectId }) => {
+> = ({ item, data, configs, projectId, onHasCommentDraftChange }) => {
const [visibleTraces, setVisibleTraces] = useState(PAGE_SIZE);
const [currentTraceIndex, setCurrentTraceIndex] = useState(1);
@@ -154,6 +155,7 @@ export const SessionAnnotationProcessor: React.FC<
scores={data?.scores ?? []}
configs={configs}
environment={data?.environment}
+ onHasCommentDraftChange={onHasCommentDraftChange}
/>
);
diff --git a/web/src/features/annotation-queues/components/processors/TraceAnnotationProcessor.tsx b/web/src/features/annotation-queues/components/processors/TraceAnnotationProcessor.tsx
index 9f69e02124..09a2c236f1 100644
--- a/web/src/features/annotation-queues/components/processors/TraceAnnotationProcessor.tsx
+++ b/web/src/features/annotation-queues/components/processors/TraceAnnotationProcessor.tsx
@@ -20,11 +20,12 @@ interface TraceAnnotationProcessorProps {
view: "showTree" | "hideTree";
configs: ValidatedScoreConfig[];
projectId: string;
+ onHasCommentDraftChange?: (hasDraft: boolean) => void;
}
export const TraceAnnotationProcessor: React.FC<
TraceAnnotationProcessorProps
-> = ({ item, data, view, configs, projectId }) => {
+> = ({ item, data, view, configs, projectId, onHasCommentDraftChange }) => {
const traceId = item.parentTraceId ?? item.objectId;
const [currentObservationId, setCurrentObservationId] = useQueryParam(
@@ -101,6 +102,7 @@ export const TraceAnnotationProcessor: React.FC<
scores={data?.scores ?? []}
configs={configs}
environment={data?.environment}
+ onHasCommentDraftChange={onHasCommentDraftChange}
/>
);
diff --git a/web/src/features/annotation-queues/components/shared/AnnotationDrawerSection.tsx b/web/src/features/annotation-queues/components/shared/AnnotationDrawerSection.tsx
index 9f7fc15763..78b010e5bd 100644
--- a/web/src/features/annotation-queues/components/shared/AnnotationDrawerSection.tsx
+++ b/web/src/features/annotation-queues/components/shared/AnnotationDrawerSection.tsx
@@ -1,4 +1,9 @@
import { Card } from "@/src/components/ui/card";
+import {
+ ResizableHandle,
+ ResizablePanel,
+ ResizablePanelGroup,
+} from "@/src/components/ui/resizable";
import useSessionStorage from "@/src/components/useSessionStorage";
import { AnnotateDrawerContent } from "@/src/features/scores/components/AnnotateDrawerContent";
import { type ScoreTarget } from "@/src/features/scores/types";
@@ -10,7 +15,6 @@ import {
} from "@langfuse/shared";
import { TriangleAlertIcon } from "lucide-react";
import { useSession } from "next-auth/react";
-import { useRouter } from "next/router";
import { useMemo, useState } from "react";
import { CommentsSection } from "./CommentsSection";
@@ -23,18 +27,24 @@ interface AnnotationDrawerSectionProps {
scores: APIScoreV2[];
configs: ValidatedScoreConfig[];
environment?: string;
+ onHasCommentDraftChange?: (hasDraft: boolean) => void;
}
export const AnnotationDrawerSection: React.FC<
AnnotationDrawerSectionProps
-> = ({ item, scoreTarget, scores, configs, environment }) => {
- const router = useRouter();
+> = ({
+ item,
+ scoreTarget,
+ scores,
+ configs,
+ environment,
+ onHasCommentDraftChange,
+}) => {
const session = useSession();
- const projectId = router.query.projectId as string;
const [showSaving, setShowSaving] = useState(false);
- const [showComments, setShowComments] = useSessionStorage(
- `annotationQueueShowComments-${projectId}`,
- false,
+ const [verticalSize, setVerticalSize] = useSessionStorage(
+ `annotationQueueDrawerVertical-${item.projectId}`,
+ 60,
);
const isLockedByOtherUser = item.lockedByUserId !== session.data?.user?.id;
@@ -49,8 +59,15 @@ export const AnnotationDrawerSection: React.FC<
return (
<Card className="col-span-2 flex h-full flex-col overflow-hidden">
- <div className="grid h-full w-full grid-cols-1 grid-rows-[minmax(auto,1fr),minmax(min-content,auto)] justify-between">
- <div className="w-full overflow-auto">
+ <ResizablePanelGroup
+ direction="vertical"
+ onLayout={(sizes) => setVerticalSize(sizes[0])}
+ >
+ <ResizablePanel
+ className="w-full overflow-y-auto"
+ minSize={30}
+ defaultSize={verticalSize}
+ >
<AnnotateDrawerContent
key={"annotation-drawer-content" + item.objectId}
scoreTarget={scoreTarget}
@@ -80,17 +97,19 @@ export const AnnotationDrawerSection: React.FC<
}
isDrawerOpen={true}
/>
- </div>
- <div className="relative max-h-64 overflow-auto">
+ </ResizablePanel>
+ <ResizableHandle withHandle />
+ <ResizablePanel className="overflow-y-auto" minSize={20}>
<CommentsSection
projectId={item.projectId}
objectId={item.objectId}
objectType={item.objectType}
- showComments={showComments}
- onToggleComments={setShowComments}
+ onDraftChange={(draft) => {
+ onHasCommentDraftChange?.(draft);
+ }}
/>
- </div>
- </div>
+ </ResizablePanel>
+ </ResizablePanelGroup>
</Card>
);
};
diff --git a/web/src/features/annotation-queues/components/shared/CommentsSection.tsx b/web/src/features/annotation-queues/components/shared/CommentsSection.tsx
index 0d0e87494a..36e3630189 100644
--- a/web/src/features/annotation-queues/components/shared/CommentsSection.tsx
+++ b/web/src/features/annotation-queues/components/shared/CommentsSection.tsx
@@ -1,9 +1,3 @@
-import {
- Accordion,
- AccordionContent,
- AccordionItem,
- AccordionTrigger,
-} from "@/src/components/ui/accordion";
import { CommentList } from "@/src/features/comments/CommentList";
import { type AnnotationQueueObjectType } from "@langfuse/shared";
@@ -11,41 +5,23 @@ interface CommentsSectionProps {
projectId: string;
objectId: string;
objectType: AnnotationQueueObjectType;
- showComments: boolean;
- onToggleComments: (show: boolean) => void;
+ onDraftChange?: (hasDraft: boolean) => void;
}
export const CommentsSection: React.FC<CommentsSectionProps> = ({
projectId,
objectId,
objectType,
- showComments,
- onToggleComments,
+ onDraftChange,
}) => {
return (
- <Accordion
- type="single"
- collapsible
- className="mx-4 mt-4"
- value={showComments ? "item-1" : ""}
- onValueChange={(value) => onToggleComments(value === "item-1")}
- >
- <AccordionItem value="item-1" className="border-none">
- <div className="sticky top-0 z-10 border-b bg-background">
- <AccordionTrigger onClick={() => onToggleComments(!showComments)}>
- Comments
- </AccordionTrigger>
- </div>
- <AccordionContent>
- <CommentList
- projectId={projectId}
- objectId={objectId}
- objectType={objectType}
- className="rounded-t-none border-t-transparent"
- cardView
- />
- </AccordionContent>
- </AccordionItem>
- </Accordion>
+ <CommentList
+ projectId={projectId}
+ objectId={objectId}
+ objectType={objectType}
+ className="border-transparent p-2"
+ cardView
+ onDraftChange={onDraftChange}
+ />
);
};
diff --git a/web/src/features/comments/CommentDrawerButton.tsx b/web/src/features/comments/CommentDrawerButton.tsx
index fd881bc164..b92c6a4796 100644
--- a/web/src/features/comments/CommentDrawerButton.tsx
+++ b/web/src/features/comments/CommentDrawerButton.tsx
@@ -61,13 +61,13 @@ export function CommentDrawerButton({
</Button>
</DrawerTrigger>
<DrawerContent overlayClassName="bg-primary/10">
- <div className="mx-auto w-full overflow-y-auto md:max-h-full">
- <DrawerHeader className="sticky top-0 z-10 rounded-sm bg-background">
+ <div className="mx-auto flex h-full w-full flex-col overflow-hidden md:max-h-full">
+ <DrawerHeader className="flex-shrink-0 rounded-sm bg-background">
<DrawerTitle>
<Header title="Comments"></Header>
</DrawerTitle>
</DrawerHeader>
- <div data-vaul-no-drag className="px-2">
+ <div data-vaul-no-drag className="min-h-0 flex-1 px-2 pb-2">
<CommentList
projectId={projectId}
objectId={objectId}
diff --git a/web/src/features/comments/CommentList.tsx b/web/src/features/comments/CommentList.tsx
index 3b7f9f7ca6..7583d504ad 100644
--- a/web/src/features/comments/CommentList.tsx
+++ b/web/src/features/comments/CommentList.tsx
@@ -21,7 +21,13 @@ import { zodResolver } from "@hookform/resolvers/zod";
import { type CommentObjectType, CreateCommentData } from "@langfuse/shared";
import { ArrowUpToLine, LoaderCircle, Trash } from "lucide-react";
import { useSession } from "next-auth/react";
-import React, { useEffect, useMemo } from "react";
+import React, {
+ useCallback,
+ useEffect,
+ useMemo,
+ useRef,
+ useState,
+} from "react";
import { useForm } from "react-hook-form";
import { type z } from "zod/v4";
@@ -31,14 +37,19 @@ export function CommentList({
objectType,
cardView = false,
className,
+ onDraftChange,
}: {
projectId: string;
objectId: string;
objectType: CommentObjectType;
cardView?: boolean;
className?: string;
+ onDraftChange?: (hasDraft: boolean) => void;
}) {
const session = useSession();
+ const [textareaKey, setTextareaKey] = useState(0);
+ const commentsContainerRef = useRef<HTMLDivElement>(null);
+ const textareaRef = useRef<HTMLTextAreaElement | null>(null);
const hasReadAccess = useHasProjectAccess({
projectId,
scope: "comments:read",
@@ -73,12 +84,62 @@ export function CommentList({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [objectId, objectType]);
+ const handleTextareaResize = useCallback((target: HTMLTextAreaElement) => {
+ // Use requestAnimationFrame for optimal performance
+ requestAnimationFrame(() => {
+ if (target) {
+ target.style.height = "auto";
+ const newHeight = Math.min(target.scrollHeight, 100);
+ target.style.height = `${newHeight}px`;
+ }
+ });
+ }, []);
+
+ const debouncedResize = useCallback(() => {
+ let timeoutId: NodeJS.Timeout;
+
+ const debounced = (target: HTMLTextAreaElement) => {
+ clearTimeout(timeoutId);
+ timeoutId = setTimeout(() => handleTextareaResize(target), 16); // ~60fps
+ };
+
+ return {
+ resize: debounced,
+ cleanup: () => clearTimeout(timeoutId),
+ };
+ }, [handleTextareaResize]);
+
+ const resizeHandler = useMemo(() => debouncedResize(), [debouncedResize]);
+
+ useEffect(() => {
+ return () => {
+ resizeHandler.cleanup();
+ };
+ }, [resizeHandler]);
+
+ // Notify parent when a draft comment exists in the textarea
+ const watchedContent = form.watch("content");
+ useEffect(() => {
+ if (!onDraftChange) return;
+ onDraftChange(Boolean(watchedContent && watchedContent.trim().length > 0));
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [watchedContent]);
+
const utils = api.useUtils();
const createCommentMutation = api.comments.create.useMutation({
onSuccess: async () => {
await Promise.all([utils.comments.invalidate()]);
form.reset();
+ setTextareaKey((prev) => prev + 1); // Force textarea remount to reset height
+
+ // Scroll to top of comments list
+ if (commentsContainerRef.current) {
+ commentsContainerRef.current.scrollTo({
+ top: 0,
+ behavior: "smooth",
+ });
+ }
},
});
@@ -119,7 +180,7 @@ export function CommentList({
return (
<div
className={cn(
- "flex min-h-[9rem] items-center justify-center rounded border border-dashed p-2",
+ "flex min-h-[5rem] items-center justify-center rounded border border-dashed p-1",
className,
)}
>
@@ -131,116 +192,154 @@ export function CommentList({
);
return (
- <div className={cn(cardView && "rounded-md border", className)}>
- {cardView && (
- <div className="border-b px-3 py-1 text-sm font-medium">Comments</div>
+ <div
+ className={cn(
+ cardView && "rounded-md border",
+ "flex h-full min-h-0 flex-col",
+ className,
)}
- {hasWriteAccess && (
- <div className="mx-2 mb-2 mt-2 rounded-md border">
- <div className="flex flex-row border-b px-3 py-1 text-sm">
- <div className="flex-1 text-sm font-medium">New comment</div>
- <div className="text-xs text-muted-foreground">
- supports markdown
- </div>
- </div>
- <Form {...form}>
- <form className="relative">
- <FormField
- control={form.control}
- name="content"
- render={({ field }) => (
- <FormItem>
- <FormControl>
- <Textarea
- placeholder="Add comment..."
- {...field}
- onKeyDown={handleKeyDown} // cmd+enter to submit
- className="border-none text-sm focus:outline-none focus:ring-0 focus-visible:ring-0 focus-visible:ring-offset-0 active:ring-0"
- />
- </FormControl>
- <FormMessage className="ml-2 text-sm" />
- </FormItem>
- )}
- />
- <div className="flex justify-end">
- <Button
- type="submit"
- size="icon-xs"
- variant="outline"
- title="Submit comment"
- loading={createCommentMutation.isLoading}
- onClick={() => {
- form.handleSubmit(onSubmit)();
- }}
- className="absolute bottom-2 right-2"
- >
- <ArrowUpToLine className="h-3 w-3" />
- </Button>
- </div>
- </form>
- </Form>
+ >
+ {cardView && (
+ <div className="flex-shrink-0 border-b px-2 py-1 text-sm font-medium">
+ Comments ({comments.data?.length ?? 0})
</div>
)}
- <div className="mb-2">
- {commentsWithFormattedTimestamp?.map((comment) => (
- <div
- key={comment.id}
- className="group grid grid-cols-[auto,1fr] gap-1 p-2"
- >
- <Avatar className="mt-1 h-7 w-7">
- <AvatarImage src={comment.authorUserImage ?? undefined} />
- <AvatarFallback>
- {comment.authorUserName
- ? comment.authorUserName
- .split(" ")
- .map((word) => word[0])
- .slice(0, 2)
- .concat("")
- : (comment.authorUserId ?? "U")}
- </AvatarFallback>
- </Avatar>
- <div className="relative rounded-md">
- <div className="flex h-8 flex-row items-center justify-between px-1 py-1 text-sm">
- <div className="text-sm font-medium">
- {comment.authorUserName ?? comment.authorUserId ?? "User"}
- </div>
- <div className="flex flex-row items-center gap-2">
- <div className="text-xs text-muted-foreground">
- {comment.timestamp}
- </div>
- <div className="hidden min-h-6 justify-end group-hover:flex">
- {session.data?.user?.id === comment.authorUserId && (
- <Button
- type="button"
- size="icon-xs"
- variant="destructive"
- title="Delete comment"
- loading={deleteCommentMutation.isLoading}
- className="-mr-2"
- onClick={() => {
- if (
- confirm(
- "Are you sure you want to delete this comment?",
- )
- )
- deleteCommentMutation.mutateAsync({
- commentId: comment.id,
- projectId,
- objectId,
- objectType,
- });
- }}
- >
- <Trash className="h-3 w-3" />
- </Button>
- )}
+ <div className="flex min-h-0 flex-1 flex-col">
+ <div
+ ref={commentsContainerRef}
+ className="min-h-0 flex-1 overflow-y-auto"
+ >
+ <div className="p-1">
+ {commentsWithFormattedTimestamp?.map((comment) => (
+ <div
+ key={comment.id}
+ className="group grid grid-cols-[auto,1fr] gap-1 p-1"
+ >
+ <Avatar className="mt-0.5 h-6 w-6">
+ <AvatarImage src={comment.authorUserImage ?? undefined} />
+ <AvatarFallback>
+ {comment.authorUserName
+ ? comment.authorUserName
+ .split(" ")
+ .map((word) => word[0])
+ .slice(0, 2)
+ .concat("")
+ : (comment.authorUserId ?? "U")}
+ </AvatarFallback>
+ </Avatar>
+ <div className="relative rounded-md">
+ <div className="flex h-6 flex-row items-center justify-between px-1 py-0 text-sm">
+ <div className="text-sm font-medium">
+ {comment.authorUserName ?? comment.authorUserId ?? "User"}
+ </div>
+ <div className="flex flex-row items-center gap-2">
+ <div className="text-xs text-muted-foreground">
+ {comment.timestamp}
+ </div>
+ <div className="hidden min-h-5 justify-end group-hover:flex">
+ {session.data?.user?.id === comment.authorUserId && (
+ <Button
+ type="button"
+ size="icon-xs"
+ variant="ghost"
+ title="Delete comment"
+ loading={deleteCommentMutation.isLoading}
+ className="-mr-1"
+ onClick={() => {
+ if (
+ confirm(
+ "Are you sure you want to delete this comment?",
+ )
+ )
+ deleteCommentMutation.mutateAsync({
+ commentId: comment.id,
+ projectId,
+ objectId,
+ objectType,
+ });
+ }}
+ >
+ <Trash className="h-3 w-3" />
+ </Button>
+ )}
+ </div>
+ </div>
</div>
+ <MarkdownView markdown={comment.content} />
</div>
</div>
- <MarkdownView markdown={comment.content} />
+ ))}
+ </div>
+ </div>
+
+ {hasWriteAccess && (
+ <div className="mx-2 my-1 flex-shrink-0 rounded-md border">
+ <div className="flex flex-row border-b px-2 py-1 text-xs">
+ <div className="flex-1 font-medium">New comment</div>
+ <div className="text-xs text-muted-foreground">
+ supports markdown
+ </div>
</div>
+ <Form {...form}>
+ <form className="relative">
+ <FormField
+ control={form.control}
+ name="content"
+ render={({ field }) => (
+ <FormItem>
+ <FormControl>
+ <Textarea
+ key={textareaKey} // remount textarea to reset height after submission
+ placeholder="Add comment..."
+ {...field}
+ ref={(el) => {
+ if (textareaRef.current !== el) {
+ textareaRef.current = el;
+ }
+ // Call the field ref if it exists (for react-hook-form)
+ if (typeof field.ref === "function") {
+ field.ref(el);
+ }
+ }}
+ onKeyDown={handleKeyDown}
+ className="max-h-[100px] min-h-[2.25rem] w-full resize-none overflow-hidden border-none py-2 pr-7 text-sm leading-tight focus:outline-none focus:ring-0 focus-visible:ring-0 focus-visible:ring-offset-0 active:ring-0"
+ style={{
+ whiteSpace: "pre-wrap",
+ wordWrap: "break-word",
+ overflowWrap: "break-word",
+ height: "auto",
+ minHeight: "2.25rem",
+ }}
+ onInput={(e) => {
+ const target = e.target as HTMLTextAreaElement;
+ resizeHandler.resize(target);
+ }}
+ autoFocus
+ />
+ </FormControl>
+ <FormMessage className="ml-2 text-sm" />
+ </FormItem>
+ )}
+ />
+ <div className="flex justify-end">
+ <Button
+ type="submit"
+ size="icon-xs"
+ variant="outline"
+ title="Submit comment"
+ loading={createCommentMutation.isLoading}
+ onClick={() => {
+ form.handleSubmit(onSubmit)();
+ }}
+ className="absolute bottom-1 right-1"
+ >
+ <ArrowUpToLine className="h-3 w-3" />
+ </Button>
+ </div>
+ </form>
+ </Form>
</div>
- ))}
+ )}
</div>
</div>
);