{"version":3,"file":"usePrependMessagesCount.mjs","names":[],"sources":["../../../../../../src/components/MessageList/hooks/VirtualizedMessageList/usePrependMessagesCount.ts"],"sourcesContent":["import { useMemo, useRef } from 'react';\nimport type { RenderedMessage } from '../../utils';\nimport { isLocalMessage } from '../../utils';\n\nconst STATUSES_EXCLUDED_FROM_PREPEND = {\n  failed: true,\n  sending: true,\n} as const as Record<string, boolean>;\n\nexport function usePrependedMessagesCount(\n  messages: RenderedMessage[],\n  hasDateSeparator: boolean,\n) {\n  const firstRealMessageIndex = hasDateSeparator ? 1 : 0;\n  const firstMessageOnFirstLoadedPage = useRef<RenderedMessage>(undefined);\n  const previousFirstMessageOnFirstLoadedPage = useRef<RenderedMessage>(undefined);\n  const previousNumItemsPrepended = useRef(0);\n\n  const numItemsPrepended = useMemo(() => {\n    if (!messages || !messages.length) {\n      previousNumItemsPrepended.current = 0;\n      return 0;\n    }\n\n    const currentFirstMessage = messages?.[firstRealMessageIndex];\n\n    const noNewMessages =\n      currentFirstMessage?.id === previousFirstMessageOnFirstLoadedPage.current?.id;\n\n    // Messages A then B sent within milliseconds (e.g. in an empty channel) can briefly reorder:\n    // when the response for A arrives with a later `created_at`, B (\"sending\") sorts first and A\n    // moves to index 1, which would count as a prepend and make VirtualizedMessageList read a\n    // non-existent index. So a first message that is \"sending\" or \"failed\" is never a prepend.\n    const currentFirstMessageStatus = isLocalMessage(currentFirstMessage)\n      ? currentFirstMessage.status\n      : undefined;\n    const firstMsgMovedAfterMessagesInExcludedStatus = !!(\n      currentFirstMessageStatus &&\n      STATUSES_EXCLUDED_FROM_PREPEND[currentFirstMessageStatus]\n    );\n\n    if (noNewMessages || firstMsgMovedAfterMessagesInExcludedStatus) {\n      return previousNumItemsPrepended.current;\n    }\n\n    if (!firstMessageOnFirstLoadedPage.current) {\n      firstMessageOnFirstLoadedPage.current = currentFirstMessage;\n    }\n    previousFirstMessageOnFirstLoadedPage.current = currentFirstMessage;\n    // Counts the prepended messages, starting at the previous count since it cannot shrink.\n    for (\n      let prependedMessageCount = previousNumItemsPrepended.current;\n      prependedMessageCount < messages.length;\n      prependedMessageCount += 1\n    ) {\n      const messageIsFirstOnFirstLoadedPage =\n        messages[prependedMessageCount].id === firstMessageOnFirstLoadedPage.current?.id;\n\n      if (messageIsFirstOnFirstLoadedPage) {\n        previousNumItemsPrepended.current = prependedMessageCount - firstRealMessageIndex;\n        return previousNumItemsPrepended.current;\n      }\n    }\n\n    // No match means the list jumped: reset the prepended count.\n    firstMessageOnFirstLoadedPage.current = currentFirstMessage;\n    previousNumItemsPrepended.current = 0;\n    return 0;\n    // TODO: there's a bug here, the messages prop is the same array instance (something mutates it)\n    // that's why the second dependency is necessary\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [firstRealMessageIndex, messages, messages?.length]);\n\n  return numItemsPrepended;\n}\n"],"mappings":";;;AAIA,IAAM,iCAAiC;CACrC,QAAQ;CACR,SAAS;AACX;AAEA,SAAgB,0BACd,UACA,kBACA;CACA,MAAM,wBAAwB,mBAAmB,IAAI;CACrD,MAAM,gCAAgC,OAAwB,KAAA,CAAS;CACvE,MAAM,wCAAwC,OAAwB,KAAA,CAAS;CAC/E,MAAM,4BAA4B,OAAO,CAAC;CAyD1C,OAvD0B,cAAc;EACtC,IAAI,CAAC,YAAY,CAAC,SAAS,QAAQ;GACjC,0BAA0B,UAAU;GACpC,OAAO;EACT;EAEA,MAAM,sBAAsB,WAAW;EAEvC,MAAM,gBACJ,qBAAqB,OAAO,sCAAsC,SAAS;EAM7E,MAAM,4BAA4B,eAAe,mBAAmB,IAChE,oBAAoB,SACpB,KAAA;EACJ,MAAM,6CAA6C,CAAC,EAClD,6BACA,+BAA+B;EAGjC,IAAI,iBAAiB,4CACnB,OAAO,0BAA0B;EAGnC,IAAI,CAAC,8BAA8B,SACjC,8BAA8B,UAAU;EAE1C,sCAAsC,UAAU;EAEhD,KACE,IAAI,wBAAwB,0BAA0B,SACtD,wBAAwB,SAAS,QACjC,yBAAyB,GAKzB,IAFE,SAAS,sBAAsB,CAAC,OAAO,8BAA8B,SAAS,IAE3C;GACnC,0BAA0B,UAAU,wBAAwB;GAC5D,OAAO,0BAA0B;EACnC;EAIF,8BAA8B,UAAU;EACxC,0BAA0B,UAAU;EACpC,OAAO;CAIT,GAAG;EAAC;EAAuB;EAAU,UAAU;CAAM,CAE9C;AACT"}