|
| 1 | +import {useQuestion} from '../hooks/useQuestion'; |
| 2 | + |
| 3 | +const getDifficultyColor = (difficulty: string) => { |
| 4 | + switch (difficulty.toLowerCase()) { |
| 5 | + case 'easy': |
| 6 | + return 'bg-green-100 text-green-700'; |
| 7 | + case 'medium': |
| 8 | + return 'bg-yellow-100 text-yellow-700'; |
| 9 | + case 'hard': |
| 10 | + return 'bg-red-100 text-red-700'; |
| 11 | + default: |
| 12 | + return 'bg-gray-100 text-gray-700'; |
| 13 | + } |
| 14 | +}; |
| 15 | + |
| 16 | +const parseTextWithCode = (text: string) => { |
| 17 | + const parts = text.split(/(`[^`]+`)/g); |
| 18 | + return parts.map((part, index) => { |
| 19 | + if (part.startsWith('`') && part.endsWith('`')) { |
| 20 | + const code = part.slice(1, -1); |
| 21 | + return ( |
| 22 | + <code key={index} className="bg-gray-100 px-1 rounded"> |
| 23 | + {code} |
| 24 | + </code> |
| 25 | + ); |
| 26 | + } |
| 27 | + return <span key={index}>{part}</span>; |
| 28 | + }); |
| 29 | +}; |
| 30 | + |
| 31 | +export default function QuestionPanel({questionId}: {questionId: string}) { |
| 32 | + const {question, isLoading, error} = useQuestion(questionId); |
| 33 | + if (isLoading) { |
| 34 | + return ( |
| 35 | + <div className="w-96 bg-white border-r border-gray-200 overflow-y-auto p-6 flex items-center justify-center"> |
| 36 | + <div className="text-gray-500">Loading question...</div> |
| 37 | + </div> |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + if (error || !question) { |
| 42 | + return ( |
| 43 | + <div className="w-96 bg-white border-r border-gray-200 overflow-y-auto p-6"> |
| 44 | + <div className="text-red-500">{error || 'Question not found'}</div> |
| 45 | + </div> |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + return ( |
| 50 | + <div className="w-96 bg-white border-r border-gray-200 overflow-y-auto p-6"> |
| 51 | + <h1 className="text-2xl font-bold mb-4">{question.title}</h1> |
| 52 | + |
| 53 | + {/* Tags */} |
| 54 | + <div className="flex gap-2 mb-6"> |
| 55 | + <span |
| 56 | + className={`px-3 py-1 rounded-full text-sm font-medium ${getDifficultyColor(question.difficulty)}`} |
| 57 | + > |
| 58 | + {question.difficulty} |
| 59 | + </span> |
| 60 | + </div> |
| 61 | + |
| 62 | + {/* Problem Description */} |
| 63 | + <div className="mb-6"> |
| 64 | + <h2 className="font-semibold text-gray-900 mb-2">Problem Description</h2> |
| 65 | + <p className="text-gray-700 text-sm leading-relaxed"> |
| 66 | + {parseTextWithCode(question.description)} |
| 67 | + </p> |
| 68 | + </div> |
| 69 | + |
| 70 | + {/* Examples */} |
| 71 | + {question.examples.map((example, index) => { |
| 72 | + const lines = example.split('\n').filter(line => line.trim()); |
| 73 | + return ( |
| 74 | + <div key={index} className="mb-6"> |
| 75 | + <h3 className="font-semibold text-gray-900 mb-2"> |
| 76 | + {lines[0].includes('Example') ? lines[0] : `Example ${index + 1}:`} |
| 77 | + </h3> |
| 78 | + <div className="bg-gray-50 p-3 rounded text-sm font-mono space-y-1"> |
| 79 | + {lines.slice(1).map((line, lineIndex) => { |
| 80 | + const trimmedLine = line.trim(); |
| 81 | + return ( |
| 82 | + <div |
| 83 | + key={lineIndex} |
| 84 | + className={trimmedLine.startsWith('Explanation') ? 'text-gray-600' : ''} |
| 85 | + > |
| 86 | + {trimmedLine} |
| 87 | + </div> |
| 88 | + ); |
| 89 | + })} |
| 90 | + </div> |
| 91 | + </div> |
| 92 | + ); |
| 93 | + })} |
| 94 | + |
| 95 | + {/* Constraints */} |
| 96 | + {question.constraints && question.constraints.length > 0 && ( |
| 97 | + <div> |
| 98 | + <h3 className="font-semibold text-gray-900 mb-2">Constraints:</h3> |
| 99 | + <ul className="text-sm text-gray-700 space-y-1 list-disc list-inside"> |
| 100 | + {question.constraints.map((constraint, index) => ( |
| 101 | + <li key={index}>{parseTextWithCode(constraint)}</li> |
| 102 | + ))} |
| 103 | + </ul> |
| 104 | + </div> |
| 105 | + )} |
| 106 | + </div> |
| 107 | + ); |
| 108 | +} |
0 commit comments