import { useEffect, useState } from "react";
import { getCommitSignatures } from "~/fns/security";
export type CommitSignature = Awaited<ReturnType<typeof getCommitSignatures>>[number];
const MAX_SHAS = 90;
/** Loads verification badges after render so commit lists never block on signature checks. */
export function useCommitSignatures(owner: string, repo: string, shas: string[]): Record<string, CommitSignature> {
const [signatures, setSignatures] = useState<Record<string, CommitSignature>>({});
const key = shas.slice(0, MAX_SHAS).join(",");
useEffect(() => {
if (!key) return;
let cancelled = false;
getCommitSignatures({ data: { owner, repo, shas: key.split(",") } })
.then((rows) => {
if (!cancelled) setSignatures(Object.fromEntries(rows.map((row) => [row.sha, row])));
})
.catch(() => undefined);
return () => {
cancelled = true;
};
}, [owner, repo, key]);
return signatures;
}