{"version":3,"file":"UnreadMessagesNotification.mjs","names":[],"sources":["../../../../src/components/MessageList/UnreadMessagesNotification.tsx"],"sourcesContent":["import React from 'react';\nimport {\n  useChannel,\n  useChatContext,\n  useComponentContextIcons,\n  useTranslationContext,\n} from '../../context';\nimport { useMessagePaginator } from '../../hooks';\nimport { Button } from '../Button';\nimport clsx from 'clsx';\nimport { useThreadContext } from '../Threads';\nimport type { MessagePaginatorState, UnreadSnapshotState } from 'stream-chat';\nimport { useStateStore } from '../../store';\n\nexport type UnreadMessagesNotificationProps = {\n  /** Page size used when jumping to the first unread message. */\n  queryMessageLimit?: number;\n  /** Whether to show the unread count. Defaults to `true`. */\n  showCount?: boolean;\n  /**\n   * Whether the first unread message is below the viewport, which turns the arrow down. When\n   * omitted, it is derived from the loaded window: below when every loaded message is older than the\n   * last read one.\n   */\n  unreadIsBelow?: boolean;\n  // todo: maybe remove?\n  unreadCount?: number;\n};\n\nconst unreadStateSnapshotSelector = (state: UnreadSnapshotState) => ({\n  lastReadAt: state.lastReadAt,\n  unreadCount: state.unreadCount,\n});\n\nconst newestLoadedMessageSelector = (state: MessagePaginatorState) => ({\n  newestLoadedAt: state.items?.[state.items.length - 1]?.created_at,\n});\n\nexport const UnreadMessagesNotification = ({\n  queryMessageLimit,\n  showCount = true,\n  unreadIsBelow: unreadIsBelowProp,\n}: UnreadMessagesNotificationProps) => {\n  const { IconArrowDown, IconArrowUp, IconXmark } = useComponentContextIcons();\n  // todo: move into a hook dedicated to unread count from the snapshot\n  const channel = useChannel();\n  const { client } = useChatContext();\n  const thread = useThreadContext();\n  const messagePaginator = useMessagePaginator();\n  const { lastReadAt, unreadCount } = useStateStore(\n    messagePaginator.unreadStateSnapshot,\n    unreadStateSnapshotSelector,\n  );\n  const { newestLoadedAt } = useStateStore(\n    messagePaginator.state,\n    newestLoadedMessageSelector,\n  );\n  // The first unread message is usually above what is on screen. It is below when the loaded window\n  // is older than the last read message, e.g. after jumping back in history while new ones arrived.\n  const unreadIsBelow =\n    unreadIsBelowProp ??\n    (lastReadAt != null && newestLoadedAt != null && newestLoadedAt <= lastReadAt);\n  const ArrowIcon = unreadIsBelow ? IconArrowDown : IconArrowUp;\n\n  const { t } = useTranslationContext();\n\n  return (\n    <div\n      className={clsx('str-chat__unread-messages-notification', {\n        'str-chat__unread-messages-notification--with-count': unreadCount && showCount,\n      })}\n      data-testid='unread-messages-notification'\n    >\n      <Button\n        appearance='outline'\n        onClick={() =>\n          messagePaginator.jumpToTheFirstUnreadMessage({\n            pageSize: queryMessageLimit,\n          })\n        }\n        variant='secondary'\n      >\n        <ArrowIcon />\n        {unreadCount && showCount\n          ? t('messageList.unreadMessagesNotification.unread.text', {\n              count: unreadCount,\n              defaultValue_one: '{{count}} unread',\n              defaultValue_other: '{{count}} unread',\n            })\n          : t(\n              'messageList.unreadMessagesNotification.unreadMessages.text',\n              'Unread messages',\n            )}\n      </Button>\n      <Button\n        appearance='outline'\n        aria-label={t(\n          'messageList.unreadMessagesNotification.markMessagesRead.ariaLabel',\n          'Mark messages as read',\n        )}\n        onClick={() => {\n          messagePaginator.clearUnreadSnapshot();\n          client.messageDeliveryReporter.throttledMarkRead(thread ?? channel);\n        }}\n        variant='secondary'\n      >\n        <IconXmark />\n      </Button>\n    </div>\n  );\n};\n"],"mappings":";;;;;;;;;;;;AA6BA,IAAM,+BAA+B,WAAgC;CACnE,YAAY,MAAM;CAClB,aAAa,MAAM;AACrB;AAEA,IAAM,+BAA+B,WAAkC,EACrE,gBAAgB,MAAM,QAAQ,MAAM,MAAM,SAAS,EAAE,EAAE,WACzD;AAEA,IAAa,8BAA8B,EACzC,mBACA,YAAY,MACZ,eAAe,wBACsB;CACrC,MAAM,EAAE,eAAe,aAAa,cAAc,yBAAyB;CAE3E,MAAM,UAAU,WAAW;CAC3B,MAAM,EAAE,WAAW,eAAe;CAClC,MAAM,SAAS,iBAAiB;CAChC,MAAM,mBAAmB,oBAAoB;CAC7C,MAAM,EAAE,YAAY,gBAAgB,cAClC,iBAAiB,qBACjB,2BACF;CACA,MAAM,EAAE,mBAAmB,cACzB,iBAAiB,OACjB,2BACF;CAMA,MAAM,YAFJ,sBACC,cAAc,QAAQ,kBAAkB,QAAQ,kBAAkB,cACnC,gBAAgB;CAElD,MAAM,EAAE,MAAM,sBAAsB;CAEpC,OACE,qBAAC,OAAD;EACE,WAAW,KAAK,0CAA0C,EACxD,sDAAsD,eAAe,UACvE,CAAC;EACD,eAAY;YAJd,CAME,qBAAC,QAAD;GACE,YAAW;GACX,eACE,iBAAiB,4BAA4B,EAC3C,UAAU,kBACZ,CAAC;GAEH,SAAQ;aAPV,CASE,oBAAC,WAAD,CAAY,CAAA,GACX,eAAe,YACZ,EAAE,sDAAsD;IACtD,OAAO;IACP,kBAAkB;IAClB,oBAAoB;GACtB,CAAC,IACD,EACE,8DACA,iBACF,CACE;MACR,oBAAC,QAAD;GACE,YAAW;GACX,cAAY,EACV,qEACA,uBACF;GACA,eAAe;IACb,iBAAiB,oBAAoB;IACrC,OAAO,wBAAwB,kBAAkB,UAAU,OAAO;GACpE;GACA,SAAQ;aAER,oBAAC,WAAD,CAAY,CAAA;EACN,CAAA,CACL;;AAET"}