{"version":3,"file":"useNewMessageNotification.mjs","names":[],"sources":["../../../../../../src/components/MessageList/hooks/VirtualizedMessageList/useNewMessageNotification.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\nimport type { LocalMessage, MessagePaginatorState } from 'stream-chat';\nimport type { RenderedMessage } from '../../utils';\nimport { useMessagePaginator } from '../../../../hooks';\nimport { useStateStore } from '../../../../store';\n\nconst messagePaginatorStateSelector = (state: MessagePaginatorState) => ({\n  hasMoreNewer: state.hasMoreHead,\n});\n\n/**\n * Whether a change of the last rendered message means a message arrived at the latest end of the\n * list, as opposed to the loaded window moving.\n *\n * - A window that is not at the latest messages (`hasMoreNewer`) changes its last message only by\n *   loading pages or jumping, never by a message arriving.\n * - A window that no longer contains the previous last message was replaced (a jump, e.g. back to\n *   the latest messages) rather than extended at its end. The first rendered window has no\n *   previous last message, so it never counts as an arrival either.\n */\nconst isArrivalAtLatestMessages = ({\n  hasMoreNewer,\n  messages,\n  prevMessageId,\n}: {\n  hasMoreNewer: boolean;\n  messages: RenderedMessage[];\n  prevMessageId: string;\n}) => !hasMoreNewer && messages.some((message) => message.id === prevMessageId);\n\nexport function useNewMessageNotification(\n  messages: RenderedMessage[],\n  currentUserId: string | undefined,\n  // hasMoreNewer?: boolean,\n) {\n  const [newMessagesNotification, setNewMessagesNotification] = useState(false);\n  const [isMessageListScrolledToBottom, setIsMessageListScrolledToBottom] =\n    useState(true);\n  const messagePaginator = useMessagePaginator();\n  const { hasMoreNewer } = useStateStore(\n    messagePaginator.state,\n    messagePaginatorStateSelector,\n  );\n  const lastMessageId = useRef('');\n  const atBottom = useRef(false);\n\n  useEffect(() => {\n    if (!messages?.length) return;\n\n    const lastMessage = messages[messages.length - 1];\n    const prevMessageId = lastMessageId.current;\n    lastMessageId.current = lastMessage.id || '';\n\n    // Unchanged last message: older messages were loaded at the top.\n    if (lastMessage.id === prevMessageId) return;\n\n    if (!isArrivalAtLatestMessages({ hasMoreNewer, messages, prevMessageId })) return;\n\n    // At the bottom, `followOutput` scrolls the list.\n    if (atBottom.current) return;\n\n    // Only messages from other users notify.\n    if ((lastMessage as LocalMessage).user?.id !== currentUserId) {\n      setNewMessagesNotification(true);\n    }\n  }, [currentUserId, messages, hasMoreNewer]);\n\n  return {\n    atBottom,\n    isMessageListScrolledToBottom,\n    newMessagesNotification,\n    setIsMessageListScrolledToBottom,\n    setNewMessagesNotification,\n  };\n}\n"],"mappings":";;;;AAMA,IAAM,iCAAiC,WAAkC,EACvE,cAAc,MAAM,YACtB;;;;;;;;;;;AAYA,IAAM,6BAA6B,EACjC,cACA,UACA,oBAKI,CAAC,gBAAgB,SAAS,MAAM,YAAY,QAAQ,OAAO,aAAa;AAE9E,SAAgB,0BACd,UACA,eAEA;CACA,MAAM,CAAC,yBAAyB,8BAA8B,SAAS,KAAK;CAC5E,MAAM,CAAC,+BAA+B,oCACpC,SAAS,IAAI;CAEf,MAAM,EAAE,iBAAiB,cADA,oBAEvB,CAAA,CAAiB,OACjB,6BACF;CACA,MAAM,gBAAgB,OAAO,EAAE;CAC/B,MAAM,WAAW,OAAO,KAAK;CAE7B,gBAAgB;EACd,IAAI,CAAC,UAAU,QAAQ;EAEvB,MAAM,cAAc,SAAS,SAAS,SAAS;EAC/C,MAAM,gBAAgB,cAAc;EACpC,cAAc,UAAU,YAAY,MAAM;EAG1C,IAAI,YAAY,OAAO,eAAe;EAEtC,IAAI,CAAC,0BAA0B;GAAE;GAAc;GAAU;EAAc,CAAC,GAAG;EAG3E,IAAI,SAAS,SAAS;EAGtB,IAAK,YAA6B,MAAM,OAAO,eAC7C,2BAA2B,IAAI;CAEnC,GAAG;EAAC;EAAe;EAAU;CAAY,CAAC;CAE1C,OAAO;EACL;EACA;EACA;EACA;EACA;CACF;AACF"}