-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem-description.tsx
More file actions
210 lines (200 loc) · 6.95 KB
/
problem-description.tsx
File metadata and controls
210 lines (200 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"use client"
import { motion } from "framer-motion"
import { Badge } from "@/components/ui/badge"
import { ScrollArea } from "@/components/ui/scroll-area"
import { getLocalizedProblemText, type Problem } from "@/lib/problems"
import { cn } from "@/lib/utils"
import { useAppLanguage } from "@/lib/use-app-language"
const difficultyConfig: Record<string, { bg: string; text: string }> = {
Easy: {
bg: "bg-[hsl(145,65%,93%)]",
text: "text-[hsl(145,65%,32%)]",
},
Medium: {
bg: "bg-[hsl(38,92%,92%)]",
text: "text-[hsl(38,72%,38%)]",
},
Hard: {
bg: "bg-[hsl(0,72%,93%)]",
text: "text-[hsl(0,72%,42%)]",
},
}
interface ProblemDescriptionProps {
problem: Problem
}
function renderExponentText(text: string, keyPrefix: string) {
const parts = text.split(/(\^-?\d+)/g)
return parts.map((part, i) => {
if (!part) return null
if (part.startsWith("^") && /^-?\d+$/.test(part.slice(1))) {
return (
<sup
key={`${keyPrefix}-sup-${i}`}
className="relative -top-[0.3em] text-[0.72em] leading-none align-baseline"
>
{part.slice(1)}
</sup>
)
}
return <span key={`${keyPrefix}-txt-${i}`}>{part}</span>
})
}
function renderMarkdown(text: string) {
const parts = text.split(/(`[^`]+`)|(\*\*[^*]+\*\*)/g)
return parts.map((part, i) => {
if (!part) return null
if (part.startsWith("`") && part.endsWith("`")) {
return (
<code
key={i}
className="rounded-lg bg-muted px-1.5 py-0.5 font-mono text-[13px] text-primary"
>
{part.slice(1, -1)}
</code>
)
}
if (part.startsWith("**") && part.endsWith("**")) {
return (
<strong key={i} className="font-semibold text-foreground">
{part.slice(2, -2)}
</strong>
)
}
return <span key={i}>{renderExponentText(part, `md-${i}`)}</span>
})
}
export function ProblemDescription({ problem }: ProblemDescriptionProps) {
const { language, copy } = useAppLanguage()
const diffStyle = difficultyConfig[problem.difficulty]
const localized = getLocalizedProblemText(problem, language)
const isNew = (problem.tags ?? []).includes("NEW")
return (
<ScrollArea className="h-full">
<div className="p-6 lg:p-8">
<motion.div
initial={{ opacity: 0, y: 12 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.4, ease: "easeOut" }}
>
<div className="flex items-center gap-3">
<h1 className="text-xl font-bold text-foreground lg:text-2xl">
{localized.text.title}
</h1>
{isNew && (
<Badge
variant="secondary"
className="border-0 text-xs font-semibold bg-[#fff4e5] text-[#b85a00]"
>
NEW
</Badge>
)}
{localized.isFallback && (
<Badge
variant="secondary"
className="border-0 text-xs font-semibold bg-[#eef4ff] text-[#2f66d0]"
>
{copy.problem.fallbackEnglish}
</Badge>
)}
<Badge
variant="secondary"
className={cn(
"border-0 text-xs font-semibold",
diffStyle.bg,
diffStyle.text
)}
>
{problem.difficulty}
</Badge>
</div>
<p className="mt-1 text-xs text-muted-foreground">
{localized.text.category}
</p>
</motion.div>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.4, delay: 0.15, ease: "easeOut" }}
className="mt-6"
>
{localized.text.description.split("\n\n").map((paragraph, i) => {
if (paragraph.startsWith("1.") || paragraph.startsWith("2.") || paragraph.startsWith("3.")) {
const items = paragraph.split("\n")
return (
<ol key={i} className="mt-3 flex flex-col gap-1.5 pl-5 text-sm leading-relaxed text-foreground/80 list-decimal">
{items.map((item, j) => (
<li key={j}>{renderMarkdown(item.replace(/^\d+\.\s*/, ""))}</li>
))}
</ol>
)
}
return (
<p
key={i}
className="mt-3 text-sm leading-relaxed text-foreground/80"
>
{renderMarkdown(paragraph)}
</p>
)
})}
</motion.div>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.4, delay: 0.25, ease: "easeOut" }}
className="mt-8 flex flex-col gap-4"
>
{localized.text.examples.map((example, i) => (
<div
key={i}
className="rounded-2xl border border-border/60 bg-muted/50 p-4"
>
<p className="text-xs font-semibold text-muted-foreground">
{copy.problem.examples} {i + 1}
</p>
<div className="mt-2 flex flex-col gap-1.5">
<div className="flex items-start gap-2">
<span className="flex-shrink-0 text-xs font-medium text-muted-foreground">
{copy.problem.input}:
</span>
<code className="font-mono text-xs text-foreground">
{example.input}
</code>
</div>
<div className="flex items-start gap-2">
<span className="flex-shrink-0 text-xs font-medium text-muted-foreground">
{copy.problem.output}:
</span>
<code className="font-mono text-xs text-primary">
{example.output}
</code>
</div>
{example.explanation && (
<p className="mt-1 text-xs leading-relaxed text-muted-foreground">
{example.explanation}
</p>
)}
</div>
</div>
))}
</motion.div>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.4, delay: 0.35, ease: "easeOut" }}
className="mt-8"
>
<h3 className="text-sm font-semibold text-foreground">{copy.problem.constraints}</h3>
<ul className="mt-2 flex flex-col gap-1">
{localized.text.constraints.map((constraint, i) => (
<li key={i} className="flex items-start gap-2 text-xs text-muted-foreground">
<span className="mt-1.5 h-1 w-1 flex-shrink-0 rounded-full bg-primary/40" />
<code className="font-mono">{renderExponentText(constraint, `constraint-${i}`)}</code>
</li>
))}
</ul>
</motion.div>
</div>
</ScrollArea>
)
}