RotNot / src /app /coding /page.tsx
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
8.59 kB
"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&apos;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>
);
}