Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

1.9 KB
Raw Blame
import { t } from 'i18next';

import { usePresence } from '@/hooks/use-presence';

import { Tooltip, TooltipContent, TooltipTrigger } from '../ui/tooltip';

import { ApAvatar } from './ap-avatar';

const MAX_VISIBLE_AVATARS = 5;
const AVATAR_SIZE = 28;
const BORDER_COLORS = [
  'var(--swatch-1-mark)',
  'var(--swatch-3-mark)',
  'var(--swatch-4-mark)',
  'var(--swatch-6-mark)',
  'var(--swatch-8-mark)',
  'var(--swatch-9-mark)',
  'var(--swatch-11-mark)',
  'var(--swatch-12-mark)',
];

function getBorderColor(userId: string): string {
  let hash = 0;
  for (let i = 0; i < userId.length; i++) {
    hash = (hash * 31 + userId.charCodeAt(i)) | 0;
  }
  return BORDER_COLORS[Math.abs(hash) % BORDER_COLORS.length];
}

export function ActiveUsersWidget({ resourceId }: ActiveUsersWidgetProps) {
  const activeUsers = usePresence({ resourceId });

  if (activeUsers.length === 0) {
    return null;
  }

  const visibleUsers = activeUsers.slice(0, MAX_VISIBLE_AVATARS);
  const overflowCount = activeUsers.length - MAX_VISIBLE_AVATARS;

  return (
    <div className="flex items-center gap-1">
      {visibleUsers.map((user) => (
        <div
          key={user.userId}
          className="rounded-full border-2"
          style={{ borderColor: getBorderColor(user.userId) }}
        >
          <ApAvatar id={user.userId} size="small" />
        </div>
      ))}
      {overflowCount > 0 && (
        <Tooltip>
          <TooltipTrigger asChild>
            <div
              className="flex items-center justify-center rounded-full bg-gray-3 text-xs font-medium text-gray-11 border"
              style={{ width: `${AVATAR_SIZE}px`, height: `${AVATAR_SIZE}px` }}
            >
              +{overflowCount}
            </div>
          </TooltipTrigger>
          <TooltipContent side="bottom">
            {t('+{count} more', { count: overflowCount })}
          </TooltipContent>
        </Tooltip>
      )}
    </div>
  );
}

type ActiveUsersWidgetProps = {
  resourceId: string;
};