Skip to content

Open-source, local-first AI agent for real work.

3.9 KB
Raw Blame
import { useTranslation } from "@vetta-org/plugin-sdk";
import { Button } from "@vetta-org/ui";
import type { KeyboardEvent } from "react";
import { useState } from "react";
import type { ReviewComment, ReviewSide } from "../git/turnReview";
import { CloseIcon, CommentIcon } from "./icons";

/** A line range on one side of a diff, as picked from the gutter. */
export interface CommentRange {
	side: ReviewSide;
	start: number;
	end: number;
}

function useRangeLabel(): (range: CommentRange) => string {
	const { t } = useTranslation();
	return (range) => {
		const start = Math.min(range.start, range.end);
		const end = Math.max(range.start, range.end);
		const lines = start === end ? t("review.line", { line: start }) : t("review.lines", { start, end });
		return range.side === "deletions" ? `${lines} · ${t("review.removedSide")}` : lines;
	};
}

/*
 * Both cards sit inside the diff's line grid, which inherits a monospace,
 * pre-formatted context from the renderer; `git-annotation` resets that.
 */
const CARD = "git-annotation mx-2 my-1.5 max-w-[640px] rounded-lg border border-border/70 bg-card text-[12px] shadow-xs";

/** A comment already added to the review, shown under the lines it is about. */
export function ReviewCommentCard({ comment, onRemove }: { comment: ReviewComment; onRemove: () => void }): JSX.Element {
	const { t } = useTranslation();
	const label = useRangeLabel();
	return (
		<div className={`${CARD} px-2.5 py-2`}>
			<div className="flex items-center gap-1.5 text-[11px] text-muted-foreground">
				<CommentIcon className="h-3 w-3 text-sky-500" />
				<span className="truncate">{label(comment)}</span>
				<button
					type="button"
					onClick={onRemove}
					title={t("review.removeComment")}
					aria-label={t("review.removeComment")}
					className="ml-auto flex size-5 items-center justify-center rounded text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
				>
					<CloseIcon className="h-3 w-3" />
				</button>
			</div>
			<p className="mt-1 whitespace-pre-wrap break-words leading-relaxed text-foreground">{comment.body}</p>
		</div>
	);
}

/** The inline editor that opens when the user clicks "+" beside a line. */
export function ReviewCommentComposer({
	range,
	onSubmit,
	onCancel,
}: {
	range: CommentRange;
	onSubmit: (body: string) => void;
	onCancel: () => void;
}): JSX.Element {
	const { t } = useTranslation();
	const label = useRangeLabel();
	const [body, setBody] = useState("");
	const submit = (): void => {
		if (body.trim()) onSubmit(body.trim());
	};
	const onKeyDown = (event: KeyboardEvent<HTMLTextAreaElement>): void => {
		if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {
			event.preventDefault();
			submit();
		} else if (event.key === "Escape") {
			event.preventDefault();
			onCancel();
		}
	};
	return (
		<div className={`${CARD} overflow-hidden transition-shadow focus-within:ring-1 focus-within:ring-ring/40`}>
			<div className="flex items-center gap-1.5 px-2.5 pt-2 text-[11px] text-muted-foreground">
				<CommentIcon className="h-3 w-3 text-sky-500" />
				{t("review.commentOn", { lines: label(range) })}
			</div>
			<textarea
				// biome-ignore lint/a11y/noAutofocus: the editor exists because the user just asked to write here.
				autoFocus
				value={body}
				onChange={(event) => setBody(event.target.value)}
				onKeyDown={onKeyDown}
				rows={3}
				aria-label={t("review.commentPlaceholder")}
				placeholder={t("review.commentPlaceholder")}
				className="block min-h-[60px] w-full resize-y bg-transparent px-2.5 py-1.5 text-[12.5px] leading-relaxed text-foreground outline-none placeholder:text-muted-foreground/50"
			/>
			<div className="flex items-center justify-end gap-1 px-1.5 pb-1.5">
				<Button type="button" size="xs" variant="ghost" onClick={onCancel}>
					{t("review.cancel")}
				</Button>
				<Button type="button" size="xs" variant="default" disabled={!body.trim()} onClick={submit}>
					{t("review.addComment")}
				</Button>
			</div>
		</div>
	);
}