import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models"; import { type QueryFunction, type QueryKey, useQuery, } from "@tanstack/react-query"; import { useTranslation } from "react-i18next"; import { ScrollView, View, type ViewProps } from "react-native"; import { Text } from "@/components/common/Text"; import MoviePoster from "@/components/posters/MoviePoster"; import { useInView } from "@/hooks/useInView"; import ContinueWatchingPoster from "../ContinueWatchingPoster"; import { TouchableItemRouter } from "../common/TouchableItemRouter"; import { ItemCardText } from "../ItemCardText"; import SeriesPoster from "../posters/SeriesPoster"; interface Props extends ViewProps { title?: string | null; orientation?: "horizontal" | "vertical"; disabled?: boolean; queryKey: QueryKey; queryFn: QueryFunction; hideIfEmpty?: boolean; scrollY?: number; // For lazy loading enableLazyLoading?: boolean; // Enable/disable lazy loading } export const ScrollingCollectionList: React.FC = ({ title, orientation = "vertical", disabled = false, queryFn, queryKey, hideIfEmpty = false, scrollY = 0, enableLazyLoading = false, ...props }) => { const { ref, isInView, onLayout } = useInView(scrollY, { enabled: enableLazyLoading, }); const { data, isLoading } = useQuery({ queryKey: queryKey, queryFn, staleTime: 60 * 1000, // 1 minute refetchOnMount: false, refetchOnWindowFocus: false, refetchOnReconnect: true, enabled: enableLazyLoading ? isInView : true, }); const { t } = useTranslation(); // Show skeleton if loading OR if lazy loading is enabled and not in view yet const shouldShowSkeleton = isLoading || (enableLazyLoading && !isInView); if (hideIfEmpty === true && data?.length === 0 && !shouldShowSkeleton) return null; if (disabled || !title) return null; return ( {title} {!shouldShowSkeleton && data?.length === 0 && ( {t("home.no_items")} )} {shouldShowSkeleton ? ( {[1, 2, 3].map((i) => ( Nisi mollit voluptate amet. Lorem ipsum ))} ) : ( {data?.map((item) => ( {item.Type === "Episode" && orientation === "horizontal" && ( )} {item.Type === "Episode" && orientation === "vertical" && ( )} {item.Type === "Movie" && orientation === "horizontal" && ( )} {item.Type === "Movie" && orientation === "vertical" && ( )} {item.Type === "Series" && orientation === "vertical" && ( )} {item.Type === "Series" && orientation === "horizontal" && ( )} {item.Type === "Program" && ( )} ))} )} ); };