import { useEffect, useState } from "react"; import { Button } from "@/components/ui/button"; import { Users, Timer, Tag, LockIcon, } from "lucide-react"; import AppBreadcrumb from "@/components/generic/Breadcrumb/AppBreadcrumb"; import { Badge } from "@/components/ui/badge"; import { Skeleton } from "@/components/ui/skeleton"; import { Table, TableHeader, TableBody, TableHead, TableRow, TableCell, } from "@/components/ui/table"; import { useNavigate, useLocation } from "react-router-dom"; import { toast } from "sonner"; import { useClientCourses } from "@/contexts/ClientCoursesContext"; import { useLibrary } from "@/contexts/ClientLibraryContext"; import { useClientTiers } from "@/contexts/ClientTiersProvider"; import { UnitCard, UnitCardSkeleton } from "../components/UnitCard"; import LessonUpsellModal from "../components/LessonUpsellModal"; import { LessonCard, LessonCardSkeleton } from "../components/LessonCard"; import { resolveTierBadge } from "@/utils/tierBadge.util"; import { cn } from "@/lib/utils"; import { useGroup } from "@/contexts/ClientGroupContext"; import { useClientAdvertisements } from "@/contexts/ClientAdvertisementContext"; import { Hero, HeroSkeleton } from "@/components/generic/Blocks/Client/Advertisements/Hero"; // ─── Helpers ────────────────────────────────────────────────────────────────── function formatDuration(seconds = 0) { if (!seconds) return null; const h = Math.floor(seconds / 3600); const m = Math.floor((seconds % 3600) / 60); if (h && m) return `${h}h ${m}m`; if (h) return `${h}h`; return `${m}m`; } // ── Course Card ────────────────────────────────────────────────────────────── const CourseCard = ({ course, onViewDetails }) => { const { tierMap } = useClientTiers(); const slug = course.subscription ?? "free"; const locked = course.is_locked; const duration = formatDuration(course.duration_seconds); const { rank, label, cls } = resolveTierBadge(slug, tierMap); return (
onViewDetails(course)} >
{rank > 0 || locked ? : } {label} {course.level && ( {course.level.charAt(0).toUpperCase() + course.level.slice(1)} )} {locked && ( Locked )}

{course.title}

{course.description && (

{course.description}

)}
{duration ?? "—"}
{locked && ( Upgrade to unlock )}
); }; // ─── Course Card Skeleton ───────────────────────────────────────────────────── const CourseCardSkeleton = () => (
); // ─── Groups Table ───────────────────────────────────────────────────────────── const GroupsTable = ({ groups, onView }) => (
# Group Name Code Task Lists {groups.map((g, i) => { const taskListCount = Number(g.task_list_count ?? g.taskLists?.length ?? 0); return ( {i + 1} {g.name} {g.group_code} {taskListCount} ); })}
); const GroupsTableSkeleton = () => (
# Group Name Code Task Lists {[1, 2].map((i) => ( ))}
); // ─── Client Dashboard ───────────────────────────────────────────────────────── const Client = () => { const navigate = useNavigate(); const { state: navState } = useLocation(); const { courses, coursesLoading, getCourses } = useClientCourses(); const { units, unitsLoading, getUnits, lessons, lessonsLoading, getLessons } = useLibrary(); const { tierMap } = useClientTiers(); const { groups, fetchGroups, loading: groupLoading } = useGroup(); const { adLists, listLoading, getActiveAdvertisementList, handleAdCtaClick, } = useClientAdvertisements(); const [lessonModalOpen, setLessonModalOpen] = useState(false); const [selectedLesson, setSelectedLesson] = useState(null); const heroAds = adLists["dashboard.hero"] ?? []; // Show welcome toast on first registration useEffect(() => { if (!navState?.justRegistered) return; toast('Welcome to Philproperties!', { description: 'You earned the Early Access badge. Check your notifications for details.', duration: 6000, }); window.history.replaceState({}, ''); }, []); // Show a one-time notice the first time a manually-registered account // gets folded into a Google sign-in with the same email. useEffect(() => { if (!navState?.justLinkedGoogle) return; toast('Your Google account is now affiliated with this account.', { description: 'You can sign in with Google going forward.', duration: 6000, }); window.history.replaceState({}, ''); }, []); useEffect(() => { getCourses(); getUnits(); getLessons(); }, []); useEffect(() => { fetchGroups(); }, []) // ── Resolve hero ad carousel once on mount ──────────────────────────────── useEffect(() => { getActiveAdvertisementList("dashboard.hero"); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); // Show only first 3 const featuredCourses = courses.slice(0, 3); const featuredUnits = units.slice(0, 3); const featuredLessons = lessons.slice(0, 3); const breadcrumbItems = [ { label: "My Groups", icon: }, ]; // ── Card click — always go to the detail page; locked items show their // upgrade prompt there, on the primary action button ────────────────────── const handleViewDetails = (course) => navigate(`/course/${course.course_id}`); const handleViewUnitDetails = (unit) => navigate(`/units/${unit.uuid}`); const handleViewLessonDetails = (lesson) => { if (lesson.is_locked) { setSelectedLesson(lesson); setLessonModalOpen(true); } else { navigate(`/lessons/${lesson.uuid}`); } }; return (
{/* ── Hero Advertisement Carousel ── */} {listLoading["dashboard.hero"] ? ( ) : ( )} {/* ── My Groups ── */}
{!groupLoading && groups.length > 0 && ( {groups.length} group{groups.length !== 1 ? 's' : ''} )}
{groupLoading ? ( ) : groups.length === 0 ? (

You are not assigned to any group.

) : ( navigate(`/group/${g.group_id}`)} /> )}
{/* ── Featured Courses (first 3) ── */}

Courses

{coursesLoading ? (
{Array.from({ length: 3 }).map((_, i) => ( ))}
) : featuredCourses.length === 0 ? (

No courses available yet.

) : (
{featuredCourses.map((course) => ( ))}
)}
{/* ── Featured Units (first 3) ── */}

Units

{unitsLoading ? (
{Array.from({ length: 3 }).map((_, i) => ( ))}
) : featuredUnits.length === 0 ? (

No units available yet.

) : (
{featuredUnits.map((unit) => ( ))}
)}
{/* ── Featured Lessons (first 3) ── */}

Lessons

{lessonsLoading ? (
{Array.from({ length: 3 }).map((_, i) => ( ))}
) : featuredLessons.length === 0 ? (

No lessons available yet.

) : (
{featuredLessons.map((lesson) => ( ))}
)}
{/* ── Upsell Modal — only for locked lessons ── */}
); }; export default Client;