{"version":3,"file":"FloatingDateSeparator.mjs","names":[],"sources":["../../../../src/components/MessageList/FloatingDateSeparator.tsx"],"sourcesContent":["import React, { useLayoutEffect } from 'react';\nimport type { RefObject } from 'react';\n\nimport { DateSeparator as DefaultDateSeparator } from '../DateSeparator';\nimport { useComponentContext } from '../../context/ComponentContext';\nimport { useFloatingDateSeparatorMessageList } from './hooks/MessageList';\nimport { useFloatingDateSeparator } from './hooks/VirtualizedMessageList';\nimport { VIRTUALIZED_ITEM_DATE_SEPARATOR_SELECTOR } from './hooks/floatingDateSlot';\nimport { useIsScrolling } from './hooks/useIsScrolling';\nimport type { RenderedMessage } from './utils';\n\n/** How long the floating date stays up after the last `scroll` event, so it never rests over a message. */\nconst FLOATING_DATE_IDLE_HIDE_MS = 1000;\n\n/**\n * The scroller is an element whose inline separators can be queried and measured against its top\n * edge. A window scroller is not.\n */\nconst canMeasureFloatingSlot = (\n  scrollElement: HTMLElement | Window | null,\n): scrollElement is HTMLElement =>\n  scrollElement !== null && 'querySelectorAll' in scrollElement;\n\n/** Start of `date`'s local calendar day, the unit a date separator labels. */\nconst getStartOfDay = (date: Date) =>\n  new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();\n\n/**\n * In `VirtualizedMessageList` every rendered separator from the first rendered item on falls on the\n * same day as that item or later, so a measured date on an earlier day comes from items Virtuoso\n * had not yet swapped out when the list was measured, e.g. right after a jump down.\n */\nconst isOnEarlierDayThanFirstRenderedItem = (\n  measuredDate: Date,\n  firstRenderedItemDate: Date | null,\n) =>\n  firstRenderedItemDate !== null &&\n  getStartOfDay(measuredDate) < getStartOfDay(firstRenderedItemDate);\n\ntype BaseProps = {\n  processedMessages: RenderedMessage[];\n  withDateSeparator: boolean;\n};\n\nexport type FloatingDateSeparatorProps = BaseProps &\n  (\n    | {\n        /** For `VirtualizedMessageList`: ref the parent calls with the rendered items. */\n        itemsRenderedRef: RefObject<((rendered: RenderedMessage[]) => void) | null>;\n        /** For `VirtualizedMessageList`: the element (or window) Virtuoso scrolls. */\n        scrollElement: HTMLElement | Window | null;\n      }\n    | {\n        /** For `MessageList`: scroll container queried for the in-flow date separators. */\n        listElement: HTMLDivElement | null;\n      }\n  );\n\n/**\n * Renders a floating date separator while the list is being scrolled and the in-flow date has\n * scrolled out of view. Its state is internal so the parent list does not re-render when it changes.\n * Pass `itemsRenderedRef` and `scrollElement` for `VirtualizedMessageList` and `listElement` for\n * `MessageList`.\n */\nexport const FloatingDateSeparator = (props: FloatingDateSeparatorProps) => {\n  const { DateSeparator = DefaultDateSeparator } = useComponentContext();\n  const { processedMessages, withDateSeparator } = props;\n\n  const isVirtualized = 'itemsRenderedRef' in props;\n  // Without date separators nothing floats, so no hook below listens to the list.\n  const scrollElement = !withDateSeparator\n    ? null\n    : 'scrollElement' in props\n      ? props.scrollElement\n      : props.listElement;\n  const measuredElement = canMeasureFloatingSlot(scrollElement) ? scrollElement : null;\n\n  const virtuosoResult = useFloatingDateSeparator({\n    processedMessages,\n    withDateSeparator,\n  });\n  const measured = useFloatingDateSeparatorMessageList({\n    datedSeparatorSelector: isVirtualized\n      ? VIRTUALIZED_ITEM_DATE_SEPARATOR_SELECTOR\n      : undefined,\n    listElement: measuredElement,\n    processedMessages,\n    withDateSeparator,\n  });\n  const measuredDate = measuredElement ? measured.floatingDate : null;\n\n  const { firstRenderedItemDate, isFirstRenderedItemDateSeparator } = virtuosoResult;\n  const currentMeasuredDate =\n    measuredDate &&\n    !isOnEarlierDayThanFirstRenderedItem(measuredDate, firstRenderedItemDate)\n      ? measuredDate\n      : null;\n  // `VirtualizedMessageList` unmounts separators far above the viewport, so when none of the\n  // rendered ones has scrolled past the top, the date comes from the first rendered item. A\n  // separator as that item gives no date: its label is still on screen.\n  const floatingDate = isVirtualized\n    ? (currentMeasuredDate ??\n      (isFirstRenderedItemDateSeparator ? null : firstRenderedItemDate))\n    : measuredDate;\n  // A window scroller can't be measured, so an inline separator as the first rendered item stands in\n  // for one sitting where the floating separator is drawn. That holds while Virtuoso renders nothing\n  // above the viewport (`overscan` and `increaseViewportBy.top` of 0, the list's defaults).\n  const isInlineSeparatorInFloatingSlot = measuredElement\n    ? measured.isFloatingSlotTaken\n    : isFirstRenderedItemDateSeparator;\n\n  const isScrolling = useIsScrolling(scrollElement, FLOATING_DATE_IDLE_HIDE_MS);\n\n  const itemsRenderedRef = isVirtualized ? props.itemsRenderedRef : null;\n  useLayoutEffect(() => {\n    if (!itemsRenderedRef) return;\n    itemsRenderedRef.current = virtuosoResult.onItemsRendered;\n    return () => {\n      itemsRenderedRef.current = null;\n    };\n  }, [itemsRenderedRef, virtuosoResult.onItemsRendered]);\n\n  if (\n    !isScrolling ||\n    !floatingDate ||\n    isInlineSeparatorInFloatingSlot ||\n    !DateSeparator\n  ) {\n    return null;\n  }\n\n  return <DateSeparator date={floatingDate} floating />;\n};\n"],"mappings":";;;;;;;;;;AAYA,IAAM,6BAA6B;;;;;AAMnC,IAAM,0BACJ,kBAEA,kBAAkB,QAAQ,sBAAsB;;AAGlD,IAAM,iBAAiB,SACrB,IAAI,KAAK,KAAK,YAAY,GAAG,KAAK,SAAS,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ;;;;;;AAOxE,IAAM,uCACJ,cACA,0BAEA,0BAA0B,QAC1B,cAAc,YAAY,IAAI,cAAc,qBAAqB;;;;;;;AA2BnE,IAAa,yBAAyB,UAAsC;CAC1E,MAAM,EAAE,eAAA,kBAAgB,kBAAyB,oBAAoB;CACrE,MAAM,EAAE,mBAAmB,sBAAsB;CAEjD,MAAM,gBAAgB,sBAAsB;CAE5C,MAAM,gBAAgB,CAAC,oBACnB,OACA,mBAAmB,QACjB,MAAM,gBACN,MAAM;CACZ,MAAM,kBAAkB,uBAAuB,aAAa,IAAI,gBAAgB;CAEhF,MAAM,iBAAiB,yBAAyB;EAC9C;EACA;CACF,CAAC;CACD,MAAM,WAAW,oCAAoC;EACnD,wBAAwB,gBACpB,2CACA,KAAA;EACJ,aAAa;EACb;EACA;CACF,CAAC;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