← Files Daybreak Defense FactoryARCHIVED FILE
.internal/defense-factory-ui/src/patch-model.ts
2.15 KB · Nov 28, 2026 · 18:13 UTC
import type { components } from "@oai/aardvark-client/components";
import { skipToken, useMutation, useQuery } from "@tanstack/react-query";
import { useCloud } from "./app-context";
import {
DefenseFactoryRequestError,
downloadFile,
securityClient,
} from "./client";
export function useFindingPatch(
finding: components["schemas"]["ScanFindingResponse"],
open: boolean,
refreshFinding: () => Promise<unknown>,
) {
const cloud = useCloud();
const task = finding.proposed_patch?.latest_task;
const path = task ? { id: finding.hid, task_id: task.id } : null;
const queryKey = [
"finding-patch",
cloud.accountId,
cloud.identity?.userId,
finding.hid,
task?.id,
task?.status,
];
async function refreshIfTaskMissing(error: unknown): Promise<never> {
if (
error instanceof DefenseFactoryRequestError &&
(error.status === 404 || error.category === "not_found")
) {
// Only the latest task is readable. Refresh the report so its task ID
// selects the replacement preview, even if the old task was finished.
await refreshFinding();
}
throw error;
}
const query = useQuery({
queryKey,
queryFn:
open && path && finding.proposed_patch?.status !== "in_progress"
? async () => {
const result = await securityClient
.request({
operation: "findings_patch_get",
parameters: { path },
})
.catch(refreshIfTaskMissing);
if ("contentType" in result)
throw new Error("Expected a patch preview");
return result;
}
: skipToken,
retry: false,
});
const download = useMutation({
mutationKey: queryKey,
mutationFn: async () => {
if (!path) return;
const result = await securityClient
.request({
operation: "findings_patch_get",
parameters: { path, query: { download: true } },
})
.catch(refreshIfTaskMissing);
if (!("contentType" in result))
throw new Error("Expected a patch download");
await downloadFile(result);
},
});
return { queryKey, query, download };
}
SHA-256: 4921f1ef87f862057c089ba4674120b7319c0106e14b1ca329b93891fa7d6d8a