{"version":3,"file":"ChannelList.mjs","names":[],"sources":["../../../../src/components/ChannelList/ChannelList.tsx"],"sourcesContent":["import type { ComponentProps, ComponentType } from 'react';\nimport React, { useEffect, useMemo, useRef } from 'react';\nimport type { Channel, ChannelPaginator, ChannelPaginatorState } from 'stream-chat';\n\nimport { ChannelListItem } from '../ChannelListItem';\nimport { EmptyStateIndicator } from '../EmptyStateIndicator';\nimport { InfiniteScrollWithComponents } from '../InfiniteScrollPaginator/InfiniteScrollWithComponents';\nimport {\n  ListLoadErrorIndicator as BaseListLoadErrorIndicator,\n  type ListLoadErrorIndicatorProps,\n  LoadingChannels,\n  LoadingIndicator,\n} from '../Loading';\nimport { getFirstPageLoadError } from '../Loading/getFirstPageLoadError';\nimport { useFirstPageLoadRetry } from '../Loading/hooks/useFirstPageLoadRetry';\nimport { useAddNotificationOnError } from '../Notifications/hooks/useAddNotificationOnError';\nimport { useChatContext } from '../../context/ChatContext';\nimport { useComponentContext } from '../../context/ComponentContext';\nimport { useTranslationContext } from '../../context/TranslationContext';\nimport { ThemeScope } from '../../context/ThemeScopeContext';\nimport { useChannelListKeyboardNavigation } from './hooks/useChannelListKeyboardNavigation';\nimport { useStateStore } from '../../store';\n\n/**\n * Attributes set on the list's `role=\"listbox\"` element, the scroll container that holds the\n * channel rows. Only its accessible name can be set; role, ref and key handling stay the list's.\n */\nexport type ChannelListListboxProps = Pick<\n  ComponentProps<'div'>,\n  'aria-label' | 'aria-labelledby'\n>;\n\nexport type ChannelListProps = {\n  /** Paginator that supplies the channels of this list. */\n  paginator: ChannelPaginator;\n  /**\n   * Names the listbox in place of the default \"Channel list\". Name each list when a page shows\n   * several of them, e.g. pinned, inbox and archived channels: `{ 'aria-label': 'Archived' }`, or\n   * `{ 'aria-labelledby': headingId }` to use a visible heading. `aria-labelledby` wins over\n   * `aria-label` when both are set.\n   */\n  listboxProps?: ChannelListListboxProps;\n  /**\n   * The minimum interval, in milliseconds, between two checks of the scroll position for loading\n   * the next page (`InfiniteScrollPaginator`'s `loadNextDebounceMs`).\n   */\n  loadMoreDebounceMs?: number;\n  loadMoreThresholdPx?: number;\n};\n\nconst createChannelPaginatorStateSelector =\n  (paginator: ChannelPaginator) => (state: ChannelPaginatorState) => ({\n    // No loaded page: the initial state, and where a paginator returns when its data is discarded\n    // (e.g. `client.disconnectUser` resets every registered list). Not `items === undefined`: a\n    // live event can route a channel into a list that was never queried, which defines `items`\n    // without loading a page. A loaded empty page (also one set with `setItems`, e.g. a catch-all\n    // list seeded so it never queries) must not trigger a query. A failed first page loads none\n    // either, but holds the error until a retry or a reset; with `throwErrors` set, that error is\n    // all that keeps the list from querying again and again.\n    awaitsFirstPage:\n      !paginator.hasLoadedPage && !state.isLoading && !state.lastQueryError,\n    firstPageLoadError: getFirstPageLoadError(state),\n    lastQueryError: state.lastQueryError,\n  });\n\n/**\n * Channel list driven by a single `ChannelPaginator`: it renders the paginator's state and drives\n * pagination. Selecting a channel is up to the `ListItem`; the default `ChannelListItem` opens it\n * through workspace navigation.\n */\nexport const ChannelList = ({\n  listboxProps,\n  loadMoreDebounceMs,\n  loadMoreThresholdPx,\n  paginator,\n}: ChannelListProps) => {\n  const { channelManager } = useChatContext();\n  const { t } = useTranslationContext();\n  const channelPaginatorStateSelector = useMemo(\n    () => createChannelPaginatorStateSelector(paginator),\n    [paginator],\n  );\n  const { awaitsFirstPage, firstPageLoadError, lastQueryError } = useStateStore(\n    paginator.state,\n    channelPaginatorStateSelector,\n  );\n  const {\n    EmptyListIndicator = DefaultEmptyChannelList,\n    EndReachedIndicator = DefaultEndReachedIndicator,\n    FirstPageLoadingIndicator = LoadingChannels,\n    ListItem = DefaultListItem,\n    ListLoadErrorIndicator = DefaultListLoadErrorIndicator,\n    LoadingNextPageIndicator = DefaultLoadingNextPageIndicator,\n  } = useComponentContext();\n\n  // The scroll container (the `InfiniteScrollPaginator` root, reached via `listboxRef`) is the\n  // `role=\"listbox\"` that owns the `role=\"option\"` rows, so the hook needs no extra wrapper.\n  // The hook's focus-after-Load-more branch is inert here: the list paginates on scroll to the\n  // bottom and renders no `load-more-button`.\n  const listboxRef = useRef<HTMLDivElement>(null);\n  const { onClickCapture, onKeyDown } = useChannelListKeyboardNavigation(listboxRef);\n\n  // Ref-counted, so `Chat` and other lists can register the same subscriptions.\n  useEffect(() => channelManager.registerSubscriptions(), [channelManager]);\n\n  // Loads the first page, and again whenever the list returns to \"no page loaded\": the paginator\n  // outlives this component, so querying only on mount would leave a reset list permanently empty.\n  // A paginator with a loaded page has queried already (or was seeded); its pages arrive through\n  // that query or its recovery, and a query from here would load the next page.\n  useEffect(() => {\n    if (!awaitsFirstPage) return;\n    paginator.toTailDebounced();\n  }, [awaitsFirstPage, paginator]);\n\n  useAddNotificationOnError(lastQueryError, {\n    emitter: 'ChannelList',\n    message: t('channelList.loadError.text', \"Couldn't load conversations\"),\n    targetPanels: ['channel-list'],\n    type: 'api:channel:query:failed',\n  });\n\n  const firstPageLoadRetry = useFirstPageLoadRetry({\n    firstPageLoadError,\n    loadedMessage: t('channelList.loaded.text', 'Conversations loaded'),\n    paginator,\n    reload: paginator.reload,\n  });\n\n  if (firstPageLoadRetry.error) {\n    return (\n      <ThemeScope>\n        <ListLoadErrorIndicator\n          error={firstPageLoadRetry.error}\n          isRetrying={firstPageLoadRetry.isRetrying}\n          retry={firstPageLoadRetry.retry}\n        />\n      </ThemeScope>\n    );\n  }\n\n  // Rendered without `ChannelNavigation` or `ChatView` above it, the list brings the theme scope.\n  return (\n    <ThemeScope>\n      <InfiniteScrollWithComponents<Channel>\n        aria-label={\n          listboxProps?.['aria-label'] ??\n          (listboxProps?.['aria-labelledby']\n            ? undefined\n            : t('channelList.channelList.ariaLabel', 'Channel list'))\n        }\n        aria-labelledby={listboxProps?.['aria-labelledby']}\n        contentProps={{ role: 'presentation' }}\n        EmptyListIndicator={EmptyListIndicator}\n        EndReachedIndicator={EndReachedIndicator}\n        FirstPageLoadingIndicator={FirstPageLoadingIndicator}\n        ListItem={ListItem as ComponentType<{ item: Channel }>}\n        LoadingNextPageIndicator={LoadingNextPageIndicator}\n        loadNextDebounceMs={loadMoreDebounceMs}\n        loadNextOnScrollToBottom={paginator.toTail}\n        onClickCapture={onClickCapture}\n        onKeyDown={onKeyDown}\n        paginator={paginator}\n        ref={listboxRef}\n        role='listbox'\n        threshold={loadMoreThresholdPx}\n      />\n    </ThemeScope>\n  );\n};\n\nconst DefaultListLoadErrorIndicator = (props: ListLoadErrorIndicatorProps) => (\n  <BaseListLoadErrorIndicator {...props} listType='channel' />\n);\n\nconst DefaultEmptyChannelList = () => <EmptyStateIndicator listType='channel' />;\n\nconst DefaultEndReachedIndicator = () => null;\n\nconst DefaultListItem = ({ item }: { item: unknown }) => (\n  <ChannelListItem channel={item as Channel} />\n);\n\nconst DefaultLoadingNextPageIndicator = ({ isLoading }: { isLoading?: boolean }) =>\n  isLoading ? <LoadingIndicator /> : null;\n"],"mappings":";;;;;;;;;;;;;;;;;;AAkDA,IAAM,uCACH,eAAiC,WAAkC;CAQlE,iBACE,CAAC,UAAU,iBAAiB,CAAC,MAAM,aAAa,CAAC,MAAM;CACzD,oBAAoB,sBAAsB,KAAK;CAC/C,gBAAgB,MAAM;AACxB;;;;;;AAOF,IAAa,eAAe,EAC1B,cACA,oBACA,qBACA,gBACsB;CACtB,MAAM,EAAE,mBAAmB,eAAe;CAC1C,MAAM,EAAE,MAAM,sBAAsB;CACpC,MAAM,gCAAgC,cAC9B,oCAAoC,SAAS,GACnD,CAAC,SAAS,CACZ;CACA,MAAM,EAAE,iBAAiB,oBAAoB,mBAAmB,cAC9D,UAAU,OACV,6BACF;CACA,MAAM,EACJ,qBAAqB,yBACrB,sBAAsB,4BACtB,4BAA4B,iBAC5B,WAAW,iBACX,yBAAyB,+BACzB,2BAA2B,oCACzB,oBAAoB;CAMxB,MAAM,aAAa,OAAuB,IAAI;CAC9C,MAAM,EAAE,gBAAgB,cAAc,iCAAiC,UAAU;CAGjF,gBAAgB,eAAe,sBAAsB,GAAG,CAAC,cAAc,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