|
| 1 | +import Image from 'next/image'; |
| 2 | +import { categoryTabStyles } from '@/data/categoryStyles'; |
| 3 | + |
| 4 | +import { ArrowLeft, CheckCircle, RotateCcw, SearchX } from 'lucide-react'; |
| 5 | +import { getTranslations } from 'next-intl/server'; |
| 6 | +import { cn } from '@/lib/utils'; |
| 7 | + |
| 8 | +import { QuizReviewList } from '@/components/dashboard/QuizReviewList'; |
| 9 | +import { DynamicGridBackground } from '@/components/shared/DynamicGridBackground'; |
| 10 | +import { getAttemptReviewDetails } from '@/db/queries/quiz'; |
| 11 | +import { Link, redirect } from '@/i18n/routing'; |
| 12 | +import { getCurrentUser } from '@/lib/auth'; |
| 13 | + |
| 14 | +export async function generateMetadata({ |
| 15 | + params, |
| 16 | +}: { |
| 17 | + params: Promise<{ locale: string }>; |
| 18 | +}) { |
| 19 | + const { locale } = await params; |
| 20 | + const t = await getTranslations({ locale, namespace: 'dashboard.quizReview' }); |
| 21 | + |
| 22 | + return { |
| 23 | + title: t('title'), |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +export default async function QuizReviewPage({ |
| 28 | + params, |
| 29 | +}: { |
| 30 | + params: Promise<{ locale: string; attemptId: string }>; |
| 31 | +}) { |
| 32 | + const session = await getCurrentUser(); |
| 33 | + const { locale, attemptId } = await params; |
| 34 | + |
| 35 | + if (!session) { |
| 36 | + redirect({ href: '/login', locale }); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const t = await getTranslations('dashboard.quizReview'); |
| 41 | + const review = await getAttemptReviewDetails(attemptId, session.id, locale); |
| 42 | + |
| 43 | + const cardStyles = |
| 44 | + 'relative overflow-hidden rounded-2xl border border-gray-100 dark:border-white/5 bg-white/60 dark:bg-neutral-900/60 backdrop-blur-xl p-6 sm:p-8'; |
| 45 | + |
| 46 | + const btnOutline = |
| 47 | + 'inline-flex items-center gap-2 rounded-full border border-gray-200 dark:border-white/10 bg-white/50 dark:bg-neutral-900/50 backdrop-blur-sm px-6 py-2.5 text-sm font-medium text-gray-600 dark:text-gray-300 transition-colors hover:bg-white hover:text-[var(--accent-primary)] dark:hover:bg-neutral-800 dark:hover:text-[var(--accent-primary)]'; |
| 48 | + |
| 49 | + const btnPrimary = |
| 50 | + 'inline-flex items-center gap-2 rounded-full px-6 py-2.5 text-sm font-semibold text-white bg-[var(--accent-primary)] hover:bg-[var(--accent-hover)] transition-all hover:scale-105'; |
| 51 | + |
| 52 | + // Not found / not owned |
| 53 | + if (!review) { |
| 54 | + return ( |
| 55 | + <DynamicGridBackground className="min-h-screen bg-gray-50 py-10 dark:bg-transparent"> |
| 56 | + <main className="relative z-10 mx-auto max-w-4xl px-4 sm:px-6"> |
| 57 | + <div className={`${cardStyles} text-center`}> |
| 58 | + <SearchX className="mx-auto mb-4 h-10 w-10 text-gray-400" /> |
| 59 | + <h2 className="mb-2 text-xl font-bold text-gray-900 dark:text-white"> |
| 60 | + {t('notFound')} |
| 61 | + </h2> |
| 62 | + <Link href="/dashboard" className={btnOutline}> |
| 63 | + <ArrowLeft className="h-4 w-4" /> |
| 64 | + {t('backToDashboard')} |
| 65 | + </Link> |
| 66 | + </div> |
| 67 | + </main> |
| 68 | + </DynamicGridBackground> |
| 69 | + ); |
| 70 | + } |
| 71 | + const slug = review.categorySlug; |
| 72 | + const categoryStyle = |
| 73 | + slug && slug in categoryTabStyles |
| 74 | + ? categoryTabStyles[slug as keyof typeof categoryTabStyles] |
| 75 | + : null; |
| 76 | + |
| 77 | + const incorrectCount = review.incorrectQuestions.length; |
| 78 | + |
| 79 | + // All correct |
| 80 | + if (incorrectCount === 0) { |
| 81 | + return ( |
| 82 | + <DynamicGridBackground className="min-h-screen bg-gray-50 py-10 dark:bg-transparent"> |
| 83 | + <main className="relative z-10 mx-auto max-w-4xl px-4 sm:px-6"> |
| 84 | + <div className={`${cardStyles} text-center`}> |
| 85 | + <CheckCircle className="mx-auto mb-4 h-10 w-10 text-emerald-500" /> |
| 86 | + <h2 className="mb-2 text-xl font-bold text-gray-900 dark:text-white"> |
| 87 | + {t('allCorrect')} |
| 88 | + </h2> |
| 89 | + <p className="mb-6 text-gray-500 dark:text-gray-400"> |
| 90 | + {t('allCorrectHint')} |
| 91 | + </p> |
| 92 | + <div className="flex flex-col items-center gap-3 sm:flex-row sm:justify-center"> |
| 93 | + <Link href="/dashboard" className={btnOutline}> |
| 94 | + <ArrowLeft className="h-4 w-4" /> |
| 95 | + {t('backToDashboard')} |
| 96 | + </Link> |
| 97 | + <Link href={`/quiz/${review.quizSlug}`} className={btnPrimary}> |
| 98 | + <RotateCcw className="h-4 w-4" /> |
| 99 | + {t('retakeQuiz')} |
| 100 | + </Link> |
| 101 | + </div> |
| 102 | + </div> |
| 103 | + </main> |
| 104 | + </DynamicGridBackground> |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + // Main review |
| 109 | + return ( |
| 110 | + <DynamicGridBackground className="min-h-screen bg-gray-50 py-10 dark:bg-transparent"> |
| 111 | + <main className="relative z-10 mx-auto max-w-4xl px-4 sm:px-6"> |
| 112 | + <header className="mb-8"> |
| 113 | + <div className="flex items-center gap-3"> |
| 114 | + {categoryStyle && ( |
| 115 | + <span className="relative h-8 w-8 shrink-0 sm:h-10 sm:w-10"> |
| 116 | + <Image |
| 117 | + src={categoryStyle.icon} |
| 118 | + alt="" |
| 119 | + fill |
| 120 | + className={cn('object-contain', 'iconClassName' in categoryStyle && categoryStyle.iconClassName)} |
| 121 | + /> |
| 122 | + </span> |
| 123 | + )} |
| 124 | + <h1 className="text-3xl font-bold text-gray-900 dark:text-gray-100"> |
| 125 | + {review.quizTitle ?? review.quizSlug} |
| 126 | + </h1> |
| 127 | + </div> |
| 128 | + <p className="mt-2 text-lg text-gray-600 dark:text-gray-400"> |
| 129 | + {t('title')} —{' '} |
| 130 | + {t('subtitle', { |
| 131 | + incorrect: incorrectCount, |
| 132 | + total: review.totalQuestions, |
| 133 | + })} |
| 134 | + </p> |
| 135 | + </header> |
| 136 | + <QuizReviewList |
| 137 | + questions={review.incorrectQuestions} |
| 138 | + accentColor={categoryStyle?.accent} |
| 139 | + /> |
| 140 | + <div className="mt-8 flex flex-col items-center gap-3 sm:flex-row sm:justify-center"> |
| 141 | + <Link href="/dashboard" className={btnOutline}> |
| 142 | + <ArrowLeft className="h-4 w-4" /> |
| 143 | + {t('backToDashboard')} |
| 144 | + </Link> |
| 145 | + <Link href={`/quiz/${review.quizSlug}`} className={btnPrimary}> |
| 146 | + <RotateCcw className="h-4 w-4" /> |
| 147 | + {t('retakeQuiz')} |
| 148 | + </Link> |
| 149 | + </div> |
| 150 | + </main> |
| 151 | + </DynamicGridBackground> |
| 152 | + ); |
| 153 | +} |
0 commit comments