{"version":3,"file":"DisposedChannelRelease.mjs","names":[],"sources":["../../../../src/plugins/SlotLayout/DisposedChannelRelease.tsx"],"sourcesContent":["import React, { useEffect } from 'react';\nimport type { Channel, ChannelStateData } from 'stream-chat';\n\nimport { useStateStore } from '../../store';\nimport { useChatViewContext } from './ChatView';\nimport { useSlotChannels } from './hooks';\nimport type { SlotName } from './layoutController/layoutControllerTypes';\nimport { getChatViewEntityBinding } from './slotBinding';\n\nconst pendingDisposalSelector = ({ pendingDisposal }: ChannelStateData) => ({\n  pendingDisposal,\n});\n\n/**\n * Empties `slot` once stream-chat disposes of its channel (`channel.pendingDisposal`): the channel\n * was deleted or the current user was removed from it. A hidden channel is not disposed of, so it\n * stays.\n */\nconst ReleaseWhenDisposed = ({ channel, slot }: { channel: Channel; slot: SlotName }) => {\n  const { layoutController } = useChatViewContext();\n  const { pendingDisposal } = useStateStore(channel.state, pendingDisposalSelector);\n\n  useEffect(() => {\n    if (!pendingDisposal) return;\n    const { activeView, layouts } = layoutController.state.getLatestValue();\n    const bound = getChatViewEntityBinding(layouts?.[activeView]?.slotBindings[slot]);\n    // The slot may already show something else.\n    if (bound?.source !== channel) return;\n    layoutController.release(slot);\n  }, [channel, layoutController, pendingDisposal, slot]);\n\n  return null;\n};\n\n/**\n * Empties every slot whose channel stream-chat disposed of. Opt-in: mount it once inside\n * `<ChatView>`.\n *\n * ```tsx\n * <ChatView>\n *   <DisposedChannelRelease />\n * </ChatView>\n * ```\n */\nexport const DisposedChannelRelease = () => (\n  <>\n    {useSlotChannels().map(({ channel, slot }) => (\n      <ReleaseWhenDisposed channel={channel} key={slot} slot={slot} />\n    ))}\n  </>\n);\n"],"mappings":";;;;;;;AASA,IAAM,2BAA2B,EAAE,uBAAyC,EAC1E,gBACF;;;;;;AAOA,IAAM,uBAAuB,EAAE,SAAS,WAAiD;CACvF,MAAM,EAAE,qBAAqB,mBAAmB;CAChD,MAAM,EAAE,oBAAoB,cAAc,QAAQ,OAAO,uBAAuB;CAEhF,gBAAgB;EACd,IAAI,CAAC,iBAAiB;EACtB,MAAM,EAAE,YAAY,YAAY,iBAAiB,MAAM,eAAe;EAGtE,IAFc,yBAAyB,UAAU,WAAW,EAAE,aAAa,KAEvE,CAAA,EAAO,WAAW,SAAS;EAC/B,iBAAiB,QAAQ,IAAI;CAC/B,GAAG;EAAC;EAAS;EAAkB;EAAiB;CAAI,CAAC;CAErD,OAAO;AACT;;;;;;;;;;;AAYA,IAAa,+BACX,oBAAA,YAAA,EAAA,UACG,gBAAgB,CAAC,CAAC,KAAK,EAAE,SAAS,WACjC,oBAAC,qBAAD;CAA8B;CAA0B;AAAO,GAAnB,IAAmB,CAChE,EACD,CAAA"}