Skip to content

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

refactor(web): Clean up `CommentDrawerButton` (#16570)

Ben Bachemd41a33cfe8ee
diff --git a/web/src/components/session/SessionTraceActionButtons.tsx b/web/src/components/session/SessionTraceActionButtons.tsx
index 6d8584e829..d272384d17 100644
--- a/web/src/components/session/SessionTraceActionButtons.tsx
+++ b/web/src/components/session/SessionTraceActionButtons.tsx
@@ -1,14 +1,15 @@
/* eslint-disable @repo/no-style-props */
import { type RouterOutputs } from "@/src/utils/api";
import { getNumberFromMap } from "@/src/utils/map-utils";
+import { ActionButtonCountBadge } from "@/src/components/ui/action-button-count-badge";
+import { Button } from "@/src/components/ui/button";
import { AnnotateDrawer } from "@/src/features/scores/components/AnnotateDrawer";
-import { CommentDrawerButton } from "@/src/features/comments/CommentDrawerButton";
+import { CommentDrawerController } from "@/src/features/comments/CommentDrawerController";
import { NewDatasetItemFromTraceId } from "@/src/components/session/NewDatasetItemFromTrace";
-import { Button } from "@/src/components/ui/button";
import { AnnotationQueueItemDropdownMenuController } from "@/src/features/annotation-queues/components/AnnotationQueueItemDropdownMenuController";
import { AnnotationQueueItemCountBadge } from "@/src/features/annotation-queues/components/AnnotationQueueItemCountBadge";
-import { ChevronDown } from "lucide-react";
import { cn } from "@/src/utils/tailwind";
+import { ChevronDown, MessageSquare, MessageSquareOff } from "lucide-react";
type TraceScores =
RouterOutputs["sessions"]["byIdWithScores"]["traces"][number]["scores"];
@@ -33,6 +34,7 @@ export function SessionTraceActionButtons({
className?: string;
}) {
const size = density === "compact" ? "xs" : "default";
+ const commentCount = getNumberFromMap(traceCommentCounts, traceId);
return (
<div className={cn("flex flex-wrap items-start gap-2", className)}>
@@ -86,14 +88,35 @@ export function SessionTraceActionButtons({
)}
</AnnotationQueueItemDropdownMenuController>
</div>
- <CommentDrawerButton
+ <CommentDrawerController
projectId={projectId}
- variant="outline"
objectId={traceId}
objectType="TRACE"
- count={getNumberFromMap(traceCommentCounts, traceId)}
- size={size}
- />
+ count={commentCount}
+ >
+ {({ disabled, openDrawer }) => (
+ <Button
+ type="button"
+ variant="outline"
+ size={size}
+ disabled={disabled}
+ onClick={openDrawer}
+ className="gap-1"
+ >
+ {disabled ? (
+ <MessageSquareOff className="text-muted-foreground h-4 w-4" />
+ ) : (
+ <>
+ <MessageSquare className="h-4 w-4" />
+ <span>Add comment</span>
+ {!!commentCount ? (
+ <ActionButtonCountBadge count={commentCount} />
+ ) : null}
+ </>
+ )}
+ </Button>
+ )}
+ </CommentDrawerController>
</div>
);
}
diff --git a/web/src/components/session/index.tsx b/web/src/components/session/index.tsx
index d093c1bfbc..447036e395 100644
--- a/web/src/components/session/index.tsx
+++ b/web/src/components/session/index.tsx
@@ -15,8 +15,9 @@ import { useRouter } from "next/router";
import React, { useEffect, useState, useCallback, useRef } from "react";
import { useVirtualizer } from "@tanstack/react-virtual";
import { AnnotateDrawer } from "@/src/features/scores/components/AnnotateDrawer";
+import { ActionButtonCountBadge } from "@/src/components/ui/action-button-count-badge";
import { Button } from "@/src/components/ui/button";
-import { CommentDrawerButton } from "@/src/features/comments/CommentDrawerButton";
+import { CommentDrawerController } from "@/src/features/comments/CommentDrawerController";
import { useSession } from "next-auth/react";
import {
CheckIcon,
@@ -25,6 +26,8 @@ import {
CopyIcon,
Download,
ExternalLinkIcon,
+ MessageSquare,
+ MessageSquareOff,
ListPlus,
MoreVertical,
} from "lucide-react";
@@ -548,14 +551,45 @@ export const SessionPage: React.FC<{
listKey="sessions"
/>
)}
- <CommentDrawerButton
+ <CommentDrawerController
key="comment"
- variant="outline"
projectId={projectId}
objectId={sessionId}
objectType="SESSION"
count={getNumberFromMap(sessionCommentCounts.data, sessionId)}
- />
+ >
+ {({ disabled, openDrawer }) => (
+ <Button
+ type="button"
+ variant="outline"
+ disabled={disabled}
+ onClick={openDrawer}
+ className="gap-1"
+ >
+ {disabled ? (
+ <MessageSquareOff className="text-muted-foreground h-4 w-4" />
+ ) : (
+ <>
+ <MessageSquare className="h-4 w-4" />
+ <span>Add comment</span>
+ {getNumberFromMap(
+ sessionCommentCounts.data,
+ sessionId,
+ ) ? (
+ <ActionButtonCountBadge
+ count={
+ getNumberFromMap(
+ sessionCommentCounts.data,
+ sessionId,
+ ) ?? 0
+ }
+ />
+ ) : null}
+ </>
+ )}
+ </Button>
+ )}
+ </CommentDrawerController>
<div className="flex items-start">
<AnnotateDrawer
projectId={projectId}
@@ -618,14 +652,41 @@ export const SessionPage: React.FC<{
label="Share"
/>
<CopySessionIdButton sessionId={sessionId} layout="menu" />
- <CommentDrawerButton
- variant="outline"
+ <CommentDrawerController
projectId={projectId}
objectId={sessionId}
objectType="SESSION"
count={getNumberFromMap(sessionCommentCounts.data, sessionId)}
- layout="menu"
- />
+ >
+ {({ disabled, openDrawer }) => (
+ <Button
+ type="button"
+ variant="ghost"
+ size="sm"
+ disabled={disabled}
+ onClick={openDrawer}
+ className="w-full justify-start gap-2 font-normal"
+ >
+ {disabled ? (
+ <MessageSquareOff className="text-muted-foreground h-4 w-4" />
+ ) : (
+ <MessageSquare className="h-4 w-4" />
+ )}
+ <span className="text-sm">Add comment</span>
+ {!disabled &&
+ getNumberFromMap(sessionCommentCounts.data, sessionId) ? (
+ <ActionButtonCountBadge
+ count={
+ getNumberFromMap(
+ sessionCommentCounts.data,
+ sessionId,
+ ) ?? 0
+ }
+ />
+ ) : null}
+ </Button>
+ )}
+ </CommentDrawerController>
<AnnotateDrawer
projectId={projectId}
scoreTarget={{
@@ -1460,14 +1521,45 @@ const LoadedSessionEventsPage: React.FC<{
listKey="sessions"
/>
)}
- <CommentDrawerButton
+ <CommentDrawerController
key="comment"
- variant="outline"
projectId={projectId}
objectId={sessionId}
objectType="SESSION"
count={getNumberFromMap(sessionCommentCounts.data, sessionId)}
- />
+ >
+ {({ disabled, openDrawer }) => (
+ <Button
+ type="button"
+ variant="outline"
+ disabled={disabled}
+ onClick={openDrawer}
+ className="gap-1"
+ >
+ {disabled ? (
+ <MessageSquareOff className="text-muted-foreground h-4 w-4" />
+ ) : (
+ <>
+ <MessageSquare className="h-4 w-4" />
+ <span>Add comment</span>
+ {getNumberFromMap(
+ sessionCommentCounts.data,
+ sessionId,
+ ) ? (
+ <ActionButtonCountBadge
+ count={
+ getNumberFromMap(
+ sessionCommentCounts.data,
+ sessionId,
+ ) ?? 0
+ }
+ />
+ ) : null}
+ </>
+ )}
+ </Button>
+ )}
+ </CommentDrawerController>
<div className="flex items-start">
<AnnotateDrawer
projectId={projectId}
@@ -1553,14 +1645,41 @@ const LoadedSessionEventsPage: React.FC<{
label="Share"
/>
<CopySessionIdButton sessionId={sessionId} layout="menu" />
- <CommentDrawerButton
- variant="outline"
+ <CommentDrawerController
projectId={projectId}
objectId={sessionId}
objectType="SESSION"
count={getNumberFromMap(sessionCommentCounts.data, sessionId)}
- layout="menu"
- />
+ >
+ {({ disabled, openDrawer }) => (
+ <Button
+ type="button"
+ variant="ghost"
+ size="sm"
+ disabled={disabled}
+ onClick={openDrawer}
+ className="w-full justify-start gap-2 font-normal"
+ >
+ {disabled ? (
+ <MessageSquareOff className="text-muted-foreground h-4 w-4" />
+ ) : (
+ <MessageSquare className="h-4 w-4" />
+ )}
+ <span className="text-sm">Add comment</span>
+ {!disabled &&
+ getNumberFromMap(sessionCommentCounts.data, sessionId) ? (
+ <ActionButtonCountBadge
+ count={
+ getNumberFromMap(
+ sessionCommentCounts.data,
+ sessionId,
+ ) ?? 0
+ }
+ />
+ ) : null}
+ </Button>
+ )}
+ </CommentDrawerController>
<AnnotateDrawer
projectId={projectId}
scoreTarget={{
diff --git a/web/src/features/annotation-queues/components/processors/SessionAnnotationProcessor.tsx b/web/src/features/annotation-queues/components/processors/SessionAnnotationProcessor.tsx
index d751085891..165f21c40a 100644
--- a/web/src/features/annotation-queues/components/processors/SessionAnnotationProcessor.tsx
+++ b/web/src/features/annotation-queues/components/processors/SessionAnnotationProcessor.tsx
@@ -9,6 +9,7 @@ import { LazyTraceEventsRow } from "@/src/components/session/TraceEventsRow";
import { asCommentCounts } from "@/src/components/session/sessionDetailPageTypes";
import { useState, useMemo, useCallback } from "react";
import { Button } from "@/src/components/ui/button";
+import { ActionButtonCountBadge } from "@/src/components/ui/action-button-count-badge";
import { ItemBadge } from "@/src/components/ItemBadge";
import { CopyIdsPopover } from "@/src/features/traces";
import { Badge } from "@/src/components/ui/badge";
@@ -18,8 +19,9 @@ import { Card } from "@/src/components/ui/card";
import { useV4Beta } from "@/src/features/events/hooks/useV4Beta";
import { api } from "@/src/utils/api";
import { JsonSkeleton } from "@/src/components/ui/CodeJsonViewer";
-import { CommentDrawerButton } from "@/src/features/comments/CommentDrawerButton";
+import { CommentDrawerController } from "@/src/features/comments/CommentDrawerController";
import { getNumberFromMap } from "@/src/utils/map-utils";
+import { MessageSquare, MessageSquareOff } from "lucide-react";
type SessionAnnotationQueueItem = AnnotationQueueItem & {
parentTraceId?: string | null;
@@ -132,13 +134,44 @@ export const SessionAnnotationProcessor: React.FC<
idItems={[{ id: item.objectId, name: "Session ID" }]}
/>
</div>
- <CommentDrawerButton
+ <CommentDrawerController
projectId={projectId}
- variant="outline"
objectId={item.objectId}
objectType="SESSION"
count={getNumberFromMap(sessionCommentCounts.data, item.objectId)}
- />
+ >
+ {({ disabled, openDrawer }) => (
+ <Button
+ type="button"
+ variant="outline"
+ disabled={disabled}
+ onClick={openDrawer}
+ className="gap-1"
+ >
+ {disabled ? (
+ <MessageSquareOff className="text-muted-foreground h-4 w-4" />
+ ) : (
+ <>
+ <MessageSquare className="h-4 w-4" />
+ <span>Add comment</span>
+ {getNumberFromMap(
+ sessionCommentCounts.data,
+ item.objectId,
+ ) ? (
+ <ActionButtonCountBadge
+ count={
+ getNumberFromMap(
+ sessionCommentCounts.data,
+ item.objectId,
+ ) ?? 0
+ }
+ />
+ ) : null}
+ </>
+ )}
+ </Button>
+ )}
+ </CommentDrawerController>
</div>
<div className="mt-2 mb-4 grid w-full min-w-0 items-center justify-between px-4">
<div className="flex max-w-full min-w-0 shrink flex-col">
diff --git a/web/src/features/comments/CommentCountIcon.tsx b/web/src/features/comments/CommentCountIcon.tsx
index b430b7bb96..f07c44fe1d 100644
--- a/web/src/features/comments/CommentCountIcon.tsx
+++ b/web/src/features/comments/CommentCountIcon.tsx
@@ -1,5 +1,5 @@
// LFE-7628: general comment counts use the square speech bubble, matching the
-// CommentDrawerButton, to stay distinct from per-score comment indicators.
+// CommentDrawerController, to stay distinct from per-score comment indicators.
import { MessageSquareMore } from "lucide-react";
export function CommentCountIcon({ count }: { count?: number }) {
diff --git a/web/src/features/comments/CommentDrawerButton.tsx b/web/src/features/comments/CommentDrawerButton.tsx
deleted file mode 100644
index 4b80d36af9..0000000000
--- a/web/src/features/comments/CommentDrawerButton.tsx
+++ /dev/null
@@ -1,238 +0,0 @@
-/* eslint-disable @repo/no-style-props, @repo/no-abstracted-overlay-trigger */
-import Header from "@/src/components/layouts/header";
-import { ActionButtonCountBadge } from "@/src/components/ui/action-button-count-badge";
-import { Button, type ButtonProps } from "@/src/components/ui/button";
-import {
- Drawer,
- DrawerContent,
- DrawerHeader,
- DrawerTitle,
- DrawerTrigger,
-} from "@/src/components/ui/drawer";
-import { CommentList } from "@/src/features/comments/CommentList";
-import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
-import { type CommentObjectType } from "@langfuse/shared";
-// LFE-7628: general (trace/observation/session) comments use a square speech
-// bubble to stay visually distinct from per-score comments, which use the round
-// MessageCircle bubble in the annotation form.
-import { MessageSquare, MessageSquareOff } from "lucide-react";
-import React, { useState, useEffect, useRef } from "react";
-import { useRouter } from "next/router";
-import { type SelectionData } from "./contexts/InlineCommentSelectionContext";
-
-export function CommentDrawerButton({
- projectId,
- objectId,
- objectType,
- count,
- variant = "secondary",
- className,
- size = "default",
- pendingSelection,
- onSelectionUsed,
- onCommentChange,
- isOpen: controlledIsOpen,
- onOpenChange: controlledOnOpenChange,
- layout = "toolbar",
-}: {
- projectId: string;
- objectId: string;
- objectType: CommentObjectType;
- count?: number;
- variant?: ButtonProps["variant"];
- className?: string;
- size?: ButtonProps["size"];
- pendingSelection?: SelectionData | null;
- onSelectionUsed?: () => void;
- onCommentChange?: () => void | Promise<void>;
- isOpen?: boolean;
- onOpenChange?: (open: boolean) => void;
- /**
- * "toolbar" (default) is the inline button; "menu" renders the same drawer
- * trigger as a full-width labeled row for the mobile header overflow popover.
- */
- layout?: "toolbar" | "menu";
-}) {
- const isMenu = layout === "menu";
- const router = useRouter();
- const [isMentionDropdownOpen, setIsMentionDropdownOpen] = useState(false);
- const [internalIsDrawerOpen, setInternalIsDrawerOpen] = useState(false);
- const hasAutoOpenedRef = useRef(false); // Track if we've already auto-opened for current deep link
-
- const isDrawerOpen = controlledIsOpen ?? internalIsDrawerOpen;
- const setIsDrawerOpen = controlledOnOpenChange ?? setInternalIsDrawerOpen;
- const hasFocusedRef = useRef(false); // Track if we've already focused the drawer
-
- const hasReadAccess = useHasProjectAccess({
- projectId,
- scope: "comments:read",
- });
- const hasWriteAccess = useHasProjectAccess({
- projectId,
- scope: "comments:CUD",
- });
-
- // Auto-open drawer when comments=open query param is present AND matches this drawer's object
- useEffect(() => {
- const shouldAutoOpen =
- router.query.comments === "open" &&
- router.query.commentObjectType === objectType &&
- router.query.commentObjectId === objectId &&
- hasReadAccess &&
- !isDrawerOpen &&
- !hasAutoOpenedRef.current;
-
- // Only open if drawer is not already open AND we haven't auto-opened yet for this deep link
- if (shouldAutoOpen) {
- hasAutoOpenedRef.current = true;
- setIsDrawerOpen(true);
-
- // Scroll to specific comment if hash is present
- if (router.asPath.includes("#comment-")) {
- // Wait for drawer animation to complete before scrolling
- setTimeout(() => {
- const hash = router.asPath.split("#")[1];
- const element = document.getElementById(hash);
- if (element) {
- element.scrollIntoView({ behavior: "smooth", block: "center" });
- }
- }, 300);
- }
- }
-
- // Reset the flag when query params are cleared (user navigated away from deep link)
- if (router.query.comments !== "open" && hasAutoOpenedRef.current) {
- hasAutoOpenedRef.current = false;
- }
- }, [
- router.query.comments,
- router.query.commentObjectType,
- router.query.commentObjectId,
- router.asPath,
- hasReadAccess,
- objectType,
- objectId,
- isDrawerOpen,
- setIsDrawerOpen,
- ]);
-
- if (!hasReadAccess || (!hasWriteAccess && !count))
- return (
- <Button
- type="button"
- variant={isMenu ? "ghost" : "secondary"}
- size={isMenu ? "sm" : size}
- className={
- isMenu ? "w-full justify-start gap-2 font-normal" : className
- }
- disabled
- >
- <MessageSquareOff
- className={
- isMenu
- ? "text-muted-foreground h-4 w-4"
- : size === "sm"
- ? "text-muted-foreground h-3.5 w-3.5"
- : "text-muted-foreground h-4 w-4"
- }
- />
- {isMenu ? <span className="text-sm">Add comment</span> : null}
- </Button>
- );
-
- return (
- <Drawer
- open={isDrawerOpen}
- onOpenChange={(open) => {
- // Prevent drawer from closing when mention dropdown is open
- if (!open && isMentionDropdownOpen) {
- // Keep drawer open
- return;
- }
- setIsDrawerOpen(open);
-
- // Reset focus tracking when drawer closes
- if (!open) {
- hasFocusedRef.current = false;
- }
-
- // Clear URL parameters and hash when drawer is closed
- if (!open && router.query.comments === "open") {
- const { comments, commentObjectType, commentObjectId, ...rest } =
- router.query;
- router.replace(
- {
- pathname: router.pathname,
- query: rest,
- },
- undefined,
- { shallow: true },
- );
- }
- }}
- >
- <DrawerTrigger asChild>
- <Button
- type="button"
- variant={isMenu ? "ghost" : variant}
- size={isMenu ? "sm" : size}
- className={
- isMenu ? "w-full justify-start gap-2 font-normal" : className
- }
- id="comment-drawer-button"
- >
- <div
- className={
- isMenu ? "flex items-center gap-2" : "flex items-center gap-1"
- }
- >
- <MessageSquare
- className={
- isMenu ? "h-4 w-4" : size === "sm" ? "h-3.5 w-3.5" : "h-4 w-4"
- }
- />
- <span className={isMenu ? "text-sm" : undefined}>Add comment</span>
- {!!count ? <ActionButtonCountBadge count={count} /> : null}
- </div>
- </Button>
- </DrawerTrigger>
- <DrawerContent
- overlayClassName="bg-primary/10"
- className="h-screen-with-banner max-h-screen-with-banner overflow-hidden"
- >
- <div
- className="mx-auto flex h-full w-full flex-col overflow-hidden focus:ring-0 focus:outline-hidden focus-visible:ring-0 focus-visible:outline-hidden md:max-h-full"
- tabIndex={-1}
- ref={(el) => {
- // Auto-focus drawer content when it opens (only once)
- if (el && isDrawerOpen && !hasFocusedRef.current) {
- hasFocusedRef.current = true;
- setTimeout(() => el.focus({ preventScroll: true }), 100);
- }
- }}
- >
- <DrawerHeader className="bg-background sr-only shrink-0 rounded-sm">
- <DrawerTitle>
- <Header title="Comments"></Header>
- </DrawerTitle>
- </DrawerHeader>
- <div
- data-vaul-no-drag
- className="min-h-0 flex-1 overflow-hidden px-2 py-2"
- >
- <CommentList
- projectId={projectId}
- objectId={objectId}
- objectType={objectType}
- onMentionDropdownChange={setIsMentionDropdownOpen}
- isDrawerOpen={isDrawerOpen}
- pendingSelection={pendingSelection}
- onSelectionUsed={onSelectionUsed}
- onCommentChange={onCommentChange}
- />
- </div>
- </div>
- </DrawerContent>
- </Drawer>
- );
-}
diff --git a/web/src/features/comments/CommentDrawerController.tsx b/web/src/features/comments/CommentDrawerController.tsx
new file mode 100644
index 0000000000..fc1795c61c
--- /dev/null
+++ b/web/src/features/comments/CommentDrawerController.tsx
@@ -0,0 +1,195 @@
+import Header from "@/src/components/layouts/header";
+import {
+ Drawer,
+ DrawerContent,
+ DrawerHeader,
+ DrawerTitle,
+} from "@/src/components/ui/drawer";
+import { CommentList } from "@/src/features/comments/CommentList";
+import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
+import { type CommentObjectType } from "@langfuse/shared";
+import { useRouter } from "next/router";
+import { type ReactNode, useEffect, useRef, useState } from "react";
+import { type SelectionData } from "./contexts/InlineCommentSelectionContext";
+
+type CommentDrawerContentProps = {
+ projectId: string;
+ objectId: string;
+ objectType: CommentObjectType;
+ isOpen: boolean;
+ pendingSelection?: SelectionData | null;
+ onSelectionUsed?: () => void;
+ onCommentChange?: () => void | Promise<void>;
+ onMentionDropdownChange: (isOpen: boolean) => void;
+};
+
+function CommentDrawerContent({
+ projectId,
+ objectId,
+ objectType,
+ isOpen,
+ pendingSelection,
+ onSelectionUsed,
+ onCommentChange,
+ onMentionDropdownChange,
+}: CommentDrawerContentProps) {
+ const hasFocusedRef = useRef(false);
+
+ return (
+ <DrawerContent
+ overlayClassName="bg-primary/10"
+ className="h-screen-with-banner max-h-screen-with-banner overflow-hidden"
+ >
+ <div
+ className="mx-auto flex h-full w-full flex-col overflow-hidden focus:ring-0 focus:outline-hidden focus-visible:ring-0 focus-visible:outline-hidden md:max-h-full"
+ tabIndex={-1}
+ ref={(element) => {
+ if (element && isOpen && !hasFocusedRef.current) {
+ hasFocusedRef.current = true;
+ setTimeout(() => element.focus({ preventScroll: true }), 100);
+ }
+ }}
+ >
+ <DrawerHeader className="bg-background sr-only shrink-0 rounded-sm">
+ <DrawerTitle>
+ <Header title="Comments" />
+ </DrawerTitle>
+ </DrawerHeader>
+ <div
+ data-vaul-no-drag
+ className="min-h-0 flex-1 overflow-hidden px-2 py-2"
+ >
+ <CommentList
+ projectId={projectId}
+ objectId={objectId}
+ objectType={objectType}
+ onMentionDropdownChange={onMentionDropdownChange}
+ isDrawerOpen={isOpen}
+ pendingSelection={pendingSelection}
+ onSelectionUsed={onSelectionUsed}
+ onCommentChange={onCommentChange}
+ />
+ </div>
+ </div>
+ </DrawerContent>
+ );
+}
+
+export type CommentDrawerControllerProps = {
+ projectId: string;
+ objectId: string;
+ objectType: CommentObjectType;
+ count?: number;
+ pendingSelection?: SelectionData | null;
+ onSelectionUsed?: () => void;
+ onCommentChange?: () => void | Promise<void>;
+ isOpen?: boolean;
+ onOpenChange?: (open: boolean) => void;
+ children: (control: {
+ disabled: boolean;
+ openDrawer: () => void;
+ }) => ReactNode;
+};
+
+export function CommentDrawerController({
+ children,
+ projectId,
+ objectId,
+ objectType,
+ count,
+ pendingSelection,
+ onSelectionUsed,
+ onCommentChange,
+ isOpen: controlledIsOpen,
+ onOpenChange: controlledOnOpenChange,
+}: CommentDrawerControllerProps) {
+ const router = useRouter();
+ const [isMentionDropdownOpen, setIsMentionDropdownOpen] = useState(false);
+ const [internalIsOpen, setInternalIsOpen] = useState(false);
+ const hasAutoOpenedRef = useRef(false);
+ const isOpen = controlledIsOpen ?? internalIsOpen;
+ const setIsOpen = controlledOnOpenChange ?? setInternalIsOpen;
+
+ const hasReadAccess = useHasProjectAccess({
+ projectId,
+ scope: "comments:read",
+ });
+ const hasWriteAccess = useHasProjectAccess({
+ projectId,
+ scope: "comments:CUD",
+ });
+ const disabled = !hasReadAccess || (!hasWriteAccess && !count);
+
+ useEffect(() => {
+ const shouldAutoOpen =
+ router.query.comments === "open" &&
+ router.query.commentObjectType === objectType &&
+ router.query.commentObjectId === objectId &&
+ hasReadAccess &&
+ !isOpen &&
+ !hasAutoOpenedRef.current;
+
+ if (shouldAutoOpen) {
+ hasAutoOpenedRef.current = true;
+ setIsOpen(true);
+
+ if (router.asPath.includes("#comment-")) {
+ setTimeout(() => {
+ const hash = router.asPath.split("#")[1];
+ document.getElementById(hash)?.scrollIntoView({
+ behavior: "smooth",
+ block: "center",
+ });
+ }, 300);
+ }
+ }
+
+ if (router.query.comments !== "open" && hasAutoOpenedRef.current) {
+ hasAutoOpenedRef.current = false;
+ }
+ }, [
+ router.query.comments,
+ router.query.commentObjectType,
+ router.query.commentObjectId,
+ router.asPath,
+ hasReadAccess,
+ objectType,
+ objectId,
+ isOpen,
+ setIsOpen,
+ ]);
+
+ const openDrawer = () => {
+ if (!disabled) setIsOpen(true);
+ };
+
+ const handleOpenChange = (open: boolean) => {
+ if (!open && isMentionDropdownOpen) return;
+
+ setIsOpen(open);
+
+ if (!open && router.query.comments === "open") {
+ const { comments, commentObjectType, commentObjectId, ...rest } =
+ router.query;
+ router.replace({ pathname: router.pathname, query: rest }, undefined, {
+ shallow: true,
+ });
+ }
+ };
+
+ return (
+ <Drawer open={hasReadAccess && isOpen} onOpenChange={handleOpenChange}>
+ {children({ disabled, openDrawer })}
+ <CommentDrawerContent
+ projectId={projectId}
+ objectId={objectId}
+ objectType={objectType}
+ isOpen={isOpen}
+ pendingSelection={pendingSelection}
+ onSelectionUsed={onSelectionUsed}
+ onCommentChange={onCommentChange}
+ onMentionDropdownChange={setIsMentionDropdownOpen}
+ />
+ </Drawer>
+ );
+}
diff --git a/web/src/features/prompts/components/prompt-detail.tsx b/web/src/features/prompts/components/prompt-detail.tsx
index 671aceaab6..325ec4f0fb 100644
--- a/web/src/features/prompts/components/prompt-detail.tsx
+++ b/web/src/features/prompts/components/prompt-detail.tsx
@@ -36,9 +36,16 @@ import { ChatMlArraySchema } from "@/src/components/schemas/ChatMlSchema";
import LegacyGenerations from "@/src/components/table/use-cases/observations";
import EventsTable from "@/src/features/events/components/EventsTable";
import { useV4Beta } from "@/src/features/events/hooks/useV4Beta";
-import { FlaskConical, MoreVertical, Plus } from "lucide-react";
+import {
+ FlaskConical,
+ MessageSquare,
+ MessageSquareOff,
+ MoreVertical,
+ Plus,
+} from "lucide-react";
import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
import { Button } from "@/src/components/ui/button";
+import { ActionButtonCountBadge } from "@/src/components/ui/action-button-count-badge";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import {
Dialog,
@@ -59,7 +66,7 @@ import {
import { DeletePromptVersion } from "@/src/features/prompts/components/delete-prompt-version";
import { TagPromptDetailsPopover } from "@/src/features/tag/components/TagPromptDetailsPopover";
import { SetPromptVersionLabels } from "@/src/features/prompts/components/SetPromptVersionLabels";
-import { CommentDrawerButton } from "@/src/features/comments/CommentDrawerButton";
+import { CommentDrawerController } from "@/src/features/comments/CommentDrawerController";
import { Command, CommandInput } from "@/src/components/ui/command";
import {
PromptReferenceProvider,
@@ -443,16 +450,41 @@ export const PromptDetail = ({
</DialogContent>
</Dialog>
)}
- <CommentDrawerButton
+ <CommentDrawerController
projectId={projectId as string}
objectId={prompt.id}
objectType="PROMPT"
count={getNumberFromMap(commentCounts, prompt.id)}
- variant="outline"
onCommentChange={() =>
utils.prompts.allVersions.invalidate(promptHistoryInput)
}
- />
+ >
+ {({ disabled, openDrawer }) => (
+ <Button
+ type="button"
+ variant="outline"
+ disabled={disabled}
+ onClick={openDrawer}
+ className="gap-1"
+ >
+ {disabled ? (
+ <MessageSquareOff className="text-muted-foreground h-4 w-4" />
+ ) : (
+ <>
+ <MessageSquare className="h-4 w-4" />
+ <span>Add comment</span>
+ {getNumberFromMap(commentCounts, prompt.id) ? (
+ <ActionButtonCountBadge
+ count={
+ getNumberFromMap(commentCounts, prompt.id) ?? 0
+ }
+ />
+ ) : null}
+ </>
+ )}
+ </Button>
+ )}
+ </CommentDrawerController>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" size="icon">
diff --git a/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader.tsx b/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader.tsx
index db04387d2f..121c534a1f 100644
--- a/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader.tsx
+++ b/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader.tsx
@@ -22,9 +22,9 @@ import { ItemBadge } from "@/src/components/ItemBadge";
import { LocalIsoDate } from "@/src/components/LocalIsoDate";
import { NewDatasetItemFromExistingObject } from "@/src/features/datasets/components/NewDatasetItemFromExistingObject";
import { AnnotateDrawer } from "@/src/features/scores/components/AnnotateDrawer";
+import { CommentDrawerController } from "@/src/features/comments/CommentDrawerController";
import { AnnotationQueueItemDropdownMenuController } from "@/src/features/annotation-queues/components/AnnotationQueueItemDropdownMenuController";
import { AnnotationQueueItemCountBadge } from "@/src/features/annotation-queues/components/AnnotationQueueItemCountBadge";
-import { CommentDrawerButton } from "@/src/features/comments/CommentDrawerButton";
import { JumpToPlaygroundButton } from "@/src/features/playground/page/components/JumpToPlaygroundButton";
import { PromptBadge } from "@/src/features/traces/components/PromptBadge";
import {
@@ -51,11 +51,14 @@ import { useViewPreferences } from "@/src/features/traces/contexts/ViewPreferenc
import { useV4Beta } from "@/src/features/events/hooks/useV4Beta";
import { useTraceData } from "@/src/features/traces/contexts/TraceDataContext";
import { Button } from "@/src/components/ui/button";
+import { ActionButtonCountBadge } from "@/src/components/ui/action-button-count-badge";
import {
ChevronDown,
EllipsisVertical,
ListPlus,
LockIcon,
+ MessageSquare,
+ MessageSquareOff,
MoreHorizontal,
SquarePen,
} from "lucide-react";
@@ -196,7 +199,7 @@ export const ObservationDetailViewHeader = memo(
</PopoverTrigger>
<PopoverContent
align="end"
- // forceMount + hide-when-closed: CommentDrawerButton lives in
+ // forceMount + hide-when-closed: CommentDrawerController lives in
// here, and its deep-link auto-open effect (?comments=open) and
// controlled inline-selection flow only work while mounted. A
// default Popover unmounts its content when closed (the default
@@ -297,17 +300,37 @@ export const ObservationDetailViewHeader = memo(
layout="menu"
/>
)}
- <CommentDrawerButton
+ <CommentDrawerController
projectId={projectId}
objectId={observation.id}
objectType="OBSERVATION"
count={commentCount}
- layout="menu"
pendingSelection={pendingSelection}
onSelectionUsed={onSelectionUsed}
isOpen={isCommentDrawerOpen}
onOpenChange={onCommentDrawerOpenChange}
- />
+ >
+ {({ disabled, openDrawer }) => (
+ <Button
+ type="button"
+ variant="ghost"
+ size="sm"
+ disabled={disabled}
+ onClick={openDrawer}
+ className="w-full justify-start gap-2 font-normal"
+ >
+ {disabled ? (
+ <MessageSquareOff className="text-muted-foreground h-4 w-4" />
+ ) : (
+ <MessageSquare className="h-4 w-4" />
+ )}
+ <span className="text-sm">Add comment</span>
+ {!disabled && commentCount ? (
+ <ActionButtonCountBadge count={commentCount} />
+ ) : null}
+ </Button>
+ )}
+ </CommentDrawerController>
</PopoverContent>
</Popover>
)}
@@ -409,17 +432,39 @@ export const ObservationDetailViewHeader = memo(
size="sm"
/>
)}
- <CommentDrawerButton
+ <CommentDrawerController
projectId={projectId}
objectId={observation.id}
objectType="OBSERVATION"
count={commentCount}
- size="sm"
pendingSelection={pendingSelection}
onSelectionUsed={onSelectionUsed}
isOpen={isCommentDrawerOpen}
onOpenChange={onCommentDrawerOpenChange}
- />
+ >
+ {({ disabled, openDrawer }) => (
+ <Button
+ type="button"
+ variant="secondary"
+ size="sm"
+ disabled={disabled}
+ onClick={openDrawer}
+ className="gap-1"
+ >
+ {disabled ? (
+ <MessageSquareOff className="text-muted-foreground h-3.5 w-3.5" />
+ ) : (
+ <>
+ <MessageSquare className="h-3.5 w-3.5" />
+ <span>Add comment</span>
+ {!!commentCount ? (
+ <ActionButtonCountBadge count={commentCount} />
+ ) : null}
+ </>
+ )}
+ </Button>
+ )}
+ </CommentDrawerController>
</div>
)}
</div>
diff --git a/web/src/features/traces/components/TraceDetailView/components/TraceDetailViewHeader.tsx b/web/src/features/traces/components/TraceDetailView/components/TraceDetailViewHeader.tsx
index b5a5ef43a3..a25fc024ce 100644
--- a/web/src/features/traces/components/TraceDetailView/components/TraceDetailViewHeader.tsx
+++ b/web/src/features/traces/components/TraceDetailView/components/TraceDetailViewHeader.tsx
@@ -24,9 +24,10 @@ import { LocalIsoDate } from "@/src/components/LocalIsoDate";
import { DetailHeaderActionsMenuController } from "@/src/features/traces/components/DetailHeaderActionsMenuController";
import { NewDatasetItemFromExistingObject } from "@/src/features/datasets/components/NewDatasetItemFromExistingObject";
import { AnnotateDrawer } from "@/src/features/scores/components/AnnotateDrawer";
+import { CommentDrawerController } from "@/src/features/comments/CommentDrawerController";
+import { ActionButtonCountBadge } from "@/src/components/ui/action-button-count-badge";
import { AnnotationQueueItemDropdownMenuController } from "@/src/features/annotation-queues/components/AnnotationQueueItemDropdownMenuController";
import { AnnotationQueueItemCountBadge } from "@/src/features/annotation-queues/components/AnnotationQueueItemCountBadge";
-import { CommentDrawerButton } from "@/src/features/comments/CommentDrawerButton";
import {
SessionBadge,
UserIdBadge,
@@ -47,6 +48,8 @@ import {
ChevronDown,
EllipsisVertical,
ListPlus,
+ MessageSquare,
+ MessageSquareOff,
MoreHorizontal,
} from "lucide-react";
import {
@@ -150,7 +153,7 @@ export const TraceDetailViewHeader = memo(function TraceDetailViewHeader({
</PopoverTrigger>
<PopoverContent
align="end"
- // forceMount + hide-when-closed: CommentDrawerButton lives in
+ // forceMount + hide-when-closed: CommentDrawerController lives in
// here, and its deep-link auto-open effect (?comments=open) and
// controlled inline-selection flow only work while mounted. A
// default Popover unmounts its content when closed (the default
@@ -204,17 +207,37 @@ export const TraceDetailViewHeader = memo(function TraceDetailViewHeader({
</AnnotationQueueItemDropdownMenuController>
</>
)}
- <CommentDrawerButton
+ <CommentDrawerController
projectId={projectId}
objectId={trace.id}
objectType="TRACE"
count={commentCount}
- layout="menu"
pendingSelection={pendingSelection}
onSelectionUsed={onSelectionUsed}
isOpen={isCommentDrawerOpen}
onOpenChange={onCommentDrawerOpenChange}
- />
+ >
+ {({ disabled, openDrawer }) => (
+ <Button
+ type="button"
+ variant="ghost"
+ size="sm"
+ disabled={disabled}
+ onClick={openDrawer}
+ className="w-full justify-start gap-2 font-normal"
+ >
+ {disabled ? (
+ <MessageSquareOff className="text-muted-foreground h-4 w-4" />
+ ) : (
+ <MessageSquare className="h-4 w-4" />
+ )}
+ <span className="text-sm">Add comment</span>
+ {!disabled && commentCount ? (
+ <ActionButtonCountBadge count={commentCount} />
+ ) : null}
+ </Button>
+ )}
+ </CommentDrawerController>
</PopoverContent>
</Popover>
)}
@@ -272,17 +295,39 @@ export const TraceDetailViewHeader = memo(function TraceDetailViewHeader({
</AnnotationQueueItemDropdownMenuController>
</div>
)}
- <CommentDrawerButton
+ <CommentDrawerController
projectId={projectId}
objectId={trace.id}
objectType="TRACE"
count={commentCount}
- size="sm"
pendingSelection={pendingSelection}
onSelectionUsed={onSelectionUsed}
isOpen={isCommentDrawerOpen}
onOpenChange={onCommentDrawerOpenChange}
- />
+ >
+ {({ disabled, openDrawer }) => (
+ <Button
+ type="button"
+ variant="secondary"
+ size="sm"
+ disabled={disabled}
+ onClick={openDrawer}
+ className="gap-1"
+ >
+ {disabled ? (
+ <MessageSquareOff className="text-muted-foreground h-3.5 w-3.5" />
+ ) : (
+ <>
+ <MessageSquare className="h-3.5 w-3.5" />
+ <span>Add comment</span>
+ {!!commentCount ? (
+ <ActionButtonCountBadge count={commentCount} />
+ ) : null}
+ </>
+ )}
+ </Button>
+ )}
+ </CommentDrawerController>
</div>
)}
</div>