Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

3.1 KB
Raw Blame
import { useEffect, useState } from "react";
import "@editor/ui/Avatar.css";

export type AvatarSize = "xs" | "sm" | "md" | "lg" | "xl";
export type AvatarTone =
  | "blue"
  | "purple"
  | "green"
  | "amber"
  | "red"
  | "neutral";

export interface AvatarProps {
  /** Image source. Falls back to initials when missing or load fails. */
  src?: string;
  /** Full name. Initials are derived from the first letter of each word, max 2. */
  name: string | undefined;
  size?: AvatarSize;
  /** Background tone when rendering initials. Defaults to blue. */
  tone?: AvatarTone;
  /** Optional click handler — renders as a button when supplied. */
  onClick?: () => void;
  ariaLabel?: string;
  /** Marks the button as the current destination when it is a nav control. */
  ariaCurrent?: "page" | "true";
  className?: string;
}

function avatarInitials(name: string | undefined | null): string {
  // Defensive: a roster row whose display name never arrived used to throw
  // here and take the whole section down with it.
  const parts = (name ?? "").trim().split(/\s+/).filter(Boolean);
  if (parts.length === 0) return "?";
  // Single word (a username or an email) reads as one letter — two letters of
  // "admin" ("AD") looks like a different person's initials, not a truncation.
  if (parts.length === 1) return parts[0].slice(0, 1).toUpperCase();
  return (parts[0][0] + parts[parts.length - 1][0]).toUpperCase();
}

/**
 * Round avatar showing either an image or coloured initials. Used by the
 * Header account button, reviewer rows, and assistant message gutters.
 */
export function Avatar({
  src,
  name,
  size = "md",
  tone = "blue",
  onClick,
  ariaLabel,
  ariaCurrent,
  className,
}: AvatarProps) {
  // A picture URL that 404s (expired signed URL, deleted upload) must not leave
  // an empty disc — fall back to the same initials the no-picture case shows, so
  // every surface rendering this identity agrees on what it draws.
  const [srcFailed, setSrcFailed] = useState(false);
  useEffect(() => setSrcFailed(false), [src]);
  const showImage = Boolean(src) && !srcFailed;

  const classes = [
    "sui-avatar",
    `sui-avatar--${size}`,
    // The tone is the ground the initials sit on, so it is theirs alone: painted
    // under a picture it shows through every transparent pixel of an uploaded
    // logo, which reads as a coloured disc nobody asked for.
    showImage ? "sui-avatar--image" : `sui-avatar--${tone}`,
    onClick ? "sui-avatar--interactive" : "",
    className ?? "",
  ]
    .filter(Boolean)
    .join(" ");

  const content = showImage ? (
    <img
      src={src}
      alt={ariaLabel ?? name}
      className="sui-avatar__img"
      onError={() => setSrcFailed(true)}
    />
  ) : (
    <span className="sui-avatar__initials" aria-hidden>
      {avatarInitials(name)}
    </span>
  );

  if (onClick) {
    return (
      <button
        type="button"
        className={classes}
        onClick={onClick}
        aria-label={ariaLabel ?? name}
        aria-current={ariaCurrent}
      >
        {content}
      </button>
    );
  }
  return (
    <span className={classes} role="img" aria-label={ariaLabel ?? name}>
      {content}
    </span>
  );
}