@mmon-co/ncept-bb 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,450 @@
1
+ import { useEffect, useState, type ButtonHTMLAttributes } from "react";
2
+ import { experimental_useRpcQuery, useRpc, useBbNavigate } from "@get-bb/plugin-sdk/app";
3
+ import { rpcContract } from "./contract";
4
+ import type { entrySchema } from "./entries";
5
+ import type { learningReading } from "./reading";
6
+ import type { z } from "zod";
7
+ import { Button } from "./ui/button";
8
+ import { LearnMap } from "./map";
9
+
10
+ function MenuItem({
11
+ hint,
12
+ children,
13
+ ...props
14
+ }: { hint?: string } & Omit<ButtonHTMLAttributes<HTMLButtonElement>, "className">) {
15
+ return (
16
+ <button
17
+ {...props}
18
+ className="block w-full cursor-pointer rounded-md px-3 py-2 text-left text-xs hover:bg-state-hover disabled:pointer-events-none disabled:opacity-50"
19
+ >
20
+ <span className="block font-medium">{children}</span>
21
+ {hint && <small className="block text-muted-foreground">{hint}</small>}
22
+ </button>
23
+ );
24
+ }
25
+
26
+ export function MissionPage({
27
+ projectId,
28
+ missionId,
29
+ sessions,
30
+ pending,
31
+ open,
32
+ refreshed,
33
+ }: {
34
+ projectId: string;
35
+ missionId: string;
36
+ sessions: z.infer<typeof learningReading>["sessions"];
37
+ pending: boolean;
38
+ refreshed: () => void;
39
+ open: (intent: z.infer<typeof entrySchema>) => Promise<void>;
40
+ }) {
41
+ const rpc = useRpc<typeof rpcContract>();
42
+ const navigate = useBbNavigate();
43
+ const reading = experimental_useRpcQuery({
44
+ contract: rpcContract,
45
+ method: "mission",
46
+ input: { projectId, missionId },
47
+ });
48
+ const [menu, setMenu] = useState<"skip" | "revise" | "share">();
49
+ const [shareTitle, setShareTitle] = useState("");
50
+ const [sharing, setSharing] = useState(false);
51
+ const [shared, setShared] = useState<string>();
52
+ const [picking, setPicking] = useState(false);
53
+ const [finishing, setFinishing] = useState(false);
54
+ const [retiring, setRetiring] = useState(false);
55
+ const [outcome, setOutcome] = useState<"achieved" | "abandoned">("achieved");
56
+ const [telling, setTelling] = useState(false);
57
+ const [request, setRequest] = useState("");
58
+ const [error, setError] = useState<string>();
59
+ useEffect(() => {
60
+ void reading.refetch();
61
+ const refresh = () => {
62
+ void reading.refetch();
63
+ };
64
+ window.addEventListener("focus", refresh);
65
+ return () => window.removeEventListener("focus", refresh);
66
+ }, [projectId, missionId]);
67
+ async function visit(session: string) {
68
+ setError(undefined);
69
+ try {
70
+ const result = await rpc.call("thread", { projectId, session });
71
+ if (!result.threadId) throw new Error("No thread found for this Session");
72
+ navigate.toThread(result.threadId);
73
+ } catch (error) {
74
+ setError(String(error));
75
+ }
76
+ }
77
+ async function share() {
78
+ setSharing(true);
79
+ setError(undefined);
80
+ try {
81
+ setShared((await rpc.call("share", { projectId, missionId, title: shareTitle.trim() })).url);
82
+ } catch (error) {
83
+ setError(String(error));
84
+ } finally {
85
+ setSharing(false);
86
+ }
87
+ }
88
+ async function retire() {
89
+ setRetiring(true);
90
+ setError(undefined);
91
+ try {
92
+ await rpc.call("retire", { projectId, missionId, outcome });
93
+ setFinishing(false);
94
+ setMenu(undefined);
95
+ await reading.refetch();
96
+ refreshed();
97
+ } catch (error) {
98
+ setError(String(error));
99
+ } finally {
100
+ setRetiring(false);
101
+ }
102
+ }
103
+ const data = reading.data;
104
+ if (!data) return <p role={reading.error ? "alert" : undefined}>{reading.error?.message ?? "Loading Mission…"}</p>;
105
+ const stops = data.sections.flatMap((section) => section.lessons);
106
+ const activeIndex = stops.findIndex((stop) => stop.active);
107
+ const currentIndex = activeIndex >= 0 ? activeIndex : stops.findIndex((stop) => !stop.cleared);
108
+ const later = currentIndex < 0 ? [] : stops.slice(currentIndex + 1);
109
+ const currentSection =
110
+ data.sections.find((section) => section.lessons.some((stop) => stop.active)) ??
111
+ data.sections.find((section) => section.lessons.some((stop) => !stop.cleared));
112
+ const asides = sessions.filter((session) => data.asides.includes(session.id));
113
+ const history = data.history.filter(
114
+ (entry) => entry.session !== data.active && !data.asides.includes(entry.session ?? ""),
115
+ );
116
+ return (
117
+ <>
118
+ <nav aria-label="Breadcrumb" className="text-sm text-muted-foreground flex gap-2 flex-wrap">
119
+ <button onClick={() => navigate.toPluginPanel("learn")}>Learn</button>
120
+ <span>/</span>
121
+ <button onClick={() => navigate.toPluginPanel("learn")}>Missions</button>
122
+ <span>/</span>
123
+ <span className="text-foreground">{data.statement}</span>
124
+ </nav>
125
+ <header className="space-y-4">
126
+ <h1 className="text-xl">{data.statement}</h1>
127
+ {data.retired && <p className="text-sm text-muted-foreground">Retired</p>}
128
+ <div className="flex flex-wrap gap-4">
129
+ <Button disabled={pending || data.retired} onClick={() => void open({ kind: "continue", missionId })}>
130
+ {data.active ? "Continue" : "Start"}
131
+ {data.next ? " · " + data.next : ""} →
132
+ </Button>
133
+ <Button
134
+ variant="outline"
135
+ aria-expanded={menu === "skip"}
136
+ onClick={() => {
137
+ setMenu(menu === "skip" ? undefined : "skip");
138
+ setTelling(false);
139
+ }}
140
+ >
141
+ Skip…
142
+ </Button>
143
+ <Button
144
+ variant="outline"
145
+ aria-expanded={menu === "revise"}
146
+ onClick={() => {
147
+ setMenu(menu === "revise" ? undefined : "revise");
148
+ setTelling(false);
149
+ }}
150
+ >
151
+ Revise…
152
+ </Button>
153
+ <Button
154
+ variant="outline"
155
+ aria-expanded={menu === "share"}
156
+ onClick={() => {
157
+ setMenu(menu === "share" ? undefined : "share");
158
+ setTelling(false);
159
+ setShared(undefined);
160
+ setShareTitle(shareTitle || data.statement.replace(/\s+/g, " ").slice(0, 80));
161
+ }}
162
+ >
163
+ Share…
164
+ </Button>
165
+ </div>
166
+ {menu && (
167
+ <section
168
+ aria-label={menu === "skip" ? "Skip" : menu === "revise" ? "Revise" : "Share"}
169
+ className="rounded-lg border border-border p-2 max-w-sm bg-popover text-popover-foreground space-y-1"
170
+ >
171
+ {menu === "skip" ? (
172
+ <>
173
+ <MenuItem
174
+ disabled={pending || data.retired || !later.length}
175
+ onClick={() => void open({ kind: "skip-lesson", missionId })}
176
+ >
177
+ Just this lesson
178
+ </MenuItem>
179
+ <MenuItem
180
+ disabled={pending || data.retired || !later.length}
181
+ onClick={() => setPicking(!picking)}
182
+ hint="Lessons in between are marked probably known."
183
+ >
184
+ Skip to…
185
+ </MenuItem>
186
+ <MenuItem
187
+ disabled={pending || data.retired}
188
+ hint="Opens a thread with what you already know."
189
+ onClick={() => setTelling(true)}
190
+ >
191
+ Tell the tutor
192
+ </MenuItem>
193
+ </>
194
+ ) : menu === "revise" ? (
195
+ <>
196
+ <MenuItem
197
+ disabled={pending || !data.canReviseLesson || data.retired}
198
+ hint="Too hard, too easy or off target: the tutor reworks it with you."
199
+ onClick={() => void open({ kind: "revise-lesson", missionId })}
200
+ >
201
+ This lesson
202
+ </MenuItem>
203
+ <MenuItem
204
+ disabled={pending || data.retired}
205
+ hint="Opens a thread to rework the Mission's path."
206
+ onClick={() => void open({ kind: "revise-route", missionId })}
207
+ >
208
+ The route
209
+ </MenuItem>
210
+ <MenuItem
211
+ disabled={pending || data.retired}
212
+ onClick={() => setFinishing(!finishing)}
213
+ hint="As achieved or abandoned."
214
+ >
215
+ Finish…
216
+ </MenuItem>
217
+ </>
218
+ ) : null}
219
+ {menu === "skip" && picking && (
220
+ <section aria-label="Skip to" className="p-2 space-y-1">
221
+ {later.map((stop, index) => (
222
+ <MenuItem
223
+ key={index}
224
+ disabled={pending || data.retired}
225
+ onClick={() => void open({ kind: "skip-to", missionId, stop: stop.skip })}
226
+ >
227
+ {stop.checkpoint ? "Checkpoint: " : stop.number + " · "}
228
+ {stop.name}
229
+ </MenuItem>
230
+ ))}
231
+ </section>
232
+ )}
233
+ {menu === "revise" && finishing && (
234
+ <form
235
+ aria-label="Finish Mission"
236
+ className="p-2 space-y-3"
237
+ onSubmit={(event) => {
238
+ event.preventDefault();
239
+ void retire();
240
+ }}
241
+ >
242
+ <p className="text-sm">Finish this Mission?</p>
243
+ {(["achieved", "abandoned"] as const).map((value) => (
244
+ <label key={value} className="flex gap-2 text-sm">
245
+ <input
246
+ type="radio"
247
+ name="outcome"
248
+ checked={outcome === value}
249
+ disabled={retiring}
250
+ onChange={() => setOutcome(value)}
251
+ />
252
+ {value === "achieved" ? "Achieved" : "Abandoned"}
253
+ </label>
254
+ ))}
255
+ <div className="flex gap-4">
256
+ <Button type="submit" disabled={retiring || data.retired}>
257
+ Finish Mission
258
+ </Button>
259
+ <Button type="button" variant="ghost" disabled={retiring} onClick={() => setFinishing(false)}>
260
+ Cancel
261
+ </Button>
262
+ </div>
263
+ </form>
264
+ )}
265
+ {menu === "share" &&
266
+ (shared ? (
267
+ <div className="p-2 space-y-3">
268
+ <p className="text-sm">Anyone with this link can read the map and learn from it.</p>
269
+ <input
270
+ readOnly
271
+ aria-label="Link"
272
+ value={shared}
273
+ onFocus={(event) => event.currentTarget.select()}
274
+ className="w-full rounded-md border border-input bg-background p-3 text-sm"
275
+ />
276
+ <Button type="button" onClick={() => void navigator.clipboard.writeText(shared)}>
277
+ Copy link
278
+ </Button>
279
+ </div>
280
+ ) : (
281
+ <form
282
+ aria-label="Share Mission"
283
+ className="p-2 space-y-3"
284
+ onSubmit={(event) => {
285
+ event.preventDefault();
286
+ if (shareTitle.trim()) void share();
287
+ }}
288
+ >
289
+ <label className="block text-sm" htmlFor="share-title">
290
+ Title
291
+ </label>
292
+ <input
293
+ id="share-title"
294
+ autoFocus
295
+ value={shareTitle}
296
+ disabled={sharing}
297
+ onChange={(event) => setShareTitle(event.target.value)}
298
+ className="w-full rounded-md border border-input bg-background p-3 text-sm"
299
+ />
300
+ <small className="block text-muted-foreground">
301
+ Publishes this Mission's map and its Materials on the Hub at a link. It isn't listed.
302
+ </small>
303
+ <Button disabled={sharing || !shareTitle.trim()} type="submit">
304
+ {sharing ? "Sharing…" : "Share"}
305
+ </Button>
306
+ </form>
307
+ ))}
308
+ {telling && (
309
+ <form
310
+ className="p-2 space-y-3"
311
+ onSubmit={(event) => {
312
+ event.preventDefault();
313
+ if (request.trim()) void open({ kind: "tell-tutor", missionId, request: request.trim() });
314
+ }}
315
+ >
316
+ <label className="block text-sm" htmlFor="tutor-request">
317
+ Tell the tutor
318
+ </label>
319
+ <textarea
320
+ id="tutor-request"
321
+ autoFocus
322
+ value={request}
323
+ onChange={(event) => setRequest(event.target.value)}
324
+ placeholder="What do you already know?"
325
+ className="w-full min-h-24 rounded-md border border-input bg-background p-3 text-sm"
326
+ />
327
+ <Button disabled={pending || !request.trim()} type="submit">
328
+ Open thread →
329
+ </Button>
330
+ </form>
331
+ )}
332
+ </section>
333
+ )}
334
+ </header>
335
+ <div className="h-56 overflow-hidden rounded-xl border border-border">
336
+ <LearnMap projectId={projectId} mission={missionId} still />
337
+ </div>
338
+ {error && (
339
+ <p role="alert" className="text-destructive">
340
+ {error}
341
+ </p>
342
+ )}
343
+ {data.active && !data.sections.some((section) => section.lessons.some((stop) => stop.active)) && (
344
+ <p className="text-sm">Open · {sessions.find((session) => session.id === data.active)?.title ?? "Session"}</p>
345
+ )}
346
+ <section
347
+ aria-label="Itinerary"
348
+ className="rounded-xl border border-border overflow-hidden divide-y divide-border"
349
+ >
350
+ {data.sections.map((section) => (
351
+ <div key={section.number}>
352
+ <h2 className="px-4 py-3 bg-muted flex flex-wrap justify-between gap-2 text-sm">
353
+ <span>
354
+ Milestone {section.number} · {section.label}
355
+ {!data.retired && section === currentSection ? " · you are here" : ""}
356
+ </span>
357
+ <span className="text-muted-foreground">
358
+ {section.lessons.length
359
+ ? section.lessons.filter((stop) => stop.numbered && stop.cleared).length +
360
+ " of " +
361
+ section.lessons.filter((stop) => stop.numbered).length +
362
+ " lessons"
363
+ : "not planned yet"}
364
+ </span>
365
+ </h2>
366
+ {section.lessons.map((stop, index) => {
367
+ const active = stop.active;
368
+ return (
369
+ <div key={index} className={"px-4 py-3 text-sm " + (active ? "bg-accent" : "")}>
370
+ <div className="flex items-center gap-3">
371
+ <span className="text-muted-foreground">
372
+ {stop.cleared ? "✓" : stop.checkpoint ? "◇" : stop.number}
373
+ </span>
374
+ <span className="flex-1">
375
+ {stop.checkpoint ? "Checkpoint: " : stop.kind === "review" ? "Review: " : ""}
376
+ {stop.name}
377
+ </span>
378
+ <span className="text-xs text-muted-foreground">
379
+ {active
380
+ ? "open"
381
+ : stop.cleared
382
+ ? stop.probablyKnown
383
+ ? "probably known"
384
+ : "cleared"
385
+ : stop.minutes === undefined
386
+ ? ""
387
+ : "about " + Math.round(stop.minutes) + " min"}
388
+ </span>
389
+ </div>
390
+ {active &&
391
+ asides.map((aside) => (
392
+ <div key={aside.id} className="pl-6 pt-3 flex items-center gap-3">
393
+ <span className="flex-1 text-muted-foreground">↳ Aside · {aside.title}</span>
394
+ <Button variant="ghost" onClick={() => void visit(aside.id)}>
395
+ Resume
396
+ </Button>
397
+ </div>
398
+ ))}
399
+ </div>
400
+ );
401
+ })}
402
+ </div>
403
+ ))}
404
+ {!data.sections.length && <p className="p-4 text-muted-foreground">No stops planned yet</p>}
405
+ </section>
406
+ {!!asides.length && !data.sections.some((section) => section.lessons.some((stop) => stop.active)) && (
407
+ <section className="space-y-3">
408
+ <h2 className="text-sm text-muted-foreground">Kept asides</h2>
409
+ {asides.map((aside) => (
410
+ <div key={aside.id} className="flex items-center gap-3 text-sm">
411
+ <span className="flex-1">{aside.title}</span>
412
+ <Button variant="outline" onClick={() => void visit(aside.id)}>
413
+ Resume
414
+ </Button>
415
+ </div>
416
+ ))}
417
+ </section>
418
+ )}
419
+ <section className="space-y-3">
420
+ <h2 className="text-sm text-muted-foreground">History</h2>
421
+ {!history.length && <p className="text-sm text-muted-foreground">No past Sessions yet</p>}
422
+ {history.map((entry, index) => (
423
+ <div key={index} className="border-b border-border py-3 text-sm">
424
+ {entry.session ? (
425
+ <button
426
+ className="text-left hover:underline"
427
+ onClick={() => {
428
+ if (entry.session) void visit(entry.session);
429
+ }}
430
+ >
431
+ {entry.parent ? "↳ Aside · " : ""}
432
+ {entry.label}
433
+ </button>
434
+ ) : (
435
+ <span>{entry.label}</span>
436
+ )}
437
+ <p className="text-xs text-muted-foreground mt-1">
438
+ {entry.session
439
+ ? sessions.find((session) => session.id === entry.session)?.closed
440
+ ? "closed · "
441
+ : "open · "
442
+ : ""}
443
+ {new Date(entry.at).toLocaleDateString()}
444
+ </p>
445
+ </div>
446
+ ))}
447
+ </section>
448
+ </>
449
+ );
450
+ }
@@ -0,0 +1,37 @@
1
+ import { z } from "zod";
2
+ import { skipStop } from "./entries";
3
+
4
+ const stop = z.object({
5
+ kind: z.string(),
6
+ name: z.string(),
7
+ nodes: z.array(z.string()),
8
+ number: z.number(),
9
+ numbered: z.boolean(),
10
+ cleared: z.boolean(),
11
+ probablyKnown: z.boolean(),
12
+ active: z.boolean(),
13
+ minutes: z.number().optional(),
14
+ checkpoint: z.boolean(),
15
+ skip: skipStop,
16
+ });
17
+ export const missionReading = z.object({
18
+ id: z.string(),
19
+ statement: z.string(),
20
+ active: z.string().nullable(),
21
+ retired: z.boolean(),
22
+ canReviseLesson: z.boolean(),
23
+ next: z.string().nullable(),
24
+ asides: z.array(z.string()),
25
+ sections: z.array(
26
+ z.object({ number: z.number(), label: z.string(), minutesLeft: z.number(), lessons: z.array(stop) }),
27
+ ),
28
+ history: z.array(
29
+ z.object({
30
+ kind: z.string(),
31
+ at: z.string(),
32
+ session: z.string().optional(),
33
+ parent: z.string().optional(),
34
+ label: z.string(),
35
+ }),
36
+ ),
37
+ });
package/src/reading.ts ADDED
@@ -0,0 +1,55 @@
1
+ import { z } from "zod";
2
+
3
+ export const learningReading = z.object({
4
+ missions: z.array(
5
+ z.object({
6
+ id: z.string(),
7
+ statement: z.string(),
8
+ active: z.boolean(),
9
+ retired: z.boolean(),
10
+ nodes: z.number(),
11
+ next: z.string().nullable(),
12
+ }),
13
+ ),
14
+ dueReview: z.array(z.object({ id: z.string(), label: z.string() })),
15
+ sessions: z.array(
16
+ z.object({
17
+ id: z.string(),
18
+ mode: z.string(),
19
+ closed: z.boolean(),
20
+ title: z.string(),
21
+ mission: z.string().optional(),
22
+ missionLabel: z.string().optional(),
23
+ parent: z.string().optional(),
24
+ nodes: z.array(z.object({ id: z.string(), label: z.string() })),
25
+ threadId: z.string().nullable().optional(),
26
+ }),
27
+ ),
28
+ });
29
+ export const mapReading = z.object({
30
+ nodes: z.array(
31
+ z.object({
32
+ id: z.string(),
33
+ label: z.string(),
34
+ status: z.string(),
35
+ frontier: z.boolean(),
36
+ x: z.number(),
37
+ y: z.number(),
38
+ }),
39
+ ),
40
+ edges: z.array(z.object({ from: z.string(), to: z.string() })),
41
+ /** `cone` is the Mission's targets and everything they need. */
42
+ missions: z.array(z.object({ id: z.string(), statement: z.string(), cone: z.array(z.string()) })),
43
+ });
44
+ /** The state a Node's mark shows, in the words the Map uses. */
45
+ export function stateName(node: { status: string; frontier: boolean }): string {
46
+ if (node.status === "done") return "demonstrated";
47
+ if (node.frontier) return "frontier";
48
+ return node.status === "tentative" ? "tentative" : "unknown";
49
+ }
50
+ export const profileChoice = z.object({
51
+ projectId: z.string(),
52
+ name: z.string(),
53
+ directory: z.string(),
54
+ hostId: z.string(),
55
+ });