Download src/app/settings/page.tsx from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 18 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/settings/page.tsx
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/app/settings/page.tsx
-
curl -L -o page.tsx https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/settings/page.tsx
18 kB
| "use client"; | |
| import { useState, useEffect, useCallback } from "react"; | |
| import { motion } from "framer-motion"; | |
| import { Settings, User, Target, Castle, Check, AlertCircle, Loader2, Download, Database, Sparkles, Plug } from "lucide-react"; | |
| import { | |
| apiClient, | |
| ApiClientError, | |
| HF_TOKEN_STORAGE_KEY, | |
| HF_MODEL_STORAGE_KEY, | |
| } from "@/lib/api-client"; | |
| const ACCENT = "var(--primary)"; | |
| const DAILY_GOAL_OPTIONS = [ | |
| { value: 50, label: "50 XP", description: "Light — ~1 quick exercise" }, | |
| { value: 100, label: "100 XP", description: "Standard — ~2–3 exercises" }, | |
| { value: 150, label: "150 XP", description: "Focused — ~4 exercises" }, | |
| { value: 200, label: "200 XP", description: "Dedicated — ~5–6 exercises" }, | |
| { value: 300, label: "300 XP", description: "Hardcore — full session" }, | |
| ]; | |
| interface SettingsData { | |
| name: string; | |
| dailyGoalXp: number; | |
| lichessUsername: string; | |
| chesscomUsername: string; | |
| } | |
| type SaveState = "idle" | "saving" | "saved" | "error"; | |
| function SectionCard({ | |
| icon, | |
| title, | |
| children, | |
| }: { | |
| icon: React.ReactNode; | |
| title: string; | |
| children: React.ReactNode; | |
| }) { | |
| return ( | |
| <motion.div | |
| initial={{ opacity: 0, y: 16 }} | |
| animate={{ opacity: 1, y: 0 }} | |
| className="glass-md rounded-2xl p-6" | |
| > | |
| <div className="flex items-center gap-2 mb-5"> | |
| <span style={{ color: ACCENT }}>{icon}</span> | |
| <h2 className="text-base font-semibold">{title}</h2> | |
| </div> | |
| {children} | |
| </motion.div> | |
| ); | |
| } | |
| function FieldLabel({ htmlFor, children }: { htmlFor: string; children: React.ReactNode }) { | |
| return ( | |
| <label | |
| htmlFor={htmlFor} | |
| className="block text-xs font-semibold uppercase tracking-wider mb-1.5" | |
| style={{ color: "var(--text-muted)" }} | |
| > | |
| {children} | |
| </label> | |
| ); | |
| } | |
| function TextInput({ | |
| id, | |
| value, | |
| onChange, | |
| placeholder, | |
| maxLength, | |
| type = "text", | |
| }: { | |
| id: string; | |
| value: string; | |
| onChange: (v: string) => void; | |
| placeholder?: string; | |
| maxLength?: number; | |
| type?: "text" | "password"; | |
| }) { | |
| return ( | |
| <input | |
| id={id} | |
| type={type} | |
| value={value} | |
| onChange={(e) => onChange(e.target.value)} | |
| placeholder={placeholder} | |
| maxLength={maxLength} | |
| className="w-full px-4 py-2.5 rounded-xl text-sm outline-none transition-all" | |
| style={{ | |
| background: "rgba(255,255,255,0.06)", | |
| border: "1px solid rgba(255,255,255,0.1)", | |
| color: "var(--text-primary)", | |
| }} | |
| onFocus={(e) => { | |
| e.target.style.borderColor = ACCENT; | |
| }} | |
| onBlur={(e) => { | |
| e.target.style.borderColor = "rgba(255,255,255,0.1)"; | |
| }} | |
| /> | |
| ); | |
| } | |
| export default function SettingsPage() { | |
| const [settings, setSettings] = useState<SettingsData>({ | |
| name: "", | |
| dailyGoalXp: 100, | |
| lichessUsername: "", | |
| chesscomUsername: "", | |
| }); | |
| const [loading, setLoading] = useState(true); | |
| const [saveState, setSaveState] = useState<SaveState>("idle"); | |
| const [errorMsg, setErrorMsg] = useState<string | null>(null); | |
| const [exportState, setExportState] = useState<"idle" | "exporting" | "done" | "error">("idle"); | |
| // Hugging Face per-user credentials (browser-only — never server-persisted) | |
| const [hfToken, setHfToken] = useState(""); | |
| const [hfModel, setHfModel] = useState(""); | |
| const [hfSaveState, setHfSaveState] = useState<SaveState>("idle"); | |
| const [hfTestState, setHfTestState] = useState<"idle" | "testing" | "ok" | "error">("idle"); | |
| const [hfTestMsg, setHfTestMsg] = useState<string | null>(null); | |
| // Load current settings | |
| useEffect(() => { | |
| apiClient.getSettings() | |
| .then((data) => { | |
| setSettings({ | |
| name: data.name ?? "", | |
| dailyGoalXp: data.dailyGoalXp ?? 100, | |
| lichessUsername: data.lichessUsername ?? "", | |
| chesscomUsername: data.chesscomUsername ?? "", | |
| }); | |
| }) | |
| .catch(() => setErrorMsg("Failed to load settings")) | |
| .finally(() => setLoading(false)); | |
| }, []); | |
| // Load Hugging Face credentials from localStorage (client-only) | |
| useEffect(() => { | |
| try { | |
| setHfToken(window.localStorage.getItem(HF_TOKEN_STORAGE_KEY) ?? ""); // eslint-disable-line react-hooks/set-state-in-effect | |
| setHfModel(window.localStorage.getItem(HF_MODEL_STORAGE_KEY) ?? ""); | |
| } catch { | |
| /* localStorage unavailable — ignore */ | |
| } | |
| }, []); | |
| // Persist HF credentials to localStorage so the API client attaches them | |
| // as x-hf-token / x-hf-model headers on every request. | |
| const persistHf = useCallback(() => { | |
| try { | |
| if (hfToken.trim()) window.localStorage.setItem(HF_TOKEN_STORAGE_KEY, hfToken.trim()); | |
| else window.localStorage.removeItem(HF_TOKEN_STORAGE_KEY); | |
| if (hfModel.trim()) window.localStorage.setItem(HF_MODEL_STORAGE_KEY, hfModel.trim()); | |
| else window.localStorage.removeItem(HF_MODEL_STORAGE_KEY); | |
| } catch { | |
| /* ignore */ | |
| } | |
| }, [hfToken, hfModel]); | |
| const handleSaveHf = useCallback(() => { | |
| persistHf(); | |
| setHfSaveState("saved"); | |
| setTimeout(() => setHfSaveState("idle"), 2500); | |
| }, [persistHf]); | |
| const handleTestHf = useCallback(async () => { | |
| persistHf(); // test what the user just typed | |
| setHfTestState("testing"); | |
| setHfTestMsg(null); | |
| try { | |
| const res = await apiClient.getLlmHealth(); | |
| if (res.ok) { | |
| setHfTestState("ok"); | |
| setHfTestMsg(`Connected via ${res.provider}${res.model ? ` (${res.model})` : ""}.`); | |
| } else { | |
| setHfTestState("error"); | |
| setHfTestMsg(res.error ?? "Connection failed."); | |
| } | |
| } catch (err) { | |
| setHfTestState("error"); | |
| setHfTestMsg(err instanceof Error ? err.message : "Connection failed."); | |
| } | |
| }, [persistHf]); | |
| const handleExport = useCallback(async () => { | |
| setExportState("exporting"); | |
| try { | |
| const data = await apiClient.exportData(); | |
| const blob = new Blob([JSON.stringify(data, null, 2)], { type: "application/json" }); | |
| const url = URL.createObjectURL(blob); | |
| const a = document.createElement("a"); | |
| a.href = url; | |
| const dateStr = new Date().toISOString().slice(0, 10); | |
| a.download = `rotnot-export-${dateStr}.json`; | |
| document.body.appendChild(a); | |
| a.click(); | |
| document.body.removeChild(a); | |
| URL.revokeObjectURL(url); | |
| setExportState("done"); | |
| setTimeout(() => setExportState("idle"), 3000); | |
| } catch { | |
| setExportState("error"); | |
| setTimeout(() => setExportState("idle"), 3000); | |
| } | |
| }, []); | |
| const handleSave = useCallback(async () => { | |
| setSaveState("saving"); | |
| setErrorMsg(null); | |
| try { | |
| const updated = await apiClient.updateSettings(settings); | |
| setSettings({ | |
| name: updated.name ?? "", | |
| dailyGoalXp: updated.dailyGoalXp ?? 100, | |
| lichessUsername: updated.lichessUsername ?? "", | |
| chesscomUsername: updated.chesscomUsername ?? "", | |
| }); | |
| setSaveState("saved"); | |
| setTimeout(() => setSaveState("idle"), 2500); | |
| } catch (err) { | |
| setErrorMsg( | |
| err instanceof ApiClientError | |
| ? ((err.body as { error?: string })?.error ?? "Save failed") | |
| : err instanceof Error | |
| ? err.message | |
| : "Unknown error", | |
| ); | |
| setSaveState("error"); | |
| setTimeout(() => setSaveState("idle"), 3000); | |
| } | |
| }, [settings]); | |
| if (loading) { | |
| return ( | |
| <div className="flex items-center justify-center min-h-[40vh]"> | |
| <Loader2 className="animate-spin" size={28} style={{ color: "var(--text-muted)" }} /> | |
| </div> | |
| ); | |
| } | |
| return ( | |
| <div className="max-w-xl mx-auto space-y-5"> | |
| {/* Header */} | |
| <motion.div | |
| initial={{ opacity: 0, y: -10 }} | |
| animate={{ opacity: 1, y: 0 }} | |
| className="flex items-center gap-3 mb-2" | |
| > | |
| <Settings size={22} style={{ color: ACCENT }} /> | |
| <div> | |
| <h1 className="text-xl font-bold tracking-tight">Settings</h1> | |
| <p className="text-xs" style={{ color: "var(--text-muted)" }}> | |
| Customize your RotNot experience | |
| </p> | |
| </div> | |
| </motion.div> | |
| {/* Profile */} | |
| <SectionCard icon={<User size={16} />} title="Profile"> | |
| <FieldLabel htmlFor="display-name">Display Name</FieldLabel> | |
| <TextInput | |
| id="display-name" | |
| value={settings.name} | |
| onChange={(v) => setSettings((s) => ({ ...s, name: v }))} | |
| placeholder="Your name" | |
| maxLength={50} | |
| /> | |
| <p className="mt-1.5 text-xs" style={{ color: "var(--text-muted)" }}> | |
| Shown on your profile and leaderboard | |
| </p> | |
| </SectionCard> | |
| {/* Daily Goal */} | |
| <SectionCard icon={<Target size={16} />} title="Daily XP Goal"> | |
| <div className="grid grid-cols-1 gap-2"> | |
| {DAILY_GOAL_OPTIONS.map((opt) => { | |
| const active = settings.dailyGoalXp === opt.value; | |
| return ( | |
| <button | |
| key={opt.value} | |
| onClick={() => setSettings((s) => ({ ...s, dailyGoalXp: opt.value }))} | |
| className="flex items-center justify-between px-4 py-3 rounded-xl text-sm transition-all text-left" | |
| style={{ | |
| background: active ? `color-mix(in srgb, ${ACCENT} 15%, transparent)` : "rgba(255,255,255,0.04)", | |
| border: `1px solid ${active ? ACCENT : "rgba(255,255,255,0.08)"}`, | |
| color: active ? "var(--text-primary)" : "var(--text-secondary)", | |
| }} | |
| > | |
| <div> | |
| <span className="font-semibold">{opt.label}</span> | |
| <span className="ml-2 text-xs" style={{ color: "var(--text-muted)" }}> | |
| {opt.description} | |
| </span> | |
| </div> | |
| {active && <Check size={14} style={{ color: ACCENT }} />} | |
| </button> | |
| ); | |
| })} | |
| </div> | |
| </SectionCard> | |
| {/* Connected Accounts */} | |
| <SectionCard icon={<Castle size={16} />} title="Connected Accounts"> | |
| <div className="space-y-4"> | |
| <div> | |
| <FieldLabel htmlFor="lichess-username">Lichess Username</FieldLabel> | |
| <TextInput | |
| id="lichess-username" | |
| value={settings.lichessUsername} | |
| onChange={(v) => setSettings((s) => ({ ...s, lichessUsername: v }))} | |
| placeholder="e.g. DrNykterstein" | |
| maxLength={30} | |
| /> | |
| <p className="mt-1.5 text-xs" style={{ color: "var(--text-muted)" }}> | |
| Used to fetch Lichess puzzles with your auth token | |
| </p> | |
| </div> | |
| <div> | |
| <FieldLabel htmlFor="chesscom-username">Chess.com Username</FieldLabel> | |
| <TextInput | |
| id="chesscom-username" | |
| value={settings.chesscomUsername} | |
| onChange={(v) => setSettings((s) => ({ ...s, chesscomUsername: v }))} | |
| placeholder="e.g. MagnusCarlsen" | |
| maxLength={30} | |
| /> | |
| <p className="mt-1.5 text-xs" style={{ color: "var(--text-muted)" }}> | |
| Used to display your Chess.com ratings on the profile | |
| </p> | |
| </div> | |
| </div> | |
| </SectionCard> | |
| {/* Hugging Face (AI provider) */} | |
| <SectionCard icon={<Sparkles size={16} />} title="Hugging Face (AI)"> | |
| <p className="text-sm mb-4" style={{ color: "var(--text-secondary)" }}> | |
| Power the AI features (Feynman, Debate, Critical Thinking, coding written | |
| grading, …) with your own free Hugging Face token. Create a read token at{" "} | |
| <a | |
| href="https://huggingface.co/settings/tokens" | |
| target="_blank" | |
| rel="noopener noreferrer" | |
| style={{ color: ACCENT }} | |
| > | |
| huggingface.co/settings/tokens | |
| </a> | |
| . | |
| </p> | |
| <div className="space-y-4"> | |
| <div> | |
| <FieldLabel htmlFor="hf-token">Access Token</FieldLabel> | |
| <TextInput | |
| id="hf-token" | |
| type="password" | |
| value={hfToken} | |
| onChange={setHfToken} | |
| placeholder="hf_…" | |
| /> | |
| </div> | |
| <div> | |
| <FieldLabel htmlFor="hf-model">Model (optional)</FieldLabel> | |
| <TextInput | |
| id="hf-model" | |
| value={hfModel} | |
| onChange={setHfModel} | |
| placeholder="deepseek-ai/DeepSeek-V4-Flash:fastest" | |
| /> | |
| <p className="mt-1.5 text-xs" style={{ color: "var(--text-muted)" }}> | |
| Defaults to DeepSeek-V4-Flash. Enter any Hugging Face model id to | |
| override (e.g. <code>deepseek-ai/DeepSeek-V4-Flash</code>,{" "} | |
| <code>meta-llama/Llama-3.3-70B-Instruct</code>). | |
| </p> | |
| </div> | |
| <div className="flex items-center gap-3 flex-wrap"> | |
| <motion.button | |
| onClick={handleSaveHf} | |
| whileTap={{ scale: 0.97 }} | |
| className="flex items-center gap-2 px-5 py-2.5 rounded-xl text-sm font-semibold transition-all" | |
| style={{ | |
| background: hfSaveState === "saved" ? "#10b981" : ACCENT, | |
| color: "#fff", | |
| }} | |
| > | |
| {hfSaveState === "saved" ? <Check size={14} /> : null} | |
| {hfSaveState === "saved" ? "Saved!" : "Save Token"} | |
| </motion.button> | |
| <motion.button | |
| onClick={handleTestHf} | |
| disabled={hfTestState === "testing"} | |
| whileTap={{ scale: 0.97 }} | |
| className="flex items-center gap-2 px-5 py-2.5 rounded-xl text-sm font-semibold transition-all disabled:opacity-60" | |
| style={{ | |
| background: | |
| hfTestState === "error" | |
| ? "#f43f5e" | |
| : hfTestState === "ok" | |
| ? "#10b981" | |
| : "rgba(255,255,255,0.08)", | |
| border: "1px solid rgba(255,255,255,0.12)", | |
| color: hfTestState === "idle" || hfTestState === "testing" ? "var(--text-primary)" : "#fff", | |
| }} | |
| > | |
| {hfTestState === "testing" && <Loader2 size={14} className="animate-spin" />} | |
| {hfTestState === "ok" && <Check size={14} />} | |
| {hfTestState === "error" && <AlertCircle size={14} />} | |
| {hfTestState === "idle" && <Plug size={14} />} | |
| {hfTestState === "testing" ? "Testing…" : "Test connection"} | |
| </motion.button> | |
| </div> | |
| {hfTestMsg && ( | |
| <motion.p | |
| initial={{ opacity: 0 }} | |
| animate={{ opacity: 1 }} | |
| className="text-xs" | |
| style={{ color: hfTestState === "ok" ? "#10b981" : "#f43f5e" }} | |
| > | |
| {hfTestMsg} | |
| </motion.p> | |
| )} | |
| <p className="text-xs" style={{ color: "var(--text-muted)" }}> | |
| 🔒 Your token stays in this browser (localStorage) and is sent only with | |
| your own requests over HTTPS. It is never logged or stored on the server. | |
| </p> | |
| </div> | |
| </SectionCard> | |
| {/* Data & Privacy */} | |
| <SectionCard icon={<Database size={16} />} title="Data & Privacy"> | |
| <p className="text-sm mb-4" style={{ color: "var(--text-secondary)" }}> | |
| Download a complete JSON export of all your RotNot data — XP history, activities, | |
| skill trees, spaced repetition queue, and daily logs. | |
| </p> | |
| <motion.button | |
| onClick={handleExport} | |
| disabled={exportState === "exporting"} | |
| whileTap={{ scale: 0.97 }} | |
| className="flex items-center gap-2 px-5 py-2.5 rounded-xl text-sm font-semibold transition-all disabled:opacity-60" | |
| style={{ | |
| background: exportState === "error" | |
| ? "#f43f5e" | |
| : exportState === "done" | |
| ? "#10b981" | |
| : "rgba(255,255,255,0.08)", | |
| border: "1px solid rgba(255,255,255,0.12)", | |
| color: exportState === "idle" ? "var(--text-primary)" : "#fff", | |
| }} | |
| > | |
| {exportState === "exporting" && <Loader2 size={14} className="animate-spin" />} | |
| {exportState === "done" && <Check size={14} />} | |
| {exportState === "error" && <AlertCircle size={14} />} | |
| {exportState === "idle" && <Download size={14} />} | |
| {exportState === "exporting" | |
| ? "Exporting…" | |
| : exportState === "done" | |
| ? "Downloaded!" | |
| : exportState === "error" | |
| ? "Export failed" | |
| : "Download your data"} | |
| </motion.button> | |
| </SectionCard> | |
| {/* Save button + feedback */} | |
| <div className="flex items-center gap-4 pt-1"> | |
| <motion.button | |
| onClick={handleSave} | |
| disabled={saveState === "saving"} | |
| whileTap={{ scale: 0.97 }} | |
| className="flex items-center gap-2 px-6 py-2.5 rounded-xl text-sm font-semibold transition-all disabled:opacity-60" | |
| style={{ | |
| background: saveState === "error" | |
| ? "#f43f5e" | |
| : saveState === "saved" | |
| ? "#10b981" | |
| : ACCENT, | |
| color: "#fff", | |
| }} | |
| > | |
| {saveState === "saving" && <Loader2 size={14} className="animate-spin" />} | |
| {saveState === "saved" && <Check size={14} />} | |
| {saveState === "error" && <AlertCircle size={14} />} | |
| {saveState === "saving" | |
| ? "Saving…" | |
| : saveState === "saved" | |
| ? "Saved!" | |
| : saveState === "error" | |
| ? "Error" | |
| : "Save Settings"} | |
| </motion.button> | |
| {errorMsg && ( | |
| <motion.p | |
| initial={{ opacity: 0 }} | |
| animate={{ opacity: 1 }} | |
| className="text-xs" | |
| style={{ color: "#f43f5e" }} | |
| > | |
| {errorMsg} | |
| </motion.p> | |
| )} | |
| </div> | |
| </div> | |
| ); | |
| } | |