Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
3.4 KB
Raw Blame
"use client";

import { useState, useEffect } from "react";
import { Bot, Users } from "@multica/ui/icons";
import { cn } from "@multica/ui/lib/utils";
import {
  AVATAR_SIZE_PX,
  DEFAULT_AVATAR_SIZE,
  type AvatarSize,
} from "@multica/ui/lib/avatar-size";
import { parseAvatarEmoji } from "@multica/ui/lib/avatar-emoji";
import { MulticaIcon } from "./multica-icon";

interface ActorAvatarProps {
  name: string;
  initials: string;
  avatarUrl?: string | null;
  isAgent?: boolean;
  isSystem?: boolean;
  isSquad?: boolean;
  size?: AvatarSize;
  className?: string;
}

function ActorAvatar({
  name,
  initials,
  avatarUrl,
  isAgent,
  isSystem,
  isSquad,
  size = DEFAULT_AVATAR_SIZE,
  className,
}: ActorAvatarProps) {
  const [imgError, setImgError] = useState(false);
  // A control that fixes its own content size (e.g. a toolbar pill) sets
  // `--avatar-slot-size`; it wins over `size` so the avatar can't outgrow the
  // control. A CSS variable rather than React context: it follows the DOM, so
  // hover cards portaled out of the control keep their own avatar sizes.
  const diameter = `var(--avatar-slot-size, ${AVATAR_SIZE_PX[size]}px)`;
  const scaled = (ratio: number) => `calc(${diameter} * ${ratio})`;
  const emoji = parseAvatarEmoji(avatarUrl);
  const showsSystemMark = isSystem && !emoji && (!avatarUrl || imgError);

  useEffect(() => {
    setImgError(false);
  }, [avatarUrl]);

  // Every actor — member, agent, squad, or system — takes the same shape:
  // `--avatar-radius`, a circle unless a theme sets it. This is the single
  // source of truth for avatar shape; the upload editors mirror it
  // (packages/views/common/avatar-upload-control.tsx, avatar-crop-dialog.tsx).
  return (
    <div
      data-slot="avatar"
      data-mc="avatar"
      data-type={isSystem ? "system" : isAgent ? "agent" : isSquad ? "squad" : "member"}
      className={cn(
        "inline-flex shrink-0 items-center justify-center font-medium overflow-hidden",
        // The system mark draws a round tile; filling the box with the tile
        // colour keeps it whole when a theme squares the avatar.
        showsSystemMark
          ? "bg-multica-ink dark:bg-multica-paper"
          : (!avatarUrl || emoji || imgError) && "bg-muted text-muted-foreground",
        className,
        // rounded-avatar stays last so a call-site `className` can never
        // override the shape — it is the theme's choice, not a per-site one.
        "rounded-avatar"
      )}
      style={{ width: diameter, height: diameter, fontSize: scaled(0.45) }}
    >
      {emoji ? (
        <span
          role="img"
          aria-label={name}
          className="select-none leading-none"
          style={{ fontSize: scaled(0.58) }}
        >
          {emoji}
        </span>
      ) : avatarUrl && !imgError ? (
        <img
          src={avatarUrl}
          alt={name}
          className="h-full w-full object-cover"
          onError={() => setImgError(true)}
        />
      ) : isSystem ? (
        // `size` only picks the optical size; the slot decides the rendered size.
        <MulticaIcon shape="circle" size={AVATAR_SIZE_PX[size]} style={{ width: "100%", height: "100%" }} />
      ) : isAgent ? (
        <Bot style={{ width: scaled(0.55), height: scaled(0.55) }} />
      ) : isSquad ? (
        <Users style={{ width: scaled(0.55), height: scaled(0.55) }} />
      ) : (
        initials
      )}
    </div>
  );
}

export { ActorAvatar, type ActorAvatarProps };