{"version":3,"file":"Channel.mjs","names":[],"sources":["../../../../src/components/Channel/Channel.tsx"],"sourcesContent":["import type { ComponentProps, PropsWithChildren } from 'react';\nimport React, { useEffect, useMemo } from 'react';\nimport clsx from 'clsx';\nimport type { Channel as StreamChannel } from 'stream-chat';\n\nimport { ChannelInstanceProvider, useChatContext } from '../../context';\nimport { ThemeScopeProvider } from '../../context/ThemeScopeContext';\n\nimport { CHANNEL_CONTAINER_ID } from './constants';\nimport { DEFAULT_NEXT_CHANNEL_PAGE_SIZE } from '../../constants/limits';\n\nimport {\n  useChannelContainerClasses,\n  useImageFlagEmojisOnWindowsClass,\n} from './hooks/useChannelContainerClasses';\nimport { WithAudioPlayback } from '../AudioPlayback';\n\nexport type ChannelProps = {\n  /**\n   * Channel to bind to this subtree, initialized beforehand: `Channel` does not query. Render\n   * `<ChannelPlaceholder />` while none is selected.\n   */\n  channel: StreamChannel;\n};\n\n/**\n * The channel content column (`.str-chat__channel`) with no channel bound, to fill the layout slot\n * while none is selected. `Channel` renders it as its container.\n */\nexport const ChannelPlaceholder = ({\n  children,\n  className: additionalClassName,\n  ...props\n}: PropsWithChildren<ComponentProps<'div'>>) => {\n  const { customClasses, theme } = useChatContext();\n  const { channelClass, chatClass } = useChannelContainerClasses({\n    customClasses,\n  });\n  const className = clsx(chatClass, theme, channelClass, additionalClassName);\n  return (\n    <div id={CHANNEL_CONTAINER_ID} {...props} className={className}>\n      {/* The root carries the `str-chat <theme>` scope, so components inside add none of their own. */}\n      <ThemeScopeProvider>{children}</ThemeScopeProvider>\n    </div>\n  );\n};\n\n/** Binds `channel` to its subtree and keeps it active while mounted, without querying it. */\nexport const Channel = (props: PropsWithChildren<ChannelProps>) => {\n  const { channel, children } = props;\n\n  const { client } = useChatContext();\n  const windowsEmojiClass = useImageFlagEmojisOnWindowsClass();\n\n  // An active channel stays in the client's channel store, and channel-list hydration does not\n  // re-seed its message list (`channel.reload()` owns that window). Claims are refcounted; the\n  // returned function ends only this mount's claim.\n  useEffect(() => channel.activate(), [channel]);\n\n  // Depends on the instance, not the `cid`: children subscribe to this channel's stores.\n  const channelInstanceContextValue = useMemo(() => ({ channel }), [channel]);\n\n  useEffect(() => {\n    // Re-seeds the unread boundary behind the separator and \"N new\" banner: a cached channel is not\n    // re-queried on reopen, so the LLC's auto-seed does not run. Skipped when the channel is marked\n    // unread, because seeding clears that mark. Marking read is left to `useMarkRead`.\n    if (\n      !channel.messagePaginator.unreadStateSnapshot.getLatestValue().firstUnreadMessageId\n    ) {\n      channel.messagePaginator.seedUnreadSnapshot();\n    }\n\n    // Each subscription is released by its own handle, so subscribing and unsubscribing stay\n    // paired.\n    const subscriptions = [\n      // Channel state is not normalized, so references to the deleted user are refreshed by a\n      // re-query. It updates members, read state and watchers, but not the loaded messages: the\n      // requested page is older than the loaded window.\n      // todo: remove with REACT-1175 (single source of truth for user references)\n      client.on('user.deleted', async () => {\n        const oldestID = channel.messagePaginator.items?.[0]?.id;\n\n        await channel.query({\n          messages: { id_lt: oldestID, limit: DEFAULT_NEXT_CHANNEL_PAGE_SIZE },\n          watchers: { limit: DEFAULT_NEXT_CHANNEL_PAGE_SIZE },\n        });\n      }),\n    ];\n\n    return () => {\n      subscriptions.forEach((subscription) => subscription.unsubscribe());\n    };\n  }, [channel, client]);\n\n  return (\n    <ChannelPlaceholder className={windowsEmojiClass}>\n      <ChannelInstanceProvider value={channelInstanceContextValue}>\n        <WithAudioPlayback playbackScope={channel}>{children}</WithAudioPlayback>\n      </ChannelInstanceProvider>\n    </ChannelPlaceholder>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;AA6BA,IAAa,sBAAsB,EACjC,UACA,WAAW,qBACX,GAAG,YAC2C;CAC9C,MAAM,EAAE,eAAe,UAAU,eAAe;CAChD,MAAM,EAAE,cAAc,cAAc,2BAA2B,EAC7D,cACF,CAAC;CACD,MAAM,YAAY,KAAK,WAAW,OAAO,cAAc,mBAAmB;CAC1E,OACE,oBAAC,OAAD;EAAK,IAAI;EAAsB,GAAI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