{"version":3,"file":"useSlotEntity.mjs","names":[],"sources":["../../../../../src/plugins/SlotLayout/hooks/useSlotEntity.ts"],"sourcesContent":["import { getChatViewEntityBinding } from '../ChatView';\nimport { useLayoutViewState } from './useLayoutViewState';\n\nimport type { ChatView, ChatViewEntityBinding } from '../ChatView';\nimport type { SlotName } from '../layoutController/layoutControllerTypes';\n\ntype SlotEntityKind = ChatViewEntityBinding['kind'];\n\ntype SlotEntitySourceMap = {\n  [TKind in SlotEntityKind]: Extract<ChatViewEntityBinding, { kind: TKind }>['source'];\n};\n\ntype SlotEntitySourceByKind<TKind extends SlotEntityKind> = SlotEntitySourceMap[TKind];\n\ntype UseSlotEntityOptions<TKind extends SlotEntityKind> = {\n  /** Entity kind to resolve from the slot bindings. */\n  kind: TKind;\n  /** Only this slot is checked. Defaults to every slot of the view, in `availableSlots` order. */\n  slot?: SlotName;\n  /**\n   * View whose slots are read, even when another view is active. Defaults to the active view.\n   * Needed by state that must not clear on a view switch, such as URL sync.\n   */\n  view?: ChatView;\n};\n\nconst resolveCandidateSlots = (\n  slot: SlotName | undefined,\n  availableSlots: SlotName[],\n) => {\n  if (slot) return [slot];\n  return availableSlots;\n};\n\nconst isEntityOfKind = <TKind extends SlotEntityKind>(\n  entity: ChatViewEntityBinding | undefined,\n  kind: TKind,\n): entity is Extract<ChatViewEntityBinding, { kind: TKind }> => entity?.kind === kind;\n\n/**\n * Resolves the `source` of the first slot binding of the given kind, or `undefined` when none\n * matches.\n *\n * @example\n * ```tsx\n * const channel = useSlotEntity({ kind: 'channel' });\n * // channel is Stream Channel | undefined\n * ```\n *\n * @example\n * ```tsx\n * const thread = useSlotEntity({ kind: 'thread', slot: 'main-thread' });\n * // thread is Thread | undefined from that slot only\n * ```\n *\n * @example\n * ```tsx\n * const list = useSlotEntity({ kind: 'channelList', slot: 'list' });\n * // list is Record<string, never> | undefined\n * ```\n */\nexport const useSlotEntity = <TKind extends SlotEntityKind>({\n  kind,\n  slot,\n  view,\n}: UseSlotEntityOptions<TKind>): SlotEntitySourceByKind<TKind> | undefined => {\n  const { availableSlots, slotBindings } = useLayoutViewState(view);\n\n  const candidateSlots = resolveCandidateSlots(slot, availableSlots);\n  for (const candidateSlot of candidateSlots) {\n    const entity = getChatViewEntityBinding(slotBindings[candidateSlot]);\n    if (isEntityOfKind(entity, kind)) {\n      // Safe due to discriminant check on `kind` above.\n      return entity.source as SlotEntitySourceByKind<TKind>;\n    }\n  }\n\n  return undefined;\n};\n\n/**\n * Like {@link useSlotEntity}, but resolves the source of the topmost layer of a slot (the last\n * entry of `slotLayers[slot]`) instead of its base binding, when that layer's `kind` matches.\n *\n * A layer never replaces the base binding, which {@link useSlotEntity} keeps returning.\n *\n * @example\n * ```tsx\n * const profile = useSlotTopLayerEntity({ kind: 'userProfile', slot: 'main-thread' });\n * // profile is { userId } | undefined, present while a userProfile layer covers the slot\n * ```\n */\nexport const useSlotTopLayerEntity = <TKind extends SlotEntityKind>({\n  kind,\n  slot,\n  view,\n}: UseSlotEntityOptions<TKind>): SlotEntitySourceByKind<TKind> | undefined => {\n  const { availableSlots, slotLayers } = useLayoutViewState(view);\n\n  for (const candidateSlot of resolveCandidateSlots(slot, availableSlots)) {\n    const layers = slotLayers?.[candidateSlot];\n    const topBinding = layers?.[layers.length - 1];\n    const entity = getChatViewEntityBinding(topBinding);\n    if (isEntityOfKind(entity, kind)) {\n      // Safe due to discriminant check on `kind` above.\n      return entity.source as SlotEntitySourceByKind<TKind>;\n    }\n  }\n\n  return undefined;\n};\n\n/**\n * Resolves every slot binding of the given kind as `{ slot, source }` pairs, in `availableSlots`\n * order.\n *\n * Prefer it over `useSlotEntity` when several slots can hold the same kind, such as two channels\n * side by side.\n *\n * @example\n * ```tsx\n * const channels = useSlotEntities({ kind: 'channel' });\n * // channels is Array<{ slot: SlotName; source: Channel }>\n * ```\n */\nexport const useSlotEntities = <TKind extends SlotEntityKind>({\n  kind,\n  slot,\n  view,\n}: UseSlotEntityOptions<TKind>): Array<{\n  slot: SlotName;\n  source: SlotEntitySourceByKind<TKind>;\n}> => {\n  const { availableSlots, slotBindings } = useLayoutViewState(view);\n\n  return resolveCandidateSlots(slot, availableSlots).reduce<\n    Array<{ slot: SlotName; source: SlotEntitySourceByKind<TKind> }>\n  >((matches, candidateSlot) => {\n    const entity = getChatViewEntityBinding(slotBindings[candidateSlot]);\n    if (isEntityOfKind(entity, kind)) {\n      // Safe due to discriminant check on `kind` above.\n      matches.push({\n        slot: candidateSlot,\n        source: entity.source as SlotEntitySourceByKind<TKind>,\n      });\n    }\n    return matches;\n  }, []);\n};\n\n/**\n * The channel in `slot`. Without `slot` it is the channel of the first slot holding one, which only\n * suits a single-channel workspace; use {@link useSlotChannels} for several.\n *\n * @example\n * ```tsx\n * const channel = useSlotChannel({ slot: 'main' });\n * ```\n */\nexport const useSlotChannel = (options?: { slot?: SlotName; view?: ChatView }) =>\n  useSlotEntity({ kind: 'channel', slot: options?.slot, view: options?.view });\n\n/**\n * Every slot currently holding a channel, as `{ channel, slot }` pairs: one panel to render per\n * entry.\n *\n * @example\n * ```tsx\n * useSlotChannels().map(({ channel, slot }) => <ChannelPanel key={slot} channel={channel} />)\n * ```\n */\nexport const useSlotChannels = (options?: { slot?: SlotName; view?: ChatView }) =>\n  useSlotEntities({ kind: 'channel', slot: options?.slot, view: options?.view }).map(\n    ({ slot, source }) => ({\n      channel: source,\n      slot,\n    }),\n  );\n\n/**\n * The thread in `slot`, or in the first slot holding one when `slot` is omitted.\n *\n * @example\n * ```tsx\n * const thread = useSlotThread();\n * ```\n */\nexport const useSlotThread = (options?: { slot?: SlotName; view?: ChatView }) =>\n  useSlotEntity({ kind: 'thread', slot: options?.slot, view: options?.view });\n\n/**\n * Every slot currently holding a thread, as `{ thread, slot }` pairs: one panel to render per\n * entry.\n *\n * @example\n * ```tsx\n * useSlotThreads().map(({ thread, slot }) => (\n *   <Thread key={slot} thread={thread}><ThreadHeader /><MessageList /></Thread>\n * ))\n * ```\n */\nexport const useSlotThreads = (options?: { slot?: SlotName; view?: ChatView }) =>\n  useSlotEntities({ kind: 'thread', slot: options?.slot, view: options?.view }).map(\n    ({ slot, source }) => ({\n      slot,\n      thread: source,\n    }),\n  );\n"],"mappings":";;;AA0BA,IAAM,yBACJ,MACA,mBACG;CACH,IAAI,MAAM,OAAO,CAAC,IAAI;CACtB,OAAO;AACT;AAEA,IAAM,kBACJ,QACA,SAC8D,QAAQ,SAAS;;;;;;;;;;;;;;;;;;;;;;;AAwBjF,IAAa,iBAA+C,EAC1D,MACA,MACA,WAC4E;CAC5E,MAAM,EAAE,gBAAgB,iBAAiB,mBAAmB,IAAI;CAEhE,MAAM,iBAAiB,sBAAsB,MAAM,cAAc;CACjE,KAAK,MAAM,iBAAiB,gBAAgB;EAC1C,MAAM,SAAS,yBAAyB,aAAa,cAAc;EACnE,IAAI,eAAe,QAAQ,IAAI,GAE7B,OAAO,OAAO;CAElB;AAGF;;;;;;;;;;;;;AAcA,IAAa,yBAAuD,EAClE,MACA,MACA,WAC4E;CAC5E,MAAM,EAAE,gBAAgB,eAAe,mBAAmB,IAAI;CAE9D,KAAK,MAAM,iBAAiB,sBAAsB,MAAM,cAAc,GAAG;EACvE,MAAM,SAAS,aAAa;EAC5B,MAAM,aAAa,SAAS,OAAO,SAAS;EAC5C,MAAM,SAAS,yBAAyB,UAAU;EAClD,IAAI,eAAe,QAAQ,IAAI,GAE7B,OAAO,OAAO;CAElB;AAGF;;;;;;;;;;;;;;AAeA,IAAa,mBAAiD,EAC5D,MACA,MACA,WAII;CACJ,MAAM,EAAE,gBAAgB,iBAAiB,mBAAmB,IAAI;CAEhE,OAAO,sBAAsB,MAAM,cAAc,CAAC,CAAC,QAEhD,SAAS,kBAAkB;EAC5B,MAAM,SAAS,yBAAyB,aAAa,cAAc;EACnE,IAAI,eAAe,QAAQ,IAAI,GAE7B,QAAQ,KAAK;GACX,MAAM;GACN,QAAQ,OAAO;EACjB,CAAC;EAEH,OAAO;CACT,GAAG,CAAC,CAAC;AACP;;;;;;;;;;AAWA,IAAa,kBAAkB,YAC7B,cAAc;CAAE,MAAM;CAAW,MAAM,SAAS;CAAM,MAAM,SAAS;AAAK,CAAC;;;;;;;;;;AAW7E,IAAa,mBAAmB,YAC9B,gBAAgB;CAAE,MAAM;CAAW,MAAM,SAAS;CAAM,MAAM,SAAS;AAAK,CAAC,CAAC,CAAC,KAC5E,EAAE,MAAM,cAAc;CACrB,SAAS;CACT;AACF,EACF;;;;;;;;;AAUF,IAAa,iBAAiB,YAC5B,cAAc;CAAE,MAAM;CAAU,MAAM,SAAS;CAAM,MAAM,SAAS;AAAK,CAAC;;;;;;;;;;;;AAa5E,IAAa,kBAAkB,YAC7B,gBAAgB;CAAE,MAAM;CAAU,MAAM,SAAS;CAAM,MAAM,SAAS;AAAK,CAAC,CAAC,CAAC,KAC3E,EAAE,MAAM,cAAc;CACrB;CACA,QAAQ;AACV,EACF"}