File size: 11,021 Bytes
edfa7f5 d70dffe edfa7f5 d70dffe edfa7f5 d70dffe edfa7f5 | 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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | import { useEffect, useRef, useState } from "react";
import {
Check,
ChevronLeft,
ChevronRight,
GraduationCap,
Loader2,
Play,
X,
} from "lucide-react";
import { gloss } from "../lib/glossary";
import { LESSONS_DONE_LS as DONE_LS } from "../lib/learnProgress";
import { LESSONS, type Lesson, type LessonKey } from "../lib/lessons";
import { listRuns } from "../lib/runStore";
import { useApp } from "../lib/store";
import { TipIcon } from "./TipIcon";
/**
* "Learn the basics" — the guided-lessons overlay (lib/lessons.ts).
*
* A small dismissable card, fixed bottom-right (z-40, under the mobile
* drawers at z-50 and clear of the phone Run-FAB via bottom-20). It is
* NOT a separate mode: each lesson step drives the normal app through
* the same pendingRestore/auto-run bridges scenario boots use, and the
* card floats over whatever view that produced. Flow per lesson:
* question → [Run it] → whatToLookFor once the run archives → next
* step / next lesson. Lesson runs carry RunRecord.scenario = "L1"…"L4"
* so they never pollute evidence pools (same exclusion the F-scenario
* pickers apply).
*
* Run-completion detection: historyVersion bumps when the auto-run
* archives; we then read back the lesson-tagged record (its run_id +
* root_seed feed L2's comparison selection and L4's seed replay).
* Progress persists in localStorage "quda.lessonsDone".
*/
function loadDone(): Set<LessonKey> {
try {
const raw = JSON.parse(localStorage.getItem(DONE_LS) ?? "[]");
return new Set(Array.isArray(raw) ? (raw as LessonKey[]) : []);
} catch {
return new Set();
}
}
export function LessonCard() {
const open = useApp((s) => s.lessonsOpen);
const setOpen = useApp((s) => s.setLessonsOpen);
const running = useApp((s) => s.running);
const historyVersion = useApp((s) => s.historyVersion);
const [activeKey, setActiveKey] = useState<LessonKey | null>(null);
const [stepIdx, setStepIdx] = useState(0);
const [phase, setPhase] = useState<"ask" | "running" | "look">("ask");
const [done, setDone] = useState<Set<LessonKey>>(loadDone);
const baseHv = useRef(0);
// Runs THIS lesson produced (reset on lesson switch) — L2's compare
// step and L4's replay step resolve ids/seeds from here.
const lessonRuns = useRef<{ run_id: string; root_seed: number | null }[]>([]);
const lesson: Lesson | null =
activeKey != null ? (LESSONS.find((l) => l.key === activeKey) ?? null) : null;
const markDone = (key: LessonKey) =>
setDone((prev) => {
const next = new Set(prev).add(key);
try {
localStorage.setItem(DONE_LS, JSON.stringify([...next]));
} catch {
/* private mode — session-scoped progress is fine */
}
return next;
});
const pickLesson = (key: LessonKey | null) => {
setActiveKey(key);
setStepIdx(0);
setPhase("ask");
lessonRuns.current = [];
};
// Handoff bridge: the learn-from-zero track asks us to open directly
// on a lesson (Step5Certainty sets pendingLessonKey before flipping
// lessonsOpen). Consume-and-clear, one shot.
const pendingKey = useApp((s) => s.pendingLessonKey);
useEffect(() => {
if (!open || pendingKey == null) return;
if (LESSONS.some((l) => l.key === pendingKey))
pickLesson(pendingKey as LessonKey);
useApp.getState().setPendingLessonKey(null);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [open, pendingKey]);
const launch = (l: Lesson, idx: number) => {
const step = l.steps[idx];
const first = lessonRuns.current[0];
const app = useApp.getState();
// Lessons run in the Pipeline editor — leave the board if needed.
app.setWorkspaceMode("compose");
if (step.openGateDiff) app.setGateDiffDefaultOpen(true);
app.requestRestore({
graph: step.graph,
sampleKey: step.sampleKey,
pinSeed:
step.seed === "pinned"
? (step.pinSeed ?? null)
: step.seed === "replayFirst"
? (first?.root_seed ?? null)
: null,
// replayFirst threads the ancestor's run_id → forked_from →
// the fork edge L4 points at.
sourceRunId: step.seed === "replayFirst" ? (first?.run_id ?? null) : null,
precisionTarget: null,
autoRunAfter: true,
scenario: l.key,
});
if (step.evidenceTab) app.setPendingEvidenceTab(step.evidenceTab);
if (step.expandEvidence) app.bumpHintExpandRightPane();
baseHv.current = app.historyVersion;
setStepIdx(idx);
setPhase("running");
};
// Completion watcher — see the header comment.
useEffect(() => {
if (phase !== "running" || lesson == null) return;
if (historyVersion <= baseHv.current) return;
let cancelled = false;
(async () => {
const runs = await listRuns(50);
const mine = runs
.filter((r) => r.scenario === lesson.key)
.sort((a, b) => b.created_at - a.created_at);
if (cancelled) return;
const latest = mine[0];
if (latest)
lessonRuns.current.push({
run_id: latest.run_id,
root_seed: latest.root_seed ?? null,
});
const step = lesson.steps[stepIdx];
if (step.compareLessonRuns && lessonRuns.current.length >= 2) {
const ids = lessonRuns.current.slice(-2).map((r) => r.run_id);
const app = useApp.getState();
app.setCompareIds(ids);
app.setPendingEvidenceTab("compare");
app.bumpHintExpandRightPane();
}
if (stepIdx === lesson.steps.length - 1) markDone(lesson.key);
setPhase("look");
})();
return () => {
cancelled = true;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [historyVersion, phase, lesson, stepIdx]);
if (!open) return null;
const nextLesson = LESSONS.find((l) => !done.has(l.key) && l.key !== activeKey);
return (
<div
className="fixed right-3 bottom-20 md:bottom-3 z-40 w-[300px] max-w-[calc(100vw-24px)] rounded-lg border border-edge bg-surface shadow-xl"
role="dialog"
aria-label="Guided lessons"
data-marker="guided-lessons"
>
<div className="px-3 py-2 flex items-center gap-1.5 border-b border-edge/60">
<GraduationCap className="w-3.5 h-3.5 text-accent shrink-0" />
<span className="text-[11px] font-medium text-ink flex-1 truncate">
{lesson ? lesson.title : "Learn the basics"}
</span>
{lesson && (
<button
type="button"
onClick={() => pickLesson(null)}
className="text-mute hover:text-ink p-0.5"
title="All lessons"
aria-label="Back to lesson list"
>
<ChevronLeft className="w-3.5 h-3.5" />
</button>
)}
<button
type="button"
onClick={() => setOpen(false)}
className="text-mute hover:text-ink p-0.5"
title="Close lessons"
aria-label="Close lessons"
>
<X className="w-3.5 h-3.5" />
</button>
</div>
{lesson == null ? (
<div className="p-2">
{LESSONS.map((l, i) => (
<button
key={l.key}
type="button"
onClick={() => pickLesson(l.key)}
className="w-full flex items-center gap-2 px-2 py-1.5 rounded-md text-left hover:bg-surfaceAlt transition-colors"
>
<span
className={`shrink-0 w-4 h-4 rounded-full border text-[9px] flex items-center justify-center ${
done.has(l.key)
? "border-ok/60 bg-ok/10 text-ok"
: "border-edge text-mute"
}`}
aria-hidden
>
{done.has(l.key) ? <Check className="w-2.5 h-2.5" /> : i + 1}
</span>
<span className="text-[11px] text-ink flex-1 truncate">
{l.title}
</span>
<ChevronRight className="w-3 h-3 text-mute/60 shrink-0" />
</button>
))}
<p className="px-2 pt-1.5 pb-0.5 text-[9px] leading-snug text-mute/70">
Four tiny experiments, one question each. Lesson runs are
tagged and never count as your evidence.
</p>
</div>
) : (
<div className="p-3">
{/* back-pointer for learn-from-zero graduates: L1 is the
designed landing spot of the step-5 handoff */}
{lesson.key === "L1" && (
<p className="text-[10px] text-mute mb-1">
Fresh from the basics? This one runs the real thing — a live
pipeline, archived like any run.
</p>
)}
<p className="text-[11px] leading-snug text-ink">{lesson.question}</p>
<div className="mt-1.5 flex flex-wrap gap-1">
{lesson.terms.map((t) => (
<span
key={t}
className="inline-flex items-center gap-0.5 text-[9px] text-mute border border-edge/60 rounded px-1 py-px"
>
{t}
<TipIcon hint={gloss(t)} size={9} />
</span>
))}
</div>
{phase === "look" && (
<p className="mt-2 text-[11px] leading-snug text-mute border-l-2 border-accent/50 pl-2">
{lesson.whatToLookFor}
</p>
)}
<div className="mt-2.5 flex flex-wrap items-center gap-1.5">
{phase === "running" ? (
<span className="inline-flex items-center gap-1.5 text-[11px] text-mute">
<Loader2 className="w-3.5 h-3.5 animate-spin" /> Running…
</span>
) : phase === "ask" || stepIdx + 1 < lesson.steps.length ? (
<button
type="button"
disabled={running}
onClick={() =>
launch(lesson, phase === "ask" ? stepIdx : stepIdx + 1)
}
className="btn-primary !h-7 !px-2.5 text-[11px] disabled:opacity-40 disabled:cursor-not-allowed"
title={running ? "Wait for the current run to finish" : undefined}
>
<Play className="w-3 h-3" />
{lesson.steps[phase === "ask" ? stepIdx : stepIdx + 1].label}
</button>
) : nextLesson ? (
<button
type="button"
onClick={() => pickLesson(nextLesson.key)}
className="btn !h-7 !px-2.5 text-[11px]"
>
Next lesson: {nextLesson.title}
<ChevronRight className="w-3 h-3" />
</button>
) : (
<button
type="button"
onClick={() => setOpen(false)}
className="btn !h-7 !px-2.5 text-[11px]"
>
<Check className="w-3 h-3" /> All done — close
</button>
)}
</div>
</div>
)}
</div>
);
}
|