TypeScript
refactor(web): ChatView's attachment preview handoff lives in useAttachmentPreviewHandoff (#23067)
Julius Marminge5b8298c23848
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex f9ca25f49c3..95fdfef8371 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -1,3 +1,4 @@+import { useAttachmentPreviewHandoff } from "./chat/useAttachmentPreviewHandoff";import { useTimelineRewind } from "./chat/useTimelineRewind";import { useThreadLifecycle } from "./chat/useThreadLifecycle";import { useRightPanelActions } from "./chat/useRightPanelActions";@@ -125,7 +126,6 @@ import { useActiveThreadRef } from "../hooks/useActiveThreadRef";import { composerRequiresModifier } from "../composer-logic";import {derivePhase,- selectHandoffImageResources,deriveActivePlanState,selectComposerTasksPlan,deriveActiveWorkStartedAt,@@ -343,7 +343,6 @@ import {resolveComposerProviderSelection,resolveThreadMetadataUpdateForNextTurn,resolveSendEnvMode,- revokeBlobPreviewUrl,revokeUserMessagePreviewUrls,startNewThreadForProject,codexArtifactTemplatePromptToAppend,@@ -359,7 +358,6 @@ import { useAtomCommand } from "../state/use-atom-command";import { useOrchestrationCommand } from "../state/use-orchestration-command";import { useAtomQueryRunner } from "../state/use-atom-query-runner";import { Button } from "./ui/button";-import { useAssetUrls } from "../assets/assetUrls";import { observeResize } from "~/lib/observeResize";import { ThreadVisitWatermark } from "./chat/ThreadVisitWatermark";import { ChatTerminalDrawers } from "./chat/ChatTerminalDrawers";@@ -697,10 +695,6 @@ export default function ChatView(props: ChatViewProps) {const feedbackUploading = feedbackSubmissions.some((submission) => submission.status === "uploading",);- const optimisticUserMessagesRef = useRef(optimisticUserMessages);- useLayoutEffect(() => {- optimisticUserMessagesRef.current = optimisticUserMessages;- });const [localDraftErrorsByDraftId, setLocalDraftErrorsByDraftId] = useState<Record<string, LocalThreadErrorEntry>>({});@@ -732,9 +726,6 @@ export default function ChatView(props: ChatViewProps) {const [terminalFocusRequestId, setTerminalFocusRequestId] = useState(0);const [terminalUiLaunchContext, setTerminalUiLaunchContext] =useState<TerminalLaunchContext | null>(null);- const [attachmentPreviewHandoffByMessageId, setAttachmentPreviewHandoffByMessageId] = useState<- Record<string, string[]>- >({});// Overrides chosen before a server thread's first turn; they belong to that thread only.const [pendingServerThreadEnvModeEntry, setPendingServerThreadEnvModeEntry] = useState<{readonly threadId: ThreadId;@@ -761,8 +752,6 @@ export default function ChatView(props: ChatViewProps) {const composerMountedRef = useRef(true);const [scrollToEndClearance, setScrollToEndClearance] = useState(0);const [timelineOverflows, setTimelineOverflows] = useState(false);- const attachmentPreviewHandoffByMessageIdRef = useRef<Record<string, string[]>>({});- const attachmentPreviewPromotionInFlightByMessageIdRef = useRef<Record<string, true>>({});const fanoutSelectionsAtom = draftFanoutSelectionsAtom(routeThreadKey);const { sendInFlight: sendInFlightRef } = draftSendState(routeThreadKey);const multipleModelSelections = useAtomValue(fanoutSelectionsAtom);@@ -1693,69 +1682,6 @@ export default function ChatView(props: ChatViewProps) {const sendQueuesBehindSetup =isServerThread && (activeRunPreparing || activeActivityRun?.status === "starting");const [setupQueueSendThreadKey, setSetupQueueSendThreadKey] = useState<string | null>(null);- useEffect(() => {- attachmentPreviewHandoffByMessageIdRef.current = attachmentPreviewHandoffByMessageId;- }, [attachmentPreviewHandoffByMessageId]);- const clearAttachmentPreviewHandoff = (- messageId: MessageId,- previewUrls?: ReadonlyArray<string>,- ) => {- delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];- const currentPreviewUrls =- previewUrls ?? attachmentPreviewHandoffByMessageIdRef.current[messageId] ?? [];- setAttachmentPreviewHandoffByMessageId((existing) => {- if (!(messageId in existing)) {- return existing;- }- const next = { ...existing };- delete next[messageId];- attachmentPreviewHandoffByMessageIdRef.current = next;- return next;- });- for (const previewUrl of currentPreviewUrls) {- revokeBlobPreviewUrl(previewUrl);- }- };- const clearAttachmentPreviewHandoffStable = useStableCallback(clearAttachmentPreviewHandoff);- const clearAttachmentPreviewHandoffs = () => {- attachmentPreviewPromotionInFlightByMessageIdRef.current = {};- for (const previewUrls of Object.values(attachmentPreviewHandoffByMessageIdRef.current)) {- for (const previewUrl of previewUrls) {- revokeBlobPreviewUrl(previewUrl);- }- }- attachmentPreviewHandoffByMessageIdRef.current = {};- setAttachmentPreviewHandoffByMessageId({});- };- const clearAttachmentPreviewHandoffsStable = useStableCallback(clearAttachmentPreviewHandoffs);- useEffect(() => {- return () => {- clearAttachmentPreviewHandoffsStable();- for (const message of optimisticUserMessagesRef.current) {- revokeUserMessagePreviewUrls(message);- }- };- }, [clearAttachmentPreviewHandoffsStable]);- const handoffAttachmentPreviews = (messageId: MessageId, previewUrls: string[]) => {- if (previewUrls.length === 0) return;-- const previousPreviewUrls = attachmentPreviewHandoffByMessageIdRef.current[messageId] ?? [];- const nextPreviewUrlSet = new Set(previewUrls);- for (const previewUrl of previousPreviewUrls) {- if (!nextPreviewUrlSet.has(previewUrl)) {- revokeBlobPreviewUrl(previewUrl);- }- }- setAttachmentPreviewHandoffByMessageId((existing) => {- const next = {- ...existing,- [messageId]: previewUrls,- };- attachmentPreviewHandoffByMessageIdRef.current = next;- return next;- });- };- const handoffAttachmentPreviewsStable = useStableCallback(handoffAttachmentPreviews);const downloadFileAttachment = async (attachment: ChatFileAttachment) => {const connection = readPreparedConnection(environmentId);if (!connection) {@@ -1790,132 +1716,12 @@ export default function ChatView(props: ChatViewProps) {}};const openFileAttachmentStable = useStableCallback(openFileAttachment);- const serverAttachmentResources = selectHandoffImageResources(- Object.keys(attachmentPreviewHandoffByMessageId).length === 0- ? undefined- : serverVisibleTurnItems.flatMap(({ item }) =>- item.type === "user_message"- ? [{ id: item.messageId, role: "user" as const, attachments: item.attachments }]- : [],- ),- attachmentPreviewHandoffByMessageId,- );- const serverAttachmentUrls = useAssetUrls(environmentId, serverAttachmentResources);- const serverAttachmentUrlById = new Map(- serverAttachmentResources.flatMap((resource, index) => {- const url = serverAttachmentUrls[index];- return url ? [[resource.attachmentId, url] as const] : [];- }),- );- useEffect(() => {- if (typeof Image === "undefined" || serverVisibleTurnItems.length === 0) {- return;- }-- const cleanups: Array<() => void> = [];- const userMessagesById = new Map(- serverVisibleTurnItems.flatMap((row) =>- row.item.type === "user_message" ? [[String(row.item.messageId), row.item] as const] : [],- ),- );-- for (const [messageId, handoffPreviewUrls] of Object.entries(- attachmentPreviewHandoffByMessageId,- )) {- if (attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId]) {- continue;- }-- const serverMessage = userMessagesById.get(messageId);- if (serverMessage === undefined || serverMessage.attachments.length === 0) {- continue;- }-- const serverPreviewUrls = serverMessage.attachments.flatMap((attachment) =>- attachment.type === "image"- ? [serverAttachmentUrlById.get(attachment.id)].filter(- (previewUrl): previewUrl is string => previewUrl !== undefined,- )- : [],- );- if (- serverPreviewUrls.length === 0 ||- serverPreviewUrls.length !== handoffPreviewUrls.length ||- serverPreviewUrls.some((previewUrl) => previewUrl.startsWith("blob:"))- ) {- continue;- }-- attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId] = true;-- let cancelled = false;- const imageInstances: HTMLImageElement[] = [];-- const preloadServerPreviews = Promise.all(- serverPreviewUrls.map(- (previewUrl) =>- new Promise<void>((resolve, reject) => {- const image = new Image();- imageInstances.push(image);- const handleLoad = () => resolve();- const handleError = () =>- reject(new Error(`Failed to load server preview for ${messageId}.`));- image.addEventListener("load", handleLoad, { once: true });- image.addEventListener("error", handleError, { once: true });- image.src = previewUrl;- }),- ),- );-- void preloadServerPreviews- .then(() => {- if (cancelled) {- return;- }- clearAttachmentPreviewHandoffStable(messageId as MessageId, handoffPreviewUrls);- })- .catch(() => {- if (!cancelled) {- delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];- }- });-- cleanups.push(() => {- cancelled = true;- delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];- for (const image of imageInstances) {- image.src = "";- }- });- }-- return () => {- for (const cleanup of cleanups) {- cleanup();- }- };- }, [- attachmentPreviewHandoffByMessageId,- clearAttachmentPreviewHandoffStable,- serverAttachmentUrlById,- serverVisibleTurnItems,- ]);- const timelineAttachmentUrlById = (() => {- const urls = new Map(serverAttachmentUrlById);- for (const row of serverVisibleTurnItems) {- if (row.item.type !== "user_message") continue;- const handoffUrls = attachmentPreviewHandoffByMessageId[row.item.messageId];- if (handoffUrls === undefined) continue;- let imageIndex = 0;- for (const attachment of row.item.attachments) {- if (attachment.type !== "image") continue;- const handoffUrl = handoffUrls[imageIndex];- imageIndex += 1;- if (handoffUrl !== undefined) urls.set(attachment.id, handoffUrl);- }- }- return urls;- })();+ const { handoffAttachmentPreviewsStable, timelineAttachmentUrlById } =+ useAttachmentPreviewHandoff({+ environmentId,+ serverVisibleTurnItems,+ optimisticUserMessages,+ });const anchoredTimelineMessages = feedbackSubmissions.flatMap((submission) =>submission.status === "interrupted"? []diff --git a/apps/web/src/components/chat/useAttachmentPreviewHandoff.ts b/apps/web/src/components/chat/useAttachmentPreviewHandoff.tsnew file mode 100644index 00000000000..4012c7f1e55--- /dev/null+++ b/apps/web/src/components/chat/useAttachmentPreviewHandoff.ts@@ -0,0 +1,225 @@+import { useStableCallback } from "../../hooks/useStableCallback";+import { type EnvironmentId, type MessageId } from "@t3tools/contracts";+import { useEffect, useLayoutEffect, useRef, useState } from "react";+import { selectHandoffImageResources } from "../../session-logic";+import { type ChatMessage } from "../../types";+import type { useThreadVisibleTurnItems } from "../../state/entities";+import { revokeBlobPreviewUrl, revokeUserMessagePreviewUrls } from "../ChatView.logic";+import { useAssetUrls } from "../../assets/assetUrls";+interface AttachmentPreviewHandoffInput {+ readonly environmentId: EnvironmentId;+ readonly serverVisibleTurnItems: ReturnType<typeof useThreadVisibleTurnItems>;+ readonly optimisticUserMessages: ReadonlyArray<ChatMessage>;+}++/**+ * A sent image keeps showing its local blob preview until the server's copy has loaded, so the+ * timeline does not flash an empty frame. Returns the attachment URLs the timeline renders and+ * the handoff for a message whose optimistic row was replaced by the server's.+ */+export function useAttachmentPreviewHandoff(input: AttachmentPreviewHandoffInput) {+ const { environmentId, serverVisibleTurnItems, optimisticUserMessages } = input;++ const optimisticUserMessagesRef = useRef(optimisticUserMessages);+ useLayoutEffect(() => {+ optimisticUserMessagesRef.current = optimisticUserMessages;+ });+ const [attachmentPreviewHandoffByMessageId, setAttachmentPreviewHandoffByMessageId] = useState<+ Record<string, string[]>+ >({});+ const attachmentPreviewHandoffByMessageIdRef = useRef<Record<string, string[]>>({});+ const attachmentPreviewPromotionInFlightByMessageIdRef = useRef<Record<string, true>>({});+ useEffect(() => {+ attachmentPreviewHandoffByMessageIdRef.current = attachmentPreviewHandoffByMessageId;+ }, [attachmentPreviewHandoffByMessageId]);+ const clearAttachmentPreviewHandoff = (+ messageId: MessageId,+ previewUrls?: ReadonlyArray<string>,+ ) => {+ delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];+ const currentPreviewUrls =+ previewUrls ?? attachmentPreviewHandoffByMessageIdRef.current[messageId] ?? [];+ setAttachmentPreviewHandoffByMessageId((existing) => {+ if (!(messageId in existing)) {+ return existing;+ }+ const next = { ...existing };+ delete next[messageId];+ attachmentPreviewHandoffByMessageIdRef.current = next;+ return next;+ });+ for (const previewUrl of currentPreviewUrls) {+ revokeBlobPreviewUrl(previewUrl);+ }+ };+ const clearAttachmentPreviewHandoffStable = useStableCallback(clearAttachmentPreviewHandoff);+ const clearAttachmentPreviewHandoffs = () => {+ attachmentPreviewPromotionInFlightByMessageIdRef.current = {};+ for (const previewUrls of Object.values(attachmentPreviewHandoffByMessageIdRef.current)) {+ for (const previewUrl of previewUrls) {+ revokeBlobPreviewUrl(previewUrl);+ }+ }+ attachmentPreviewHandoffByMessageIdRef.current = {};+ setAttachmentPreviewHandoffByMessageId({});+ };+ const clearAttachmentPreviewHandoffsStable = useStableCallback(clearAttachmentPreviewHandoffs);+ useEffect(() => {+ return () => {+ clearAttachmentPreviewHandoffsStable();+ for (const message of optimisticUserMessagesRef.current) {+ revokeUserMessagePreviewUrls(message);+ }+ };+ }, [clearAttachmentPreviewHandoffsStable]);+ const handoffAttachmentPreviews = (messageId: MessageId, previewUrls: string[]) => {+ if (previewUrls.length === 0) return;++ const previousPreviewUrls = attachmentPreviewHandoffByMessageIdRef.current[messageId] ?? [];+ const nextPreviewUrlSet = new Set(previewUrls);+ for (const previewUrl of previousPreviewUrls) {+ if (!nextPreviewUrlSet.has(previewUrl)) {+ revokeBlobPreviewUrl(previewUrl);+ }+ }+ setAttachmentPreviewHandoffByMessageId((existing) => {+ const next = {+ ...existing,+ [messageId]: previewUrls,+ };+ attachmentPreviewHandoffByMessageIdRef.current = next;+ return next;+ });+ };+ const handoffAttachmentPreviewsStable = useStableCallback(handoffAttachmentPreviews);+ const serverAttachmentResources = selectHandoffImageResources(+ Object.keys(attachmentPreviewHandoffByMessageId).length === 0+ ? undefined+ : serverVisibleTurnItems.flatMap(({ item }) =>+ item.type === "user_message"+ ? [{ id: item.messageId, role: "user" as const, attachments: item.attachments }]+ : [],+ ),+ attachmentPreviewHandoffByMessageId,+ );+ const serverAttachmentUrls = useAssetUrls(environmentId, serverAttachmentResources);+ const serverAttachmentUrlById = new Map(+ serverAttachmentResources.flatMap((resource, index) => {+ const url = serverAttachmentUrls[index];+ return url ? [[resource.attachmentId, url] as const] : [];+ }),+ );+ useEffect(() => {+ if (typeof Image === "undefined" || serverVisibleTurnItems.length === 0) {+ return;+ }++ const cleanups: Array<() => void> = [];+ const userMessagesById = new Map(+ serverVisibleTurnItems.flatMap((row) =>+ row.item.type === "user_message" ? [[String(row.item.messageId), row.item] as const] : [],+ ),+ );++ for (const [messageId, handoffPreviewUrls] of Object.entries(+ attachmentPreviewHandoffByMessageId,+ )) {+ if (attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId]) {+ continue;+ }++ const serverMessage = userMessagesById.get(messageId);+ if (serverMessage === undefined || serverMessage.attachments.length === 0) {+ continue;+ }++ const serverPreviewUrls = serverMessage.attachments.flatMap((attachment) =>+ attachment.type === "image"+ ? [serverAttachmentUrlById.get(attachment.id)].filter(+ (previewUrl): previewUrl is string => previewUrl !== undefined,+ )+ : [],+ );+ if (+ serverPreviewUrls.length === 0 ||+ serverPreviewUrls.length !== handoffPreviewUrls.length ||+ serverPreviewUrls.some((previewUrl) => previewUrl.startsWith("blob:"))+ ) {+ continue;+ }++ attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId] = true;++ let cancelled = false;+ const imageInstances: HTMLImageElement[] = [];++ const preloadServerPreviews = Promise.all(+ serverPreviewUrls.map(+ (previewUrl) =>+ new Promise<void>((resolve, reject) => {+ const image = new Image();+ imageInstances.push(image);+ const handleLoad = () => resolve();+ const handleError = () =>+ reject(new Error(`Failed to load server preview for ${messageId}.`));+ image.addEventListener("load", handleLoad, { once: true });+ image.addEventListener("error", handleError, { once: true });+ image.src = previewUrl;+ }),+ ),+ );++ void preloadServerPreviews+ .then(() => {+ if (cancelled) {+ return;+ }+ clearAttachmentPreviewHandoffStable(messageId as MessageId, handoffPreviewUrls);+ })+ .catch(() => {+ if (!cancelled) {+ delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];+ }+ });++ cleanups.push(() => {+ cancelled = true;+ delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];+ for (const image of imageInstances) {+ image.src = "";+ }+ });+ }++ return () => {+ for (const cleanup of cleanups) {+ cleanup();+ }+ };+ }, [+ attachmentPreviewHandoffByMessageId,+ clearAttachmentPreviewHandoffStable,+ serverAttachmentUrlById,+ serverVisibleTurnItems,+ ]);+ const timelineAttachmentUrlById = (() => {+ const urls = new Map(serverAttachmentUrlById);+ for (const row of serverVisibleTurnItems) {+ if (row.item.type !== "user_message") continue;+ const handoffUrls = attachmentPreviewHandoffByMessageId[row.item.messageId];+ if (handoffUrls === undefined) continue;+ let imageIndex = 0;+ for (const attachment of row.item.attachments) {+ if (attachment.type !== "image") continue;+ const handoffUrl = handoffUrls[imageIndex];+ imageIndex += 1;+ if (handoffUrl !== undefined) urls.set(attachment.id, handoffUrl);+ }+ }+ return urls;+ })();+ return {+ handoffAttachmentPreviewsStable,+ timelineAttachmentUrlById,+ };+}