Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { Flex, Spinner, Text } from "@radix-ui/themes";
import { useReviewNavigationStore } from "@renderer/features/code-review/stores/reviewNavigationStore";
import type { ChangedFile, Task } from "@shared/types";
import { useMemo } from "react";
import { useReviewComment } from "../hooks/useReviewComment";
import type { DiffOptions, OnCommentCallback } from "../types";
import type { DiffOptions } from "../types";
import { InteractiveFileDiff } from "./InteractiveFileDiff";
import {
DeferredDiffPlaceholder,
Expand All @@ -26,7 +25,6 @@ export function CloudReviewPage({ task }: CloudReviewPageProps) {
);
const { effectiveBranch, prUrl, isRunActive, remoteFiles, isLoading } =
useCloudChangedFiles(taskId, task, isReviewOpen);
const onComment = useReviewComment(taskId);

const allPaths = useMemo(() => remoteFiles.map((f) => f.path), [remoteFiles]);

Expand Down Expand Up @@ -102,11 +100,11 @@ export function CloudReviewPage({ task }: CloudReviewPageProps) {
<div key={file.path} data-file-path={file.path}>
<CloudFileDiff
file={file}
taskId={taskId}
prUrl={prUrl}
options={diffOptions}
collapsed={isCollapsed}
onToggle={() => toggleFile(file.path)}
onComment={onComment}
/>
</div>
);
Expand All @@ -117,18 +115,18 @@ export function CloudReviewPage({ task }: CloudReviewPageProps) {

function CloudFileDiff({
file,
taskId,
prUrl,
options,
collapsed,
onToggle,
onComment,
}: {
file: ChangedFile;
taskId: string;
prUrl: string | null;
options: DiffOptions;
collapsed: boolean;
onToggle: () => void;
onComment: OnCommentCallback;
}) {
const fileDiff = useMemo((): FileDiffMetadata | undefined => {
if (!file.patch) return undefined;
Expand Down Expand Up @@ -158,7 +156,7 @@ function CloudFileDiff({
<InteractiveFileDiff
fileDiff={fileDiff}
options={{ ...options, collapsed }}
onComment={onComment}
taskId={taskId}
renderCustomHeader={(fd) => (
<DiffFileHeader
fileDiff={fd}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { sendPromptToAgent } from "@features/sessions/utils/sendPromptToAgent";
import type { AnnotationSide } from "@pierre/diffs";
import { useCallback, useRef } from "react";
import { buildInlineCommentPrompt } from "../utils/reviewPrompts";

interface CommentAnnotationProps {
onSubmit: (text: string) => void;
onCancel: () => void;
taskId: string;
filePath: string;
startLine: number;
endLine: number;
side: AnnotationSide;
onDismiss: () => void;
}

export function CommentAnnotation({
onSubmit,
onCancel,
taskId,
filePath,
startLine,
endLine,
side,
onDismiss,
}: CommentAnnotationProps) {
const textareaRef = useRef<HTMLTextAreaElement>(null);

Expand All @@ -23,9 +34,13 @@ export function CommentAnnotation({
const handleSubmit = useCallback(() => {
const text = textareaRef.current?.value?.trim();
if (text) {
onSubmit(text);
onDismiss();
sendPromptToAgent(
taskId,
buildInlineCommentPrompt(filePath, startLine, endLine, side, text),
);
}
}, [onSubmit]);
}, [taskId, filePath, startLine, endLine, side, onDismiss]);

const handleKeyDown = useCallback(
(e: React.KeyboardEvent) => {
Expand All @@ -35,10 +50,10 @@ export function CommentAnnotation({
}
if (e.key === "Escape") {
e.preventDefault();
onCancel();
onDismiss();
}
},
[handleSubmit, onCancel],
[handleSubmit, onDismiss],
);

return (
Expand All @@ -63,7 +78,7 @@ export function CommentAnnotation({
</button>
<button
type="button"
onClick={onCancel}
onClick={onDismiss}
className="cursor-pointer border-none bg-transparent px-2 py-0.5 text-[var(--gray-9)] text-xs leading-[18px]"
>
Cancel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function PatchDiffView({
repoPath,
options,
renderCustomHeader,
onComment,
taskId,
}: PatchDiffProps) {
const trpc = useTRPC();
const queryClient = useQueryClient();
Expand Down Expand Up @@ -78,22 +78,6 @@ function PatchDiffView({
[hunkAnnotations, commentAnnotation],
);

const handleCommentSubmit = useCallback(
(text: string) => {
const meta = commentAnnotation?.metadata;
if (!currentFilePath || !meta || meta.kind !== "comment") return;
reset();
onComment?.(
currentFilePath,
meta.startLine,
meta.endLine,
meta.side,
text,
);
},
[currentFilePath, commentAnnotation, reset, onComment],
);

const handleRevert = useCallback(
async (hunkIndex: number) => {
const filePath = filePathRef.current;
Expand Down Expand Up @@ -150,8 +134,16 @@ function PatchDiffView({
const renderAnnotation = useCallback(
(annotation: DiffLineAnnotation<AnnotationMetadata>) => {
if (annotation.metadata.kind === "comment") {
const { startLine, endLine, side } = annotation.metadata;
return (
<CommentAnnotation onSubmit={handleCommentSubmit} onCancel={reset} />
<CommentAnnotation
taskId={taskId ?? ""}
filePath={currentFilePath}
startLine={startLine}
endLine={endLine}
side={side}
onDismiss={reset}
/>
);
}

Expand Down Expand Up @@ -184,7 +176,7 @@ function PatchDiffView({
</div>
);
},
[handleRevert, handleCommentSubmit, reset, revertingHunks],
[handleRevert, reset, revertingHunks, taskId, currentFilePath],
);

const mergedOptions = useMemo(
Expand Down Expand Up @@ -214,7 +206,7 @@ function FilesDiffView({
newFile,
options,
renderCustomHeader,
onComment,
taskId,
}: FilesDiffProps) {
const {
selectedRange,
Expand All @@ -231,24 +223,22 @@ function FilesDiffView({
[commentAnnotation],
);

const handleCommentSubmit = useCallback(
(text: string) => {
const meta = commentAnnotation?.metadata;
if (!filePath || !meta || meta.kind !== "comment") return;
reset();
onComment?.(filePath, meta.startLine, meta.endLine, meta.side, text);
},
[filePath, commentAnnotation, reset, onComment],
);

const renderAnnotation = useCallback(
(annotation: DiffLineAnnotation<AnnotationMetadata>) => {
if (annotation.metadata.kind !== "comment") return null;
const { startLine, endLine, side } = annotation.metadata;
return (
<CommentAnnotation onSubmit={handleCommentSubmit} onCancel={reset} />
<CommentAnnotation
taskId={taskId ?? ""}
filePath={filePath}
startLine={startLine}
endLine={endLine}
side={side}
onDismiss={reset}
/>
);
},
[handleCommentSubmit, reset],
[reset, taskId, filePath],
);

const mergedOptions = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import { useTRPC } from "@renderer/trpc/client";
import type { ChangedFile, Task } from "@shared/types";
import { useQuery } from "@tanstack/react-query";
import { useMemo } from "react";
import { useReviewComment } from "../hooks/useReviewComment";
import { useReviewDiffs } from "../hooks/useReviewDiffs";
import type { DiffOptions, OnCommentCallback } from "../types";
import type { DiffOptions } from "../types";
import { InteractiveFileDiff } from "./InteractiveFileDiff";
import {
DeferredDiffPlaceholder,
Expand All @@ -32,8 +31,6 @@ export function ReviewPage({ task }: ReviewPageProps) {
const isReviewOpen = useReviewNavigationStore(
(s) => (s.reviewModes[taskId] ?? "closed") !== "closed",
);
const onComment = useReviewComment(taskId);

const {
changedFiles,
changesLoading,
Expand Down Expand Up @@ -79,7 +76,6 @@ export function ReviewPage({ task }: ReviewPageProps) {
revealFile,
getDeferredReason,
openFile,
onComment,
};

return (
Expand Down Expand Up @@ -118,7 +114,7 @@ export function ReviewPage({ task }: ReviewPageProps) {
options={diffOptions}
collapsed={isCollapsed}
onToggle={() => toggleFile(key)}
onComment={onComment}
taskId={taskId}
/>
</div>
);
Expand Down Expand Up @@ -148,7 +144,6 @@ interface FileDiffListProps {
revealFile: (key: string) => void;
getDeferredReason: (key: string) => DeferredReason | null;
openFile: (taskId: string, path: string, preview: boolean) => void;
onComment: OnCommentCallback;
}

function FileDiffList({
Expand All @@ -162,7 +157,6 @@ function FileDiffList({
revealFile,
getDeferredReason,
openFile,
onComment,
}: FileDiffListProps) {
return files.map((fileDiff) => {
const filePath = fileDiff.name ?? fileDiff.prevName ?? "";
Expand Down Expand Up @@ -193,7 +187,7 @@ function FileDiffList({
fileDiff={fileDiff}
repoPath={repoPath}
options={{ ...diffOptions, collapsed: isCollapsed }}
onComment={onComment}
taskId={taskId}
renderCustomHeader={(fd) => (
<DiffFileHeader
fileDiff={fd}
Expand All @@ -213,17 +207,17 @@ function FileDiffList({
function UntrackedFileDiff({
file,
repoPath,
taskId,
options,
collapsed,
onToggle,
onComment,
}: {
file: ChangedFile;
repoPath: string;
taskId: string;
options: DiffOptions;
collapsed: boolean;
onToggle: () => void;
onComment: OnCommentCallback;
}) {
const trpc = useTRPC();
const { data: content } = useQuery(
Expand All @@ -245,7 +239,7 @@ function UntrackedFileDiff({
oldFile={oldFile}
newFile={newFile}
options={{ ...options, collapsed }}
onComment={onComment}
taskId={taskId}
renderCustomHeader={(fd) => (
<DiffFileHeader
fileDiff={fd}
Expand Down

This file was deleted.

12 changes: 2 additions & 10 deletions apps/code/src/renderer/features/code-review/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,13 @@ export type AnnotationMetadata = HunkRevertMetadata | CommentMetadata;

export type DiffOptions = FileDiffOptions<AnnotationMetadata>;

export type OnCommentCallback = (
filePath: string,
startLine: number,
endLine: number,
side: AnnotationSide,
comment: string,
) => void;

export type PatchDiffProps = FileDiffProps<AnnotationMetadata> & {
repoPath?: string;
onComment?: OnCommentCallback;
taskId?: string;
};

export type FilesDiffProps = MultiFileDiffProps<AnnotationMetadata> & {
onComment?: OnCommentCallback;
taskId?: string;
};

export type InteractiveFileDiffProps = PatchDiffProps | FilesDiffProps;
Loading
Loading