diff --git a/apps/web/components/diff/diff-viewer-comment-edit.test.tsx b/apps/web/components/diff/diff-viewer-comment-edit.test.tsx new file mode 100644 index 0000000000..1279e28382 --- /dev/null +++ b/apps/web/components/diff/diff-viewer-comment-edit.test.tsx @@ -0,0 +1,154 @@ +import type { ReactNode } from "react"; +import type { DiffLineAnnotation } from "@pierre/diffs"; +import { cleanup, fireEvent, render, screen, waitFor, act } from "@testing-library/react"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +import { DiffViewer } from "./diff-viewer"; +import type { FileDiffData, DiffComment } from "@/lib/diff/types"; +import { useCommentsStore } from "@/lib/state/slices/comments"; +import type { AnnotationMetadata } from "./use-diff-annotation-renderer"; + +type CapturedProps = { + lineAnnotations?: Array>; + renderAnnotation?: (annotation: DiffLineAnnotation) => ReactNode; +}; + +const captured: CapturedProps[] = []; + +vi.mock("@/components/theme/app-theme", () => ({ + useTheme: () => ({ resolvedTheme: "dark" }), +})); + +// Capture the props (notably lineAnnotations) handed to Pierre's FileDiff so we +// can assert the internal comment-edit pipeline reaches the renderer with the +// updated text. Pierre itself is a web-component-backed renderer that does not +// run meaningfully in jsdom, so stubbing it and inspecting props is the +// reliable way to test propagation. +vi.mock("@pierre/diffs/react", () => ({ + FileDiff: (props: CapturedProps) => { + captured.push(props); + return ( +
+ {props.lineAnnotations?.map((annotation, index) => ( +
+ {props.renderAnnotation?.(annotation)} +
+ ))} +
+ ); + }, +})); + +const SESSION_ID = "session-edit-test"; +const FILE_PATH = "src/example.ts"; +const COMMENT_ID = "comment-1"; +const ORIGINAL_TEXT = "original text"; +const EDITED_TEXT = "edited text"; + +const data: FileDiffData = { + filePath: FILE_PATH, + diff: "diff --git a/src/example.ts b/src/example.ts\n--- a/src/example.ts\n+++ b/src/example.ts\n@@ -1 +1 @@\n-old\n+new\n", + oldContent: "old\n", + newContent: "new\n", + additions: 1, + deletions: 1, +}; + +function comment(text: string): DiffComment { + return { + id: COMMENT_ID, + sessionId: SESSION_ID, + source: "diff", + filePath: FILE_PATH, + startLine: 1, + endLine: 1, + side: "additions", + codeContent: "new", + text, + createdAt: "2026-01-01T00:00:00Z", + status: "pending", + }; +} + +/** Latest comment annotation text handed to Pierre for the target comment. */ +function latestCommentText(): string | undefined { + for (let i = captured.length - 1; i >= 0; i--) { + const annotations = captured[i].lineAnnotations ?? []; + const match = annotations.find((a) => a.metadata.comment?.id === COMMENT_ID); + if (match) return match.metadata.comment?.text; + } + return undefined; +} + +beforeEach(() => { + window.sessionStorage.clear(); + useCommentsStore.setState({ + byId: {}, + bySession: {}, + pendingForChat: [], + editingCommentId: null, + }); + captured.length = 0; +}); + +afterEach(cleanup); + +describe("DiffViewer comment editing", () => { + it("propagates an edited comment's new text to the renderer", async () => { + useCommentsStore.getState().addComment(comment(ORIGINAL_TEXT)); + + render(); + + await waitFor(() => expect(latestCommentText()).toBe(ORIGINAL_TEXT)); + + act(() => { + useCommentsStore.getState().updateComment(COMMENT_ID, { text: EDITED_TEXT }); + }); + + await waitFor(() => expect(latestCommentText()).toBe(EDITED_TEXT)); + }); + + it("submits edited text from the inline comment form", async () => { + useCommentsStore.getState().addComment(comment(ORIGINAL_TEXT)); + + render(); + + await waitFor(() => expect(screen.getByText(ORIGINAL_TEXT)).toBeTruthy()); + + fireEvent.click(screen.getByLabelText("Edit comment")); + fireEvent.change(screen.getByPlaceholderText("Add a comment..."), { + target: { value: EDITED_TEXT }, + }); + fireEvent.click(screen.getByRole("button", { name: /Update/i })); + + await waitFor(() => expect(latestCommentText()).toBe(EDITED_TEXT)); + expect(screen.getByText(EDITED_TEXT)).toBeTruthy(); + }); + + it("routes controlled inline edits to onCommentUpdate", async () => { + const onCommentUpdate = vi.fn(); + + render( + , + ); + + await waitFor(() => expect(screen.getByText(ORIGINAL_TEXT)).toBeTruthy()); + + fireEvent.click(screen.getByLabelText("Edit comment")); + fireEvent.change(screen.getByPlaceholderText("Add a comment..."), { + target: { value: EDITED_TEXT }, + }); + fireEvent.click(screen.getByRole("button", { name: /Update/i })); + + await waitFor(() => + expect(onCommentUpdate).toHaveBeenCalledWith(COMMENT_ID, { text: EDITED_TEXT }), + ); + expect(useCommentsStore.getState().byId[COMMENT_ID]?.text).not.toBe(EDITED_TEXT); + }); +}); diff --git a/apps/web/components/diff/diff-viewer-resolver.tsx b/apps/web/components/diff/diff-viewer-resolver.tsx index d194c55a84..402757cb20 100644 --- a/apps/web/components/diff/diff-viewer-resolver.tsx +++ b/apps/web/components/diff/diff-viewer-resolver.tsx @@ -7,7 +7,7 @@ import { DiffViewInline as PierreDiffViewInline, } from "./diff-viewer"; import { MonacoDiffViewer } from "@/components/editors/monaco/monaco-diff-viewer"; -import type { FileDiffData, DiffComment } from "@/lib/diff/types"; +import type { FileDiffData, DiffComment, DiffCommentUpdate } from "@/lib/diff/types"; import type { RevertBlockInfo } from "./diff-viewer"; export type { RevertBlockInfo }; @@ -17,6 +17,7 @@ interface DiffViewerResolverProps { sessionId?: string; onCommentAdd?: (comment: DiffComment) => void; onCommentDelete?: (commentId: string) => void; + onCommentUpdate?: (commentId: string, updates: DiffCommentUpdate) => void; onCommentRun?: (comment: DiffComment) => void; comments?: DiffComment[]; className?: string; diff --git a/apps/web/components/diff/diff-viewer.test.tsx b/apps/web/components/diff/diff-viewer.test.tsx index 34ffeac6b6..00288efefd 100644 --- a/apps/web/components/diff/diff-viewer.test.tsx +++ b/apps/web/components/diff/diff-viewer.test.tsx @@ -38,4 +38,30 @@ describe("DiffViewer", () => { await waitFor(() => expect(fileDiffProps.length).toBeGreaterThan(0)); expect(fileDiffProps.at(-1)?.options?.overflow).toBe("wrap"); }); + + it("rerenders when the controlled delete handler changes", async () => { + const firstDelete = vi.fn(); + const secondDelete = vi.fn(); + const { rerender } = render(); + + await waitFor(() => expect(fileDiffProps.length).toBeGreaterThan(0)); + const renderCount = fileDiffProps.length; + + rerender(); + + await waitFor(() => expect(fileDiffProps.length).toBeGreaterThan(renderCount)); + }); + + it("rerenders when the controlled update handler changes", async () => { + const firstUpdate = vi.fn(); + const secondUpdate = vi.fn(); + const { rerender } = render(); + + await waitFor(() => expect(fileDiffProps.length).toBeGreaterThan(0)); + const renderCount = fileDiffProps.length; + + rerender(); + + await waitFor(() => expect(fileDiffProps.length).toBeGreaterThan(renderCount)); + }); }); diff --git a/apps/web/components/diff/diff-viewer.tsx b/apps/web/components/diff/diff-viewer.tsx index 1f36e42aa8..0415616f41 100644 --- a/apps/web/components/diff/diff-viewer.tsx +++ b/apps/web/components/diff/diff-viewer.tsx @@ -3,7 +3,7 @@ import { useState, useRef, useCallback, memo, useEffect } from "react"; import { FileDiff } from "@pierre/diffs/react"; import { cn } from "@kandev/ui/lib/utils"; -import type { FileDiffData, DiffComment } from "@/lib/diff/types"; +import type { FileDiffData, DiffComment, DiffCommentUpdate } from "@/lib/diff/types"; import { useHunkHover } from "./use-hunk-hover"; import { useAnnotationRenderer } from "./use-diff-annotation-renderer"; import { DEFAULT_DIFF_WORD_WRAP } from "./diff-defaults"; @@ -25,6 +25,7 @@ interface DiffViewerProps { sessionId?: string; onCommentAdd?: (comment: DiffComment) => void; onCommentDelete?: (commentId: string) => void; + onCommentUpdate?: (commentId: string, updates: DiffCommentUpdate) => void; onCommentRun?: (comment: DiffComment) => void; comments?: DiffComment[]; className?: string; @@ -51,6 +52,8 @@ interface DiffViewerProps { const SCALAR_PROP_KEYS: (keyof DiffViewerProps)[] = [ "enableComments", "sessionId", + "onCommentDelete", + "onCommentUpdate", "compact", "hideHeader", "className", @@ -193,6 +196,7 @@ export const DiffViewer = memo(function DiffViewer({ sessionId, onCommentAdd, onCommentDelete, + onCommentUpdate, onCommentRun, comments: externalComments, className, @@ -227,6 +231,7 @@ export const DiffViewer = memo(function DiffViewer({ sessionId, onCommentAdd, onCommentDelete, + onCommentUpdate, onCommentRun, externalComments, onRevertBlock, diff --git a/apps/web/components/diff/file-diff-viewer.tsx b/apps/web/components/diff/file-diff-viewer.tsx index d941767a93..d653076b14 100644 --- a/apps/web/components/diff/file-diff-viewer.tsx +++ b/apps/web/components/diff/file-diff-viewer.tsx @@ -3,7 +3,7 @@ import { memo, useMemo } from "react"; import { DiffViewerResolved as DiffViewer } from "./diff-viewer-resolver"; import { transformGitDiff } from "@/lib/diff"; -import type { DiffComment } from "@/lib/diff/types"; +import type { DiffComment, DiffCommentUpdate } from "@/lib/diff/types"; import type { RevertBlockInfo } from "./diff-viewer-resolver"; interface FileDiffViewerProps { @@ -14,6 +14,7 @@ interface FileDiffViewerProps { sessionId?: string; onCommentAdd?: (comment: DiffComment) => void; onCommentDelete?: (commentId: string) => void; + onCommentUpdate?: (commentId: string, updates: DiffCommentUpdate) => void; onCommentRun?: (comment: DiffComment) => void; comments?: DiffComment[]; className?: string; @@ -54,6 +55,7 @@ export const FileDiffViewer = memo(function FileDiffViewer({ sessionId, onCommentAdd, onCommentDelete, + onCommentUpdate, onCommentRun, comments, className, @@ -80,6 +82,7 @@ export const FileDiffViewer = memo(function FileDiffViewer({ sessionId={sessionId} onCommentAdd={onCommentAdd} onCommentDelete={onCommentDelete} + onCommentUpdate={onCommentUpdate} onCommentRun={onCommentRun} comments={comments} className={className} diff --git a/apps/web/components/diff/use-diff-comments.ts b/apps/web/components/diff/use-diff-comments.ts index 22bb656d71..730d967a56 100644 --- a/apps/web/components/diff/use-diff-comments.ts +++ b/apps/web/components/diff/use-diff-comments.ts @@ -6,7 +6,12 @@ import type { SelectedLineRange } from "@pierre/diffs"; import { useCommentsStore } from "@/lib/state/slices/comments"; import { useDiffFileComments } from "@/hooks/domains/comments/use-diff-comments"; import { commentsToAnnotations, extractCodeFromDiff, extractCodeFromContent } from "@/lib/diff"; -import type { DiffComment, AnnotationSide, CommentAnnotation } from "@/lib/diff/types"; +import type { + DiffComment, + DiffCommentUpdate, + AnnotationSide, + CommentAnnotation, +} from "@/lib/diff/types"; interface UseDiffCommentsOptions { sessionId: string; @@ -29,7 +34,7 @@ interface UseDiffCommentsReturn { /** Remove a comment */ removeComment: (commentId: string) => void; /** Update a comment */ - updateComment: (commentId: string, updates: Partial) => void; + updateComment: (commentId: string, updates: DiffCommentUpdate) => void; /** Currently editing comment ID */ editingCommentId: string | null; /** Set the editing comment ID */ @@ -104,7 +109,7 @@ export function useDiffComments({ ); const updateComment = useCallback( - (commentId: string, updates: Partial) => { + (commentId: string, updates: DiffCommentUpdate) => { storeUpdateComment(commentId, updates); }, [storeUpdateComment], diff --git a/apps/web/components/diff/use-diff-viewer-state.ts b/apps/web/components/diff/use-diff-viewer-state.ts index a56253ff51..d6781780a1 100644 --- a/apps/web/components/diff/use-diff-viewer-state.ts +++ b/apps/web/components/diff/use-diff-viewer-state.ts @@ -6,7 +6,7 @@ import type { AnnotationSide, ChangeContent, } from "@pierre/diffs"; -import type { FileDiffData, DiffComment } from "@/lib/diff/types"; +import type { FileDiffData, DiffComment, DiffCommentUpdate } from "@/lib/diff/types"; import { buildDiffComment, useCommentActions } from "@/lib/diff/comment-utils"; import { useDiffComments } from "./use-diff-comments"; import { useDiffMetadata } from "./use-diff-metadata"; @@ -137,6 +137,7 @@ type UseDiffViewerStateOpts = { sessionId?: string; onCommentAdd?: (comment: DiffComment) => void; onCommentDelete?: (commentId: string) => void; + onCommentUpdate?: (commentId: string, updates: DiffCommentUpdate) => void; onCommentRun?: (comment: DiffComment) => void; externalComments?: DiffComment[]; onRevertBlock?: (filePath: string, info: RevertBlockInfo) => Promise | void; @@ -201,9 +202,10 @@ type CommentHandlerOpts = { sessionId?: string; addComment: (range: SelectedLineRange, content: string) => DiffComment; removeComment: (commentId: string) => void; - updateComment: (commentId: string, updates: Partial) => void; + updateComment: (commentId: string, updates: DiffCommentUpdate) => void; setEditingComment: (commentId: string | null) => void; onCommentDelete?: (commentId: string) => void; + onCommentUpdate?: (commentId: string, updates: DiffCommentUpdate) => void; }; function useDiffViewerCommentHandlers(opts: CommentHandlerOpts) { @@ -222,6 +224,7 @@ function useDiffViewerCommentHandlers(opts: CommentHandlerOpts) { updateComment, setEditingComment, onCommentDelete, + onCommentUpdate, } = opts; const handleLineSelectionEnd = useCallback( (range: SelectedLineRange | null) => { @@ -289,6 +292,7 @@ function useDiffViewerCommentHandlers(opts: CommentHandlerOpts) { updateComment, setEditingComment, onCommentDelete, + onCommentUpdate, externalComments, }); @@ -325,6 +329,7 @@ export function useDiffViewerState(opts: UseDiffViewerStateOpts) { sessionId, onCommentAdd, onCommentDelete, + onCommentUpdate, onCommentRun, externalComments, onRevertBlock, @@ -393,6 +398,7 @@ export function useDiffViewerState(opts: UseDiffViewerStateOpts) { updateComment, setEditingComment, onCommentDelete, + onCommentUpdate, }); return { diff --git a/apps/web/components/editors/monaco/monaco-diff-viewer.tsx b/apps/web/components/editors/monaco/monaco-diff-viewer.tsx index 64d7da5a8b..8ba24e7583 100644 --- a/apps/web/components/editors/monaco/monaco-diff-viewer.tsx +++ b/apps/web/components/editors/monaco/monaco-diff-viewer.tsx @@ -4,7 +4,7 @@ import { useLayoutEffect, useMemo, useRef, useState } from "react"; import { DiffEditor } from "@monaco-editor/react"; import { useTheme } from "@/components/theme/app-theme"; import { cn } from "@kandev/ui/lib/utils"; -import type { FileDiffData, DiffComment } from "@/lib/diff/types"; +import type { FileDiffData, DiffComment, DiffCommentUpdate } from "@/lib/diff/types"; import { getMonacoLanguage } from "@/lib/editor/language-map"; import { useCommandPanelOpen } from "@/lib/commands/command-registry"; import { useDiffEditorHeight } from "@/hooks/use-diff-editor-height"; @@ -28,6 +28,7 @@ interface MonacoDiffViewerProps { sessionId?: string; onCommentAdd?: (comment: DiffComment) => void; onCommentDelete?: (commentId: string) => void; + onCommentUpdate?: (commentId: string, updates: DiffCommentUpdate) => void; onCommentRun?: (comment: DiffComment) => void; comments?: DiffComment[]; className?: string; @@ -47,6 +48,7 @@ function useMonacoDiffViewerState(props: MonacoDiffViewerProps) { compact = false, onCommentAdd, onCommentDelete, + onCommentUpdate, onCommentRun, comments: externalComments, onModifiedContentChange, @@ -68,6 +70,7 @@ function useMonacoDiffViewerState(props: MonacoDiffViewerProps) { compact, onCommentAdd, onCommentDelete, + onCommentUpdate, onCommentRun, externalComments, onModifiedContentChange, diff --git a/apps/web/components/editors/monaco/use-diff-viewer-comments.ts b/apps/web/components/editors/monaco/use-diff-viewer-comments.ts index f0500147be..9c1bcababa 100644 --- a/apps/web/components/editors/monaco/use-diff-viewer-comments.ts +++ b/apps/web/components/editors/monaco/use-diff-viewer-comments.ts @@ -1,7 +1,7 @@ import { useCallback, useEffect, useRef, useState } from "react"; import type { editor as monacoEditor } from "monaco-editor"; import type { DiffOnMount } from "@monaco-editor/react"; -import type { DiffComment } from "@/lib/diff/types"; +import type { DiffComment, DiffCommentUpdate } from "@/lib/diff/types"; import { buildDiffComment, useCommentedLines, useCommentActions } from "@/lib/diff/comment-utils"; import { useDiffComments } from "@/components/diff/use-diff-comments"; import { useGutterComments } from "@/hooks/use-gutter-comments"; @@ -31,6 +31,7 @@ interface UseDiffViewerCommentsOpts { compact: boolean; onCommentAdd?: (comment: DiffComment) => void; onCommentDelete?: (commentId: string) => void; + onCommentUpdate?: (commentId: string, updates: DiffCommentUpdate) => void; onCommentRun?: (comment: DiffComment) => void; externalComments?: DiffComment[]; onModifiedContentChange?: (filePath: string, content: string) => void; @@ -44,6 +45,7 @@ export function useDiffViewerComments(opts: UseDiffViewerCommentsOpts) { compact, onCommentAdd, onCommentDelete, + onCommentUpdate, onCommentRun, externalComments, onModifiedContentChange, @@ -245,6 +247,7 @@ export function useDiffViewerComments(opts: UseDiffViewerCommentsOpts) { updateComment, setEditingComment, onCommentDelete, + onCommentUpdate, externalComments, }); diff --git a/apps/web/components/review/review-comments-overview.test.tsx b/apps/web/components/review/review-comments-overview.test.tsx new file mode 100644 index 0000000000..7e9c2bd6dc --- /dev/null +++ b/apps/web/components/review/review-comments-overview.test.tsx @@ -0,0 +1,112 @@ +import { describe, it, expect, afterEach } from "vitest"; +import { render, screen, cleanup, within } from "@testing-library/react"; +import type { DiffComment } from "@/lib/diff/types"; +import { ReviewCommentsOverview, groupCommentsByFile } from "./review-comments-overview"; + +afterEach(cleanup); + +const APP_PATH = "src/app.tsx"; + +function comment(overrides: Partial): DiffComment { + return { + id: "c1", + sessionId: "s1", + source: "diff", + filePath: APP_PATH, + startLine: 10, + endLine: 10, + side: "additions", + codeContent: "const x = 1;", + text: "fix this", + createdAt: "2026-01-01T00:00:00Z", + status: "pending", + ...overrides, + }; +} + +describe("groupCommentsByFile", () => { + it("groups comments by file preserving first-seen order", () => { + const groups = groupCommentsByFile([ + comment({ id: "a", filePath: "b.ts" }), + comment({ id: "b", filePath: "a.ts" }), + comment({ id: "c", filePath: "b.ts" }), + ]); + expect(groups.map((g) => g.filePath)).toEqual(["b.ts", "a.ts"]); + expect(groups[0].comments.map((c) => c.id)).toEqual(["a", "c"]); + expect(groups[1].comments.map((c) => c.id)).toEqual(["b"]); + }); + + it("keeps matching paths in different repositories as separate groups", () => { + const groups = groupCommentsByFile([ + comment({ id: "a", repositoryId: "repo-1", filePath: APP_PATH }), + comment({ id: "b", repositoryId: "repo-2", filePath: APP_PATH }), + comment({ id: "c", repositoryId: "repo-1", filePath: APP_PATH }), + ]); + expect(groups.map((g) => g.filePath)).toEqual([APP_PATH, APP_PATH]); + expect(groups[0].comments.map((c) => c.id)).toEqual(["a", "c"]); + expect(groups[1].comments.map((c) => c.id)).toEqual(["b"]); + }); + + it("returns an empty list for no comments", () => { + expect(groupCommentsByFile([])).toEqual([]); + }); +}); + +describe("ReviewCommentsOverview", () => { + it("shows an empty state when there are no comments", () => { + render(); + expect(screen.getByText(/No comments to fix yet/i)).toBeDefined(); + expect(screen.queryByTestId("review-comments-overview")).toBeNull(); + }); + + it("summarizes comment and file totals with pluralization", () => { + render( + , + ); + expect(screen.getByText("3 pending review comments")).toBeDefined(); + expect(screen.getByText("across 2 files")).toBeDefined(); + }); + + it("uses singular labels for a single comment on a single file", () => { + render(); + expect(screen.getByText("1 pending review comment")).toBeDefined(); + expect(screen.getByText("across 1 file")).toBeDefined(); + }); + + it("renders the file name, line range, side and text for each comment", () => { + render( + , + ); + expect(screen.getByText("app.tsx")).toBeDefined(); + expect(screen.getByText("src")).toBeDefined(); + expect(screen.getByText("L5-8")).toBeDefined(); + expect(screen.getByText(/new/)).toBeDefined(); + expect(screen.getByText(/old/)).toBeDefined(); + expect(screen.getByText("range note")).toBeDefined(); + expect(screen.getByText("removed note")).toBeDefined(); + }); + + it("shows a per-file comment count badge", () => { + render( + , + ); + const overview = screen.getByTestId("review-comments-overview"); + expect(within(overview).getByText("2")).toBeDefined(); + }); +}); diff --git a/apps/web/components/review/review-comments-overview.tsx b/apps/web/components/review/review-comments-overview.tsx new file mode 100644 index 0000000000..e09b8c3a67 --- /dev/null +++ b/apps/web/components/review/review-comments-overview.tsx @@ -0,0 +1,120 @@ +"use client"; + +import { IconMessage } from "@tabler/icons-react"; +import type { DiffComment } from "@/lib/diff/types"; +import { formatLineRange } from "@/lib/diff"; + +type FileGroup = { key: string; filePath: string; comments: DiffComment[] }; + +function fileGroupKey(comment: DiffComment): string { + return JSON.stringify([comment.repositoryId ?? null, comment.filePath]); +} + +/** + * Groups comments by file, preserving first-seen file order so the overview + * mirrors the order comments were added / appear in the file tree. + */ +export function groupCommentsByFile(comments: DiffComment[]): FileGroup[] { + const order: string[] = []; + const byFile = new Map(); + const filePathByKey = new Map(); + for (const comment of comments) { + const key = fileGroupKey(comment); + const existing = byFile.get(key); + if (existing) { + existing.push(comment); + } else { + order.push(key); + filePathByKey.set(key, comment.filePath); + byFile.set(key, [comment]); + } + } + return order.map((key) => ({ + key, + filePath: filePathByKey.get(key)!, + comments: byFile.get(key)!, + })); +} + +function fileDir(filePath: string): string { + const idx = filePath.lastIndexOf("/"); + return idx === -1 ? "" : filePath.slice(0, idx); +} + +function fileName(filePath: string): string { + const idx = filePath.lastIndexOf("/"); + return idx === -1 ? filePath : filePath.slice(idx + 1); +} + +/** + * Scrollable overview of pending review comments, grouped per file. Rendered + * inside the "Fix Comments" hover popover on the review top bar. + */ +export function ReviewCommentsOverview({ comments }: { comments: DiffComment[] }) { + const groups = groupCommentsByFile(comments); + const total = comments.length; + + if (total === 0) { + return ( +
+ No comments to fix yet. +
+ ); + } + + return ( +
+
+ + + {total} pending review comment{total !== 1 ? "s" : ""} + + + across {groups.length} file{groups.length !== 1 ? "s" : ""} + +
+ +
+ {groups.map((group) => ( +
+
+ + {fileName(group.filePath)} + + {fileDir(group.filePath) && ( + + {fileDir(group.filePath)} + + )} + + {group.comments.length} + +
+ +
+ {group.comments.map((comment) => ( +
+
+ {formatLineRange(comment.startLine, comment.endLine)} + + · {comment.side === "additions" ? "new" : "old"} + +
+

+ {comment.text} +

+
+ ))} +
+
+ ))} +
+
+ ); +} diff --git a/apps/web/components/review/review-dialog.build-files.test.ts b/apps/web/components/review/review-dialog.build-files.test.ts index 65253b4cbe..cd04a79d03 100644 --- a/apps/web/components/review/review-dialog.build-files.test.ts +++ b/apps/web/components/review/review-dialog.build-files.test.ts @@ -1,6 +1,24 @@ import { describe, it, expect } from "vitest"; -import { buildAllFiles } from "./review-dialog"; +import { buildAllFiles, filterPendingDiffCommentsForSession } from "./review-dialog"; import type { CumulativeDiff } from "@/lib/state/slices/session-runtime/types"; +import type { Comment } from "@/lib/state/slices/comments"; + +function pendingDiffComment(overrides: Partial): Comment { + return { + id: "c1", + sessionId: "s1", + source: "diff", + filePath: "src/app.tsx", + startLine: 1, + endLine: 1, + side: "additions", + codeContent: "const value = 1;", + text: "fix this", + createdAt: "2026-01-01T00:00:00Z", + status: "pending", + ...overrides, + } as Comment; +} describe("buildAllFiles (review dialog)", () => { // Regression for the "No changes to review" bug introduced by multi-repo @@ -120,3 +138,25 @@ describe("buildAllFiles (review dialog)", () => { expect(result[0].diff).toBe("@@ -1 +1 @@\n-u\n+u"); }); }); + +describe("filterPendingDiffCommentsForSession", () => { + it("keeps only diff comments from the active session", () => { + const comments = [ + pendingDiffComment({ id: "current", sessionId: "current" }), + pendingDiffComment({ id: "other", sessionId: "other" }), + { + id: "plan", + sessionId: "current", + source: "plan", + selectedText: "task text", + text: "plan note", + createdAt: "2026-01-01T00:00:00Z", + status: "pending", + }, + ] satisfies Comment[]; + + expect(filterPendingDiffCommentsForSession(comments, "current").map((c) => c.id)).toEqual([ + "current", + ]); + }); +}); diff --git a/apps/web/components/review/review-dialog.tsx b/apps/web/components/review/review-dialog.tsx index efcac3fe95..a0dcbce3fd 100644 --- a/apps/web/components/review/review-dialog.tsx +++ b/apps/web/components/review/review-dialog.tsx @@ -5,6 +5,7 @@ import { Dialog, DialogContent, DialogTitle } from "@kandev/ui/dialog"; import type { DiffComment } from "@/lib/diff/types"; import type { FileInfo, CumulativeDiff } from "@/lib/state/slices/session-runtime/types"; import type { PRDiffFile } from "@/lib/types/github"; +import type { Comment } from "@/lib/state/slices/comments"; import { useCommentsStore, isDiffComment } from "@/lib/state/slices/comments"; import { useSessionFileReviews } from "@/hooks/use-session-file-reviews"; import { useGitOperations } from "@/hooks/use-git-operations"; @@ -151,6 +152,15 @@ export function buildAllFiles( return Array.from(fileMap.values()).sort((a, b) => a.path.localeCompare(b.path)); } +export function filterPendingDiffCommentsForSession( + comments: Comment[], + sessionId: string, +): DiffComment[] { + return comments.filter( + (comment): comment is DiffComment => comment.sessionId === sessionId && isDiffComment(comment), + ); +} + type ReviewDialogProps = { open: boolean; onOpenChange: (open: boolean) => void; @@ -324,8 +334,8 @@ function useReviewDialogState(props: ReviewDialogProps) { const sessionCommentIds = useCommentsStore((s) => s.bySession[sessionId]); const getStorePendingComments = useCommentsStore((s) => s.getPendingComments); const getPendingComments = useCallback((): DiffComment[] => { - return getStorePendingComments().filter(isDiffComment); - }, [getStorePendingComments]); + return filterPendingDiffCommentsForSession(getStorePendingComments(), sessionId); + }, [getStorePendingComments, sessionId]); const markCommentsSent = useCommentsStore((s) => s.markCommentsSent); const [filter, setFilter] = useState(""); diff --git a/apps/web/components/review/review-fix-comments-button.tsx b/apps/web/components/review/review-fix-comments-button.tsx new file mode 100644 index 0000000000..d39e91ed98 --- /dev/null +++ b/apps/web/components/review/review-fix-comments-button.tsx @@ -0,0 +1,94 @@ +"use client"; + +import { useCallback } from "react"; +import { IconMessageForward } from "@tabler/icons-react"; +import { Button } from "@kandev/ui/button"; +import { Popover, PopoverAnchor, PopoverContent } from "@kandev/ui/popover"; +import type { DiffComment } from "@/lib/diff/types"; +import { useHoverPopover } from "@/hooks/domains/github/use-hover-popover"; +import { ReviewCommentsOverview } from "./review-comments-overview"; + +const COMMENTS_HOVER_OPEN_DELAY_MS = 150; +const COMMENTS_HOVER_CLOSE_DELAY_MS = 150; + +function shouldOpenOverviewOnClick(): boolean { + return ( + typeof window !== "undefined" && + typeof window.matchMedia === "function" && + window.matchMedia("(hover: none), (pointer: coarse)").matches + ); +} + +type FixCommentsButtonProps = { + commentCount: number; + getPendingComments: () => DiffComment[]; + onFixComments: () => void; +}; + +export function FixCommentsButton({ + commentCount, + getPendingComments, + onFixComments, +}: FixCommentsButtonProps) { + const { open, onOpenChange, onTriggerEnter, onTriggerLeave, onContentEnter, onContentLeave } = + useHoverPopover({ + openDelayMs: COMMENTS_HOVER_OPEN_DELAY_MS, + closeDelayMs: COMMENTS_HOVER_CLOSE_DELAY_MS, + }); + const handleClick = useCallback(() => { + if (!open && shouldOpenOverviewOnClick()) { + onOpenChange(true); + return; + } + onFixComments(); + }, [open, onOpenChange, onFixComments]); + + return ( + + + + + + + e.preventDefault()} + > + + + + ); +} diff --git a/apps/web/components/review/review-top-bar.tsx b/apps/web/components/review/review-top-bar.tsx index 9dc634ccef..dab83ead94 100644 --- a/apps/web/components/review/review-top-bar.tsx +++ b/apps/web/components/review/review-top-bar.tsx @@ -4,7 +4,6 @@ import { memo, useCallback } from "react"; import { IconSettings, IconX, - IconMessageForward, IconLayoutColumns, IconLayoutRows, IconTextWrap, @@ -23,6 +22,7 @@ import { useAppStore } from "@/components/state-provider"; import { getWebSocketClient } from "@/lib/ws/connection"; import { updateUserSettings } from "@/lib/api"; import { VcsSplitButton } from "@/components/vcs-split-button"; +import { FixCommentsButton } from "./review-fix-comments-button"; type ReviewTopBarProps = { sessionId: string; @@ -174,13 +174,11 @@ export const ReviewTopBar = memo(function ReviewTopBar({ {commentCount > 0 && ( - + )}