codepractice-ai / components /QuestionDisplay.tsx
Mahdi Naser Moghadasi
Make coding practice page mobile-responsive
26704d7
Raw History Blame Contribute Delete
7.84 kB
'use client'
import { useState } from 'react'
import { motion, AnimatePresence } from 'framer-motion'
import { Lightbulb, ChevronDown, ChevronUp, CheckCircle, XCircle, Clock, Circle } from 'lucide-react'
import { type Question } from '@/lib/questions'
import { getProblemStatus } from '@/lib/progress'
import ReactMarkdown from 'react-markdown'
interface QuestionDisplayProps {
question: Question
}
export default function QuestionDisplay({ question }: QuestionDisplayProps) {
const [showHints, setShowHints] = useState(false)
const [hintIndex, setHintIndex] = useState(0)
const problemStatus = getProblemStatus(question.id)
const difficultyColors = {
Easy: 'bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-400',
Medium: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900/30 dark:text-yellow-400',
Hard: 'bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-400',
}
const statusConfig = {
solved: {
icon: CheckCircle,
color: 'text-green-600 dark:text-green-400',
bg: 'bg-green-50 dark:bg-green-900/20',
border: 'border-green-200 dark:border-green-800',
label: 'Solved',
},
attempted: {
icon: Clock,
color: 'text-yellow-600 dark:text-yellow-400',
bg: 'bg-yellow-50 dark:bg-yellow-900/20',
border: 'border-yellow-200 dark:border-yellow-800',
label: 'Attempted',
},
not_started: {
icon: Circle,
color: 'text-gray-400 dark:text-gray-500',
bg: 'bg-gray-50 dark:bg-gray-800',
border: 'border-gray-200 dark:border-gray-700',
label: 'Not Started',
},
}
const status = statusConfig[problemStatus]
const StatusIcon = status.icon
return (
<div className="h-full overflow-y-auto">
<div className="p-4 sm:p-6 space-y-4 sm:space-y-6">
{/* Header */}
<div>
<div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3 sm:gap-0 mb-4">
<h2 className="text-xl sm:text-2xl font-bold text-gray-800 dark:text-white">
{question.title}
</h2>
<div className="flex items-center gap-2 flex-wrap">
{/* Status Indicator - Only show if Solved */}
{problemStatus === 'solved' && (
<div className={`flex items-center gap-1.5 px-2 sm:px-3 py-1 rounded-full text-xs sm:text-sm font-medium border ${status.bg} ${status.border}`}>
<StatusIcon className={`w-3 h-3 sm:w-4 sm:h-4 ${status.color}`} />
<span className={status.color}>{status.label}</span>
</div>
)}
<span className={`px-2 sm:px-3 py-1 rounded-full text-xs sm:text-sm font-semibold ${difficultyColors[question.difficulty]}`}>
{question.difficulty}
</span>
<span className="px-2 sm:px-3 py-1 rounded-full text-xs sm:text-sm font-medium bg-primary-100 text-primary-800 dark:bg-primary-900/30 dark:text-primary-400">
{question.topic}
</span>
</div>
</div>
</div>
{/* Description */}
<div className="prose dark:prose-invert max-w-none">
<ReactMarkdown className="text-gray-700 dark:text-gray-300 leading-relaxed">
{question.description}
</ReactMarkdown>
</div>
{/* Examples */}
<div>
<h3 className="text-base sm:text-lg font-semibold text-gray-800 dark:text-white mb-3">Examples:</h3>
<div className="space-y-4">
{question.examples.map((example, index) => (
<motion.div
key={index}
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: index * 0.1 }}
className="bg-gray-100 dark:bg-gray-800 rounded-lg p-4 border-l-4 border-primary-500"
>
<div className="space-y-2">
<div>
<span className="text-sm font-semibold text-gray-600 dark:text-gray-400">Input: </span>
<code className="text-sm bg-gray-200 dark:bg-gray-700 px-2 py-1 rounded">
{example.input}
</code>
</div>
<div>
<span className="text-sm font-semibold text-gray-600 dark:text-gray-400">Output: </span>
<code className="text-sm bg-gray-200 dark:bg-gray-700 px-2 py-1 rounded">
{example.output}
</code>
</div>
{example.explanation && (
<div>
<span className="text-sm font-semibold text-gray-600 dark:text-gray-400">Explanation: </span>
<span className="text-sm text-gray-700 dark:text-gray-300">{example.explanation}</span>
</div>
)}
</div>
</motion.div>
))}
</div>
</div>
{/* Hints */}
<div>
<motion.button
whileHover={{ scale: 1.02 }}
whileTap={{ scale: 0.98 }}
onClick={() => setShowHints(!showHints)}
className="flex items-center gap-2 w-full px-3 sm:px-4 py-2 sm:py-3 bg-yellow-50 dark:bg-yellow-900/20 border border-yellow-200 dark:border-yellow-800 rounded-lg hover:bg-yellow-100 dark:hover:bg-yellow-900/30 transition-colors"
>
<Lightbulb className="w-4 h-4 sm:w-5 sm:h-5 text-yellow-600 dark:text-yellow-400" />
<span className="text-sm sm:text-base font-semibold text-yellow-800 dark:text-yellow-300">
{showHints ? 'Hide Hints' : 'Show Hints'}
</span>
{showHints ? (
<ChevronUp className="w-5 h-5 text-yellow-600 dark:text-yellow-400 ml-auto" />
) : (
<ChevronDown className="w-5 h-5 text-yellow-600 dark:text-yellow-400 ml-auto" />
)}
</motion.button>
<AnimatePresence>
{showHints && (
<motion.div
initial={{ opacity: 0, height: 0 }}
animate={{ opacity: 1, height: 'auto' }}
exit={{ opacity: 0, height: 0 }}
className="mt-4 space-y-3"
>
{question.hints.slice(0, hintIndex + 1).map((hint, index) => (
<motion.div
key={index}
initial={{ opacity: 0, x: -10 }}
animate={{ opacity: 1, x: 0 }}
transition={{ delay: index * 0.1 }}
className="flex items-start gap-3 p-4 bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg"
>
<span className="flex-shrink-0 w-6 h-6 rounded-full bg-blue-500 text-white flex items-center justify-center text-sm font-bold">
{index + 1}
</span>
<p className="text-sm text-gray-700 dark:text-gray-300 flex-1">{hint}</p>
</motion.div>
))}
{hintIndex < question.hints.length - 1 && (
<motion.button
whileHover={{ scale: 1.02 }}
whileTap={{ scale: 0.98 }}
onClick={() => setHintIndex(hintIndex + 1)}
className="w-full px-4 py-2 bg-blue-100 dark:bg-blue-900/30 text-blue-700 dark:text-blue-300 rounded-lg font-medium hover:bg-blue-200 dark:hover:bg-blue-900/40 transition-colors"
>
Show Next Hint
</motion.button>
)}
</motion.div>
)}
</AnimatePresence>
</div>
</div>
</div>
)
}