{"version":3,"file":"Thread.mjs","names":[],"sources":["../../../../src/components/Thread/Thread.tsx"],"sourcesContent":["import React, { useEffect, useRef } from 'react';\nimport type { PropsWithChildren } from 'react';\n\nimport { WithAudioPlayback } from '../AudioPlayback';\n\nimport { useChatContext } from '../../context';\nimport { ThreadProvider } from '../Threads';\nimport { useStateStore } from '../../store';\n\nimport type { Thread as StreamThread, ThreadState } from 'stream-chat';\nimport type { ChannelConfig } from 'stream-chat';\n\nconst repliesStateSelector = ({ replies }: ChannelConfig) => ({\n  repliesEnabled: replies.enabled,\n});\n\nexport type ThreadProps = PropsWithChildren<{\n  /**\n   * The thread to render. Get it from `client.threads.ensure()` or `client.threads.get()` so it is\n   * registered with the `ThreadManager` and receives events. `Thread` loads it while its state is\n   * stale, which is how a thread built from its parent message gets its replies.\n   */\n  thread: StreamThread;\n}>;\n\nconst selector = ({\n  isLoading,\n  isStateStale,\n  parentMessage,\n  replyCount,\n}: ThreadState) => ({\n  // A boolean, so incoming replies do not re-render the panel.\n  hasReplies: replyCount > 0,\n  isLoading,\n  isStateStale,\n  parentMessage,\n});\n\n/**\n * Container for a thread panel: provides the thread to its subtree, loads it while its state is\n * stale, scopes audio playback to it, and renders its children. It renders no other UI and does\n * not register the thread with the `ThreadManager`; `client.threads.ensure()` or\n * `thread.activate()` does.\n *\n * ```tsx\n * <Thread thread={thread}>\n *   <ThreadHeader />\n *   <MessageList withDateSeparator={false} />\n *   <MessageComposer focus />\n * </Thread>\n * ```\n *\n * The message list renders the parent message above its replies (see `useThreadHead`).\n */\nexport const Thread = ({ children, thread }: ThreadProps) => {\n  const { customClasses } = useChatContext();\n  const { repliesEnabled } = useStateStore(\n    thread.channel.configState,\n    repliesStateSelector,\n  );\n  const { hasReplies, isLoading, isStateStale, parentMessage } = useStateStore(\n    thread.state,\n    selector,\n  );\n  // The thread and reply state that the current staleness episode last loaded for.\n  const loadedFor = useRef<{ hasReplies: boolean; thread: StreamThread } | undefined>(\n    undefined,\n  );\n\n  // The only load trigger: a stale thread loads once per staleness episode and reply state. A\n  // thread without replies on the server answers not-found, which `reload()` takes as an empty\n  // thread and leaves stale, so it loads again once its parent reports a reply. A load in flight is\n  // waited out, so a reply reported during a load loads after it settles.\n  useEffect(() => {\n    if (!isStateStale) {\n      loadedFor.current = undefined;\n      return;\n    }\n    if (isLoading) return;\n    const last = loadedFor.current;\n    if (last?.thread === thread && last.hasReplies === hasReplies) return;\n    loadedFor.current = { hasReplies, thread };\n    void thread.reload();\n  }, [hasReplies, isLoading, isStateStale, thread]);\n\n  if (!parentMessage || repliesEnabled === false) return null;\n\n  // The thread owns its audio-player pool: a slot-bound Thread is a sibling of the channel, not\n  // nested in it, and scoping here stops thread audio when the thread closes.\n  return (\n    <div\n      className={customClasses?.thread || 'str-chat__thread-container str-chat__thread'}\n    >\n      <ThreadProvider thread={thread}>\n        <WithAudioPlayback playbackScope={thread}>{children}</WithAudioPlayback>\n      </ThreadProvider>\n    </div>\n  );\n};\n"],"mappings":";;;;;;;AAYA,IAAM,wBAAwB,EAAE,eAA8B,EAC5D,gBAAgB,QAAQ,QAC1B;AAWA,IAAM,YAAY,EAChB,WACA,cACA,eACA,kBACkB;CAElB,YAAY,aAAa;CACzB;CACA;CACA;AACF;;;;;;;;;;;;;;;;;AAkBA,IAAa,UAAU,EAAE,UAAU,aAA0B;CAC3D,MAAM,EAAE,kBAAkB,eAAe;CACzC,MAAM,EAAE,mBAAmB,cACzB,OAAO,QAAQ,aACf,oBACF;CACA,MAAM,EAAE,YAAY,WAAW,cAAc,kBAAkB,cAC7D,OAAO,OACP,QACF;CAEA,MAAM,YAAY,OAChB,KAAA,CACF;CAMA,gBAAgB;EACd,IAAI,CAAC,cAAc;GACjB,UAAU,UAAU,KAAA;GACpB;EACF;EACA,IAAI,WAAW;EACf,MAAM,OAAO,UAAU;EACvB,IAAI,MAAM,WAAW,UAAU,KAAK,eAAe,YAAY;EAC/D,UAAU,UAAU;GAAE;GAAY;EAAO;EACzC,OAAY,OAAO;CACrB,GAAG;EAAC;EAAY;EAAW;EAAc;CAAM,CAAC;CAEhD,IAAI,CAAC,iBAAiB,mBAAmB,OAAO,OAAO;CAIvD,OACE,oBAAC,OAAD;EACE,WAAW,eAAe,UAAU;YAEpC,oBAAC,gBAAD;GAAwB;aACtB,oBAAC,mBAAD;IAAmB,eAAe;IAAS;GAA4B,CAAA;EACzD,CAAA;CACb,CAAA;AAET"}