import { useState } from "react";
import type { DiffSide, RangeStart } from "~/components/DiffView";
type Target = { path: string; line: number; side: DiffSide; startLine: number | null; startSide: DiffSide | null };
export type DraftComment = Target & { key: string; body: string };
export function useReviewDraft() {
const [comments, setComments] = useState<DraftComment[]>([]);
const [composing, setComposing] = useState<Target | null>(null);
return {
comments,
composing,
start: (path: string, line: number, side: DiffSide, start?: RangeStart) =>
setComposing({ path, line, side, startLine: start?.line ?? null, startSide: start?.side ?? null }),
cancel: () => setComposing(null),
add: (body: string) => {
if (!composing) return;
setComments((current) => [
...current,
{ ...composing, body, key: `${composing.path}:${composing.side}:${composing.line}:${current.length}` },
]);
setComposing(null);
},
remove: (key: string) => setComments((current) => current.filter((comment) => comment.key !== key)),
clear: () => setComments([]),
};
}