{"version":3,"file":"ChannelDetailContext.mjs","names":[],"sources":["../../../../src/plugins/ChannelDetail/ChannelDetailContext.tsx"],"sourcesContent":["import type { PropsWithChildren } from 'react';\nimport React, { useContext, useMemo } from 'react';\nimport type { Channel } from 'stream-chat';\n\nimport { ModalContext, type ModalContextValue } from '../../context/ModalContext';\nimport { useStableCallback } from '../../utils';\n\nexport type ChannelDetailContextValue = {\n  channel: Channel;\n  /**\n   * Closes the channel detail: the views' close buttons and the actions that close the channel\n   * detail when they finish call it. It is the `close` of the nearest modal context, which\n   * `ChannelDetailProvider` replaces with its `onClose` when it is given. `undefined` when there is\n   * neither, for example in a side panel without `onClose`, so `Prompt.Header` renders no close\n   * button.\n   */\n  close?: () => void;\n};\n\ntype ChannelDetailProvidedValue = Pick<ChannelDetailContextValue, 'channel'>;\n\nconst ChannelDetailContext = React.createContext<ChannelDetailProvidedValue | undefined>(\n  undefined,\n);\n\n/**\n * The modal context for the provider's subtree. With `onClose`, it is the surrounding modal's\n * context (if any) with `close` replaced by `onClose`; without it, the surrounding one unchanged.\n * The views read `close` from the nearest modal context, so `onClose` wins over a modal around the\n * provider, and a modal opened inside it wins over `onClose`.\n */\nconst useCreateChannelDetailModalContext = (\n  onClose: (() => void) | undefined,\n): ModalContextValue | undefined => {\n  const surroundingModalContext = useContext(ModalContext);\n  // Stable while `onClose` is given, so an inline `onClose` does not re-render every consumer.\n  const stableOnClose = useStableCallback(() => onClose?.());\n  const hasOnClose = !!onClose;\n\n  return useMemo(\n    () =>\n      hasOnClose\n        ? { ...surroundingModalContext, close: stableOnClose }\n        : surroundingModalContext,\n    [hasOnClose, stableOnClose, surroundingModalContext],\n  );\n};\n\nexport type ChannelDetailProviderProps = PropsWithChildren<{\n  channel: Channel;\n  /**\n   * Closes the channel detail: the views' close buttons and the actions that close the channel\n   * detail when they finish call it. Inside a modal it is called instead of the modal's `close`, so\n   * close the modal from it. A modal opened inside the channel detail still closes itself. Without\n   * it and outside a modal, the views render no close button.\n   */\n  onClose?: () => void;\n}>;\n\nexport const ChannelDetailProvider = ({\n  channel,\n  children,\n  onClose,\n}: ChannelDetailProviderProps) => {\n  const value = useMemo(() => ({ channel }), [channel]);\n  const modalContext = useCreateChannelDetailModalContext(onClose);\n\n  return (\n    <ModalContext.Provider value={modalContext}>\n      <ChannelDetailContext.Provider value={value}>\n        {children}\n      </ChannelDetailContext.Provider>\n    </ModalContext.Provider>\n  );\n};\n\nexport const useChannelDetailContext = (): ChannelDetailContextValue => {\n  const providedValue = useContext(ChannelDetailContext);\n  // Read where the hook is called, not in the provider, so a modal opened inside the channel\n  // detail closes itself. `useModalContext` would return a no-op `close` outside a modal.\n  const close = useContext(ModalContext)?.close;\n  const contextValue = useMemo(\n    () => providedValue && { ...providedValue, close },\n    [close, providedValue],\n  );\n\n  if (!contextValue) {\n    throw new Error(\n      'The useChannelDetailContext hook was called outside of ChannelDetailProvider.',\n    );\n  }\n\n  return contextValue;\n};\n"],"mappings":";;;;;AAqBA,IAAM,uBAAuB,MAAM,cACjC,KAAA,CACF;;;;;;;AAQA,IAAM,sCACJ,YACkC;CAClC,MAAM,0BAA0B,WAAW,YAAY;CAEvD,MAAM,gBAAgB,wBAAwB,UAAU,CAAC;CACzD,MAAM,aAAa,CAAC,CAAC;CAErB,OAAO,cAEH,aACI;EAAE,GAAG;EAAyB,OAAO;CAAc,IACnD,yBACN;EAAC;EAAY;EAAe;CAAuB,CACrD;AACF;AAaA,IAAa,yBAAyB,EACpC,SACA,UACA,cACgC;CAChC,MAAM,QAAQ,eAAe,EAAE,QAAQ,IAAI,CAAC,OAAO,CAAC;CACpD,MAAM,eAAe,mCAAmC,OAAO;CAE/D,OACE,oBAAC,aAAa,UAAd;EAAuB,OAAO;YAC5B,oBAAC,qBAAqB,UAAtB;GAAsC;GACnC;EAC4B,CAAA;CACV,CAAA;AAE3B;AAEA,IAAa,gCAA2D;CACtE,MAAM,gBAAgB,WAAW,oBAAoB;CAGrD,MAAM,QAAQ,WAAW,YAAY,CAAC,EAAE;CACxC,MAAM,eAAe,cACb,iBAAiB;EAAE,GAAG;EAAe;CAAM,GACjD,CAAC,OAAO,aAAa,CACvB;CAEA,IAAI,CAAC,cACH,MAAM,IAAI,MACR,+EACF;CAGF,OAAO;AACT"}