Fifth revision: share-link AI safety, sturdier generation, cleaner UI
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 1m0s

- Malformed model JSON is sent back for repair, then retried; a failed job
  starts over once before it fails. Visitors can dismiss failed sets.
- Generated sets get a short distinct name and description; flashcards are
  atomic and have no hints; standard study shows known/learning counts.
- Share links: "In progress" list, finished sets join the normal lists with a
  "New" tag, plain progress steps, newest exam first.
- Share-link AI expires after a week, can be limited to the US or Arizona
  (DB-IP Lite or Cloudflare headers) and is locked by abuse protection
  (migration 8).
- The assistant never answers from general knowledge; MCP and web search
  tools load without a manual test.
- Plain-text copy, no question count in exam mode, filler text removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Elijah 2026-11-16 09:17:50 -07:00
parent d8df5edf30
commit 85d5c9186d
46 changed files with 1242 additions and 283 deletions

View file

@ -1,18 +1,17 @@
/**
* Generating on a share link: visitors of a shared class or exam make their own NCLEX-style
* quiz or flashcard set from its class materials, with only the choices that matter (how many,
* how hard, which materials, med math). Finished sets are added to the class, so they appear
* with the rest of the link; this section shows the ones being written and the latest ones.
* The owner's link allows a few sets a day.
* how hard, which materials, med math). Finished sets join the link's regular lists (marked
* "New" for a while); this section only shows sets still being written, and failed ones until
* the visitor dismisses them. The owner's link allows a few sets a day.
*/
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { BookOpen, ChevronRight, CircleAlert, FileText, Layers, Sparkles } from 'lucide-react';
import { BookOpen, CircleAlert, FileText, Layers, Sparkles, X } from 'lucide-react';
import { useEffect, useState } from 'react';
import { Link } from 'react-router';
import { api, errorMessage } from '@/lib/api';
import type { PublicCollection, SharedGenerated } from '@/lib/types';
import { cx, plural } from '@/lib/util';
import { Button, Callout, Dialog, Field, Input, ProgressBar, Segmented, Select, Switch } from '@/ui';
import { cx } from '@/lib/util';
import { Button, Callout, Dialog, Field, IconButton, Input, ProgressBar, Segmented, Select, Switch } from '@/ui';
type Kind = 'quiz' | 'deck';
@ -34,73 +33,47 @@ export function SharedGenerate({ token, data }: { token: string; data: PublicCol
}, [readyKey, qc, token]);
if (!info) return null;
const left = (k: Kind) => Math.max(0, info.usage[k].limit - info.usage[k].used);
// Being written, failed, or made in the last day (older ones are simply part of the class).
const recent = info.sets.filter((s) => s.status !== 'ready' || Date.now() - s.createdAt < 86_400_000);
const quizzes = recent.filter((s) => s.kind === 'quiz');
const decks = recent.filter((s) => s.kind === 'deck');
// Finished sets are listed with the rest of the link; here only what is still being written (or failed).
const pending = info.sets.filter((s) => s.status === 'generating' || s.status === 'failed');
const dismiss = async (id: string) => {
qc.setQueryData<SharedGenerated>(key, (old) => (old ? { ...old, sets: old.sets.filter((s) => s.id !== id) } : old));
await api.pubPost(`/api/public/${token}/generated/${id}/dismiss`, {}).catch(() => undefined);
};
return (
<section className="mt-7" aria-label="Practice made with AI">
<div className="mb-2.5 flex flex-wrap items-end justify-between gap-3">
<div>
<h2 className="text-[17px] font-semibold">Make your own practice</h2>
<p className="text-[13px] text-ink-3">
NCLEX-style quizzes and flashcards from these class materials. Left today: {plural(left('quiz'), 'quiz', 'quizzes')}, {plural(left('deck'), 'flashcard set')}.
</p>
</div>
<h2 className="text-[17px] font-semibold">Make your own practice</h2>
<Button variant="primary" onClick={() => setOpen(true)} disabled={left('quiz') + left('deck') === 0}>
Generate
</Button>
</div>
{recent.length > 0 && (
{pending.length > 0 && (
<div className="card overflow-hidden">
{[
['quiz', 'Quizzes', quizzes] as const,
['deck', 'Flashcards', decks] as const,
]
.filter(([, , list]) => list.length > 0)
.map(([kind, heading, list], n) => (
<section key={kind} aria-label={`AI-made ${heading.toLowerCase()}`}>
<h3 className={cx('eyebrow flex items-center gap-2 border-b border-line bg-sunken/60 px-4 py-2 [&>svg]:size-3.5', n > 0 && 'border-t')}>
{kind === 'quiz' ? <BookOpen /> : <Layers />}
{heading} you made recently
<span className="num font-normal">{list.length}</span>
</h3>
<ul className="divide-y divide-line">
{list.map((s) => {
const icon = (
<span className={cx('grid size-10 shrink-0 place-items-center rounded-[11px] [&>svg]:size-[18px]', s.status === 'failed' ? 'bg-bad-soft text-bad' : kind === 'quiz' ? 'bg-accent-soft text-accent' : 'bg-marker/45 text-ink dark:bg-marker/20 dark:text-marker')}>
{s.status === 'failed' ? <CircleAlert /> : s.status === 'generating' ? <Sparkles className="animate-pulse" /> : kind === 'quiz' ? <BookOpen /> : <Layers />}
</span>
);
if (s.status !== 'ready' || !s.itemId) {
return (
<li key={s.id} className="flex items-center gap-3 px-4 py-3.5">
{icon}
<span className="min-w-0 flex-1">
<span className="block truncate text-[15px] font-medium">{s.title}</span>
<span className={cx('text-xs', s.status === 'failed' ? 'text-bad' : 'text-ink-3')}>{s.status === 'generating' ? s.progressLabel || 'Starting…' : s.error}</span>
{s.status === 'generating' && <ProgressBar value={Math.max(0.04, s.progress)} className="mt-1.5 h-1" label={`Making ${s.title}`} />}
</span>
</li>
);
}
return (
<li key={s.id}>
<Link to={`/s/${token}/${kind}/${s.itemId}`} className="flex items-center gap-3 px-4 py-3.5 hover:bg-ink/4">
{icon}
<span className="min-w-0 flex-1">
<span className="block truncate text-[15px] font-medium">{s.title}</span>
<span className="text-xs text-ink-3">{plural(s.count, kind === 'quiz' ? 'question' : 'card')}</span>
</span>
<ChevronRight className="size-4 text-ink-3" />
</Link>
</li>
);
})}
</ul>
</section>
<h3 className="eyebrow flex items-center gap-2 border-b border-line bg-sunken/60 px-4 py-2 [&>svg]:size-3.5">
<Sparkles />
In progress
<span className="num font-normal">{pending.filter((s) => s.status === 'generating').length || ''}</span>
</h3>
<ul className="divide-y divide-line">
{pending.map((s) => (
<li key={s.id} className="flex items-center gap-3 px-4 py-3.5">
<span className={cx('grid size-10 shrink-0 place-items-center rounded-[11px] [&>svg]:size-[18px]', s.status === 'failed' ? 'bg-bad-soft text-bad' : s.kind === 'quiz' ? 'bg-accent-soft text-accent' : 'bg-marker/45 text-ink dark:bg-marker/20 dark:text-marker')}>
{s.status === 'failed' ? <CircleAlert /> : s.kind === 'quiz' ? <BookOpen className="animate-pulse" /> : <Layers className="animate-pulse" />}
</span>
<span className="min-w-0 flex-1">
<span className="block truncate text-[15px] font-medium">{s.status === 'generating' ? (s.kind === 'quiz' ? 'New quiz' : 'New flashcards') : s.title}</span>
<span className={cx('text-xs', s.status === 'failed' ? 'text-bad' : 'text-ink-3')}>{s.status === 'generating' ? s.progressLabel || 'Starting…' : s.error}</span>
{s.status === 'generating' && <ProgressBar value={Math.max(0.04, s.progress)} className="mt-1.5 h-1" label="Progress" />}
</span>
{s.status === 'failed' && (
<IconButton label="Dismiss" size="sm" onClick={() => void dismiss(s.id)}>
<X />
</IconButton>
)}
</li>
))}
</ul>
</div>
)}
<GenerateDialog
@ -189,7 +162,6 @@ function GenerateDialog({
open={open}
onOpenChange={(o) => !o && onClose()}
title="Make your own practice"
description="Written by AI from this link's class materials and added to the class when done. It takes a minute or two; you can keep studying meanwhile."
footer={
<>
<Button variant="ghost" onClick={onClose}>
@ -217,7 +189,7 @@ function GenerateDialog({
/>
{none && <Callout tone="warn">This link has made all its {kind === 'quiz' ? 'quizzes' : 'flashcard sets'} for today. Try again tomorrow.</Callout>}
{exams.length > 0 && (
<Field label="Exam" hint="The set uses only this exam's class materials and is added to it.">
<Field label="Exam">
<Select
value={examId}
onChange={(e) => {
@ -239,7 +211,7 @@ function GenerateDialog({
</Field>
)}
<div className="grid gap-4 sm:grid-cols-2">
<Field label={kind === 'quiz' ? 'Questions' : 'Cards'} hint={`Up to ${max}.`}>
<Field label={kind === 'quiz' ? `Questions (max ${max})` : `Cards (max ${max})`}>
<Input type="number" min={1} max={max} value={count} onChange={(e) => setCount(Math.max(1, Math.min(max, Number(e.target.value) || 1)))} />
</Field>
<Field label="Difficulty">
@ -269,9 +241,8 @@ function GenerateDialog({
</div>
</div>
{kind === 'quiz' && (
<Switch checked={math} onChange={setMath} label="Include med math" description={`Adds ${info.limits.mathQuestions} dosage calculation questions at this difficulty, even if the materials have none.`} />
<Switch checked={math} onChange={setMath} label={`Include ${info.limits.mathQuestions} med math questions`} />
)}
<p className="text-xs text-ink-3">{kind === 'quiz' ? 'NCLEX style with about 25% select-all-that-apply, double-checked with Python.' : 'One fact per card.'} AI can make mistakes: check anything important against the class materials.</p>
{error && (
<Callout tone="bad" className="py-2">
{error}