File size: 8,589 Bytes
9580dce | 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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | "use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { motion } from "framer-motion";
import {
Code2,
Bug,
ScanSearch,
Building2,
Loader2,
ArrowRight,
} from "lucide-react";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Button } from "@/components/ui/button";
import {
apiClient,
type CodingProblemType,
type CodingDifficulty,
type CodingLanguage,
} from "@/lib/api-client";
const CODING_COLOR = "var(--color-coding)";
const PROBLEM_TYPES: {
id: CodingProblemType;
label: string;
blurb: string;
icon: React.ReactNode;
}[] = [
{
id: "code-creation",
label: "Code Creation",
blurb: "Write a function from scratch to pass a hidden test suite.",
icon: <Code2 size={20} />,
},
{
id: "debugging",
label: "Complex Debugging",
blurb: "Find and fix the bug so all tests go green.",
icon: <Bug size={20} />,
},
{
id: "code-review",
label: "Code Review (AI output)",
blurb: "Review AI-generated code and identify its issues — there may be none.",
icon: <ScanSearch size={20} />,
},
{
id: "architecture",
label: "Architecture Discussion",
blurb: "Reason about a system design question, graded against a rubric.",
icon: <Building2 size={20} />,
},
];
const DIFFICULTY_LABELS: Record<CodingDifficulty, string> = {
easy: "Easy",
medium: "Medium",
hard: "Hard",
};
const LANGUAGES: { id: CodingLanguage; label: string }[] = [
{ id: "javascript", label: "JavaScript" },
{ id: "typescript", label: "TypeScript" },
{ id: "python", label: "Python" },
{ id: "go", label: "Go" },
];
export default function CodingPage() {
const router = useRouter();
const [type, setType] = useState<CodingProblemType>("code-creation");
const [difficulty, setDifficulty] = useState<CodingDifficulty>("easy");
const [language, setLanguage] = useState<CodingLanguage>("javascript");
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const isExecutable = type === "code-creation" || type === "debugging";
const langExecutes = language === "javascript" || language === "typescript";
async function handleGenerate() {
setLoading(true);
setError(null);
try {
const res = await apiClient.generateCodingProblem({ type, difficulty, language });
// Hand the generated problem to the problem page via sessionStorage.
sessionStorage.setItem("coding:problem", JSON.stringify(res));
router.push("/coding/problem");
} catch (e) {
setError(
e instanceof Error ? e.message : "Failed to generate a problem set. Please try again.",
);
setLoading(false);
}
}
return (
<div className="max-w-2xl mx-auto space-y-6 pb-12">
<div className="flex items-center gap-3">
<div
className="w-11 h-11 rounded-xl flex items-center justify-center shrink-0"
style={{ background: `${CODING_COLOR}1f`, color: CODING_COLOR }}
>
<Code2 size={24} />
</div>
<div>
<h1 className="text-2xl font-bold" style={{ color: "var(--text-primary)" }}>
Coding Training
</h1>
<p className="text-sm" style={{ color: "var(--text-secondary)" }}>
Keep your engineering fundamentals sharp — write, debug, review, and design.
</p>
</div>
</div>
<motion.div
initial={{ opacity: 0, y: 8 }}
animate={{ opacity: 1, y: 0 }}
className="glass-md rounded-xl p-5 space-y-5"
>
{/* Problem type */}
<div className="space-y-2">
<label className="text-xs font-semibold tracking-wide" style={{ color: "var(--text-muted)" }}>
PROBLEM TYPE
</label>
<div className="grid grid-cols-1 sm:grid-cols-2 gap-2">
{PROBLEM_TYPES.map((t) => {
const active = type === t.id;
return (
<button
key={t.id}
data-testid={`type-${t.id}`}
onClick={() => setType(t.id)}
className="flex items-start gap-3 px-4 py-3 rounded-lg text-left transition-all"
style={{
background: active ? `${CODING_COLOR}1a` : "rgba(255,255,255,0.04)",
border: `1px solid ${active ? `${CODING_COLOR}66` : "rgba(255,255,255,0.08)"}`,
}}
>
<span style={{ color: active ? CODING_COLOR : "var(--text-secondary)" }}>
{t.icon}
</span>
<span>
<span
className="block text-sm font-semibold"
style={{ color: "var(--text-primary)" }}
>
{t.label}
</span>
<span className="block text-xs mt-0.5" style={{ color: "var(--text-muted)" }}>
{t.blurb}
</span>
</span>
</button>
);
})}
</div>
</div>
{/* Difficulty + language */}
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div className="space-y-2">
<label className="text-xs font-semibold tracking-wide" style={{ color: "var(--text-muted)" }}>
DIFFICULTY
</label>
<Select value={difficulty} onValueChange={(v) => setDifficulty(v as CodingDifficulty)}>
<SelectTrigger
data-testid="difficulty-select"
className="w-full"
style={{
background: "rgba(255,255,255,0.06)",
borderColor: `${CODING_COLOR}33`,
color: "var(--text-primary)",
}}
>
<SelectValue />
</SelectTrigger>
<SelectContent>
{(["easy", "medium", "hard"] as CodingDifficulty[]).map((d) => (
<SelectItem key={d} value={d}>
{DIFFICULTY_LABELS[d]}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<label className="text-xs font-semibold tracking-wide" style={{ color: "var(--text-muted)" }}>
LANGUAGE
</label>
<Select value={language} onValueChange={(v) => setLanguage(v as CodingLanguage)}>
<SelectTrigger
data-testid="language-select"
className="w-full"
style={{
background: "rgba(255,255,255,0.06)",
borderColor: `${CODING_COLOR}33`,
color: "var(--text-primary)",
}}
>
<SelectValue />
</SelectTrigger>
<SelectContent>
{LANGUAGES.map((l) => (
<SelectItem key={l.id} value={l.id}>
{l.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
</div>
{isExecutable && !langExecutes && (
<p className="text-xs rounded-lg px-3 py-2" style={{ background: "rgba(245,158,11,0.1)", color: "#fbbf24" }}>
Live sandbox execution supports JavaScript & TypeScript. For {LANGUAGES.find((l) => l.id === language)?.label},
you'll still get a problem but auto-grading requires an executable language.
</p>
)}
{error && (
<p data-testid="generate-error" className="text-sm rounded-lg px-3 py-2" style={{ background: "rgba(244,63,94,0.12)", color: "#fb7185" }}>
{error}
</p>
)}
<Button
data-testid="generate-btn"
onClick={handleGenerate}
disabled={loading}
className="w-full font-semibold"
style={{ background: CODING_COLOR, color: "#0a0e14" }}
>
{loading ? (
<span className="flex items-center gap-2 justify-center">
<Loader2 size={16} className="animate-spin" /> Generating…
</span>
) : (
<span className="flex items-center gap-2 justify-center">
Generate Problem Set <ArrowRight size={16} />
</span>
)}
</Button>
</motion.div>
</div>
);
}
|