{"version":3,"file":"useUnreadMessagesNotificationVirtualized.mjs","names":[],"sources":["../../../../../../src/components/MessageList/hooks/VirtualizedMessageList/useUnreadMessagesNotificationVirtualized.ts"],"sourcesContent":["import { useCallback, useEffect, useState } from 'react';\nimport type { RenderedMessage } from '../../utils';\nimport type { LocalMessage, UnreadSnapshotState } from 'stream-chat';\nimport { useMessagePaginator } from '../../../../hooks';\nimport { useStateStore } from '../../../../store';\n\nexport type UseUnreadMessagesNotificationParams = {\n  showAlways: boolean;\n  // unreadCount: number;\n  // lastRead?: Date | null;\n};\n\nconst unreadStateSnapshotSelector = (state: UnreadSnapshotState) => ({\n  lastReadAt: state.lastReadAt,\n  unreadCount: state.unreadCount,\n});\n\n/**\n * Decides when `UnreadMessagesNotification` is shown in the virtualized list, which has no unread\n * separator to scroll past. With unread messages, it shows when every rendered message was created\n * after the last read time, which approximates being scrolled past the separator.\n *\n * Also returns `unreadIsBelow`, which way the notification points: the rendered messages decide it,\n * since the loaded ones can reach past them in either direction.\n *\n * @param showAlways - Also show when every rendered message is older than the last read time.\n */\nexport const useUnreadMessagesNotificationVirtualized = ({\n  showAlways,\n}: UseUnreadMessagesNotificationParams) => {\n  const [show, setShow] = useState(false);\n  const [unreadIsBelow, setUnreadIsBelow] = useState<boolean | undefined>(undefined);\n  const messagePaginator = useMessagePaginator();\n  const { lastReadAt, unreadCount } = useStateStore(\n    messagePaginator.unreadStateSnapshot,\n    unreadStateSnapshotSelector,\n  );\n\n  const toggleShowUnreadMessagesNotification = useCallback(\n    (renderedMessages: RenderedMessage[]) => {\n      if (!unreadCount) return;\n      const firstRenderedMessage = renderedMessages[0];\n      const lastRenderedMessage = renderedMessages.slice(-1)[0];\n      if (!(firstRenderedMessage && lastRenderedMessage)) return;\n\n      // All three are raw timestamps compared as-is; building `Date`s from them gives NaN.\n      const firstRenderedMessageTime =\n        (firstRenderedMessage as LocalMessage).created_at ?? 0;\n      const lastRenderedMessageTime =\n        (lastRenderedMessage as LocalMessage).created_at ?? 0;\n      const lastReadTime = lastReadAt ?? 0;\n\n      const scrolledBelowSeparator =\n        lastReadAt != null && firstRenderedMessageTime > lastReadTime;\n      const scrolledAboveSeparator =\n        lastReadAt != null && lastRenderedMessageTime < lastReadTime;\n\n      setShow(\n        showAlways\n          ? scrolledBelowSeparator || scrolledAboveSeparator\n          : scrolledBelowSeparator,\n      );\n      setUnreadIsBelow(scrolledAboveSeparator);\n    },\n    [lastReadAt, showAlways, unreadCount],\n  );\n\n  useEffect(() => {\n    if (unreadCount) return;\n    setShow(false);\n    setUnreadIsBelow(undefined);\n  }, [unreadCount]);\n\n  return { show, toggleShowUnreadMessagesNotification, unreadIsBelow };\n};\n"],"mappings":";;;;AAYA,IAAM,+BAA+B,WAAgC;CACnE,YAAY,MAAM;CAClB,aAAa,MAAM;AACrB;;;;;;;;;;;AAYA,IAAa,4CAA4C,EACvD,iBACyC;CACzC,MAAM,CAAC,MAAM,WAAW,SAAS,KAAK;CACtC,MAAM,CAAC,eAAe,oBAAoB,SAA8B,KAAA,CAAS;CAEjF,MAAM,EAAE,YAAY,gBAAgB,cADX,oBAEvB,CAAA,CAAiB,qBACjB,2BACF;CAEA,MAAM,uCAAuC,aAC1C,qBAAwC;EACvC,IAAI,CAAC,aAAa;EAClB,MAAM,uBAAuB,iBAAiB;EAC9C,MAAM,sBAAsB,iBAAiB,MAAM,EAAE,CAAC,CAAC;EACvD,IAAI,EAAE,wBAAwB,sBAAsB;EAGpD,MAAM,2BACH,qBAAsC,cAAc;EACvD,MAAM,0BACH,oBAAqC,cAAc;EACtD,MAAM,eAAe,cAAc;EAEnC,MAAM,yBACJ,cAAc,QAAQ,2BAA2B;EACnD,MAAM,yBACJ,cAAc,QAAQ,0BAA0B;EAElD,QACE,aACI,0BAA0B,yBAC1B,sBACN;EACA,iBAAiB,sBAAsB;CACzC,GACA;EAAC;EAAY;EAAY;CAAW,CACtC;CAEA,gBAAgB;EACd,IAAI,aAAa;EACjB,QAAQ,KAAK;EACb,iBAAiB,KAAA,CAAS;CAC5B,GAAG,CAAC,WAAW,CAAC;CAEhB,OAAO;EAAE;EAAM;EAAsC;CAAc;AACrE"}