diff --git a/apps/web/src/components/agent-header.tsx b/apps/web/src/components/agent-header.tsx index 30c7e39..679a33d 100644 --- a/apps/web/src/components/agent-header.tsx +++ b/apps/web/src/components/agent-header.tsx @@ -1,6 +1,6 @@ import * as stylex from "@stylexjs/stylex"; -import { Link } from "@tanstack/react-router"; -import type { ReactNode } from "react"; +import { Link, useRouter } from "@tanstack/react-router"; +import { type ReactNode, useEffect } from "react"; import type { Agent } from "../features/agents/schema"; import { useDashboardsEnabled } from "../features/dashboards/preference"; import { motion } from "../styles/motion.stylex"; @@ -19,6 +19,26 @@ export function AgentHeader({ children?: ReactNode; }) { const dashboardsEnabled = useDashboardsEnabled(); + const showDashboard = dashboard || dashboardsEnabled; + const router = useRouter(); + // Warm every tab so a click or tap renders from the router cache at once; + // the destination then refreshes in the background. Preloads within the + // router's preload stale time are free, so remounting the header is cheap. + useEffect(() => { + const timer = setTimeout(() => { + const params = { agentId: agent.id }; + const tabs = [ + "/agents/$agentId", + "/agents/$agentId/note", + ...(agent.kind === "coding" ? ["/agents/$agentId/jobs" as const] : []), + ...(showDashboard ? ["/agents/$agentId/dashboard" as const] : []), + ] as const; + for (const to of tabs) + if (!router.matchRoute({ to, params })) + void router.preloadRoute({ to, params }).catch(() => {}); + }, 250); + return () => clearTimeout(timer); + }, [router, agent.id, agent.kind, showDashboard]); return (
@@ -55,7 +75,7 @@ export function AgentHeader({ Jobs )} - {(dashboard || dashboardsEnabled) && ( + {showDashboard && ( void; conversationId?: string; - parent?: import("../features/chat/schema").Message; + parent?: Message; onOpenThread?: (id: string) => void; suggestion?: { id: number; text: string }; }) { @@ -350,6 +353,43 @@ export function Conversation({ }; }, [updateBottomButton]); + // Rows are memoized, so they get stable handlers that read the latest state. + const latest = useRef({ react, threads, onOpenThread }); + useLayoutEffect(() => { + latest.current = { react, threads, onOpenThread }; + }); + const reactToMessage = useCallback( + (messageId: string, emoji: string, active: boolean) => + latest.current.react(messageId, emoji, active), + [], + ); + const openReplyToMessage = useCallback( + async (messageId: string) => { + const { threads, onOpenThread } = latest.current; + if (!onOpenThread) return; + const existingThread = threads.find( + (thread) => thread.parentMessageId === messageId, + ); + if (existingThread) { + setThreadError(undefined); + onOpenThread(existingThread.id); + return; + } + try { + const result = await openThread({ + data: { agentId: agent.id, parentMessageId: messageId }, + }); + if (result.ok) { + setThreadError(undefined); + onOpenThread(result.value.id); + } else setThreadError(result.error); + } catch { + setThreadError("Could not open this thread. Please retry."); + } + }, + [agent.id], + ); + function replyLabel(messageId: string) { const thread = threads.find((t) => t.parentMessageId === messageId); return thread @@ -500,116 +540,23 @@ export function Conversation({ )} {threadError &&

{threadError}

} {messages.map((message, index) => { - const existingThread = threads.find( + const thread = threads.find( (thread) => thread.parentMessageId === message.id, ); - const openReply = async () => { - if (!onOpenThread) return; - if (existingThread) { - setThreadError(undefined); - onOpenThread(existingThread.id); - return; - } - try { - const result = await openThread({ - data: { - agentId: agent.id, - parentMessageId: message.id, - }, - }); - if (result.ok) { - setThreadError(undefined); - onOpenThread(result.value.id); - } else setThreadError(result.error); - } catch { - setThreadError("Could not open this thread. Please retry."); - } - }; - - const replyAction = onOpenThread && - message.role === "assistant" && ( - - ); return ( -
- {parent && - (message.role === "user" || - message.role === "assistant") ? ( - - react(message.id, emoji, active) - } - entering={entering.has(message.id)} - /> - ) : message.role === "user" ? ( - - } - files={message.files} - entering={entering.has(message.id)} - > - {message.text} - - ) : message.role === "notice" ? ( - - ) : message.role === "activity" ? ( - - ) : ( - - react(message.id, emoji, active) - } - /> - } - action={replyAction} - name={agent.name} - title={message.title} - files={message.files} - ui={message.ui} - entering={entering.has(message.id)} - > - {message.text} - - )} - {onOpenThread && existingThread && ( - - )} {computerEnabled && computerOpen && message.id === computerAnchor && ( @@ -622,7 +569,7 @@ export function Conversation({ /> )} -
+ ); })} {computerEnabled && @@ -736,6 +683,105 @@ const fadeIn = stylex.keyframes({ to: { opacity: 1 }, }); +// Polls and scroll bookkeeping re-render the conversation every second, so +// unchanged messages must not re-render with it. +const ConversationRow = memo(function ConversationRow({ + agent, + message, + previous, + inThread, + entering, + conversationId, + canReply, + replyLabel, + onReact, + onOpenReply, + children, +}: { + agent: Agent; + message: Message; + previous?: Message; + inThread: boolean; + entering: boolean; + conversationId: string; + canReply: boolean; + replyLabel?: string; + onReact: (messageId: string, emoji: string, active: boolean) => Promise; + onOpenReply: (messageId: string) => Promise; + children?: ReactNode; +}) { + const replyAction = canReply && message.role === "assistant" && ( + + ); + return ( +
+ {inThread && (message.role === "user" || message.role === "assistant") ? ( + onReact(message.id, emoji, active)} + entering={entering} + /> + ) : message.role === "user" ? ( + + } + files={message.files} + entering={entering} + > + {message.text} + + ) : message.role === "notice" ? ( + + ) : message.role === "activity" ? ( + + ) : ( + onReact(message.id, emoji, active)} + /> + } + action={replyAction} + name={agent.name} + title={message.title} + files={message.files} + ui={message.ui} + entering={entering} + > + {message.text} + + )} + {canReply && replyLabel && ( + + )} + {children} +
+ ); +}); + const styles = stylex.create({ replyAction: { display: "flex", diff --git a/apps/web/src/components/note-workspace.tsx b/apps/web/src/components/note-workspace.tsx index e1b3233..9247a89 100644 --- a/apps/web/src/components/note-workspace.tsx +++ b/apps/web/src/components/note-workspace.tsx @@ -50,7 +50,10 @@ type Pending = }; }; -export function NoteWorkspace({ initial }: { initial: NoteSnapshot }) { +export function NoteWorkspace({ initial: loaded }: { initial: NoteSnapshot }) { + // A tab switch can render from cached loader data; the poll below catches up + // and a later background reload must not re-run draft recovery. + const [initial] = useState(loaded); const scrollRef = useRef(null); const contentRef = useRef(null); const [base, setBase] = useState(initial); @@ -160,6 +163,8 @@ export function NoteWorkspace({ initial }: { initial: NoteSnapshot }) { setError(""); void refresh(); }; + // The opening snapshot may come from the router cache. + if (navigator.onLine) void refresh(); const timer = window.setInterval(() => { if (navigator.onLine) void refresh(); }, 5000); diff --git a/apps/web/src/router.tsx b/apps/web/src/router.tsx index 1d1be72..fa91339 100644 --- a/apps/web/src/router.tsx +++ b/apps/web/src/router.tsx @@ -4,6 +4,10 @@ import { routeTree } from "./routeTree.gen"; export function getRouter() { return createRouter({ routeTree, + // Agent tabs preload each other, so keep visited and preloaded loader data + // long enough that switching tabs renders from cache and revalidates. + defaultGcTime: 30 * 60_000, + defaultPreloadGcTime: 30 * 60_000, // Conversations follow their latest message and manage older-history scroll. scrollRestoration: ({ location }) => !/^\/agents\/[0-9a-f-]{36}(?:\/dashboard)?$/i.test(location.pathname),