38 lines · 1.3 KB
import { cn } from "~/utils/cn";
import { TONE_BADGE } from "~/ui/tones";
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 px-2 py-0.5 text-2xs font-medium",
        TONE_BADGE[verified ? "success" : "warning"],
        className,
      )}
    >
      {verified ? "Verified" : "Unverified"}
    </span>
  );
}