{"version":3,"file":"useMessageFocusSignalViewed.mjs","names":[],"sources":["../../../../../../src/components/MessageList/hooks/VirtualizedMessageList/useMessageFocusSignalViewed.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\nimport type { RefObject } from 'react';\nimport type { MessageFocusSignal, MessagePaginator } from 'stream-chat';\n\nimport {\n  findMessageElement,\n  isMessageViewed,\n  MESSAGE_VIEWED_THRESHOLDS,\n} from '../../messageFocus';\n\ntype UseMessageFocusSignalViewedParams = {\n  /** Wraps the Virtuoso list; the focused message's item is looked up inside it. */\n  containerRef: RefObject<HTMLElement | null>;\n  /** Whether the focused message is among the list's processed messages. */\n  isMessageInList: boolean;\n  messageFocusSignal: MessageFocusSignal | null | undefined;\n  messagePaginator: MessagePaginator;\n  /** The element that scrolls the list, or the window in the window-scroll modes. */\n  scrollElement: HTMLElement | Window | null;\n};\n\nconst isWindow = (element: HTMLElement | Window): element is Window =>\n  typeof window !== 'undefined' && element === window;\n\n/**\n * Starts the dismissal countdown of the message-focus signal (a deep-link or quoted-reply jump, or\n * jump-to-latest) once the focused message has been viewed, as `MessageList` does. `MessagePaginator`\n * leaves that countdown to the list, so a list that never starts it keeps the message highlighted\n * until the next jump.\n *\n * In a virtualized list the focused message may not be rendered when the signal arrives (Virtuoso\n * has not scrolled to it yet), so its item is looked up again whenever the rendered items change.\n * As in `MessageList`, the countdown starts at once where the message is not in the list (e.g. a\n * deleted message under `hideDeletedMessages`) or where \"viewed\" cannot be observed.\n *\n * Signals are tracked by object, not by token: tokens count per paginator, so a list that switches\n * paginators can see the same token again for a different jump. A signal object is new for every\n * jump and stays the same while the list remounts.\n *\n * Returns a ref holding the last signal whose message was seen viewed, so the caller can stop\n * re-centering on a message the reader has already seen. It is not set when \"viewed\" cannot be\n * observed.\n */\nexport const useMessageFocusSignalViewed = ({\n  containerRef,\n  isMessageInList,\n  messageFocusSignal,\n  messagePaginator,\n  scrollElement,\n}: UseMessageFocusSignalViewedParams) => {\n  const viewedSignalRef = useRef<MessageFocusSignal | null>(null);\n  const clearScheduledSignalRef = useRef<MessageFocusSignal | null>(null);\n\n  useEffect(() => {\n    if (!messageFocusSignal) return;\n    // The list remounts (new message set, new scroller) after the countdown started: done already.\n    if (clearScheduledSignalRef.current === messageFocusSignal) return;\n    const { messageId, token } = messageFocusSignal;\n\n    const scheduleClear = () => {\n      clearScheduledSignalRef.current = messageFocusSignal;\n      messagePaginator.scheduleMessageFocusSignalClear({ token });\n    };\n\n    if (\n      !isMessageInList ||\n      typeof IntersectionObserver === 'undefined' ||\n      typeof MutationObserver === 'undefined'\n    ) {\n      // The message will not render, or \"viewed\" cannot be observed: start the countdown now to\n      // let the highlight clear.\n      scheduleClear();\n      return;\n    }\n\n    const container = containerRef.current;\n    // Virtuoso has not reported its scroller yet; this effect runs again once it does.\n    if (!container || !scrollElement) return;\n\n    const root = isWindow(scrollElement) ? null : scrollElement;\n    let observed: Element | null = null;\n\n    const viewObserver = new IntersectionObserver(\n      (entries) => {\n        const entry = [...entries].reverse().find((e) => e.target === observed);\n        if (!entry) return;\n        const rootHeight = root ? root.clientHeight : window.innerHeight;\n        if (!isMessageViewed(entry, rootHeight)) return;\n        disconnect();\n        viewedSignalRef.current = messageFocusSignal;\n        scheduleClear();\n      },\n      { root, threshold: MESSAGE_VIEWED_THRESHOLDS },\n    );\n\n    const observeTarget = () => {\n      const target = findMessageElement(container, messageId);\n      if (target === observed) return;\n      if (observed) viewObserver.unobserve(observed);\n      observed = target;\n      if (target) viewObserver.observe(target);\n    };\n\n    // Virtuoso adds and removes items as the list scrolls, so they are watched until the message\n    // is viewed.\n    const renderObserver = new MutationObserver(observeTarget);\n    const disconnect = () => {\n      viewObserver.disconnect();\n      renderObserver.disconnect();\n    };\n\n    renderObserver.observe(container, { childList: true, subtree: true });\n    observeTarget();\n\n    return disconnect;\n  }, [\n    containerRef,\n    isMessageInList,\n    messageFocusSignal,\n    messagePaginator,\n    scrollElement,\n  ]);\n\n  return viewedSignalRef;\n};\n"],"mappings":";;;AA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