Files
starr-philproperties/apps/web/src/modules/client/pages/Dashboard.jsx
T
kennethobsequio ba5bb21961 commit unsaved
Signed-off-by: Kenneth Obsequio <k80308392@gmail.com>
2026-10-12 16:45:08 +08:00

401 lines
18 KiB
React

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, panel } = resolveTierBadge(slug, tierMap);
return (
<div
className={cn(
"bg-card rounded-2xl border p-4 flex flex-col gap-2.5 transition-all cursor-pointer group",
"hover:shadow-sm",
locked
? cn("opacity-80 hover:opacity-100", panel.bg, panel.border)
: "hover:bg-muted/60 dark:hover:border-blue-500"
)}
onClick={() => onViewDetails(course)}
>
<div className="flex flex-wrap gap-1.5">
<Badge className={cls}>
{rank > 0 || locked ? <LockIcon className="size-3" /> : <Tag className="size-3" />}
{label}
</Badge>
{course.level && (
<Badge variant="outline">
{course.level.charAt(0).toUpperCase() + course.level.slice(1)}
</Badge>
)}
{locked && (
<Badge variant="secondary" className="ml-auto bg-white dark:bg-white/15 dark:text-white dark:border-white/15">
<LockIcon className="size-3" /> Locked
</Badge>
)}
</div>
<div className="flex flex-col gap-1">
<h1 className="text-lg font-medium leading-snug line-clamp-3 transition-colors group-hover:text-blue-700 dark:group-hover:text-blue-400">
{course.title}
</h1>
<p className="text-sm leading-relaxed line-clamp-2 text-muted-foreground">
{course.description || "No information yet provided"}
</p>
</div>
<div className="flex items-center justify-between pt-2 mt-auto border-t">
<div className={`flex items-center gap-1 text-sm [&_svg]:size-4 ${locked ? "text-muted-foreground" : "text-card-foreground group-hover:text-blue-700 dark:group-hover:text-blue-400"}`}>
<Timer />
{duration ?? "—"}
</div>
{locked && (
<span className="text-xs text-muted-foreground">Upgrade to unlock</span>
)}
</div>
</div>
);
};
// ─── Course Card Skeleton ─────────────────────────────────────────────────────
const CourseCardSkeleton = () => (
<div className="bg-card rounded-2xl border p-4 flex flex-col gap-2.5">
<div className="flex gap-1.5">
<Skeleton className="h-5 w-16 rounded-full" />
<Skeleton className="h-5 w-20 rounded-full" />
</div>
<Skeleton className="h-6 w-3/4" />
<Skeleton className="h-4 w-full" />
<Skeleton className="h-4 w-2/3" />
<div className="pt-2 mt-auto border-t">
<Skeleton className="h-4 w-16" />
</div>
</div>
);
// ─── Groups Table ─────────────────────────────────────────────────────────────
const GroupsTable = ({ groups, onView }) => (
<div className="bg-card border rounded-xl overflow-hidden">
<Table>
<TableHeader>
<TableRow className="hover:bg-transparent">
<TableHead className="w-10 text-center px-4">#</TableHead>
<TableHead className="px-4">Group Name</TableHead>
<TableHead className="px-4">Code</TableHead>
<TableHead className="px-4">Task Lists</TableHead>
<TableHead className="px-4" />
</TableRow>
</TableHeader>
<TableBody>
{groups.map((g, i) => {
const taskListCount = Number(g.task_list_count ?? g.taskLists?.length ?? 0);
return (
<TableRow key={g.group_id}>
<TableCell className="text-center px-4 text-muted-foreground tabular-nums">
{i + 1}
</TableCell>
<TableCell className="px-4 font-medium">{g.name}</TableCell>
<TableCell className="px-4">
<Badge variant="outline" className="font-mono text-xs">{g.group_code}</Badge>
</TableCell>
<TableCell className="px-4">
{taskListCount}
</TableCell>
<TableCell className="px-4 text-right">
<Button size="sm" variant="outline" onClick={() => onView(g)}>
View
</Button>
</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</div>
);
const GroupsTableSkeleton = () => (
<div className="bg-card border rounded-xl overflow-hidden">
<Table>
<TableHeader>
<TableRow className="hover:bg-transparent">
<TableHead className="w-10 text-center px-4">#</TableHead>
<TableHead className="px-4">Group Name</TableHead>
<TableHead className="px-4">Code</TableHead>
<TableHead className="px-4 w-full text-center whitespace-nowrap">Task Lists</TableHead>
<TableHead className="px-4" />
</TableRow>
</TableHeader>
<TableBody>
{[1, 2].map((i) => (
<TableRow key={i}>
<TableCell className="text-center px-4"><Skeleton className="h-4 w-4 mx-auto" /></TableCell>
<TableCell className="px-4"><Skeleton className="h-4 w-32" /></TableCell>
<TableCell className="px-4"><Skeleton className="h-5 w-16 rounded-full" /></TableCell>
<TableCell className="px-4"><Skeleton className="h-4 w-8 mx-auto" /></TableCell>
<TableCell className="px-4 text-right"><Skeleton className="h-8 w-14 ml-auto" /></TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
);
// ─── 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: <Users className="size-4" /> },
];
// ── 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 (
<div>
<div className="my-20">
<div className="flex flex-col gap-8 justify-between lg:container lg:mx-auto xs:pt-2 lg:pt-8 lg:px-16 xs:px-4 sm:px-6">
{/* ── Hero Advertisement Carousel ── */}
{listLoading["dashboard.hero"] ? (
<HeroSkeleton />
) : (
<Hero ads={heroAds} onCtaClick={handleAdCtaClick} />
)}
{/* ── My Groups ── */}
<div className="flex flex-col gap-3">
<div className="flex items-center justify-between">
<AppBreadcrumb items={breadcrumbItems} />
{!groupLoading && groups.length > 0 && (
<span className="text-sm text-muted-foreground">
{groups.length} group{groups.length !== 1 ? 's' : ''}
</span>
)}
</div>
{groupLoading ? (
<GroupsTableSkeleton />
) : groups.length === 0 ? (
<p className="text-sm text-muted-foreground">You are not assigned to any group.</p>
) : (
<GroupsTable
groups={groups}
onView={(g) => navigate(`/group/${g.group_id}`)}
/>
)}
</div>
{/* ── Featured Courses (first 3) ── */}
<div className="flex flex-col gap-4">
<div className="w-full flex items-center justify-between">
<h1 className="text-2xl font-medium">Courses</h1>
<Button onClick={() => navigate(`/course`)}>View All</Button>
</div>
{coursesLoading ? (
<div className="grid lg:grid-cols-3 gap-4">
{Array.from({ length: 3 }).map((_, i) => (
<CourseCardSkeleton key={i} />
))}
</div>
) : featuredCourses.length === 0 ? (
<p className="text-sm text-muted-foreground">No courses available yet.</p>
) : (
<div className="grid lg:grid-cols-3 gap-4">
{featuredCourses.map((course) => (
<CourseCard
key={course.course_id}
course={course}
onViewDetails={handleViewDetails}
/>
))}
</div>
)}
</div>
{/* ── Featured Units (first 3) ── */}
<div className="flex flex-col gap-4">
<div className="w-full flex items-center justify-between">
<h1 className="text-2xl font-medium">Units</h1>
<Button onClick={() => navigate(`/units`)}>View All</Button>
</div>
{unitsLoading ? (
<div className="grid lg:grid-cols-3 gap-4">
{Array.from({ length: 3 }).map((_, i) => (
<UnitCardSkeleton key={i} />
))}
</div>
) : featuredUnits.length === 0 ? (
<p className="text-sm text-muted-foreground">No units available yet.</p>
) : (
<div className="grid lg:grid-cols-3 gap-4">
{featuredUnits.map((unit) => (
<UnitCard
key={unit.unit_id}
unit={unit}
tierMap={tierMap}
onViewDetails={handleViewUnitDetails}
/>
))}
</div>
)}
</div>
{/* ── Featured Lessons (first 3) ── */}
<div className="flex flex-col gap-4">
<div className="w-full flex items-center justify-between">
<h1 className="text-2xl font-medium">Lessons</h1>
<Button onClick={() => navigate(`/lessons`)}>View All</Button>
</div>
{lessonsLoading ? (
<div className="grid lg:grid-cols-3 gap-4">
{Array.from({ length: 3 }).map((_, i) => (
<LessonCardSkeleton key={i} />
))}
</div>
) : featuredLessons.length === 0 ? (
<p className="text-sm text-muted-foreground">No lessons available yet.</p>
) : (
<div className="grid lg:grid-cols-3 gap-4">
{featuredLessons.map((lesson) => (
<LessonCard
key={lesson.lesson_id}
lesson={lesson}
tierMap={tierMap}
onViewDetails={handleViewLessonDetails}
/>
))}
</div>
)}
</div>
</div>
</div>
{/* ── Upsell Modal — only for locked lessons ── */}
<LessonUpsellModal
open={lessonModalOpen}
onOpenChange={setLessonModalOpen}
lesson={selectedLesson}
tierMap={tierMap}
/>
</div>
);
};
export default Client;