42 lines · 1.4 KB
import { cn } from "~/utils/cn";

const HASH_LENGTH = 7;
const FNV_OFFSET = 0x811c9dc5;
const FNV_PRIME = 0x01000193;
const HEX = 16;

/** Stable, commit-looking short hash for a section label. */
function shortHash(text: string): string {
  let hash = FNV_OFFSET;
  for (const char of text) hash = Math.imul(hash ^ char.charCodeAt(0), FNV_PRIME);
  return (hash >>> 0).toString(HEX).padStart(HASH_LENGTH, "0").slice(0, HASH_LENGTH);
}

type Props = {
  /** Reads like a commit subject, e.g. "feat: review". */
  message: string;
  id?: string;
  last?: boolean;
  className?: string;
  children: React.ReactNode;
};

/**
 * A landing-page section drawn as one entry in `git log --graph`: a rail with a commit node on the
 * left, the commit subject above the content.
 */
export function CommitSection({ message, id, last = false, className, children }: Props) {
  return (
    <section id={id} className={cn("relative md:pl-12", className)}>
      <div aria-hidden className="absolute inset-y-0 left-3 hidden md:block">
        <span className={cn("absolute top-0 left-0 w-px bg-gray-800", last ? "h-3" : "h-full")} />
        <span className="absolute top-1.5 -left-1.5 size-3 rounded-full border-2 border-accent-500 bg-gray-950" />
      </div>
      <p className="mb-4 font-mono text-xs text-gray-500">
        <span className="text-accent-400">{shortHash(message)}</span> {message}
      </p>
      {children}
    </section>
  );