import type { PresenceAvatarsProps as PresenceAvatarsContainerProps } from 'moi/collab' import type { UserProfile } from '@/lib/collab/types' import { useContext } from 'react' import type { HTMLAttributes, ReactNode, Ref } from 'react' import { motion } from 'motion/react' import { cn } from '@/client/lib/cn' import { PresenceGroupContext, usePresenceTarget, useTargetPresence } from './presence-helpers' import { useUsers } from '../hooks' import { UserAvatar } from './user-avatar' export function PresenceAvatarsContainer({ id, present, align, children, className }: PresenceAvatarsContainerProps) { const target = usePresenceTarget(id) const presence = useTargetPresence(target, present) const profiles = useUsers(presence.users.map(user => user.id)) const grouped = useContext(PresenceGroupContext).length > 0 return ( ({ ...user, profile: profiles[index] }))} // Controlled presence can occupy several targets at once; only focus moves between them. animate={grouped && present === undefined} align={align} className={className} > {children} ) } export type PresenceAvatarsProps = HTMLAttributes & { ref?: Ref users: readonly { id: string; connectionId: string; profile?: UserProfile }[] children: ReactNode animate?: boolean align?: PresenceAvatarsContainerProps['align'] } // A target owns its own gutter. Optional group-scoped layout IDs let the same // user's face glide between targets without searching or guessing DOM order. export function PresenceAvatars({ users, children, animate = false, align = 'center', className, ...rest }: PresenceAvatarsProps) { return ( {children} {users.map(({ id, connectionId, profile }) => ( ))} ) }