{"version":3,"file":"slotRegistry.mjs","names":[],"sources":["../../../../src/plugins/SlotLayout/slotRegistry.tsx"],"sourcesContent":["// Per-kind slot policy. Each registered `kind` declares how it renders (`Component`) and the policy\n// the navigation layer reads: `persistent` (nav-rail lists that `hide` rather than `close`) and\n// `view` (the ChatView a newly opened kind belongs to).\n\nimport React, { createContext, useContext } from 'react';\n\nimport { ChannelNavigation } from '../../components/ChannelList';\nimport { ThreadList } from '../../components/Threads/ThreadList';\n\nimport type { ReactNode } from 'react';\nimport type { ChatView } from './ChatView';\nimport type {\n  ChatViewEntityBinding,\n  ChatViewEntityKind,\n  ChatViewSlotRendererProps,\n  ChatViewSlotRenderers,\n} from './slotBinding';\n\nexport type SlotKindDefinition<TKind extends ChatViewEntityKind> = {\n  /**\n   * Pure renderer for this kind: it does not claim slots or read layout state. A kind may declare\n   * only policy and leave rendering to the app's `slotRenderers` (e.g. `channel`).\n   */\n  Component?: (props: ChatViewSlotRendererProps<TKind>) => ReactNode;\n  /**\n   * Marks nav-rail lists: navigation hides and unhides them but never closes or releases them, and\n   * default slot resolution never picks them for a binding of another kind.\n   */\n  persistent?: boolean;\n  /**\n   * ChatView that opening a binding of this kind switches to. Without it, the binding opens in the\n   * active view.\n   */\n  view?: ChatView;\n};\n\nexport type SlotKindRegistry = Partial<{\n  [TKind in ChatViewEntityKind]: SlotKindDefinition<TKind>;\n}>;\n\n/** Built-in renderer of the `userProfile` kind: the user ID in a `<div>`. */\nconst SlotUserProfile = ({ userId }: { userId: string }) => (\n  <div className='str-chat__chat-view__slot-user-profile' data-user-id={userId}>\n    {userId}\n  </div>\n);\n\nexport const defaultSlotKindRegistry: SlotKindRegistry = {\n  // `channel` declares only policy; its renderer comes from the `slotRenderers` prop (see\n  // `resolveSlotKindRegistry`).\n  channel: { view: 'channels' },\n  channelList: {\n    Component: () => <ChannelNavigation />,\n    persistent: true,\n  },\n  threadList: {\n    Component: () => <ThreadList />,\n    persistent: true,\n  },\n  userProfile: {\n    Component: ({ source }) => <SlotUserProfile userId={source.userId} />,\n  },\n};\n\n/** Merges the app's `slotRenderers` into a registry, replacing only each kind's `Component`. */\nexport const resolveSlotKindRegistry = (\n  slotRenderers?: ChatViewSlotRenderers,\n  base: SlotKindRegistry = defaultSlotKindRegistry,\n): SlotKindRegistry => {\n  if (!slotRenderers) return base;\n  const merged: SlotKindRegistry = { ...base };\n  (Object.keys(slotRenderers) as ChatViewEntityKind[]).forEach((kind) => {\n    const Component = slotRenderers[kind];\n    if (!Component) return;\n    // TS cannot express a correlated union across a keyed record; each key and value kind match.\n    (merged as Record<string, SlotKindDefinition<ChatViewEntityKind>>)[kind] = {\n      ...(base[kind] as SlotKindDefinition<ChatViewEntityKind> | undefined),\n      Component: Component as SlotKindDefinition<ChatViewEntityKind>['Component'],\n    };\n  });\n  return merged;\n};\n\n/**\n * Whether a slot's content renders anything. React renders nothing for `null`, `undefined` and\n * booleans, so a renderer returning `false` (as `condition && <View />` does) leaves the slot\n * empty.\n */\nexport const hasSlotContent = (content: ReactNode) =>\n  content != null && typeof content !== 'boolean';\n\n/**\n * Renders the registered `Component` of the entity's kind. A missing entity, an unregistered kind\n * or a kind without a `Component` render nothing, leaving any fallback to the caller.\n */\nexport const renderSlotFromRegistry = (\n  entity: ChatViewEntityBinding | undefined,\n  slot: string,\n  registry: SlotKindRegistry,\n): ReactNode | null => {\n  if (!entity) return null;\n  const definition = registry[entity.kind];\n  if (!definition?.Component) return null;\n  const Component = definition.Component as (props: {\n    entity: ChatViewEntityBinding;\n    slot: string;\n    source: ChatViewEntityBinding['source'];\n  }) => ReactNode;\n  return Component({ entity, slot, source: entity.source });\n};\n\n/** Whether `kind` is registered as persistent (see {@link SlotKindDefinition.persistent}). */\nexport const isPersistentSlotKind = (\n  registry: SlotKindRegistry,\n  kind?: ChatViewEntityKind,\n): boolean => !!kind && !!registry[kind]?.persistent;\n\nexport const resolveSlotKindView = (\n  registry: SlotKindRegistry,\n  kind: ChatViewEntityKind,\n): ChatView | undefined => registry[kind]?.view;\n\nexport const SlotRegistryContext = createContext<SlotKindRegistry>(\n  defaultSlotKindRegistry,\n);\n\nexport const useSlotRegistry = () => useContext(SlotRegistryContext);\n"],"mappings":";;;;;;AAyCA,IAAM,mBAAmB,EAAE,aACzB,oBAAC,OAAD;CAAK,WAAU;CAAyC,gBAAc;WACnE;AACE,CAAA;AAGP,IAAa,0BAA4C;CAGvD,SAAS,EAAE,MAAM,WAAW;CAC5B,aAAa;EACX,iBAAiB,oBAAC,mBAAD,CAAoB,CAAA;EACrC,YAAY;CACd;CACA,YAAY;EACV,iBAAiB,oBAAC,YAAD,CAAa,CAAA;EAC9B,YAAY;CACd;CACA,aAAa,EACX,YAAY,EAAE,aAAa,oBAAC,iBAAD,EAAiB,QAAQ,OAAO,OAAS,CAAA,EACtE;AACF;;AAGA,IAAa,2BACX,eACA,OAAyB,4BACJ;CACrB,IAAI,CAAC,eAAe,OAAO;CAC3B,MAAM,SAA2B,EAAE,GAAG,KAAK;CAC3C,OAAQ,KAAK,aAAa,CAAC,CAA0B,SAAS,SAAS;EACrE,MAAM,YAAY,cAAc;EAChC,IAAI,CAAC,WAAW;EAEhB,OAAmE,QAAQ;GACzE,GAAI,KAAK;GACE;EACb;CACF,CAAC;CACD,OAAO;AACT;;;;;;AAOA,IAAa,kBAAkB,YAC7B,WAAW,QAAQ,OAAO,YAAY;;;;;AAMxC,IAAa,0BACX,QACA,MACA,aACqB;CACrB,IAAI,CAAC,QAAQ,OAAO;CACpB,MAAM,aAAa,SAAS,OAAO;CACnC,IAAI,CAAC,YAAY,WAAW,OAAO;CACnC,MAAM,YAAY,WAAW;CAK7B,OAAO,UAAU;EAAE;EAAQ;EAAM,QAAQ,OAAO;CAAO,CAAC;AAC1D;;AAGA,IAAa,wBACX,UACA,SACY,CAAC,CAAC,QAAQ,CAAC,CAAC,SAAS,KAAK,EAAE;AAE1C,IAAa,uBACX,UACA,SACyB,SAAS,KAAK,EAAE;AAE3C,IAAa,sBAAsB,cACjC,uBACF;AAEA,IAAa,wBAAwB,WAAW,mBAAmB"}