{"version":3,"file":"ScrollToLatestMessageButton.mjs","names":[],"sources":["../../../../src/components/MessageList/ScrollToLatestMessageButton.tsx"],"sourcesContent":["import React, { useCallback, useEffect, useState } from 'react';\n\nimport {\n  useChannel,\n  useChatContext,\n  useComponentContextIcons,\n  useTranslationContext,\n} from '../../context';\nimport { useThreadContext } from '../Threads/ThreadContext';\nimport { useStateStore } from '../../store';\n\nimport type { ChannelStateData, EventPayload, Thread, ThreadState } from 'stream-chat';\nimport { Badge } from '../Badge';\nimport { Button } from '../Button';\n\nexport type ScrollToLatestMessageButtonProps = {\n  /** Whether newer messages than the loaded set exist. The button then shows even at the bottom. */\n  isNotAtLatestMessageSet?: boolean;\n  isMessageListScrolledToBottom?: boolean;\n  onClick: React.MouseEventHandler;\n};\n\nconst threadStateSelector = ({ parentMessage }: ThreadState) => ({\n  parentMessage,\n});\n\n/**\n * The current user's unread count in the channel, as the channel's read state holds it. The read\n * state is the one place every source of unread messages lands - live arrivals, the count a\n * (re)query returns, a mark-unread, a read from any device - so the number covers messages that\n * never arrived as an event, and drops to 0 once the channel is marked read.\n * Always 0 in a thread, which counts new replies instead.\n */\nconst useOwnChannelUnreadCount = ({ thread }: { thread: Thread | undefined }) => {\n  const { client } = useChatContext();\n  const channel = useChannel();\n  const ownUserId = client.user?.id;\n  // Selects the own count alone: other members' read and delivery updates replace the `read` map\n  // too, and must not re-render the button.\n  const ownUnreadCountSelector = useCallback(\n    ({ read }: ChannelStateData) => ({\n      unreadCount: ownUserId ? (read[ownUserId]?.unread_messages ?? 0) : 0,\n    }),\n    [ownUserId],\n  );\n  const { unreadCount = 0 } =\n    useStateStore(thread ? undefined : channel.state, ownUnreadCountSelector) ?? {};\n\n  return unreadCount;\n};\n\n/**\n * The number of replies other users posted to the thread while the list was scrolled up: the\n * growth of the parent message's `reply_count` since the user was last at the bottom.\n * Always 0 outside a thread.\n */\nconst useThreadNewReplyCount = ({\n  isMessageListScrolledToBottom,\n  thread,\n}: {\n  isMessageListScrolledToBottom?: boolean;\n  thread: Thread | undefined;\n}) => {\n  const { client } = useChatContext();\n  const channel = useChannel();\n  const { parentMessage } = useStateStore(thread?.state, threadStateSelector) ?? {};\n  const [newReplyCount, setNewReplyCount] = useState(0);\n  // The parent's reply count when the user was last at the bottom, i.e. the replies already seen.\n  const [seenReplyCount, setSeenReplyCount] = useState(parentMessage?.reply_count || 0);\n\n  useEffect(() => {\n    if (!isMessageListScrolledToBottom) return;\n    setNewReplyCount(0);\n    setSeenReplyCount(parentMessage?.reply_count || 0);\n  }, [isMessageListScrolledToBottom, parentMessage]);\n\n  useEffect(() => {\n    if (!thread || isMessageListScrolledToBottom) return;\n\n    const handleParentMessageUpdated = (event: EventPayload<'message.updated'>) => {\n      const isParentMessage = event.message?.id === parentMessage?.id;\n      const isOwnUpdate = event.user?.id === client.user?.id;\n      if (!isParentMessage || isOwnUpdate) return;\n      setNewReplyCount((event.message?.reply_count || 0) - seenReplyCount);\n    };\n\n    const subscription = channel.on('message.updated', handleParentMessageUpdated);\n    return () => subscription.unsubscribe();\n  }, [\n    channel,\n    client,\n    isMessageListScrolledToBottom,\n    parentMessage,\n    seenReplyCount,\n    thread,\n  ]);\n\n  return newReplyCount;\n};\n\nconst UnMemoizedScrollToLatestMessageButton = (\n  props: ScrollToLatestMessageButtonProps,\n) => {\n  const { IconArrowDown } = useComponentContextIcons();\n  const {\n    isMessageListScrolledToBottom,\n    isNotAtLatestMessageSet = false,\n    onClick,\n  } = props;\n\n  const { t } = useTranslationContext();\n  const thread = useThreadContext();\n  const ownChannelUnreadCount = useOwnChannelUnreadCount({ thread });\n  const threadNewReplyCount = useThreadNewReplyCount({\n    isMessageListScrolledToBottom,\n    thread,\n  });\n  const unreadCount = thread ? threadNewReplyCount : ownChannelUnreadCount;\n\n  if (isMessageListScrolledToBottom && !isNotAtLatestMessageSet) return null;\n\n  return (\n    <div className='str-chat__jump-to-latest-message'>\n      <Button\n        appearance='ghost'\n        aria-label={t(\n          'messageList.scrollLatestMessage.jumpLatestMessage.ariaLabel',\n          'Jump to latest message',\n        )}\n        aria-live='polite'\n        circular\n        className='str-chat__jump-to-latest-message__button'\n        data-testid='scroll-to-latest-message-button'\n        onClick={onClick}\n        size='md'\n        variant='secondary'\n      >\n        <IconArrowDown />\n      </Button>\n      {unreadCount > 0 && (\n        <Badge\n          className='str-chat__jump-to-latest__unread-count'\n          data-testid='unread-message-notification-counter'\n          size='md'\n          variant='primary'\n        >\n          {unreadCount}\n        </Badge>\n      )}\n    </div>\n  );\n};\n\nexport const ScrollToLatestMessageButton = React.memo(\n  UnMemoizedScrollToLatestMessageButton,\n) as typeof UnMemoizedScrollToLatestMessageButton;\n"],"mappings":";;;;;;;;;;;AAsBA,IAAM,uBAAuB,EAAE,qBAAkC,EAC/D,cACF;;;;;;;;AASA,IAAM,4BAA4B,EAAE,aAA6C;CAC/E,MAAM,EAAE,WAAW,eAAe;CAClC,MAAM,UAAU,WAAW;CAC3B,MAAM,YAAY,OAAO,MAAM;CAG/B,MAAM,yBAAyB,aAC5B,EAAE,YAA8B,EAC/B,aAAa,YAAa,KAAK,UAAU,EAAE,mBAAmB,IAAK,EACrE,IACA,CAAC,SAAS,CACZ;CACA,MAAM,EAAE,cAAc,MACpB,cAAc,SAAS,KAAA,IAAY,QAAQ,OAAO,sBAAsB,KAAK,CAAC;CAEhF,OAAO;AACT;;;;;;AAOA,IAAM,0BAA0B,EAC9B,+BACA,aAII;CACJ,MAAM,EAAE,WAAW,eAAe;CAClC,MAAM,UAAU,WAAW;CAC3B,MAAM,EAAE,kBAAkB,cAAc,QAAQ,OAAO,mBAAmB,KAAK,CAAC;CAChF,MAAM,CAAC,eAAe,oBAAoB,SAAS,CAAC;CAEpD,MAAM,CAAC,gBAAgB,qBAAqB,SAAS,eAAe,eAAe,CAAC;CAEpF,gBAAgB;EACd,IAAI,CAAC,+BAA+B;EACpC,iBAAiB,CAAC;EAClB,kBAAkB,eAAe,eAAe,CAAC;CACnD,GAAG,CAAC,+BAA+B,aAAa,CAAC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