merge new_starr_app@qas history into apps/web
git-subtree-dir: apps/web git-subtree-mainline:eaa6d2276agit-subtree-split:459af9d46a
This commit is contained in:
@@ -0,0 +1,300 @@
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useState, useEffect, useRef } from "react";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { BookOpen, Tag, CheckCheck, RefreshCcw, Lock, Zap, Info } from "lucide-react";
|
||||
import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
|
||||
import { Progress } from "@/components/ui/progress";
|
||||
import ResponsiveModal from "@/components/generic/ResponsiveModal";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { SendHorizonal } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
import api from "@/utils/api.util";
|
||||
import { useClientTiers } from "@/contexts/ClientTiersProvider";
|
||||
import { resolveTierBadge } from "@/utils/tierBadge.util";
|
||||
|
||||
const LVL_LABEL = { beginner: 'Beginner', intermediate: 'Intermediate', advanced: 'Advanced' };
|
||||
|
||||
function TierBadge({ tier, locked = false }) {
|
||||
const { tierMap } = useClientTiers();
|
||||
const { rank, label, cls } = resolveTierBadge(tier ?? 'free', tierMap);
|
||||
if (rank === 0 && !locked) return null;
|
||||
return (
|
||||
<Badge className={`gap-1 ${cls} w-fit shrink-0`}>
|
||||
{rank > 0 ? <Lock className="size-3" /> : <Tag className="size-3" />}
|
||||
{label}
|
||||
</Badge>
|
||||
);
|
||||
}
|
||||
|
||||
const ReadCourse = ({ title = "Read Course", courses = [], groupId, taskListId, taskId }) => {
|
||||
const navigate = useNavigate();
|
||||
const [selected, setSelected] = useState(null);
|
||||
const [details, setDetails] = useState({});
|
||||
const [summaries, setSummaries] = useState({});
|
||||
const [locked, setLocked] = useState({});
|
||||
const [lockedInfo, setLockedInfo] = useState({});
|
||||
const [fetching, setFetching] = useState({});
|
||||
const prevCompletedRef = useRef({});
|
||||
|
||||
useEffect(() => {
|
||||
courses.forEach((course) => {
|
||||
const prev = prevCompletedRef.current[course.id];
|
||||
if (course.completed && prev === false) {
|
||||
toast(`"${course.title}" has been automatically turned in!`);
|
||||
}
|
||||
prevCompletedRef.current[course.id] = !!course.completed;
|
||||
});
|
||||
}, [courses]);
|
||||
|
||||
useEffect(() => {
|
||||
courses.forEach(async (course) => {
|
||||
if (!course.reference_id) return;
|
||||
setFetching((prev) => ({ ...prev, [course.reference_id]: true }));
|
||||
try {
|
||||
const res = await api.get(`/client/courses/uuid/${course.reference_id}`);
|
||||
const d = res.data?.data;
|
||||
if (!d) return;
|
||||
setDetails((prev) => ({ ...prev, [course.reference_id]: d }));
|
||||
|
||||
const progRes = await api.get(`/client/courses/${d.course_id}/progress/summary`);
|
||||
const summary = progRes.data?.data;
|
||||
if (summary) setSummaries((prev) => ({ ...prev, [course.reference_id]: summary }));
|
||||
} catch (err) {
|
||||
if (err?.response?.status === 403) {
|
||||
setLocked((prev) => ({ ...prev, [course.reference_id]: true }));
|
||||
const courseData = err.response?.data?.course;
|
||||
if (courseData) setLockedInfo((prev) => ({ ...prev, [course.reference_id]: courseData }));
|
||||
} else {
|
||||
console.error('[ReadCourse] fetch failed:', err?.response?.status, err?.message);
|
||||
}
|
||||
} finally {
|
||||
setFetching((prev) => ({ ...prev, [course.reference_id]: false }));
|
||||
}
|
||||
});
|
||||
}, []);
|
||||
|
||||
const getReadingPercent = (course) => {
|
||||
const summary = summaries[course.reference_id];
|
||||
return summary ? summary.percent : (course.progress ?? 0);
|
||||
};
|
||||
|
||||
const hasLocked = Object.values(locked).some(Boolean);
|
||||
|
||||
return (
|
||||
<div className="border rounded-lg bg-card overflow-hidden">
|
||||
{/* Header */}
|
||||
<div className="px-6 py-4 border-b flex items-center gap-2">
|
||||
<BookOpen className="size-4 text-muted-foreground" />
|
||||
<h2 className="font-semibold text-sm">{title}</h2>
|
||||
<Badge variant="secondary" className="ml-auto">{courses.length}</Badge>
|
||||
</div>
|
||||
|
||||
{/* Subscription advisory */}
|
||||
{hasLocked && (
|
||||
<div className="flex items-start gap-3 px-5 py-3.5 border-b bg-amber-50 dark:bg-amber-950/30 text-amber-800 dark:text-amber-300">
|
||||
<Info className="size-4 mt-0.5 shrink-0" />
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="text-sm font-medium leading-snug">Subscription Required</p>
|
||||
<p className="text-xs mt-0.5 text-amber-700 dark:text-amber-400 leading-relaxed">
|
||||
To complete this activity, subscribe to one of our available tier plans.
|
||||
</p>
|
||||
</div>
|
||||
<Button size="sm" variant="outline" className="shrink-0 border-amber-300 dark:border-amber-700 text-amber-800 dark:text-amber-300 hover:bg-amber-100 dark:hover:bg-amber-900/40" onClick={() => navigate('/subscriptions')}>
|
||||
<Zap className="size-3.5" /> View Plans
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<ScrollArea className="w-full bg-muted overflow-hidden">
|
||||
<div className="flex gap-4 p-4">
|
||||
{courses.map((course) => {
|
||||
const info = details[course.reference_id];
|
||||
const courseInfo = lockedInfo[course.reference_id];
|
||||
const percent = getReadingPercent(course);
|
||||
const done = !!course.completed;
|
||||
const allRead = !done && percent >= 100;
|
||||
const isLocked = locked[course.reference_id];
|
||||
const isFetching = fetching[course.reference_id];
|
||||
|
||||
// ── Locked card ───────────────────────────────────────────────────
|
||||
// Skipped when already completed — a completion recorded while the
|
||||
// content was accessible still counts even if it re-locks later
|
||||
// (task_progress/reading-progress rows are never revoked on a tier
|
||||
// change), so it must keep showing as Completed, not Locked.
|
||||
if (isLocked && !done) {
|
||||
return (
|
||||
<div
|
||||
key={course.id}
|
||||
onClick={() => navigate('/subscriptions')}
|
||||
className="bg-card rounded-2xl border border-dashed p-4 flex flex-col gap-3 hover:bg-muted/60 hover:shadow-sm transition-all cursor-pointer group w-96 shrink-0 opacity-80"
|
||||
>
|
||||
<div className="flex items-center gap-1.5">
|
||||
<BookOpen className="size-3.5 text-muted-foreground shrink-0" />
|
||||
<span className="text-xs text-muted-foreground font-medium">Course</span>
|
||||
<div className="ml-auto">
|
||||
<TierBadge tier={courseInfo?.subscription} locked />
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<h1 className="text-base font-semibold leading-snug line-clamp-2 text-muted-foreground">
|
||||
{course.title}
|
||||
</h1>
|
||||
<p className="text-sm text-muted-foreground mt-1 line-clamp-2">
|
||||
This course requires a higher subscription plan.
|
||||
</p>
|
||||
</div>
|
||||
<div className="mt-auto">
|
||||
<Button size="sm" className="w-full gap-1.5" onClick={(e) => { e.stopPropagation(); navigate('/subscriptions'); }}>
|
||||
<Zap className="size-3.5" /> Upgrade to unlock
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ── Normal card ───────────────────────────────────────────────────
|
||||
return (
|
||||
<div
|
||||
key={course.id}
|
||||
onClick={() => !isFetching && setSelected(course)}
|
||||
className={`bg-card rounded-2xl border p-4 flex flex-col gap-3 transition-all w-96 shrink-0 ${
|
||||
isFetching
|
||||
? 'opacity-60 cursor-wait'
|
||||
: 'hover:bg-muted/60 hover:shadow-sm cursor-pointer group'
|
||||
}`}
|
||||
>
|
||||
{/* Card type row */}
|
||||
<div className="flex items-center gap-1.5">
|
||||
<BookOpen className="size-3.5 text-muted-foreground shrink-0" />
|
||||
<span className="text-xs text-muted-foreground font-medium">Course</span>
|
||||
<div className="ml-auto flex items-center gap-1.5">
|
||||
{info?.subscription && <TierBadge tier={info.subscription} />}
|
||||
{info?.level && (
|
||||
<Badge variant="outline" className="text-xs capitalize">
|
||||
{LVL_LABEL[info.level] ?? info.level}
|
||||
</Badge>
|
||||
)}
|
||||
{!info && course.reference_id && (
|
||||
<Badge variant="secondary" className="opacity-40 text-xs">Loading…</Badge>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h1
|
||||
onClick={(e) => {
|
||||
if (!info?.course_id) return;
|
||||
e.stopPropagation();
|
||||
navigate(`/course/${info.course_id}/unit`, {
|
||||
state: taskId ? { taskCtx: { has_task: true, groupId, taskListId, taskId } } : {},
|
||||
});
|
||||
}}
|
||||
className="text-base font-semibold leading-snug line-clamp-2 transition-colors text-blue-600 dark:text-blue-400 hover:underline cursor-pointer"
|
||||
>
|
||||
{course.title}
|
||||
</h1>
|
||||
<p className="text-sm text-muted-foreground leading-relaxed line-clamp-2 mt-1">
|
||||
{info?.description ?? ''}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col gap-2 mt-auto pt-1 border-t">
|
||||
<div className="flex items-center justify-between text-sm pt-1">
|
||||
<span className="flex items-center gap-1.5 text-muted-foreground">
|
||||
{done
|
||||
? <><CheckCheck className="size-4 text-green-500" /><span className="text-green-600 dark:text-green-400 font-medium">Completed</span></>
|
||||
: allRead
|
||||
? <><CheckCheck className="size-4 text-amber-500" /><span className="text-amber-600 dark:text-amber-400 font-medium">Lessons Done</span></>
|
||||
: <><RefreshCcw className="size-4" /> In Progress</>
|
||||
}
|
||||
</span>
|
||||
<span className="font-semibold text-xs">{percent}%</span>
|
||||
</div>
|
||||
<Progress
|
||||
value={percent}
|
||||
className={`h-1.5 ${done ? "[&>div]:bg-green-500" : allRead ? "[&>div]:bg-amber-500" : ""}`}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
<ScrollBar orientation="horizontal" />
|
||||
</ScrollArea>
|
||||
|
||||
{/* Detail modal */}
|
||||
{selected && (() => {
|
||||
const info = details[selected.reference_id];
|
||||
const percent = getReadingPercent(selected);
|
||||
const done = !!selected.completed;
|
||||
const allRead = !done && percent >= 100;
|
||||
|
||||
return (
|
||||
<ResponsiveModal
|
||||
open={!!selected}
|
||||
onOpenChange={(o) => !o && setSelected(null)}
|
||||
title={selected.title}
|
||||
description={done ? "Course Summary" : "Course Info"}
|
||||
footer={
|
||||
<>
|
||||
<Button variant="outline" onClick={() => setSelected(null)}>Cancel</Button>
|
||||
<Button
|
||||
onClick={() => {
|
||||
if (!info?.course_id) return;
|
||||
navigate(`/course/${info.course_id}/unit`, {
|
||||
state: {
|
||||
...(allRead ? { seekFirstIncomplete: true } : {}),
|
||||
...(taskId ? { taskCtx: { has_task: true, groupId, taskListId, taskId } } : {}),
|
||||
},
|
||||
});
|
||||
}}
|
||||
disabled={done || !info?.course_id}
|
||||
>
|
||||
<SendHorizonal /> Proceed
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<div className="flex flex-col gap-5">
|
||||
{done ? (
|
||||
<div className="flex items-center gap-2 bg-green-500/10 text-green-600 dark:text-green-400 rounded-lg px-4 py-3 text-sm font-medium">
|
||||
<CheckCheck className="size-4 shrink-0" /> Automatically Turned-in
|
||||
</div>
|
||||
) : allRead ? (
|
||||
<div className="flex items-center gap-2 bg-amber-500/10 text-amber-600 dark:text-amber-400 rounded-lg px-4 py-3 text-sm font-medium">
|
||||
<CheckCheck className="size-4 shrink-0" /> Lessons complete — finish quizzes & assessment to turn in
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex items-center gap-2 bg-blue-500/10 text-blue-600 dark:text-blue-400 rounded-lg px-4 py-3 text-sm font-medium">
|
||||
<RefreshCcw className="size-4 shrink-0" /> Currently in progress
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-2 divide-x rounded-lg border text-center text-sm">
|
||||
<div className="flex flex-col gap-1 py-4">
|
||||
<span className="text-xl font-bold">
|
||||
{info?.subscription ? (info.subscription.charAt(0).toUpperCase() + info.subscription.slice(1)) : '—'}
|
||||
</span>
|
||||
<span className="text-muted-foreground text-xs">Subscription</span>
|
||||
</div>
|
||||
<div className="flex flex-col gap-1 py-4">
|
||||
<span className="text-xl font-bold">
|
||||
{info?.level ? (LVL_LABEL[info.level] ?? info.level) : '—'}
|
||||
</span>
|
||||
<span className="text-muted-foreground text-xs">Level</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col gap-1">
|
||||
<p className="text-xs text-muted-foreground uppercase tracking-wide font-medium">About this course</p>
|
||||
<p className="text-sm leading-relaxed">{info?.description ?? '—'}</p>
|
||||
</div>
|
||||
</div>
|
||||
</ResponsiveModal>
|
||||
);
|
||||
})()}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ReadCourse;
|
||||
Reference in New Issue
Block a user