Resizable assistant drawer, and the same drawer on share links
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 29s

- The assistant drawer can be resized by dragging its left edge (or with the
  arrow keys), from 340 to 760 px, always leaving the page 420 px; double-click
  resets it. The width is remembered in the browser.
- Share-link visitors get the same side-drawer assistant as the owner instead
  of the small popup, with a New chat button; a full-screen sheet on phones.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Elijah 2026-11-16 12:22:04 -07:00
parent 517278be71
commit e0bc7b580e
6 changed files with 263 additions and 71 deletions

View file

@ -12,6 +12,7 @@ import { cx, useDebounced, useHotkeys, useIsDesktop, useTheme, type ThemeChoice
import { Brand } from '@/pages/Auth';
import { Button, ClassDot, Dialog, Field, IconButton, Input, Menu, MenuItem, MenuLabel, MenuSeparator, Spinner, Textarea, Tooltip, Select } from '@/ui';
import { SidebarDrafts, useDraftNotifications } from '@/features/DraftsTray';
import { SideDrawer } from '@/ui/SideDrawer';
const AssistantPanel = lazy(() => import('@/features/AssistantPanel'));
@ -589,7 +590,7 @@ export function Shell() {
</main>
</div>
{showDrawer && (
<aside aria-label="Assistant" className="fixed top-0 right-0 z-40 hidden h-dvh w-[420px] shrink-0 animate-drawer flex-col border-l border-line bg-surface shadow-3 lg:flex min-[1360px]:sticky min-[1360px]:z-20 min-[1360px]:w-[440px] min-[1360px]:shadow-none">
<SideDrawer label="Assistant">
<Suspense fallback={<div className="grid flex-1 place-items-center"><Spinner /></div>}>
<AssistantPanel
variant="drawer"
@ -600,7 +601,7 @@ export function Shell() {
}
/>
</Suspense>
</aside>
</SideDrawer>
)}
{!showDrawer && !onAssistantPage && !immersive && isDesktop && (
<Tooltip label="Assistant" side="left">

View file

@ -1,10 +1,11 @@
import { CornerDownLeft, Sparkles, Square, X } from 'lucide-react';
import { ArrowUp, CircleAlert, Plus, Sparkles, Square, X } from 'lucide-react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { errorMessage, streamEvents } from '@/lib/api';
import type { ToolActivity } from '@/lib/types';
import { cx } from '@/lib/util';
import { cx, useLocalStorage } from '@/lib/util';
import { GenUiContext, Markdown } from '@/ui/Markdown';
import { Button, Callout, IconButton, Thinking, Tooltip } from '@/ui';
import { SideDrawer } from '@/ui/SideDrawer';
import { Callout, IconButton, Thinking, Tooltip } from '@/ui';
import { ToolSteps } from './ToolSteps';
type Event = { type: 'text'; delta: string } | { type: 'tool'; tool: ToolActivity } | { type: 'error'; message: string } | { type: 'done' };
@ -14,29 +15,41 @@ interface Turn {
tools?: ToolActivity[];
}
const SUGGESTIONS = ['Summarize the key points', 'Explain the hardest idea here simply', 'Quiz me with three quick questions'];
/**
* The visitor-facing assistant on a share link (when the owner allows it). It answers from the
* shared material only; the conversation lives in this tab and is never saved on the server.
* `hidden` tucks it away (during a quiz or flashcard session) without losing the conversation.
* The visitor-facing assistant on a share link (when the owner allows it), in the same
* resizable side drawer the owner's assistant uses. It answers from the shared material only;
* the conversation lives in this tab and is never saved on the server. `hidden` tucks it away
* (during a quiz or flashcard session) without losing the conversation.
*/
export function SharedAssistant({ token, title, item, hidden }: { token: string; title: string; item?: { kind: 'quiz' | 'deck' | 'note' | 'document'; id: string }; hidden?: boolean }) {
const [open, setOpen] = useState(false);
const [open, setOpen] = useLocalStorage('sd.pub.assistantOpen', false);
const [turns, setTurns] = useState<Turn[]>([]);
const [input, setInput] = useState('');
const [busy, setBusy] = useState(false);
const [error, setError] = useState('');
const abort = useRef<AbortController | null>(null);
const list = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLTextAreaElement>(null);
const stick = useRef(true);
useEffect(() => () => abort.current?.abort(), []);
useEffect(() => {
const el = list.current;
if (el) el.scrollTop = el.scrollHeight;
if (el && stick.current) el.scrollTop = el.scrollHeight;
}, [turns, open]);
useEffect(() => {
if (open && !hidden) inputRef.current?.focus();
}, [open, hidden]);
const send = useCallback(
async (text: string) => {
if (busy) return;
async (raw: string) => {
const text = raw.trim().slice(0, 4000);
if (busy || !text) return;
setInput('');
if (inputRef.current) inputRef.current.style.height = 'auto';
stick.current = true;
// The server accepts the last 16 turns; older ones are dropped from the request.
const history: Turn[] = [...turns, { role: 'user' as const, text }].slice(-15);
setTurns([...turns, { role: 'user', text }, { role: 'assistant', text: '', tools: [] }]);
@ -66,27 +79,35 @@ export function SharedAssistant({ token, title, item, hidden }: { token: string;
},
[busy, turns, token, item],
);
const genUi = useMemo(() => ({ streaming: busy, onSend: (text: string) => void send(text.slice(0, 4000)) }), [busy, send]);
const stop = () => abort.current?.abort();
const newChat = () => {
stop();
setTurns([]);
setError('');
setInput('');
inputRef.current?.focus();
};
const genUi = useMemo(() => ({ streaming: busy, onSend: (text: string) => void send(text) }), [busy, send]);
if (hidden) return null;
if (!open) {
// The same round sparkle button the owner sees in the app.
return (
<Tooltip label="Ask the assistant" side="left">
<Tooltip label="Assistant" side="left">
<button
type="button"
aria-label="Open the assistant"
onClick={() => setOpen(true)}
className="fixed right-5 bottom-[max(1.25rem,env(safe-area-inset-bottom))] z-40 grid size-11 place-items-center rounded-full border border-line-strong bg-surface/90 text-ink-2 shadow-1 backdrop-blur transition-colors hover:border-ink-3 hover:text-ink"
className="fixed right-5 bottom-[max(1.25rem,env(safe-area-inset-bottom))] z-40 grid size-11 place-items-center rounded-full border border-line-strong bg-surface/90 text-ink-2 shadow-1 backdrop-blur transition-colors hover:border-ink-3 hover:text-ink lg:size-10"
>
<Sparkles className="size-[19px]" />
<Sparkles className="size-[18px]" />
</button>
</Tooltip>
);
}
return (
<section aria-label="Study assistant" className="fixed right-4 bottom-4 z-40 flex h-[min(620px,calc(100dvh-2rem))] w-[min(440px,calc(100vw-2rem))] animate-in flex-col overflow-hidden rounded-[18px] border border-line bg-surface shadow-3 sm:right-6">
<header className="flex items-center gap-2 border-b border-line px-3 py-2.5">
<SideDrawer label="Assistant" fullOnPhone>
<header className="flex h-14 shrink-0 items-center gap-2 border-b border-line px-3">
<span className="grid size-8 shrink-0 place-items-center rounded-[10px] bg-ink text-marker">
<Sparkles className="size-4" />
</span>
@ -94,59 +115,108 @@ export function SharedAssistant({ token, title, item, hidden }: { token: string;
<div className="text-sm font-semibold">Assistant</div>
<div className="truncate text-xs text-ink-3">{title}</div>
</div>
<IconButton label="Close" size="sm" onClick={() => setOpen(false)}>
<IconButton label="New chat" size="sm" onClick={newChat}>
<Plus />
</IconButton>
<IconButton label="Close assistant" size="sm" onClick={() => setOpen(false)}>
<X />
</IconButton>
</header>
<div ref={list} className="flex-1 space-y-4 overflow-y-auto px-4 py-3">
{turns.length === 0 && <p className="text-[13.5px] text-ink-2">Ask a question about this material: what something means, why an answer is right, or for a quick recap.</p>}
<GenUiContext.Provider value={genUi}>
{turns.map((t, i) =>
t.role === 'user' ? (
<div key={i} className="flex justify-end">
<div className="max-w-[88%] rounded-[16px] rounded-br-md bg-ink px-3.5 py-2 text-[14px] whitespace-pre-wrap text-paper">{t.text}</div>
</div>
) : (
<div key={i} className="space-y-2">
{t.tools && t.tools.length > 0 && <ToolSteps tools={t.tools} compact />}
{t.text ? (
<Markdown className="text-[14px]">{t.text}</Markdown>
) : busy && i === turns.length - 1 && !t.tools?.some((x) => x.status === 'running') ? (
<Thinking />
) : null}
</div>
),
)}
</GenUiContext.Provider>
{error && (
<Callout tone="bad" className="py-2">
{error}
</Callout>
<div
ref={list}
onScroll={(e) => {
const el = e.currentTarget;
stick.current = el.scrollHeight - el.scrollTop - el.clientHeight < 80;
}}
className="min-h-0 flex-1 overflow-y-auto px-4 py-5"
aria-live="polite"
>
{turns.length === 0 ? (
<div className="mx-auto flex h-full max-w-md flex-col justify-center pb-6">
<h2 className="display text-[26px]">Ask about this material</h2>
<p className="mt-1.5 text-sm text-ink-2">What something means, why an answer is right, or a quick recap. Answers come from what was shared here.</p>
<div className="mt-5 space-y-2">
{SUGGESTIONS.map((s) => (
<button key={s} type="button" onClick={() => void send(s)} className="group flex w-full items-center gap-3 rounded-[13px] border border-line bg-surface px-3.5 py-2.5 text-left text-sm shadow-1 hover:border-ink-3">
<span className="flex-1">{s}</span>
<ArrowUp className="size-4 shrink-0 rotate-45 text-ink-3 group-hover:text-ink" />
</button>
))}
</div>
</div>
) : (
<GenUiContext.Provider value={genUi}>
<div className="mx-auto max-w-2xl space-y-6">
{turns.map((t, i) =>
t.role === 'user' ? (
<div key={i} className="flex justify-end">
<div className="max-w-[88%] rounded-[16px] rounded-br-md bg-ink px-3.5 py-2.5 text-[15px] whitespace-pre-wrap text-paper">{t.text}</div>
</div>
) : (
<div key={i} className="space-y-2.5">
{t.tools && t.tools.length > 0 && <ToolSteps tools={t.tools} />}
{t.text ? (
<Markdown className="text-[15px]">{t.text}</Markdown>
) : busy && i === turns.length - 1 && !t.tools?.some((x) => x.status === 'running') ? (
<Thinking />
) : null}
</div>
),
)}
{error && (
<Callout tone="bad" icon={<CircleAlert />} className="py-2.5">
{error}
</Callout>
)}
</div>
</GenUiContext.Provider>
)}
</div>
<form
className="flex gap-2 border-t border-line p-3"
onSubmit={(e) => {
e.preventDefault();
if (busy) return abort.current?.abort();
const text = input.trim();
if (!text) return;
setInput('');
void send(text.slice(0, 4000));
}}
>
<input
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder="Ask a question…"
aria-label="Your question"
className={cx('h-10 min-w-0 flex-1 rounded-[10px] border border-line-strong bg-paper px-3 text-sm outline-none focus:border-accent')}
/>
<Button type="submit" variant={busy ? 'secondary' : 'primary'} icon={busy ? <Square className="fill-current" /> : <CornerDownLeft />} aria-label={busy ? 'Stop' : 'Send'}>
{busy ? 'Stop' : 'Send'}
</Button>
</form>
<p className="px-4 pb-2.5 text-[11px] text-ink-3">AI can be wrong. Nothing you type here is saved.</p>
</section>
<div className="safe-pb-3 shrink-0 border-t border-line px-3 pt-2.5">
<div className="mx-auto max-w-2xl">
<form
className="rounded-[16px] border border-line-strong bg-surface p-1.5 shadow-1 focus-within:border-accent focus-within:ring-2 focus-within:ring-accent/20"
onSubmit={(e) => {
e.preventDefault();
if (busy) stop();
else void send(input);
}}
>
<div className="flex items-end gap-1.5">
<textarea
ref={inputRef}
value={input}
rows={1}
onChange={(e) => {
setInput(e.target.value);
e.target.style.height = 'auto';
e.target.style.height = `${Math.min(e.target.scrollHeight, 180)}px`;
}}
onKeyDown={(e) => {
if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing) {
e.preventDefault();
if (!busy) void send(input);
}
}}
placeholder="Ask anything about this material…"
aria-label="Message the assistant"
className="max-h-44 min-h-9 flex-1 resize-none bg-transparent px-2.5 py-1.5 text-[15px] outline-none placeholder:text-ink-3"
/>
<button
type="submit"
disabled={!busy && !input.trim()}
aria-label={busy ? 'Stop' : 'Send'}
className={cx('grid size-9 shrink-0 place-items-center rounded-[11px] transition-colors disabled:opacity-35', busy ? 'bg-sunken text-ink' : 'bg-ink text-paper hover:bg-ink/85')}
>
{busy ? <Square className="size-3.5 fill-current" /> : <ArrowUp className="size-[18px]" />}
</button>
</div>
</form>
<p className="px-1 pt-1.5 text-center text-[11px] text-ink-3">AI can be wrong. Nothing you type here is saved.</p>
</div>
</div>
</SideDrawer>
);
}

View file

@ -592,10 +592,11 @@ export default function SharedPage() {
return <SharedCollection data={data} token={token} />;
}
})();
// A row, so the assistant drawer can sit beside the page on wide screens (it floats over it below 1360px).
return (
<>
{page}
<div className="flex min-h-dvh">
<div className="min-w-0 flex-1">{page}</div>
{assistant}
</>
</div>
);
}

108
web/src/ui/SideDrawer.tsx Normal file
View file

@ -0,0 +1,108 @@
import { useEffect, useRef, useState, type ReactNode } from 'react';
import { cx, useIsDesktop, useLocalStorage } from '@/lib/util';
/** Width limits of the right-hand drawer (the assistant), in CSS pixels. */
export const DRAWER_MIN = 340;
export const DRAWER_MAX = 760;
export const DRAWER_DEFAULT = 440;
/** Room always left for the page beside the drawer. */
const PAGE_ROOM = 420;
export function clampDrawerWidth(width: number, viewport: number): number {
const max = Math.max(DRAWER_MIN, Math.min(DRAWER_MAX, viewport - PAGE_ROOM));
return Math.round(Math.min(Math.max(width, DRAWER_MIN), max));
}
/**
* The right-hand drawer the assistant lives in, for the owner and on share links. Drag its left
* edge (or focus the edge and use the arrow keys) to make it wider or narrower; double-click the
* edge to reset. The width is remembered in this browser. Below 1360px it floats over the page;
* from 1360px it sits beside the page. On phones, `fullOnPhone` makes it a full-screen sheet.
*/
export function SideDrawer({ label, children, fullOnPhone = false }: { label: string; children: ReactNode; fullOnPhone?: boolean }) {
const desktop = useIsDesktop();
const [stored, setStored] = useLocalStorage('sd.drawer.width', DRAWER_DEFAULT);
const [live, setLive] = useState<number | null>(null);
const [viewport, setViewport] = useState(() => window.innerWidth);
const dragging = useRef(false);
const dragged = useRef<number | null>(null);
useEffect(() => {
const onResize = () => setViewport(window.innerWidth);
window.addEventListener('resize', onResize);
return () => window.removeEventListener('resize', onResize);
}, []);
// Never leave the page mid-drag with text selection off.
useEffect(() => () => document.body.classList.remove('select-none', 'cursor-col-resize'), []);
if (!desktop) {
if (!fullOnPhone) return null;
return (
<aside aria-label={label} className="fixed inset-0 z-50 flex animate-in flex-col bg-surface">
{children}
</aside>
);
}
const width = clampDrawerWidth(live ?? stored, viewport);
const max = clampDrawerWidth(DRAWER_MAX, viewport);
const finish = () => {
if (!dragging.current) return;
dragging.current = false;
document.body.classList.remove('select-none', 'cursor-col-resize');
// The ref, not state: the release can arrive before the last move has re-rendered.
if (dragged.current !== null) setStored(dragged.current);
dragged.current = null;
setLive(null);
};
const nudge = (to: number) => setStored(clampDrawerWidth(to, window.innerWidth));
return (
<aside
aria-label={label}
style={{ width }}
className="fixed top-0 right-0 z-40 flex h-dvh shrink-0 animate-drawer flex-col border-l border-line bg-surface shadow-3 min-[1360px]:sticky min-[1360px]:z-20 min-[1360px]:shadow-none"
>
<div
role="separator"
aria-orientation="vertical"
aria-label={`Resize the ${label.toLowerCase()}`}
aria-valuemin={DRAWER_MIN}
aria-valuemax={max}
aria-valuenow={width}
tabIndex={0}
title="Drag to resize · double-click to reset"
onPointerDown={(e) => {
if (e.button !== 0) return;
e.preventDefault();
e.currentTarget.setPointerCapture(e.pointerId);
dragging.current = true;
document.body.classList.add('select-none', 'cursor-col-resize');
}}
onPointerMove={(e) => {
// The drawer is flush with the right edge, so its width is the distance from the pointer to that edge.
if (!dragging.current) return;
dragged.current = clampDrawerWidth(window.innerWidth - e.clientX, window.innerWidth);
setLive(dragged.current);
}}
onPointerUp={finish}
onPointerCancel={finish}
onLostPointerCapture={finish}
onDoubleClick={() => nudge(DRAWER_DEFAULT)}
onKeyDown={(e) => {
const step = e.shiftKey ? 80 : 24;
if (e.key === 'ArrowLeft') nudge(width + step);
else if (e.key === 'ArrowRight') nudge(width - step);
else if (e.key === 'Home') nudge(DRAWER_MIN);
else if (e.key === 'End') nudge(DRAWER_MAX);
else return;
e.preventDefault();
}}
className="group absolute inset-y-0 -left-1.5 z-10 w-3 cursor-col-resize touch-none outline-none"
>
<span className={cx('absolute inset-y-0 left-1/2 w-[3px] -translate-x-1/2 rounded-full transition-colors group-hover:bg-accent/45 group-focus-visible:bg-accent', live !== null && 'bg-accent')} />
</div>
{children}
</aside>
);
}