import React, { FC, CSSProperties } from "react"; import { FixedSizeList as List } from "react-window"; import InfiniteLoader from "react-window-infinite-loader"; import type { SegmentDetailModel } from "@/models/datasets"; import SegmentCard from "./SegmentCard"; import s from "./style.module.css"; type IInfiniteVirtualListProps = { hasNextPage?: boolean; // Are there more items to load? (This information comes from the most recent API request.) isNextPageLoading: boolean; // Are we currently loading a page of items? (This may be an in-flight flag in your Redux store for example.) items: Array; // Array of items loaded so far. loadNextPage: () => Promise; // Callback function responsible for loading the next page of items. onClick: (detail: SegmentDetailModel) => void; onChangeSwitch: (segId: string, enabled: boolean) => Promise; }; const InfiniteVirtualList: FC = ({ hasNextPage, isNextPageLoading, items, loadNextPage, onClick: onClickCard, onChangeSwitch, }) => { // If there are more items to be loaded then add an extra row to hold a loading indicator. const itemCount = hasNextPage ? items.length + 1 : items.length; // Only load 1 page of items at a time. // Pass an empty callback to InfiniteLoader in case it asks us to load more than once. const loadMoreItems = isNextPageLoading ? () => { } : loadNextPage; // Every row is loaded except for our loading indicator row. const isItemLoaded = (index: number) => !hasNextPage || index < items.length; // Render an item or a loading indicator. const Item = ({ index, style }: { index: number; style: CSSProperties }) => { let content; if (!isItemLoaded(index)) { content = ( <> {[1, 2, 3].map((v) => ( ))} ); } else { content = items[index].map((segItem) => ( onClickCard(segItem)} onChangeSwitch={onChangeSwitch} loading={false} /> )); } return (
{content}
); }; return ( {({ onItemsRendered, ref }) => ( {Item} )} ); }; export default InfiniteVirtualList;