{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "rich-text-editor-presence",
  "title": "Presence",
  "description": "The online collaborator avatar stack and the connection status of the provider.",
  "dependencies": [
    "cn"
  ],
  "registryDependencies": [
    "avatar",
    "litingyes/blocove/rich-text-editor-collaboration"
  ],
  "files": [
    {
      "path": "src/registry/blocove/ui/rich-text-editor/presence.tsx",
      "content": "import type { CollaborationProvider, CollaborationUser } from '@/components/blocove/rich-text-editor/collaboration'\nimport { cn } from 'cn'\nimport { useEffect, useState } from 'react'\nimport { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'\n\n/** The initials shown when a collaborator has no avatar image. */\nexport function initialsOf(name: string) {\n  const initials = name\n    .trim()\n    .split(/\\s+/)\n    .slice(0, 2)\n    .map(part => part.charAt(0).toUpperCase())\n    .join('')\n\n  return initials || '?'\n}\n\ninterface PresenceEntry {\n  clientId: number\n  user: CollaborationUser\n}\n\n/**\n * The collaborators currently present, read from the provider's awareness. The\n * state map is external to React, so the entries are copied into state and the\n * subscription keeps them current.\n */\nfunction useCollaborationEntries(provider: CollaborationProvider) {\n  const [entries, setEntries] = useState<PresenceEntry[]>([])\n\n  useEffect(() => {\n    const awareness = provider.awareness\n\n    if (!awareness) {\n      // eslint-disable-next-line react/set-state-in-effect -- Awareness is an external store; its snapshot enters React state once the provider exposes one.\n      setEntries([])\n      return\n    }\n\n    const sync = () => {\n      const next: PresenceEntry[] = []\n\n      for (const [clientId, state] of awareness.getStates()) {\n        const candidate = state.user\n\n        if (\n          candidate\n          && typeof candidate === 'object'\n          && typeof (candidate as CollaborationUser).name === 'string'\n        ) {\n          next.push({\n            clientId,\n            user: candidate as CollaborationUser,\n          })\n        }\n      }\n\n      // eslint-disable-next-line react/set-state-in-effect -- the store lives outside React; the subscription is the only way to mirror it.\n      setEntries(next)\n    }\n\n    sync()\n    awareness.on('change', sync)\n    awareness.on('update', sync)\n\n    return () => {\n      awareness.off('change', sync)\n      awareness.off('update', sync)\n    }\n  }, [provider])\n\n  return entries\n}\n\nexport interface EditorPresenceProps {\n  /** The connected provider. Its awareness lists the online collaborators. */\n  provider: CollaborationProvider\n  /** How many avatars to render before the overflow counter. Defaults to 4. */\n  max?: number\n  className?: string\n}\n\n/**\n * The avatar stack of the collaborators currently in the document. It stays\n * empty until a first peer advertises itself through awareness.\n */\nexport function EditorPresence({\n  provider,\n  max = 4,\n  className,\n}: EditorPresenceProps) {\n  const entries = useCollaborationEntries(provider)\n\n  if (entries.length === 0) {\n    return null\n  }\n\n  const visible = entries.slice(0, max)\n  const overflow = entries.length - visible.length\n\n  return (\n    <div\n      data-slot=\"editor-presence\"\n      className={cn('flex items-center gap-2', className)}\n    >\n      <div className=\"flex items-center -space-x-2\">\n        {visible.map(({ clientId, user }) => (\n          <Avatar\n            key={clientId}\n            size=\"sm\"\n            title={user.name}\n            className=\"ring-2 ring-background\"\n          >\n            {user.avatar\n              ? <AvatarImage src={user.avatar} alt={user.name} />\n              : null}\n            <AvatarFallback\n              style={{\n                backgroundColor: user.color ?? undefined,\n                color: user.color ? '#fff' : undefined,\n              }}\n            >\n              {initialsOf(user.name)}\n            </AvatarFallback>\n          </Avatar>\n        ))}\n        {overflow > 0\n          ? (\n              <span className=\"flex size-6 items-center justify-center rounded-full bg-muted text-xs text-muted-foreground ring-2 ring-background\">\n                +\n                {overflow}\n              </span>\n            )\n          : null}\n      </div>\n      <span className=\"text-xs text-muted-foreground\">\n        {entries.length === 1\n          ? '1 online'\n          : `${entries.length} online`}\n      </span>\n    </div>\n  )\n}\n\nconst statusLabels: Record<string, string> = {\n  connected: 'Live',\n  connecting: 'Connecting…',\n  disconnected: 'Offline',\n}\n\nconst statusDots: Record<string, string> = {\n  connected: 'bg-emerald-500',\n  connecting: 'bg-amber-500',\n  disconnected: 'bg-muted-foreground',\n}\n\nexport interface EditorCollaborationStatusProps {\n  /** The provider whose connection state is shown. */\n  provider: CollaborationProvider\n  className?: string\n}\n\n/**\n * A compact connection indicator. It reads the initial status from the provider\n * and follows its `status` events, so a demo degrades to \"Offline\" instead of\n * pretending the document is shared.\n */\nexport function EditorCollaborationStatus({\n  provider,\n  className,\n}: EditorCollaborationStatusProps) {\n  const [status, setStatus] = useState(() => provider.status ?? 'connecting')\n\n  useEffect(() => {\n    if (!provider.on || !provider.off) {\n      return\n    }\n\n    const handleStatus = (payload: { status?: string }) => {\n      setStatus(payload.status ?? 'disconnected')\n    }\n\n    // A provider method, so it must be called on the provider: Hocuspocus reads\n    // its listener registry from `this`.\n    provider.on('status', handleStatus)\n\n    return () => provider.off?.('status', handleStatus)\n  }, [provider])\n\n  return (\n    <span\n      data-slot=\"editor-collaboration-status\"\n      data-status={status}\n      className={cn(\n        'inline-flex items-center gap-1.5 text-xs text-muted-foreground',\n        className,\n      )}\n    >\n      <span\n        aria-hidden\n        className={cn(\n          'size-2 rounded-full',\n          statusDots[status] ?? statusDots.disconnected,\n        )}\n      />\n      {statusLabels[status] ?? status}\n    </span>\n  )\n}\n",
      "type": "registry:ui",
      "target": "components/blocove/rich-text-editor/presence.tsx"
    }
  ],
  "categories": [
    "rich-text-editor",
    "collaboration"
  ],
  "type": "registry:ui"
}