@devries-cameron20 / t3code Mirror · read-only / commits / 5b8298c238
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 19:30 UTC

refactor(web): ChatView's attachment preview handoff lives in useAttachmentPreviewHandoff (#23067)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 21, 2026 at 13:40 UTC 5b8298c23848ffe20f2185a48d6d064eb18e1c77
2 files changed +232 -201
apps/web/src/components/ChatView.tsx
+7 -201
@@ -1,3 +1,4 @@
1 +import { useAttachmentPreviewHandoff } from "./chat/useAttachmentPreviewHandoff";
2 import { useTimelineRewind } from "./chat/useTimelineRewind";
3 import { useThreadLifecycle } from "./chat/useThreadLifecycle";
4 import { useRightPanelActions } from "./chat/useRightPanelActions";
@@ -125,7 +126,6 @@ import { useActiveThreadRef } from "../hooks/useActiveThreadRef";
126 import { composerRequiresModifier } from "../composer-logic";
127 import {
128 derivePhase,
128 - selectHandoffImageResources,
129 deriveActivePlanState,
130 selectComposerTasksPlan,
131 deriveActiveWorkStartedAt,
@@ -343,7 +343,6 @@ import {
343 resolveComposerProviderSelection,
344 resolveThreadMetadataUpdateForNextTurn,
345 resolveSendEnvMode,
346 - revokeBlobPreviewUrl,
346 revokeUserMessagePreviewUrls,
347 startNewThreadForProject,
348 codexArtifactTemplatePromptToAppend,
@@ -359,7 +358,6 @@ import { useAtomCommand } from "../state/use-atom-command";
358 import { useOrchestrationCommand } from "../state/use-orchestration-command";
359 import { useAtomQueryRunner } from "../state/use-atom-query-runner";
360 import { Button } from "./ui/button";
362 -import { useAssetUrls } from "../assets/assetUrls";
361 import { observeResize } from "~/lib/observeResize";
362 import { ThreadVisitWatermark } from "./chat/ThreadVisitWatermark";
363 import { ChatTerminalDrawers } from "./chat/ChatTerminalDrawers";
@@ -697,10 +695,6 @@ export default function ChatView(props: ChatViewProps) {
695 const feedbackUploading = feedbackSubmissions.some(
696 (submission) => submission.status === "uploading",
697 );
700 - const optimisticUserMessagesRef = useRef(optimisticUserMessages);
701 - useLayoutEffect(() => {
702 - optimisticUserMessagesRef.current = optimisticUserMessages;
703 - });
698 const [localDraftErrorsByDraftId, setLocalDraftErrorsByDraftId] = useState<
699 Record<string, LocalThreadErrorEntry>
700 >({});
@@ -732,9 +726,6 @@ export default function ChatView(props: ChatViewProps) {
726 const [terminalFocusRequestId, setTerminalFocusRequestId] = useState(0);
727 const [terminalUiLaunchContext, setTerminalUiLaunchContext] =
728 useState<TerminalLaunchContext | null>(null);
735 - const [attachmentPreviewHandoffByMessageId, setAttachmentPreviewHandoffByMessageId] = useState<
736 - Record<string, string[]>
737 - >({});
729 // Overrides chosen before a server thread's first turn; they belong to that thread only.
730 const [pendingServerThreadEnvModeEntry, setPendingServerThreadEnvModeEntry] = useState<{
731 readonly threadId: ThreadId;
@@ -761,8 +752,6 @@ export default function ChatView(props: ChatViewProps) {
752 const composerMountedRef = useRef(true);
753 const [scrollToEndClearance, setScrollToEndClearance] = useState(0);
754 const [timelineOverflows, setTimelineOverflows] = useState(false);
764 - const attachmentPreviewHandoffByMessageIdRef = useRef<Record<string, string[]>>({});
765 - const attachmentPreviewPromotionInFlightByMessageIdRef = useRef<Record<string, true>>({});
755 const fanoutSelectionsAtom = draftFanoutSelectionsAtom(routeThreadKey);
756 const { sendInFlight: sendInFlightRef } = draftSendState(routeThreadKey);
757 const multipleModelSelections = useAtomValue(fanoutSelectionsAtom);
@@ -1693,69 +1682,6 @@ export default function ChatView(props: ChatViewProps) {
1682 const sendQueuesBehindSetup =
1683 isServerThread && (activeRunPreparing || activeActivityRun?.status === "starting");
1684 const [setupQueueSendThreadKey, setSetupQueueSendThreadKey] = useState<string | null>(null);
1696 - useEffect(() => {
1697 - attachmentPreviewHandoffByMessageIdRef.current = attachmentPreviewHandoffByMessageId;
1698 - }, [attachmentPreviewHandoffByMessageId]);
1699 - const clearAttachmentPreviewHandoff = (
1700 - messageId: MessageId,
1701 - previewUrls?: ReadonlyArray<string>,
1702 - ) => {
1703 - delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];
1704 - const currentPreviewUrls =
1705 - previewUrls ?? attachmentPreviewHandoffByMessageIdRef.current[messageId] ?? [];
1706 - setAttachmentPreviewHandoffByMessageId((existing) => {
1707 - if (!(messageId in existing)) {
1708 - return existing;
1709 - }
1710 - const next = { ...existing };
1711 - delete next[messageId];
1712 - attachmentPreviewHandoffByMessageIdRef.current = next;
1713 - return next;
1714 - });
1715 - for (const previewUrl of currentPreviewUrls) {
1716 - revokeBlobPreviewUrl(previewUrl);
1717 - }
1718 - };
1719 - const clearAttachmentPreviewHandoffStable = useStableCallback(clearAttachmentPreviewHandoff);
1720 - const clearAttachmentPreviewHandoffs = () => {
1721 - attachmentPreviewPromotionInFlightByMessageIdRef.current = {};
1722 - for (const previewUrls of Object.values(attachmentPreviewHandoffByMessageIdRef.current)) {
1723 - for (const previewUrl of previewUrls) {
1724 - revokeBlobPreviewUrl(previewUrl);
1725 - }
1726 - }
1727 - attachmentPreviewHandoffByMessageIdRef.current = {};
1728 - setAttachmentPreviewHandoffByMessageId({});
1729 - };
1730 - const clearAttachmentPreviewHandoffsStable = useStableCallback(clearAttachmentPreviewHandoffs);
1731 - useEffect(() => {
1732 - return () => {
1733 - clearAttachmentPreviewHandoffsStable();
1734 - for (const message of optimisticUserMessagesRef.current) {
1735 - revokeUserMessagePreviewUrls(message);
1736 - }
1737 - };
1738 - }, [clearAttachmentPreviewHandoffsStable]);
1739 - const handoffAttachmentPreviews = (messageId: MessageId, previewUrls: string[]) => {
1740 - if (previewUrls.length === 0) return;
1741 -
1742 - const previousPreviewUrls = attachmentPreviewHandoffByMessageIdRef.current[messageId] ?? [];
1743 - const nextPreviewUrlSet = new Set(previewUrls);
1744 - for (const previewUrl of previousPreviewUrls) {
1745 - if (!nextPreviewUrlSet.has(previewUrl)) {
1746 - revokeBlobPreviewUrl(previewUrl);
1747 - }
1748 - }
1749 - setAttachmentPreviewHandoffByMessageId((existing) => {
1750 - const next = {
1751 - ...existing,
1752 - [messageId]: previewUrls,
1753 - };
1754 - attachmentPreviewHandoffByMessageIdRef.current = next;
1755 - return next;
1756 - });
1757 - };
1758 - const handoffAttachmentPreviewsStable = useStableCallback(handoffAttachmentPreviews);
1685 const downloadFileAttachment = async (attachment: ChatFileAttachment) => {
1686 const connection = readPreparedConnection(environmentId);
1687 if (!connection) {
@@ -1790,132 +1716,12 @@ export default function ChatView(props: ChatViewProps) {
1716 }
1717 };
1718 const openFileAttachmentStable = useStableCallback(openFileAttachment);
1793 - const serverAttachmentResources = selectHandoffImageResources(
1794 - Object.keys(attachmentPreviewHandoffByMessageId).length === 0
1795 - ? undefined
1796 - : serverVisibleTurnItems.flatMap(({ item }) =>
1797 - item.type === "user_message"
1798 - ? [{ id: item.messageId, role: "user" as const, attachments: item.attachments }]
1799 - : [],
1800 - ),
1801 - attachmentPreviewHandoffByMessageId,
1802 - );
1803 - const serverAttachmentUrls = useAssetUrls(environmentId, serverAttachmentResources);
1804 - const serverAttachmentUrlById = new Map(
1805 - serverAttachmentResources.flatMap((resource, index) => {
1806 - const url = serverAttachmentUrls[index];
1807 - return url ? [[resource.attachmentId, url] as const] : [];
1808 - }),
1809 - );
1810 - useEffect(() => {
1811 - if (typeof Image === "undefined" || serverVisibleTurnItems.length === 0) {
1812 - return;
1813 - }
1814 -
1815 - const cleanups: Array<() => void> = [];
1816 - const userMessagesById = new Map(
1817 - serverVisibleTurnItems.flatMap((row) =>
1818 - row.item.type === "user_message" ? [[String(row.item.messageId), row.item] as const] : [],
1819 - ),
1820 - );
1821 -
1822 - for (const [messageId, handoffPreviewUrls] of Object.entries(
1823 - attachmentPreviewHandoffByMessageId,
1824 - )) {
1825 - if (attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId]) {
1826 - continue;
1827 - }
1828 -
1829 - const serverMessage = userMessagesById.get(messageId);
1830 - if (serverMessage === undefined || serverMessage.attachments.length === 0) {
1831 - continue;
1832 - }
1833 -
1834 - const serverPreviewUrls = serverMessage.attachments.flatMap((attachment) =>
1835 - attachment.type === "image"
1836 - ? [serverAttachmentUrlById.get(attachment.id)].filter(
1837 - (previewUrl): previewUrl is string => previewUrl !== undefined,
1838 - )
1839 - : [],
1840 - );
1841 - if (
1842 - serverPreviewUrls.length === 0 ||
1843 - serverPreviewUrls.length !== handoffPreviewUrls.length ||
1844 - serverPreviewUrls.some((previewUrl) => previewUrl.startsWith("blob:"))
1845 - ) {
1846 - continue;
1847 - }
1848 -
1849 - attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId] = true;
1850 -
1851 - let cancelled = false;
1852 - const imageInstances: HTMLImageElement[] = [];
1853 -
1854 - const preloadServerPreviews = Promise.all(
1855 - serverPreviewUrls.map(
1856 - (previewUrl) =>
1857 - new Promise<void>((resolve, reject) => {
1858 - const image = new Image();
1859 - imageInstances.push(image);
1860 - const handleLoad = () => resolve();
1861 - const handleError = () =>
1862 - reject(new Error(`Failed to load server preview for ${messageId}.`));
1863 - image.addEventListener("load", handleLoad, { once: true });
1864 - image.addEventListener("error", handleError, { once: true });
1865 - image.src = previewUrl;
1866 - }),
1867 - ),
1868 - );
1869 -
1870 - void preloadServerPreviews
1871 - .then(() => {
1872 - if (cancelled) {
1873 - return;
1874 - }
1875 - clearAttachmentPreviewHandoffStable(messageId as MessageId, handoffPreviewUrls);
1876 - })
1877 - .catch(() => {
1878 - if (!cancelled) {
1879 - delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];
1880 - }
1881 - });
1882 -
1883 - cleanups.push(() => {
1884 - cancelled = true;
1885 - delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];
1886 - for (const image of imageInstances) {
1887 - image.src = "";
1888 - }
1889 - });
1890 - }
1891 -
1892 - return () => {
1893 - for (const cleanup of cleanups) {
1894 - cleanup();
1895 - }
1896 - };
1897 - }, [
1898 - attachmentPreviewHandoffByMessageId,
1899 - clearAttachmentPreviewHandoffStable,
1900 - serverAttachmentUrlById,
1901 - serverVisibleTurnItems,
1902 - ]);
1903 - const timelineAttachmentUrlById = (() => {
1904 - const urls = new Map(serverAttachmentUrlById);
1905 - for (const row of serverVisibleTurnItems) {
1906 - if (row.item.type !== "user_message") continue;
1907 - const handoffUrls = attachmentPreviewHandoffByMessageId[row.item.messageId];
1908 - if (handoffUrls === undefined) continue;
1909 - let imageIndex = 0;
1910 - for (const attachment of row.item.attachments) {
1911 - if (attachment.type !== "image") continue;
1912 - const handoffUrl = handoffUrls[imageIndex];
1913 - imageIndex += 1;
1914 - if (handoffUrl !== undefined) urls.set(attachment.id, handoffUrl);
1915 - }
1916 - }
1917 - return urls;
1918 - })();
1719 + const { handoffAttachmentPreviewsStable, timelineAttachmentUrlById } =
1720 + useAttachmentPreviewHandoff({
1721 + environmentId,
1722 + serverVisibleTurnItems,
1723 + optimisticUserMessages,
1724 + });
1725 const anchoredTimelineMessages = feedbackSubmissions.flatMap((submission) =>
1726 submission.status === "interrupted"
1727 ? []
apps/web/src/components/chat/useAttachmentPreviewHandoff.ts new
+225
@@ -0,0 +1,225 @@
1 +import { useStableCallback } from "../../hooks/useStableCallback";
2 +import { type EnvironmentId, type MessageId } from "@t3tools/contracts";
3 +import { useEffect, useLayoutEffect, useRef, useState } from "react";
4 +import { selectHandoffImageResources } from "../../session-logic";
5 +import { type ChatMessage } from "../../types";
6 +import type { useThreadVisibleTurnItems } from "../../state/entities";
7 +import { revokeBlobPreviewUrl, revokeUserMessagePreviewUrls } from "../ChatView.logic";
8 +import { useAssetUrls } from "../../assets/assetUrls";
9 +interface AttachmentPreviewHandoffInput {
10 + readonly environmentId: EnvironmentId;
11 + readonly serverVisibleTurnItems: ReturnType<typeof useThreadVisibleTurnItems>;
12 + readonly optimisticUserMessages: ReadonlyArray<ChatMessage>;
13 +}
14 +
15 +/**
16 + * A sent image keeps showing its local blob preview until the server's copy has loaded, so the
17 + * timeline does not flash an empty frame. Returns the attachment URLs the timeline renders and
18 + * the handoff for a message whose optimistic row was replaced by the server's.
19 + */
20 +export function useAttachmentPreviewHandoff(input: AttachmentPreviewHandoffInput) {
21 + const { environmentId, serverVisibleTurnItems, optimisticUserMessages } = input;
22 +
23 + const optimisticUserMessagesRef = useRef(optimisticUserMessages);
24 + useLayoutEffect(() => {
25 + optimisticUserMessagesRef.current = optimisticUserMessages;
26 + });
27 + const [attachmentPreviewHandoffByMessageId, setAttachmentPreviewHandoffByMessageId] = useState<
28 + Record<string, string[]>
29 + >({});
30 + const attachmentPreviewHandoffByMessageIdRef = useRef<Record<string, string[]>>({});
31 + const attachmentPreviewPromotionInFlightByMessageIdRef = useRef<Record<string, true>>({});
32 + useEffect(() => {
33 + attachmentPreviewHandoffByMessageIdRef.current = attachmentPreviewHandoffByMessageId;
34 + }, [attachmentPreviewHandoffByMessageId]);
35 + const clearAttachmentPreviewHandoff = (
36 + messageId: MessageId,
37 + previewUrls?: ReadonlyArray<string>,
38 + ) => {
39 + delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];
40 + const currentPreviewUrls =
41 + previewUrls ?? attachmentPreviewHandoffByMessageIdRef.current[messageId] ?? [];
42 + setAttachmentPreviewHandoffByMessageId((existing) => {
43 + if (!(messageId in existing)) {
44 + return existing;
45 + }
46 + const next = { ...existing };
47 + delete next[messageId];
48 + attachmentPreviewHandoffByMessageIdRef.current = next;
49 + return next;
50 + });
51 + for (const previewUrl of currentPreviewUrls) {
52 + revokeBlobPreviewUrl(previewUrl);
53 + }
54 + };
55 + const clearAttachmentPreviewHandoffStable = useStableCallback(clearAttachmentPreviewHandoff);
56 + const clearAttachmentPreviewHandoffs = () => {
57 + attachmentPreviewPromotionInFlightByMessageIdRef.current = {};
58 + for (const previewUrls of Object.values(attachmentPreviewHandoffByMessageIdRef.current)) {
59 + for (const previewUrl of previewUrls) {
60 + revokeBlobPreviewUrl(previewUrl);
61 + }
62 + }
63 + attachmentPreviewHandoffByMessageIdRef.current = {};
64 + setAttachmentPreviewHandoffByMessageId({});
65 + };
66 + const clearAttachmentPreviewHandoffsStable = useStableCallback(clearAttachmentPreviewHandoffs);
67 + useEffect(() => {
68 + return () => {
69 + clearAttachmentPreviewHandoffsStable();
70 + for (const message of optimisticUserMessagesRef.current) {
71 + revokeUserMessagePreviewUrls(message);
72 + }
73 + };
74 + }, [clearAttachmentPreviewHandoffsStable]);
75 + const handoffAttachmentPreviews = (messageId: MessageId, previewUrls: string[]) => {
76 + if (previewUrls.length === 0) return;
77 +
78 + const previousPreviewUrls = attachmentPreviewHandoffByMessageIdRef.current[messageId] ?? [];
79 + const nextPreviewUrlSet = new Set(previewUrls);
80 + for (const previewUrl of previousPreviewUrls) {
81 + if (!nextPreviewUrlSet.has(previewUrl)) {
82 + revokeBlobPreviewUrl(previewUrl);
83 + }
84 + }
85 + setAttachmentPreviewHandoffByMessageId((existing) => {
86 + const next = {
87 + ...existing,
88 + [messageId]: previewUrls,
89 + };
90 + attachmentPreviewHandoffByMessageIdRef.current = next;
91 + return next;
92 + });
93 + };
94 + const handoffAttachmentPreviewsStable = useStableCallback(handoffAttachmentPreviews);
95 + const serverAttachmentResources = selectHandoffImageResources(
96 + Object.keys(attachmentPreviewHandoffByMessageId).length === 0
97 + ? undefined
98 + : serverVisibleTurnItems.flatMap(({ item }) =>
99 + item.type === "user_message"
100 + ? [{ id: item.messageId, role: "user" as const, attachments: item.attachments }]
101 + : [],
102 + ),
103 + attachmentPreviewHandoffByMessageId,
104 + );
105 + const serverAttachmentUrls = useAssetUrls(environmentId, serverAttachmentResources);
106 + const serverAttachmentUrlById = new Map(
107 + serverAttachmentResources.flatMap((resource, index) => {
108 + const url = serverAttachmentUrls[index];
109 + return url ? [[resource.attachmentId, url] as const] : [];
110 + }),
111 + );
112 + useEffect(() => {
113 + if (typeof Image === "undefined" || serverVisibleTurnItems.length === 0) {
114 + return;
115 + }
116 +
117 + const cleanups: Array<() => void> = [];
118 + const userMessagesById = new Map(
119 + serverVisibleTurnItems.flatMap((row) =>
120 + row.item.type === "user_message" ? [[String(row.item.messageId), row.item] as const] : [],
121 + ),
122 + );
123 +
124 + for (const [messageId, handoffPreviewUrls] of Object.entries(
125 + attachmentPreviewHandoffByMessageId,
126 + )) {
127 + if (attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId]) {
128 + continue;
129 + }
130 +
131 + const serverMessage = userMessagesById.get(messageId);
132 + if (serverMessage === undefined || serverMessage.attachments.length === 0) {
133 + continue;
134 + }
135 +
136 + const serverPreviewUrls = serverMessage.attachments.flatMap((attachment) =>
137 + attachment.type === "image"
138 + ? [serverAttachmentUrlById.get(attachment.id)].filter(
139 + (previewUrl): previewUrl is string => previewUrl !== undefined,
140 + )
141 + : [],
142 + );
143 + if (
144 + serverPreviewUrls.length === 0 ||
145 + serverPreviewUrls.length !== handoffPreviewUrls.length ||
146 + serverPreviewUrls.some((previewUrl) => previewUrl.startsWith("blob:"))
147 + ) {
148 + continue;
149 + }
150 +
151 + attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId] = true;
152 +
153 + let cancelled = false;
154 + const imageInstances: HTMLImageElement[] = [];
155 +
156 + const preloadServerPreviews = Promise.all(
157 + serverPreviewUrls.map(
158 + (previewUrl) =>
159 + new Promise<void>((resolve, reject) => {
160 + const image = new Image();
161 + imageInstances.push(image);
162 + const handleLoad = () => resolve();
163 + const handleError = () =>
164 + reject(new Error(`Failed to load server preview for ${messageId}.`));
165 + image.addEventListener("load", handleLoad, { once: true });
166 + image.addEventListener("error", handleError, { once: true });
167 + image.src = previewUrl;
168 + }),
169 + ),
170 + );
171 +
172 + void preloadServerPreviews
173 + .then(() => {
174 + if (cancelled) {
175 + return;
176 + }
177 + clearAttachmentPreviewHandoffStable(messageId as MessageId, handoffPreviewUrls);
178 + })
179 + .catch(() => {
180 + if (!cancelled) {
181 + delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];
182 + }
183 + });
184 +
185 + cleanups.push(() => {
186 + cancelled = true;
187 + delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];
188 + for (const image of imageInstances) {
189 + image.src = "";
190 + }
191 + });
192 + }
193 +
194 + return () => {
195 + for (const cleanup of cleanups) {
196 + cleanup();
197 + }
198 + };
199 + }, [
200 + attachmentPreviewHandoffByMessageId,
201 + clearAttachmentPreviewHandoffStable,
202 + serverAttachmentUrlById,
203 + serverVisibleTurnItems,
204 + ]);
205 + const timelineAttachmentUrlById = (() => {
206 + const urls = new Map(serverAttachmentUrlById);
207 + for (const row of serverVisibleTurnItems) {
208 + if (row.item.type !== "user_message") continue;
209 + const handoffUrls = attachmentPreviewHandoffByMessageId[row.item.messageId];
210 + if (handoffUrls === undefined) continue;
211 + let imageIndex = 0;
212 + for (const attachment of row.item.attachments) {
213 + if (attachment.type !== "image") continue;
214 + const handoffUrl = handoffUrls[imageIndex];
215 + imageIndex += 1;
216 + if (handoffUrl !== undefined) urls.set(attachment.id, handoffUrl);
217 + }
218 + }
219 + return urls;
220 + })();
221 + return {
222 + handoffAttachmentPreviewsStable,
223 + timelineAttachmentUrlById,
224 + };
225 +}