25 lines · 1.0 KB
import { useQuery } from "@tanstack/react-query";
import { getCommitSignatures } from "~/fns/security";

export type CommitSignature = Awaited<ReturnType<typeof getCommitSignatures>>[number];

const MAX_SHAS = 90;
const NO_SIGNATURES: Record<string, CommitSignature> = {};

export const commitSignaturesQueryKey = (owner: string, repo: string, shas: string[]) =>
  ["commit-signatures", owner, repo, shas.join(",")] as const;

/** 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 wanted = shas.slice(0, MAX_SHAS);
  const query = useQuery({
    queryKey: commitSignaturesQueryKey(owner, repo, wanted),
    queryFn: async () => {
      const rows = await getCommitSignatures({ data: { owner, repo, shas: wanted } });
      return Object.fromEntries(rows.map((row) => [row.sha, row]));
    },
    enabled: wanted.length > 0,
  });
  return query.data ?? NO_SIGNATURES;
}