Third revision: web search, MCP sign-in, exams, intelligent UI, clarifying questions
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 1m0s

- Built-in Parallel web search (keyless; optional sign-in for higher limits and research)
  with its own settings page; OAuth sign-in (RFC 9728/8414/7591, PKCE, RFC 8707) for any
  MCP server, with token refresh and pre-registered client support
- Friendly tool steps (icon, name, status; details on click) and ids stripped from results;
  per-chat tool switches; taller thinking box
- Exams within classes: exam tabs, scoped lists, uploads, imports and Generate; exam-only
  material for generation; class setting for what the groups are called
- Assistant can ask clarifying questions in an inline form before generating
- OpenUI blocks (charts, questions, flashcards, inputs) in assistant, Explain and share chats
- Explain beside the quiz on wide screens, grounded in the cited source pages
- Quiz results: percentage ring coloured by score band, rainbow at 100
- Share links: assistant button like the owner's, hidden during quizzes; documents readable
  in the browser
- Live draft and library updates over server-sent events

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Elijah 2026-11-15 20:30:00 -07:00
parent 60cf2322f7
commit c595e0ff2c
54 changed files with 4710 additions and 387 deletions

View file

@ -1,5 +1,5 @@
import { useQuery } from '@tanstack/react-query';
import { ArrowLeft, BookOpen, ChevronRight, Download, FileText, Layers, Link2Off, Moon, NotebookPen, Play, RotateCcw, Sun, Timer } from 'lucide-react';
import { useInfiniteQuery, useQuery } from '@tanstack/react-query';
import { AlignLeft, ArrowLeft, BookOpen, Eye, ChevronRight, Download, FileText, Layers, Link2Off, Moon, NotebookPen, Play, RotateCcw, Sun, Timer } from 'lucide-react';
import { useEffect, useMemo, useState, type ReactNode } from 'react';
import { Link, useNavigate, useParams } from 'react-router';
import { isFullyCorrect, scoreQuestion, seededShuffle } from '@shared/content';
@ -9,18 +9,18 @@ import { SharedAssistant } from '@/features/SharedAssistant';
import { QuizResults, QuizRunner } from '@/features/quiz';
import { api, ApiError } from '@/lib/api';
import type { AnswerState, PublicCollection, PublicDeck, PublicDocument, PublicNote, PublicPayload, PublicQuiz, SnapshotQuestion } from '@/lib/types';
import { cx, formatBytes, plural, useDocumentTitle, useLocalStorage, useTheme } from '@/lib/util';
import { cx, formatBytes, plural, useDocumentTitle, useLocalStorage, useMediaQuery, useTheme } from '@/lib/util';
import { Markdown } from '@/ui/Markdown';
import { Button, EmptyState, IconButton, Input, PageLoading, Switch } from '@/ui';
import { Button, EmptyState, IconButton, Input, PageLoading, Segmented, Switch } from '@/ui';
/* ------------------------------ frame ------------------------------ */
function Frame({ children, back }: { children: ReactNode; back?: { to: string; label: string } }) {
function Frame({ children, back, wide }: { children: ReactNode; back?: { to: string; label: string }; wide?: boolean }) {
const [, setTheme] = useTheme();
const dark = document.documentElement.dataset.theme === 'dark';
return (
<div className="min-h-dvh">
<header className="mx-auto flex h-14 max-w-3xl items-center gap-2 px-4 sm:px-5">
<header className={cx('mx-auto flex h-14 items-center gap-2 px-4 sm:px-5', wide ? 'max-w-5xl' : 'max-w-3xl')}>
{back ? (
<Link to={back.to} className="-ml-2 flex h-9 items-center gap-1.5 rounded-lg px-2 text-sm font-medium text-ink-2 hover:bg-ink/6 hover:text-ink">
<ArrowLeft className="size-4" /> {back.label}
@ -36,7 +36,7 @@ function Frame({ children, back }: { children: ReactNode; back?: { to: string; l
{dark ? <Sun /> : <Moon />}
</IconButton>
</header>
<main className="mx-auto max-w-3xl animate-in px-4 pt-4 pb-20 sm:px-5 sm:pt-8">{children}</main>
<main className={cx('mx-auto animate-in px-4 pt-4 pb-20 sm:px-5 sm:pt-8', wide ? 'max-w-5xl' : 'max-w-3xl')}>{children}</main>
<footer className="pb-8 text-center text-xs text-ink-3">Shared with Study Desk. Your progress is saved in this browser only.</footer>
</div>
);
@ -118,7 +118,7 @@ function buildQuizState(quiz: PublicQuiz, mode: QuizState['mode'], shuffleQ: boo
};
}
function SharedQuiz({ quiz, storageKey, back, explainUrl }: { quiz: PublicQuiz; storageKey: string; back?: { to: string; label: string }; explainUrl?: string }) {
function SharedQuiz({ quiz, storageKey, back, explainUrl, onImmersive }: { quiz: PublicQuiz; storageKey: string; back?: { to: string; label: string }; explainUrl?: string; onImmersive?: (on: boolean) => void }) {
const renderExtra = explainUrl ? (q: SnapshotQuestion, selected: string[]) => <Explain url={explainUrl} body={{ kind: 'question', quizId: quiz.id, questionId: q.id, selected }} /> : undefined;
const [state, setState] = useLocalStorage<QuizState | null>(storageKey, null);
const [prefs, setPrefs] = useLocalStorage('sd.pub.prefs2', { mode: 'practice' as 'practice' | 'exam', shuffleQ: true, shuffleO: true, timed: false, minutes: 0 });
@ -142,6 +142,12 @@ function SharedQuiz({ quiz, storageKey, back, explainUrl }: { quiz: PublicQuiz;
}, [quiz, state]);
const changed = Boolean(state?.version && state.version !== quizVersion(quiz));
// While a quiz is being taken the assistant button steps aside.
const taking = Boolean(state && questions.length > 0 && running && !state.done);
useEffect(() => {
onImmersive?.(taking);
return () => onImmersive?.(false);
}, [taking, onImmersive]);
const expired = Boolean(state && !state.done && state.deadlineAt && Date.now() > state.deadlineAt);
// A timed exam whose deadline passed while the page was closed is submitted as it stands.
@ -288,9 +294,13 @@ function SharedQuiz({ quiz, storageKey, back, explainUrl }: { quiz: PublicQuiz;
/* ------------------------------ deck ------------------------------ */
function SharedDeck({ deck, storageKey, back, explainUrl }: { deck: PublicDeck; storageKey: string; back?: { to: string; label: string }; explainUrl?: string }) {
function SharedDeck({ deck, storageKey, back, explainUrl, onImmersive }: { deck: PublicDeck; storageKey: string; back?: { to: string; label: string }; explainUrl?: string; onImmersive?: (on: boolean) => void }) {
const [saved, setSaved] = useLocalStorage<CramState | null>(storageKey, null);
const [running, setRunning] = useState(false);
useEffect(() => {
onImmersive?.(running);
return () => onImmersive?.(false);
}, [running, onImmersive]);
const state = useMemo(() => reconcileCramState(saved, deck.cards), [saved, deck.cards]);
if (running && state) {
@ -378,24 +388,92 @@ function SharedNote({ note, back }: { note: PublicNote; back?: { to: string; lab
);
}
function SharedDocument({ doc, token, nested, back }: { doc: PublicDocument; token: string; nested?: boolean; back?: { to: string; label: string } }) {
const url = nested ? `/api/public/${token}/items/document/${doc.id}/file` : `/api/public/${token}/file`;
const inline = doc.docKind === 'pdf' || doc.docKind === 'image';
interface TextChunk {
id: number;
seq: number;
text: string;
heading: string;
pageStart: number | null;
pageEnd: number | null;
}
/** The document's extracted text, page by page: readable on any device, including phones. */
function SharedDocumentText({ base }: { base: string }) {
const q = useInfiniteQuery({
queryKey: ['public', 'text', base],
queryFn: ({ pageParam, signal }) => api.pub<{ chunks: TextChunk[]; nextOffset: number | null }>(`${base}/text?offset=${pageParam}&limit=30`, signal),
initialPageParam: 0,
getNextPageParam: (last) => last.nextOffset ?? undefined,
});
const chunks = q.data?.pages.flatMap((p) => p.chunks) ?? [];
if (q.isLoading) return <div className="card mt-5 h-64 animate-pulse" />;
if (chunks.length === 0) return <div className="card mt-5 p-6 text-sm text-ink-2">No readable text was found in this file. Download it to open it.</div>;
let lastPage: number | null = null;
return (
<Frame back={back}>
<article className="card mt-5 px-5 py-6 sm:px-9 sm:py-8">
{chunks.map((c) => {
const newPage = c.pageStart !== null && c.pageStart !== lastPage;
if (c.pageStart !== null) lastPage = c.pageEnd ?? c.pageStart;
return (
<section key={c.id} className="mb-5">
{newPage && <div className="eyebrow mb-2 border-t border-line pt-3 first:border-0 first:pt-0">Page {c.pageStart}</div>}
{c.heading && <h3 className="mb-1.5 text-[15px] font-semibold">{c.heading}</h3>}
<div className="reading text-[16px] leading-relaxed whitespace-pre-wrap">{c.text}</div>
</section>
);
})}
{q.hasNextPage && (
<Button variant="secondary" block loading={q.isFetchingNextPage} onClick={() => q.fetchNextPage()}>
Show more
</Button>
)}
</article>
);
}
function SharedDocument({ doc, token, nested, back }: { doc: PublicDocument; token: string; nested?: boolean; back?: { to: string; label: string } }) {
const base = nested ? `/api/public/${token}/items/document/${doc.id}` : `/api/public/${token}`;
const fileUrl = `${base}/file`;
const office = doc.docKind === 'docx' || doc.docKind === 'pptx';
const hasOriginal = doc.docKind === 'pdf' || doc.docKind === 'image' || office;
// Phones rarely show PDFs inside a page, so they start on the text.
const phone = useMediaQuery('(max-width: 639px)');
const [view, setView] = useState<'original' | 'text'>(hasOriginal && !(phone && doc.docKind === 'pdf') ? 'original' : 'text');
return (
<Frame back={back} wide>
<div className="eyebrow mb-2 flex items-center gap-1.5">
<FileText className="size-3.5" /> Shared document
</div>
<h1 className="display text-[30px] sm:text-[36px]">{doc.title}</h1>
<p className="mt-2 text-sm text-ink-3">
{doc.filename} · {formatBytes(doc.size)}
{doc.pageCount ? ` · ${plural(doc.pageCount, 'page')}` : ''}
</p>
<Button href={url} download={inline ? undefined : doc.filename} target={inline ? '_blank' : undefined} variant="primary" size="lg" icon={<Download />} className="mt-5">
{inline ? 'Open file' : 'Download'}
</Button>
{doc.docKind === 'image' && <img src={url} alt={doc.title} className="card mt-6 max-w-full p-2" />}
{doc.docKind === 'pdf' && <iframe src={url} title={doc.title} className="card mt-6 hidden h-[78vh] w-full sm:block" />}
<div className="mt-3 flex flex-wrap items-center gap-3">
{hasOriginal && (
<Segmented
size="sm"
label="View"
value={view}
onChange={setView}
options={[
{ value: 'original', label: 'Original', icon: <Eye /> },
{ value: 'text', label: 'Text', icon: <AlignLeft /> },
]}
/>
)}
<span className="text-sm text-ink-3">
{doc.filename} · {formatBytes(doc.size)}
{doc.pageCount ? ` · ${plural(doc.pageCount, 'page')}` : ''}
</span>
<Button href={fileUrl} download={doc.filename} size="sm" variant="ghost" icon={<Download />} className="ml-auto">
Download
</Button>
</div>
{view === 'text' || !hasOriginal ? (
<SharedDocumentText base={base} />
) : doc.docKind === 'image' ? (
<img src={fileUrl} alt={doc.title} className="card mt-5 max-w-full p-2" />
) : (
// Office previews are self-contained HTML that the server sandboxes with its Content-Security-Policy.
<iframe src={office ? `${base}/preview` : fileUrl} title={doc.title} className="card mt-5 h-[80vh] w-full bg-surface" />
)}
</Frame>
);
}
@ -444,6 +522,7 @@ export default function SharedPage() {
const { token = '', kind, itemId } = useParams();
const navigate = useNavigate();
const nested = Boolean(kind && itemId);
const [immersive, setImmersive] = useState(false);
const q = useQuery({
queryKey: ['public', token, kind ?? '', itemId ?? ''],
queryFn: ({ signal }) => api.pub<PublicPayload>(nested ? `/api/public/${token}/items/${kind}/${itemId}` : `/api/public/${token}`, signal),
@ -473,13 +552,13 @@ export default function SharedPage() {
const back = nested ? { to: `/s/${token}`, label: 'All materials' } : undefined;
const key = `sd.pub.${token.slice(0, 12)}.${data.kind}.${'id' in data ? data.id : ''}`;
const explainUrl = data.ai?.explain ? `/api/public/${token}/explain` : undefined;
const assistant = data.ai?.assistant ? <SharedAssistant token={token} title={data.title} item={nested && data.kind !== 'collection' ? { kind: data.kind, id: data.id } : undefined} /> : null;
const assistant = data.ai?.assistant ? <SharedAssistant token={token} title={data.title} hidden={immersive} item={nested && data.kind !== 'collection' ? { kind: data.kind, id: data.id } : undefined} /> : null;
const page = (() => {
switch (data.kind) {
case 'quiz':
return <SharedQuiz key={key} quiz={data} storageKey={key} back={back} explainUrl={explainUrl} />;
return <SharedQuiz key={key} quiz={data} storageKey={key} back={back} explainUrl={explainUrl} onImmersive={setImmersive} />;
case 'deck':
return <SharedDeck key={key} deck={data} storageKey={key} back={back} explainUrl={explainUrl} />;
return <SharedDeck key={key} deck={data} storageKey={key} back={back} explainUrl={explainUrl} onImmersive={setImmersive} />;
case 'note':
return <SharedNote note={data} back={back} />;
case 'document':