import React, { useState } from "react"; import { Button } from "@/shared/common/ui/button"; import { X, ChevronLeft, ChevronRight } from "lucide-react"; import { t } from "i18next"; interface UnitSelectionModalProps { units: unknown[]; isLoading: boolean; onSelectUnit: (unitId: string) => void; onClose: () => void; } const UNITS_PER_PAGE = 10; export const UnitSelectionModal: React.FC = ({ units, isLoading, onSelectUnit, onClose, }) => { const [currentPage, setCurrentPage] = useState(0); const typedUnits = units .filter( (unit): unit is { id: string; name: any; description?: string } => typeof unit === "object" && unit !== null && "id" in unit && "name" in unit ); const totalPages = Math.ceil(typedUnits.length / UNITS_PER_PAGE); const startIndex = currentPage * UNITS_PER_PAGE; const endIndex = startIndex + UNITS_PER_PAGE; const currentUnits = typedUnits.slice(startIndex, endIndex); const handleNext = () => { if (currentPage < totalPages - 1) { setCurrentPage(currentPage + 1); } }; const handlePrevious = () => { if (currentPage > 0) { setCurrentPage(currentPage - 1); } }; return (
{/* Header */}

{t("selectUnit")}

{/* Content */} {isLoading ? (

{t("loadingUnits")}

) : ( <> {/* Units List */}
{currentUnits.length > 0 ? ( currentUnits.map((unit) => ( )) ) : (

{t("noUnitsFound")}

)}
{/* Pagination Info */} {totalPages > 1 && (
{t("page", { current: currentPage + 1, total: totalPages, defaultValue: `Page ${currentPage + 1} of ${totalPages}`, })}
)} {/* Footer with Pagination */}
{typedUnits.length > 0 ? `${startIndex + 1} - ${Math.min(endIndex, typedUnits.length)} of ${typedUnits.length}` : "0"}
)}
); };