"use client";

import Link from "next/link";
import { profilePath } from "@/lib/profilePath";

/**
 * A person's name or avatar, linking to their profile.
 *
 * Six list rows rendered this as a `<span onClick={() => router.push(…)}>` or an
 * `<Image onClick>`: a navigation with no keyboard access, nothing announced to
 * a screen reader, and no middle-click or "open in new tab". `next/link` gives
 * all of that back and keeps the client-side transition.
 *
 * A row whose payload has no id renders its children unwrapped rather than a
 * link to nowhere — the handlers this replaces mostly guarded on that with an
 * early `return`, which left a cursor that looked clickable and did nothing.
 */
export default function ProfileLink({
  id,
  className,
  children,
  /** Set on a link inside another clickable row, so the row does not also fire. */
  stopPropagation = false,
}: {
  id: number | string | null | undefined;
  className?: string;
  children: React.ReactNode;
  stopPropagation?: boolean;
}) {
  const href = profilePath(id);

  if (!href) return <>{children}</>;

  return (
    <Link
      href={href}
      className={className}
      onClick={
        stopPropagation ? (event) => event.stopPropagation() : undefined
      }
    >
      {children}
    </Link>
  );
}
