{"version":3,"file":"ChannelListItem.mjs","names":[],"sources":["../../../../src/components/ChannelListItem/ChannelListItem.tsx"],"sourcesContent":["import throttle from 'lodash.throttle';\nimport React, { useContext, useEffect, useMemo, useState } from 'react';\nimport type { Channel, LocalMessage, MessagePaginatorAggregateState } from 'stream-chat';\n\nimport { useStateStore } from '../../store';\n\nimport { ChannelListItemUI as DefaultChannelListItemUI } from './ChannelListItemUI';\nimport { useIsChannelMuted } from './hooks/useIsChannelMuted';\nimport { useChannelPreviewInfo } from './hooks/useChannelPreviewInfo';\nimport type { MessageDeliveryStatus } from './hooks/useMessageDeliveryStatus';\nimport { useMessageDeliveryStatus } from './hooks/useMessageDeliveryStatus';\nimport type { GroupChannelDisplayInfo } from './utils';\nimport type { ChannelListItemLabelConfig } from './utils.a11y';\nimport {\n  useChatContext,\n  useComponentContext,\n  useWorkspaceNavigation,\n} from '../../context';\nimport { useChannelMembershipState } from '../ChannelList';\nimport { requireContext } from '../../context/requireContext';\n\nexport type ChannelListItemUIProps = ChannelListItemProps & {\n  /**\n   * Configures the row's accessible name: part overrides, order, separator, or a full `build`.\n   * Supply it from a `ComponentContext`-provided `ChannelListItemUI`. See\n   * `composeChannelListItemAccessibleLabel`.\n   */\n  accessibleLabelConfig?: ChannelListItemLabelConfig;\n  displayImage?: string;\n  displayTitle?: string;\n  /** Members for the group avatar; empty unless the channel has more than 2 members. */\n  groupChannelDisplayInfo?: GroupChannelDisplayInfo;\n  /** See {@link ChannelListItemProps.previewedMessage}. */\n  previewedMessage?: LocalMessage;\n  /** Delivery status of the previewed message. Undefined when it is not an own message. */\n  messageDeliveryStatus?: MessageDeliveryStatus;\n  muted?: boolean;\n  pinned?: boolean;\n  /** Unread message count, 0 while the channel is muted. */\n  unread?: number;\n};\n\nexport type ChannelListItemProps = {\n  /** Channel to preview: a `ChannelList` item, or any channel, e.g. in search results. */\n  channel: Channel;\n  /** Whether the channel is the active one. Defaults to whether it is shown in the workspace. */\n  active?: boolean;\n  /** Custom class for the row button. */\n  className?: string;\n  /**\n   * Message previewed by the item. Defaults to the channel's latest message\n   * (`channel.messagePaginator.aggregateState.latestMessage`); a search result passes the matched\n   * message instead.\n   */\n  previewedMessage?: LocalMessage;\n  /** Replaces the default click handler, which opens the channel in the workspace. */\n  onSelect?: (event: React.MouseEvent) => void;\n};\n\nconst ChannelListItemContext = React.createContext<{ channel: Channel } | undefined>(\n  undefined,\n);\n\nexport const useChannelListItemContext = () =>\n  requireContext(\n    useContext(ChannelListItemContext),\n    'useChannelListItemContext',\n    'ChannelListItemUI',\n  );\n\nconst latestMessageSelector = ({ latestMessage }: MessagePaginatorAggregateState) => ({\n  latestMessage: latestMessage ?? undefined,\n});\n\nexport const ChannelListItem = (props: ChannelListItemProps) => {\n  const { active, channel } = props;\n  const { ChannelListItemUI = DefaultChannelListItemUI } = useComponentContext();\n  const { client } = useChatContext();\n  // Active means this channel is shown in the workspace. Keyed on its own `cid`, so every open\n  // channel highlights independently.\n  const channelOpenInSlot = useWorkspaceNavigation().isChannelActive(\n    channel.cid ?? undefined,\n  );\n  const { displayImage, displayTitle, groupChannelDisplayInfo } = useChannelPreviewInfo({\n    channel,\n  });\n  const membership = useChannelMembershipState(channel);\n\n  const { latestMessage: trackedLatestMessage } = useStateStore(\n    channel.messagePaginator.aggregateState,\n    latestMessageSelector,\n  );\n  const previewedMessage = props.previewedMessage ?? trackedLatestMessage;\n\n  const { messageDeliveryStatus } = useMessageDeliveryStatus({\n    channel,\n    lastMessage: previewedMessage,\n  });\n\n  const isActive = typeof active === 'undefined' ? !!channelOpenInSlot : active;\n  const { muted } = useIsChannelMuted(channel);\n\n  // The channel owns this number: `countUnread()` with no argument returns\n  // `read[ownUserId].unread_messages`, which every event that moves it writes first. Reading the\n  // store means the guards on those writes (a thread read is not a channel read, a read by\n  // somebody else is not ours) hold here too.\n  const [unreadCount, setUnreadCount] = useState(() => channel.countUnread());\n\n  useEffect(() => {\n    const ownUserId = client.user?.id;\n    // Throttled because a backfill or a burst of traffic can move the count many times in a frame.\n    // Leading edge, so the first change still lands at once.\n    const apply = throttle(setUnreadCount, 400);\n\n    const unsubscribe = channel.state.subscribeWithSelector(\n      ({ read }) => ({\n        unreadCount: ownUserId ? (read[ownUserId]?.unread_messages ?? 0) : 0,\n      }),\n      ({ unreadCount }) => apply(unreadCount),\n    );\n\n    return () => {\n      apply.cancel();\n      unsubscribe();\n    };\n  }, [channel, client]);\n\n  // A muted channel still counts unread but does not advertise it. Applied at render, not in the\n  // subscription, so muting and unmuting show up without waiting on the throttle.\n  const unread = muted ? 0 : unreadCount;\n\n  const channelPreviewContextValue = useMemo(() => ({ channel }), [channel]);\n\n  if (!ChannelListItemUI) return null;\n\n  return (\n    <ChannelListItemContext.Provider value={channelPreviewContextValue}>\n      <ChannelListItemUI\n        {...props}\n        active={isActive}\n        displayImage={displayImage}\n        displayTitle={displayTitle}\n        groupChannelDisplayInfo={groupChannelDisplayInfo}\n        messageDeliveryStatus={messageDeliveryStatus}\n        muted={muted}\n        pinned={membership.pinned_at != null}\n        previewedMessage={previewedMessage}\n        unread={unread}\n      />\n    </ChannelListItemContext.Provider>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;AA2DA,IAAM,yBAAyB,MAAM,cACnC,KAAA,CACF;AAEA,IAAa,kCACX,eACE,WAAW,sBAAsB,GACjC,6BACA,mBACF;AAEF,IAAM,yBAAyB,EAAE,qBAAqD,EACpF,eAAe,iBAAiB,KAAA,EAClC;AAEA,IAAa,mBAAmB,UAAgC;CAC9D,MAAM,EAAE,QAAQ,YAAY;CAC5B,MAAM,EAAE,mBAAA,sBAAoB,sBAA6B,oBAAoB;CAC7E,MAAM,EAAE,WAAW,eAAe;CAGlC,MAAM,oBAAoB,uBAAuB,CAAC,CAAC,gBACjD,QAAQ,OAAO,KAAA,CACjB;CACA,MAAM,EAAE,cAAc,cAAc,4BAA4B,sBAAsB,EACpF,QACF,CAAC;CACD,MAAM,aAAa,0BAA0B,OAAO;CAEpD,MAAM,EAAE,eAAe,yBA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