import type { Awareness } from "y-protocols/awareness"; import { awarenessColor, useAwareness, type AwarenessState } from "@/client/hooks/use-sync"; import { useCollabIdentity } from "@/client/lib/collab-identity"; interface AvatarStackProps { awareness: Awareness | null; localClientId: number | null; } interface RemoteUser { clientId: number; color: string; name: string; avatar_url: string | null; } export function AvatarStack({ awareness, localClientId }: AvatarStackProps) { const { resolveIdentity } = useCollabIdentity(); const states = useAwareness(awareness); const remoteUsers: RemoteUser[] = []; states.forEach((state: AwarenessState, clientId: number) => { if (clientId === localClientId || !state.user) return; const userId = state.user.userId ?? null; const identity = resolveIdentity(userId, clientId); remoteUsers.push({ clientId, color: awarenessColor(userId, clientId), name: identity.name, avatar_url: identity.avatar_url, }); }); if (remoteUsers.length === 0) return null; return (
{remoteUsers.slice(0, 5).map((u) => (
{u.avatar_url ? ( {u.name} ) : ( {u.name.charAt(0).toUpperCase()} )}
))} {remoteUsers.length > 5 && (
+{remoteUsers.length - 5}
)}
); }