39 lines · 1.3 KB
import { cn } from "~/utils/cn";
import type { CommitSignature } from "~/hooks/use-commit-signatures";

const REASONS: Record<string, string> = {
  unknown_key: "The signing key is not registered to any gitfor account.",
  bad_signature: "The signature does not match the commit contents.",
  email_mismatch: "The committer email does not match the key owner's account email.",
  unsupported: "The signature format or algorithm is not supported.",
  malformed: "The signature could not be parsed.",
};

export function SignatureBadge({
  signature,
  className,
}: {
  signature: CommitSignature | undefined;
  className?: string;
}) {
  if (!signature || signature.reason === "unsigned") return null;
  const verified = signature.verified;
  const title = verified
    ? `Signed with ${signature.kind === "ssh" ? "SSH" : "GPG"} key ${signature.keyFingerprint ?? ""} by ${signature.signer ?? "unknown"}`
    : (REASONS[signature.reason] ?? "Unverified signature");
  return (
    <span
      title={title}
      className={cn(
        "inline-flex items-center rounded-full border px-2 py-0.5 text-[11px] font-medium",
        verified
          ? "border-emerald-800 bg-emerald-950/50 text-emerald-300"
          : "border-amber-800 bg-amber-950/40 text-amber-300",
        className,
      )}
    >
      {verified ? "Verified" : "Unverified"}
    </span>
  );
}