Skip to content

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

refactor(web): Fix no-null-render: ToolCallDefinitionCard.tsx (#18237)

Ben Bachem0ff2248cc3e2
diff --git a/web/src/features/comments/CommentList.clienttest.tsx b/web/src/features/comments/CommentList.clienttest.tsx
index 36ef56ef52..ad033a0c76 100644
--- a/web/src/features/comments/CommentList.clienttest.tsx
+++ b/web/src/features/comments/CommentList.clienttest.tsx
@@ -2,9 +2,16 @@ import { fireEvent, render, screen } from "@testing-library/react";
import { type RouterOutputs } from "@/src/utils/api";
import { CommentList } from "./CommentList";
-const { useCommentsQuery, composerRenders } = vi.hoisted(() => ({
+const { useCommentsQuery, composerRenders, accessState } = vi.hoisted(() => ({
useCommentsQuery: vi.fn(),
composerRenders: vi.fn(),
+ accessState: {
+ hasReadAccess: true,
+ sessionStatus: "authenticated" as
+ | "authenticated"
+ | "unauthenticated"
+ | "loading",
+ },
}));
vi.mock("@/src/utils/api", () => ({
@@ -30,12 +37,13 @@ vi.mock("@/src/utils/api", () => ({
}));
vi.mock("@/src/features/rbac", () => ({
- useHasProjectAccess: () => true,
+ useHasProjectAccess: ({ scope }: { scope: string }) =>
+ scope === "comments:read" ? accessState.hasReadAccess : true,
}));
vi.mock("next-auth/react", () => ({
useSession: () => ({
- status: "authenticated",
+ status: accessState.sessionStatus,
data: { user: { id: "reviewer" } },
}),
}));
@@ -68,6 +76,35 @@ vi.mock("./components/CommentComposer", async () => {
});
describe("CommentList draft lifecycle", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ accessState.hasReadAccess = true;
+ accessState.sessionStatus = "authenticated";
+ });
+
+ it.each(["no-read-access", "unauthenticated", "loading"] as const)(
+ "does not mount the thread when access is %s",
+ (state) => {
+ if (state === "no-read-access") {
+ accessState.hasReadAccess = false;
+ } else {
+ accessState.sessionStatus = state;
+ }
+
+ const { container } = render(
+ <CommentList
+ projectId="project-1"
+ objectId="trace-1"
+ objectType="TRACE"
+ />,
+ );
+
+ expect(container).toBeEmptyDOMElement();
+ expect(useCommentsQuery).not.toHaveBeenCalled();
+ expect(composerRenders).not.toHaveBeenCalled();
+ },
+ );
+
it("retains a draft after a background query error and resets it for a different object", () => {
const comments: RouterOutputs["comments"]["getByObjectId"] = [
{
diff --git a/web/src/features/comments/CommentList.tsx b/web/src/features/comments/CommentList.tsx
index 70375daa4c..e6bb06cb23 100644
--- a/web/src/features/comments/CommentList.tsx
+++ b/web/src/features/comments/CommentList.tsx
@@ -1,5 +1,4 @@
-/* eslint-disable @repo/no-null-render */
-import { useRef } from "react";
+import { useRef, type ReactNode } from "react";
import { Spinner } from "@/src/components/design-system/Spinner/Spinner";
import { Button } from "@/src/components/ui/button";
import { useHasProjectAccess } from "@/src/features/rbac";
@@ -29,13 +28,31 @@ type CommentListProps = {
export function CommentList(props: CommentListProps) {
return (
- <CommentThread
- key={`${props.projectId}-${props.objectType}-${props.objectId}`}
- {...props}
- />
+ <CommentReadAccessGate projectId={props.projectId}>
+ <CommentThread
+ key={`${props.projectId}-${props.objectType}-${props.objectId}`}
+ {...props}
+ />
+ </CommentReadAccessGate>
);
}
+function CommentReadAccessGate({
+ projectId,
+ children,
+}: {
+ projectId: string;
+ children: ReactNode;
+}) {
+ const session = useSession();
+ const hasReadAccess = useHasProjectAccess({
+ projectId,
+ scope: "comments:read",
+ });
+ if (!hasReadAccess || session.status !== "authenticated") return null;
+ return children;
+}
+
function CommentThread({
projectId,
objectId,
@@ -52,20 +69,15 @@ function CommentThread({
}: CommentListProps) {
const session = useSession();
const commentsContainerRef = useRef<HTMLDivElement>(null);
- const hasReadAccess = useHasProjectAccess({
- projectId,
- scope: "comments:read",
- });
const hasWriteAccess = useHasProjectAccess({
projectId,
scope: "comments:CUD",
});
const comments = api.comments.getByObjectId.useQuery(
{ projectId, objectId, objectType },
- { enabled: hasReadAccess && session.status === "authenticated" },
+ { enabled: session.status === "authenticated" },
);
const utils = api.useUtils();
- if (!hasReadAccess || session.status !== "authenticated") return <></>;
if (comments.isPending)
return (
<div className="flex justify-center p-6">
diff --git a/web/src/features/traces/components/IOPreview/components/SectionToolDefinitions.tsx b/web/src/features/traces/components/IOPreview/components/SectionToolDefinitions.tsx
index ee5295eae5..02bf9de71f 100644
--- a/web/src/features/traces/components/IOPreview/components/SectionToolDefinitions.tsx
+++ b/web/src/features/traces/components/IOPreview/components/SectionToolDefinitions.tsx
@@ -29,13 +29,15 @@ export function SectionToolDefinitions({
<div className="io-message-header px-1 py-1 text-sm font-bold capitalize">
Tools
</div>
- <ToolCallDefinitionCard
- tools={tools}
- toolCallCounts={toolCallCounts}
- toolCallsByName={toolCallsByName}
- toolNameToDefinitionNumber={toolNameToDefinitionNumber}
- className="px-2"
- />
+ {tools.length > 0 && (
+ <ToolCallDefinitionCard
+ tools={tools}
+ toolCallCounts={toolCallCounts}
+ toolCallsByName={toolCallsByName}
+ toolNameToDefinitionNumber={toolNameToDefinitionNumber}
+ className="px-2"
+ />
+ )}
</div>
</div>
);
diff --git a/web/src/features/traces/components/IOPreview/components/ToolCallDefinitionCard.tsx b/web/src/features/traces/components/IOPreview/components/ToolCallDefinitionCard.tsx
index 835ec7c0bb..6979a5e711 100644
--- a/web/src/features/traces/components/IOPreview/components/ToolCallDefinitionCard.tsx
+++ b/web/src/features/traces/components/IOPreview/components/ToolCallDefinitionCard.tsx
@@ -1,4 +1,4 @@
-/* eslint-disable @repo/no-style-props, @repo/no-null-render */
+/* eslint-disable @repo/no-style-props */
import { Wrench } from "lucide-react";
import { DropdownIndicator } from "@/src/components/design-system/DropdownIndicator/DropdownIndicator";
import { Badge } from "@/src/components/ui/badge";
@@ -493,10 +493,6 @@ export function ToolCallDefinitionCard({
[tools, toolCallCounts],
);
- if (!tools || tools.length === 0) {
- return null;
- }
-
const calledToolsShouldCollapse =
calledTools.length > CALLED_TOOLS_COLLAPSE_THRESHOLD;
const availableToolsShouldCollapse =