Open-source, local-first AI agent for real work.
TypeScript agent-toolsai-agentbyokcoding-agentcross-platformdesktop-appdeveloper-toolselectronharnessknowledge-basellmwikilocal-firstmcpplugin-systemprivacy-firstskillstypescriptvettaworkflow-automation
import { useTranslation } from "@vetta-org/plugin-sdk";
import { useCallback, useState } from "react";
import { notifyError } from "../git/runtime";
import { applyTurnFiles } from "../git/turnReview";
import { ConfirmDialog } from "./ConfirmDialog";
/** Files listed by name in the overwrite confirmation before it says "and N more". */
const CONFLICT_PREVIEW = 5;
interface PendingConflict {
paths: string[];
direction: "revert" | "restore";
edited: string[];
}
/**
* Revert / restore turn files, shared by the turn card and the review view.
*
* Neither direction asks first: both are undoable from the same row. The one
* case that does ask is a file edited after the turn ended, because putting it
* back would silently drop those later edits.
*/
export function useTurnFileActions(cwd: string | null | undefined): {
busy: boolean;
apply: (paths: readonly string[], direction: "revert" | "restore") => void;
dialog: JSX.Element;
} {
const { t } = useTranslation();
const [busy, setBusy] = useState(false);
const [conflict, setConflict] = useState<PendingConflict | null>(null);
const run = useCallback(
async (paths: readonly string[], direction: "revert" | "restore", force: boolean): Promise<void> => {
if (!cwd) return;
setBusy(true);
try {
const result = await applyTurnFiles(cwd, paths, direction, force);
if (result.status === "conflict") setConflict({ paths: [...paths], direction, edited: result.paths });
} catch (error) {
notifyError(t(direction === "revert" ? "review.revertFailed" : "review.restoreFailed"), error);
} finally {
setBusy(false);
}
},
[cwd, t],
);
const apply = useCallback(
(paths: readonly string[], direction: "revert" | "restore") => {
if (!busy) void run(paths, direction, false);
},
[busy, run],
);
const dialog = (
<ConfirmDialog
open={conflict !== null}
destructive
title={t("review.conflictTitle", { count: conflict?.edited.length ?? 0 })}
description={t(conflict?.direction === "restore" ? "review.conflictRestoreDescription" : "review.conflictRevertDescription")}
detail={
conflict && (
<ul className="max-h-32 overflow-y-auto rounded border border-border bg-muted/40 px-2 py-1 text-[11px] text-muted-foreground">
{conflict.edited.slice(0, CONFLICT_PREVIEW).map((path) => (
<li key={path} className="git-mono truncate">
{path}
</li>
))}
{conflict.edited.length > CONFLICT_PREVIEW && (
<li className="italic">{t("discard.more", { count: conflict.edited.length - CONFLICT_PREVIEW })}</li>
)}
</ul>
)
}
confirmLabel={t(conflict?.direction === "restore" ? "review.conflictRestoreConfirm" : "review.conflictRevertConfirm")}
onConfirm={() => {
const target = conflict;
setConflict(null);
if (target) void run(target.paths, target.direction, true);
}}
onCancel={() => setConflict(null)}
/>
);
return { busy, apply, dialog };
}