import type { EnvironmentId, ThreadId, WorktreeSetupSnapshot } from "@t3tools/contracts";
import { resolveVisibleWorktreeSetup } from "@t3tools/client-runtime/worktree-setup";
import { useState } from "react";
import { useEnvironmentQuery } from "../../state/query";
import { vcsEnvironment } from "../../state/vcs";
import { resolveWorktreeSetupSnapshot } from "./worktree-setup-state";
export function useWorktreeSetup(input: {
environmentId: EnvironmentId | null;
threadId: ThreadId | null;
preparing: boolean;
turnStarted: boolean;
followUpSent: boolean;
}) {
const key = JSON.stringify([input.environmentId, input.threadId]);
const [held, setHeld] = useState<{ key: string; snapshot: WorktreeSetupSnapshot } | null>(null);
const live = held?.key === key ? held.snapshot : null;
const query = useEnvironmentQuery(
input.environmentId &&
input.threadId &&
(live?.phase === "running" || (!live && input.preparing))
? vcsEnvironment.worktreeSetup({
environmentId: input.environmentId,
input: { threadId: input.threadId },
})
: null,
);
const snapshot = resolveWorktreeSetupSnapshot(input.threadId, query.data, live);
// Hold the newest snapshot so it survives the subscription closing once setup stops running.
// Set during render so each emission commits once; the resolver returns `live` by identity
// on the next pass, which ends the update.
if (snapshot && snapshot !== live) setHeld({ key, snapshot });
return {
snapshot,
visible: resolveVisibleWorktreeSetup({
live: snapshot,
recorded: null,
turnStarted: input.turnStarted,
followUpSent: input.followUpSent,
}),
};
}