Files
pwa/src/pages/Account/AccountPage.tsx
T

231 lines
8.3 KiB
TypeScript
Raw Normal View History

import type { ReactNode } from 'react';
2025-11-18 20:22:25 +03:30
import { useAuth } from '@/hooks/useAuth';
import { useLearningJourney } from '@/hooks/useLearningJourney';
import { useCurrentSession } from '@/hooks/useCurrentSession';
import { PROGRAM_SESSION_COUNT } from '@/data/program/sessions';
import { getProgramProgress } from '@/services/programService';
import { Progress } from '@/components/ui/progress';
import { Navigate, useNavigate } from 'react-router-dom';
import { Check, ChevronLeft, Loader2, Lock } from 'lucide-react';
import { cn } from '@/lib/utils';
import { StaggeredList, StaggerItem, AnimationWrapper } from '@/components/motion';
import { motion } from 'framer-motion';
import { fastTransition } from '@/lib/motion';
2025-11-18 20:22:25 +03:30
type StepState = 'open' | 'locked' | 'done';
/**
* Course Status surface (profile menu → وضعیت دوره).
* Journey status only — no settings, scores, or assessment results.
*/
2025-11-18 20:22:25 +03:30
export default function AccountPage() {
const { effectiveUserId, isGuest, guestSessionHydrated } = useAuth();
const { progress, loading, permissions } = useLearningJourney(effectiveUserId ?? undefined);
const { currentSession, isProgramComplete, programProgress } = useCurrentSession(effectiveUserId);
2025-11-18 20:22:25 +03:30
const navigate = useNavigate();
if (!guestSessionHydrated || loading) {
return (
<div className="flex min-h-[50vh] items-center justify-center">
<Loader2 className="h-8 w-8 animate-spin text-neon-cyan" />
</div>
);
}
2025-11-18 20:22:25 +03:30
if (!effectiveUserId) {
return <Navigate to="/login" replace />;
}
const cachedProgress = getProgramProgress(effectiveUserId);
const effectiveProgramProgress = programProgress ?? cachedProgress;
const completedSessions = effectiveProgramProgress.completedSessions.length;
const programDone =
isProgramComplete ||
Boolean(effectiveProgramProgress.completedAt) ||
completedSessions >= PROGRAM_SESSION_COUNT;
const progressPercent = programDone
? 100
: Math.round((completedSessions / PROGRAM_SESSION_COUNT) * 100);
const preDone = Boolean(progress?.pre_test_completed_at);
const trainingDone = Boolean(progress?.training_completed_at) || programDone;
const postDone = Boolean(progress?.post_test_completed_at);
const displaySessionOrder =
currentSession?.order ??
(programDone ? PROGRAM_SESSION_COUNT : Math.max(effectiveProgramProgress.currentSessionOrder, 1));
const preState: StepState = preDone ? 'done' : 'open';
const trainingState: StepState = !permissions.canOpenTraining
? 'locked'
: trainingDone
? 'done'
: 'open';
const postState: StepState = postDone
? 'done'
: permissions.canTakePostTest || (trainingDone && preDone && !postDone)
? 'open'
: 'locked';
2025-11-18 20:22:25 +03:30
return (
<div className="h-full min-h-0 flex-1 overflow-y-auto px-4 py-6">
<div className="mx-auto w-full max-w-md space-y-4">
<AnimationWrapper immediate className="space-y-1 pb-2">
<h1 className="text-2xl font-bold tracking-tight text-foreground">وضعیت دوره</h1>
{isGuest && <p className="text-sm text-body">جلسه مهمان</p>}
</AnimationWrapper>
2025-11-18 20:22:25 +03:30
<StaggeredList itemCount={3} className="space-y-3" role="region" aria-label="وضعیت دوره">
<StaggerItem>
<StatusRow
title="پیش‌آزمون"
state={preState}
onOpen={() => navigate('/assessment/pre')}
lockedHint={preDone ? 'انجام شده — قابل تکرار نیست' : undefined}
>
{preDone ? (
<StatusDone label="انجام شده" />
) : (
<p className="text-sm text-body">برای شروع مسیر، پیش‌آزمون را انجام دهید</p>
)}
</StatusRow>
</StaggerItem>
2025-11-18 20:22:25 +03:30
<StaggerItem>
<StatusRow
title="دوره آموزشی"
state={trainingState}
onOpen={() => navigate('/chat')}
lockedHint={
trainingDone ? 'دوره تکمیل شده' : 'پس از انجام پیش‌آزمون فعال می‌شود'
}
>
{trainingDone ? (
<StatusDone label="دوره تکمیل شده" />
) : !permissions.canOpenTraining ? (
<p className="flex items-start gap-2 text-sm text-body">
<Lock className="mt-0.5 h-3.5 w-3.5 shrink-0 text-muted-foreground" aria-hidden />
<span>پس از انجام پیش‌آزمون فعال می‌شود</span>
</p>
) : (
<div className="space-y-2">
<p className="text-sm font-medium text-foreground">
جلسه {displaySessionOrder} از {PROGRAM_SESSION_COUNT}
</p>
<div className="flex items-center gap-3">
<Progress value={progressPercent} className="h-2.5 flex-1" />
<span className="min-w-[2.5rem] text-left text-sm tabular-nums text-body">
{progressPercent}٪
</span>
2025-11-18 20:22:25 +03:30
</div>
</div>
)}
</StatusRow>
</StaggerItem>
2025-11-18 20:22:25 +03:30
<StaggerItem>
<StatusRow
title="پس‌آزمون"
state={postState}
onOpen={() => navigate('/assessment/post')}
lockedHint={
postDone
? 'انجام شده — قابل تکرار نیست'
: 'پس از پایان دوره آموزشی فعال می‌شود'
}
>
{postState === 'done' && <StatusDone label="انجام شده" />}
{postState === 'open' && (
<p className="flex items-center gap-2 text-sm font-medium text-foreground">
<span
className="inline-block h-2 w-2 shrink-0 rounded-full bg-neon-teal shadow-[0_0_8px_rgba(52,184,150,0.7)]"
aria-hidden
/>
آماده انجام
</p>
)}
{postState === 'locked' && (
<p className="flex items-start gap-2 text-sm text-body">
<Lock className="mt-0.5 h-4 w-4 shrink-0 text-muted-foreground" aria-hidden />
<span>پس از پایان دوره فعال می‌شود</span>
</p>
)}
</StatusRow>
</StaggerItem>
</StaggeredList>
2025-11-18 20:22:25 +03:30
</div>
</div>
);
}
function StatusRow({
title,
children,
state,
onOpen,
lockedHint,
}: {
title: string;
children: ReactNode;
state: StepState;
onOpen: () => void;
lockedHint?: string;
}) {
const isInteractive = state === 'open';
2025-11-18 20:22:25 +03:30
const content = (
<div className="flex items-start justify-between gap-3">
<div className="min-w-0 flex-1 space-y-2 text-right">
<h2 className="text-sm font-semibold text-foreground">{title}</h2>
{children}
</div>
{isInteractive ? (
<ChevronLeft className="mt-0.5 h-5 w-5 shrink-0 text-neon-cyan" aria-hidden />
) : (
<Lock
className={cn(
'mt-0.5 h-4 w-4 shrink-0',
state === 'done' ? 'text-neon-emerald/70' : 'text-muted-foreground'
)}
aria-hidden
/>
)}
</div>
);
2025-11-18 20:22:25 +03:30
if (isInteractive) {
return (
<motion.button
type="button"
onClick={onOpen}
whileHover={{ scale: 1.02 }}
whileTap={{ scale: 0.98 }}
transition={fastTransition}
className="glass-panel w-full px-4 py-4 text-right transition-colors duration-200 hover:border-neon-cyan/55 hover:shadow-[0_0_18px_rgba(72,180,200,0.22)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-neon-cyan/50"
aria-label={`${title} — باز کردن`}
>
{content}
</motion.button>
);
}
2025-11-18 20:22:25 +03:30
return (
<div
className="glass-panel w-full px-4 py-4 opacity-90"
aria-disabled="true"
title={lockedHint}
>
{content}
</div>
);
}
function StatusDone({ label }: { label: string }) {
return (
<p className="flex items-center gap-2 text-sm font-medium text-foreground">
<Check className="h-4 w-4 shrink-0 text-neon-emerald" aria-hidden />
{label}
</p>
);
}