{"version":3,"file":"SupersededChannelSwap.mjs","names":[],"sources":["../../../../src/plugins/SlotLayout/SupersededChannelSwap.tsx"],"sourcesContent":["import { useEffect } from 'react';\nimport type {\n  ChannelStateData,\n  EditingAuditState,\n  Channel as StreamChannel,\n} from 'stream-chat';\n\nimport { useStateStore } from '../../store';\nimport { createChatViewSlotBinding, getChatViewEntityBinding } from './slotBinding';\nimport type {\n  LayoutController,\n  SlotName,\n} from './layoutController/layoutControllerTypes';\n\nconst supersededBySelector = ({ supersededBy }: ChannelStateData) => ({ supersededBy });\n// A channel created from members adopts its real cid in the query that also sets `initialized`.\nconst initializedSelector = ({ initialized }: ChannelStateData) => ({ initialized });\nconst activeSelector = ({ active }: ChannelStateData) => ({ active });\nconst editingAuditStateSelector = (state: EditingAuditState) => state;\n\n// Whether the slot still shows this channel. Read when an effect runs, not at render: the slot may\n// have been bound to something else in between, and that newer binding must not be replaced.\nconst slotShowsChannel = (\n  layoutController: LayoutController,\n  slot: SlotName,\n  channel: StreamChannel,\n) => {\n  const { activeView, layouts } = layoutController.state.getLatestValue();\n  const bound = getChatViewEntityBinding(layouts?.[activeView]?.slotBindings[slot]);\n  return bound?.kind === 'channel' && bound.source === channel;\n};\n\n/**\n * Keeps a slot's binding on its channel's current instance and cid.\n *\n * A channel created from members is bound under a temporary cid until its first query gives it the\n * real one; the slot is then rebound to the same instance under the real cid, so lookups by cid\n * find it. A channel superseded by another instance (`channel.supersededBy`) is swapped for that\n * instance, except while the successor is open in another slot and this channel's composer is not\n * empty: stream-chat moves a composer only into a channel nobody is using.\n */\nexport const SupersededChannelSwap = ({\n  bindingKey,\n  channel,\n  layoutController,\n  slot,\n}: {\n  bindingKey?: string;\n  channel: StreamChannel;\n  layoutController: LayoutController;\n  slot: SlotName;\n}) => {\n  // A slot source without channel state (a stand-in object) never swaps.\n  const { supersededBy } = useStateStore(channel.state, supersededBySelector) ?? {};\n  const successorActive =\n    useStateStore(supersededBy?.state, activeSelector)?.active ?? false;\n  // Re-renders on every composer change, so the emptiness read below stays current.\n  useStateStore(channel.messageComposer?.editingAuditState, editingAuditStateSelector);\n  const composerIsEmpty = channel.messageComposer?.compositionIsEmpty ?? true;\n  // Re-renders when the query that gives a channel created from members its real cid completes.\n  useStateStore(channel.state, initializedSelector);\n  const cid = channel.cid;\n\n  useEffect(() => {\n    if (supersededBy || !cid || bindingKey === undefined || bindingKey === cid) return;\n    if (!slotShowsChannel(layoutController, slot, channel)) return;\n    layoutController.bind(\n      slot,\n      createChatViewSlotBinding({ key: cid, kind: 'channel', source: channel }),\n    );\n  }, [bindingKey, channel, cid, layoutController, slot, supersededBy]);\n\n  useEffect(() => {\n    if (!supersededBy) return;\n    if (successorActive && !composerIsEmpty) return;\n    if (!slotShowsChannel(layoutController, slot, channel)) return;\n    layoutController.bind(\n      slot,\n      createChatViewSlotBinding({\n        key: supersededBy.cid,\n        kind: 'channel',\n        source: supersededBy,\n      }),\n    );\n  }, [channel, composerIsEmpty, layoutController, slot, successorActive, supersededBy]);\n\n  return null;\n};\n"],"mappings":";;;;AAcA,IAAM,wBAAwB,EAAE,oBAAsC,EAAE,aAAa;AAErF,IAAM,uBAAuB,EAAE,mBAAqC,EAAE,YAAY;AAClF,IAAM,kBAAkB,EAAE,cAAgC,EAAE,OAAO;AACnE,IAAM,6BAA6B,UAA6B;AAIhE,IAAM,oBACJ,kBACA,MACA,YACG;CACH,MAAM,EAAE,YAAY,YAAY,iBAAiB,MAAM,eAAe;CACtE,MAAM,QAAQ,yBAAyB,UAAU,WAAW,EAAE,aAAa,KAAK;CAChF,OAAO,OAAO,SAAS,aAAa,MAAM,WAAW;AACvD;;;;;;;;;;AAWA,IAAa,yBAAyB,EACpC,YACA,SACA,kBACA,WAMI;CAEJ,MAAM,EAAE,iBAAiB,cAAc,QAAQ,OAAO,oBAAoB,KAAK,CAAC;CAChF,MAAM,kBACJ,cAAc,cAAc,OAAO,cAAc,CAAC,EAAE,UAAU;CAEhE,cAAc,QAAQ,iBAAiB,mBAAmB,yB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