{"version":3,"file":"InfiniteScrollWithComponents.mjs","names":[],"sources":["../../../../src/components/InfiniteScrollPaginator/InfiniteScrollWithComponents.tsx"],"sourcesContent":["import type { ComponentType } from 'react';\nimport React, { forwardRef } from 'react';\nimport type { StateStore } from '@stream-io/state-store';\nimport type { PaginatorState } from 'stream-chat';\n\nimport { useStateStore } from '../../store';\nimport type { InfiniteScrollPaginatorProps } from './InfiniteScrollPaginator';\nimport { InfiniteScrollPaginator } from './InfiniteScrollPaginator';\n\n/** Structural view of a paginator, to avoid coupling to `BasePaginator`'s generic variance. */\ntype PaginatorLike<TItem> = {\n  hasMoreHead: boolean;\n  hasMoreTail: boolean;\n  state: StateStore<PaginatorState<TItem>>;\n  /**\n   * The item's identity in the paginator, used as the React key. Needed where `id` is not unique: a\n   * channel's `id` is unique only within its type, so a channel paginator keys by `cid`.\n   */\n  getItemId?: (item: TItem) => string;\n};\n\nexport type InfiniteScrollWithComponentsProps<TItem extends { id?: string }> =\n  InfiniteScrollPaginatorProps & {\n    EmptyListIndicator: React.ComponentType;\n    EndReachedIndicator: React.ComponentType<{\n      hasEnded: boolean;\n      reached: 'top' | 'bottom';\n    }>;\n    FirstPageLoadingIndicator: React.ComponentType;\n    ListItem: ComponentType<{ item: TItem }>;\n    LoadingNextPageIndicator: React.ComponentType<{ isLoading?: boolean }>;\n    paginator: PaginatorLike<TItem>;\n    hasReachedBottom?: (paginator: PaginatorLike<TItem>) => boolean;\n    hasReachedTop?: (paginator: PaginatorLike<TItem>) => boolean;\n  };\n\ntype InfiniteScrollWithComponentsComponent = <TItem extends { id?: string }>(\n  props: InfiniteScrollWithComponentsProps<TItem>,\n) => React.ReactNode;\n\n/**\n * Renders a paginator-backed list with pluggable indicator and item components, driven by the\n * paginator's `state`. The ref points to the scroll root element.\n */\nexport const InfiniteScrollWithComponents = forwardRef(\n  function InfiniteScrollWithComponents<TItem extends { id?: string }>(\n    props: InfiniteScrollWithComponentsProps<TItem>,\n    ref: React.ForwardedRef<unknown>,\n  ) {\n    const {\n      EmptyListIndicator,\n      EndReachedIndicator,\n      FirstPageLoadingIndicator,\n      hasReachedBottom,\n      hasReachedTop,\n      ListItem,\n      LoadingNextPageIndicator,\n      paginator,\n      ...componentProps\n    } = props;\n\n    const { isLoading, items } = useStateStore(paginator.state, (state) => ({\n      isLoading: state.isLoading,\n      items: state.items,\n    }));\n    const isLoadingFirstPage = items === undefined;\n    const isEmpty = items?.length === 0;\n    const topEndReached = hasReachedTop?.(paginator) ?? !paginator.hasMoreHead;\n    const bottomEndReached = hasReachedBottom?.(paginator) ?? !paginator.hasMoreTail;\n\n    if (isLoadingFirstPage) return <FirstPageLoadingIndicator />;\n    if (isEmpty) return <EmptyListIndicator />;\n\n    return (\n      <>\n        <EndReachedIndicator hasEnded={topEndReached} reached='top' />\n        <InfiniteScrollPaginator {...componentProps} ref={ref}>\n          {items.map((item) => (\n            <ListItem item={item} key={paginator.getItemId?.(item) ?? item.id} />\n          ))}\n          <LoadingNextPageIndicator isLoading={isLoading} />\n        </InfiniteScrollPaginator>\n        <EndReachedIndicator hasEnded={bottomEndReached} reached='bottom' />\n      </>\n    );\n  },\n) as InfiniteScrollWithComponentsComponent;\n"],"mappings":";;;;;;;;;AA4CA,IAAa,+BAA+B,WAC1C,SAAS,6BACP,OACA,KACA;CACA,MAAM,EACJ,oBACA,qBACA,2BACA,kBACA,eACA,UACA,0BACA,WACA,GAAG,mBACD;CAEJ,MAAM,EAAE,WAAW,UAAU,cAAc,UAAU,QAAQ,WAAW;EACtE,WAAW,MAAM;EACjB,OAAO,MAAM;CACf,EAAE;CACF,MAAM,qBAAqB,UAAU,KAAA;CACrC,MAAM,UAAU,OAAO,WAAW;CAClC,MAAM,gBAAgB,gBAAgB,SAAS,KAAK,CAAC,UAAU;CAC/D,MAAM,mBAAmB,mBAAmB,SAAS,KAAK,CAAC,UAAU;CAErE,IAAI,oBAAoB,OAAO,oBAAC,2BAAD,CAA4B,CAAA;CAC3D,IAAI,SAAS,OAAO,oBAAC,oBAAD,CAAqB,CAAA;CAEzC,OACE,qBAAA,YAAA,EAAA,UAAA;EACE,oBAAC,qBAAD;GAAqB,UAAU;GAAe,SAAQ;EAAO,CAAA;EAC7D,qBAAC,yBAAD;GAAyB,GAAI;GAAqB;aAAlD,CACG,MAAM,KAAK,SACV,oBAAC,UAAD,EAAgB,KAAoD,GAAzC,UAAU,YAAY,IAAI,KAAK,KAAK,EAAK,CACrE,GACD,oBAAC,0BAAD,EAAqC,UAAY,CAAA,CAC1B;;EACzB,oBAAC,qBAAD;GAAqB,UAAU;GAAkB,SAAQ;EAAU,CAAA;CACnE,EAAA,CAAA;AAEN,CACF"}