import { useEffect, useMemo, useState } from "react";
import { useNavigate } from "react-router-dom";
import { House, ChevronLeft, ChevronRight, Layers } from "lucide-react";
import AppBreadcrumb from "@/components/generic/Breadcrumb/AppBreadcrumb";
import { Input } from "@/components/ui/input";
import {
Select, SelectContent, SelectItem, SelectTrigger, SelectValue,
} from "@/components/ui/select";
import { Button } from "@/components/ui/button";
import { useLibrary } from "@/contexts/ClientLibraryContext";
import { useClientTiers } from "@/contexts/ClientTiersProvider";
import LessonUpsellModal from "../components/LessonUpsellModal";
import { LessonCard, LessonCardSkeleton } from "../components/LessonCard";
import { PageMeta } from "@/contexts/MetadataContext";
const ITEMS_PER_PAGE = 10;
// ─── Pagination ───────────────────────────────────────────────────────────────
const Pagination = ({ currentPage, totalPages, totalItems, itemsPerPage, onPageChange }) => {
const start = (currentPage - 1) * itemsPerPage + 1;
const end = Math.min(currentPage * itemsPerPage, totalItems);
const getPages = () => {
const pages = [];
if (totalPages <= 5) {
for (let i = 1; i <= totalPages; i++) pages.push(i);
} else {
pages.push(1);
if (currentPage > 3) pages.push("...");
for (let i = Math.max(2, currentPage - 1); i <= Math.min(totalPages - 1, currentPage + 1); i++) pages.push(i);
if (currentPage < totalPages - 2) pages.push("...");
pages.push(totalPages);
}
return pages;
};
return (
Showing {start}–{end} of{" "}
{totalItems} lessons
{getPages().map((page, i) =>
page === "..." ? (
···
) : (
)
)}
);
};
// ─── Main Page ────────────────────────────────────────────────────────────────
const LessonsList = () => {
const navigate = useNavigate();
const { lessons, lessonsLoading, getLessons } = useLibrary();
const { tierMap, getTierCategories } = useClientTiers();
const [currentPage, setCurrentPage] = useState(1);
const [search, setSearch] = useState("");
const [lockFilter, setLockFilter] = useState("All");
const [modalOpen, setModalOpen] = useState(false);
const [selectedLesson, setSelectedLesson] = useState(null);
useEffect(() => {
getLessons();
getTierCategories();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const filtered = useMemo(() =>
lessons
.filter((l) => {
const matchSearch = l.title.toLowerCase().includes(search.toLowerCase()) ||
(l.description ?? "").toLowerCase().includes(search.toLowerCase());
const matchLock = lockFilter === "All"
|| (lockFilter === "Unlocked" && !l.is_locked)
|| (lockFilter === "Locked" && l.is_locked);
return matchSearch && matchLock;
})
.sort((a, b) => a.title.localeCompare(b.title)),
[lessons, search, lockFilter]
);
const totalPages = Math.max(1, Math.ceil(filtered.length / ITEMS_PER_PAGE));
const paginated = filtered.slice((currentPage - 1) * ITEMS_PER_PAGE, currentPage * ITEMS_PER_PAGE);
const handleViewDetails = (lesson) => {
if (lesson.is_locked) {
setSelectedLesson(lesson);
setModalOpen(true);
} else {
navigate(`/lessons/${lesson.uuid}`);
}
};
const items = [
{ label: "Home", icon: , to: `/dashboard` },
{ label: "Lessons" },
];
return (
{/* Search & Filters */}
{/* Lesson Grid */}
{lessonsLoading ? (
{Array.from({ length: 8 }).map((_, i) => )}
) : paginated.length === 0 ? (
) : (
{paginated.map((lesson) => (
))}
)}
{!lessonsLoading && filtered.length > ITEMS_PER_PAGE && (
)}
);
};
export default LessonsList;