import { ArrowLeftIcon, ArrowRightIcon, } from "lucide-react"; import React, { forwardRef, useImperativeHandle, useRef, useState, } from "react"; import { useNavigate } from "react-router-dom"; import "../../assets/styles/listRow.css"; import { StreamListItem, CategoryListItem, UserListItem, VodListItem } from "./ListItem"; import { StreamType } from "../../types/StreamType"; import { CategoryType } from "../../types/CategoryType"; import { UserType } from "../../types/UserType"; import { VodType } from "../../types/VodType" type ItemType = StreamType | CategoryType | UserType | VodType; interface ListRowProps { variant?: "default" | "search"; type: "stream" | "category" | "user" | "vod"; title?: string; description?: string; items: ItemType[]; wrap?: boolean; onItemClick: (itemName: string) => void; titleClickable?: boolean; extraClasses?: string; itemExtraClasses?: string; amountForScroll?: number; children?: React.ReactNode; } interface ListRowRef { addMoreItems: (newItems: ItemType[]) => void; } const ListRow = forwardRef((props, ref) => { const { variant = "default", type, title = "", description = "", items, onItemClick, titleClickable = false, wrap = false, extraClasses = "", itemExtraClasses = "", amountForScroll = 4, children, } = props; const [currentItems, setCurrentItems] = useState(items); const slider = useRef(null); const scrollAmount = window.innerWidth * 0.3; const navigate = useNavigate(); const addMoreItems = (newItems: ItemType[]) => { setCurrentItems((prevItems) => [...prevItems, ...newItems]); }; useImperativeHandle(ref, () => ({ addMoreItems, })); const slideRight = () => { if (!wrap && slider.current) { slider.current.scrollBy({ left: +scrollAmount, behavior: "smooth" }); } }; const slideLeft = () => { if (!wrap && slider.current) { slider.current.scrollBy({ left: -scrollAmount, behavior: "smooth" }); } }; const handleTitleClick = () => { switch (type) { case "stream": break; case "category": navigate("/categories"); break; case "user": break; default: break; } }; const isStreamType = (item: ItemType): item is StreamType => item.type === "stream"; const isCategoryType = (item: ItemType): item is CategoryType => item.type === "category"; const isUserType = (item: ItemType): item is UserType => item.type === "user"; const isVodType = (item: ItemType): item is VodType => item.type === "vod"; return (
{/* List Details */}

{title}

{description}

{/* List Items */}
{!wrap && currentItems.length > amountForScroll && ( <> )}
{currentItems.length === 0 ? (

Nothing Found

) : ( <> {currentItems.map((item) => { if (type === "stream" && isStreamType(item)) { return ( onItemClick(item.username)} extraClasses={itemExtraClasses} /> ); } else if (type === "category" && isCategoryType(item)) { return ( onItemClick(item.title)} extraClasses={itemExtraClasses} /> ); } else if (type === "user" && isUserType(item)) { return ( onItemClick(item.username)} extraClasses={itemExtraClasses} /> ); } else if (type === "vod" && isVodType(item)) { return ( window.open(item.url, "_blank")} extraClasses={itemExtraClasses} /> ); } return null; })} )}
{children}
); }); export default ListRow;