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 25, 11:00 UTC
Simplify composer draft persistence and remove debug instrumentation
- Remove ChatView draft persistence debug logging and storage size plumbing - Drop in-memory persisted draft cache; use JSON storage directly - Reconcile attachment IDs from localStorage and streamline image hydration
Julius Marminge committed
Mar 1, 2026 at 11:54 UTC
0b8dfee17beb957a8855f5c1382a110fc57de88a
2 files changed
+26
-111
apps/web/src/components/ChatView.tsx
+2
-42
@@ -115,7 +115,6 @@ import {
115
type ComposerImageAttachment,
116
type DraftThreadState,
117
type PersistedComposerImageAttachment,
118
- readComposerDraftPersistStorageChars,
118
useComposerDraftStore,
119
useComposerThreadDraft,
120
} from "../composerDraftStore";
@@ -138,8 +137,6 @@ const COMPOSER_PATH_QUERY_DEBOUNCE_MS = 120;
137
const SCRIPT_TERMINAL_COLS = 120;
138
const SCRIPT_TERMINAL_ROWS = 30;
139
const WORKTREE_BRANCH_PREFIX = "t3code";
141
-const COMPOSER_DRAFT_STORAGE_KEY = "t3code:composer-drafts:v1";
142
-const ENABLE_DRAFT_PERSIST_DEBUG_LOGS = import.meta.env.DEV;
140
141
function readLastInvokedScriptByProjectFromStorage(): Record<string, string> {
142
const stored = localStorage.getItem(LAST_INVOKED_SCRIPT_BY_PROJECT_KEY);
@@ -294,13 +291,6 @@ function cloneComposerImageForRetry(image: ComposerImageAttachment): ComposerIma
291
}
292
}
293
297
-function debugDraftPersist(message: string, payload: Record<string, unknown>): void {
298
- if (!ENABLE_DRAFT_PERSIST_DEBUG_LOGS) {
299
- return;
300
- }
301
- console.debug(`[draft-persist] ${message}`, payload);
302
-}
303
-
294
const VscodeEntryIcon = memo(function VscodeEntryIcon(props: {
295
pathValue: string;
296
kind: "file" | "directory";
@@ -1261,31 +1251,8 @@ export default function ChatView({ threadId }: ChatViewProps) {
1251
return;
1252
}
1253
// Stage attachments in persisted draft state first so persist middleware can write them.
1264
- const stagedAttachmentIds = serialized.map((attachment) => attachment.id);
1265
- debugDraftPersist("stage", {
1266
- threadId,
1267
- attachmentCount: serialized.length,
1268
- serializationFailedCount: serializationFailedImageIds.length,
1269
- serializationFailedImageIds,
1270
- attachmentMeta: serialized.map((attachment) => ({
1271
- id: attachment.id,
1272
- name: attachment.name,
1273
- sizeBytes: attachment.sizeBytes,
1274
- dataUrlChars: attachment.dataUrl.length,
1275
- })),
1276
- storageCharsBefore: readComposerDraftPersistStorageChars(),
1277
- });
1278
- syncComposerDraftPersistedAttachments(
1279
- threadId,
1280
- serialized,
1281
- );
1282
- debugDraftPersist("reconcile-request", {
1283
- threadId,
1284
- stagedCount: stagedAttachmentIds.length,
1285
- serializationFailedCount: serializationFailedImageIds.length,
1286
- storageCharsAfter: readComposerDraftPersistStorageChars(),
1287
- });
1288
- } catch (error) {
1254
+ syncComposerDraftPersistedAttachments(threadId, serialized);
1255
+ } catch {
1256
const currentImageIds = new Set(composerImages.map((image) => image.id));
1257
const fallbackPersistedAttachments =
1258
useComposerDraftStore.getState().draftsByThreadId[threadId]?.persistedAttachments ?? [];
@@ -1296,13 +1263,6 @@ export default function ChatView({ threadId }: ChatViewProps) {
1263
const fallbackAttachments = fallbackPersistedAttachments.filter((attachment) =>
1264
fallbackPersistedIdSet.has(attachment.id),
1265
);
1299
- debugDraftPersist("error", {
1300
- threadId,
1301
- error: error instanceof Error ? error.message : String(error),
1302
- imageIds: composerImages.map((image) => image.id),
1303
- fallbackPersistedCount: fallbackPersistedIds.length,
1304
- storageChars: readComposerDraftPersistStorageChars(),
1305
- });
1266
if (cancelled) {
1267
return;
1268
}
apps/web/src/composerDraftStore.ts
+24
-69
@@ -116,9 +116,6 @@ const EMPTY_PERSISTED_DRAFT_STORE_STATE: PersistedComposerDraftStoreState = {
116
projectDraftThreadIdByProjectId: {},
117
};
118
119
-let persistedDraftStateCache: PersistedComposerDraftStoreState = EMPTY_PERSISTED_DRAFT_STORE_STATE;
120
-let persistedDraftStorageChars: number | null = null;
121
-
119
const EMPTY_IMAGES: ComposerImageAttachment[] = [];
120
const EMPTY_IDS: string[] = [];
121
const EMPTY_PERSISTED_ATTACHMENTS: PersistedComposerImageAttachment[] = [];
@@ -337,17 +334,25 @@ function parsePersistedDraftStateRaw(raw: string | null): PersistedComposerDraft
334
}
335
}
336
340
-function readPersistedAttachmentIdsFromCache(threadId: ThreadId): string[] {
337
+function readPersistedAttachmentIdsFromStorage(threadId: ThreadId): string[] {
338
if (threadId.length === 0) {
339
return [];
340
}
344
- return (persistedDraftStateCache.draftsByThreadId[threadId]?.attachments ?? []).map(
345
- (attachment) => attachment.id,
346
- );
341
+ try {
342
+ const raw = localStorage.getItem(COMPOSER_DRAFT_STORAGE_KEY);
343
+ const persisted = parsePersistedDraftStateRaw(raw);
344
+ return (persisted.draftsByThreadId[threadId]?.attachments ?? []).map(
345
+ (attachment) => attachment.id,
346
+ );
347
+ } catch {
348
+ return [];
349
+ }
350
}
351
349
-function fileFromDataUrl(dataUrl: string, name: string, mimeType: string): File | null {
350
- const [rawHeader, payload] = dataUrl.split(",", 2);
352
+function hydreatePersistedComposerImageAttachment(
353
+ attachment: PersistedComposerImageAttachment,
354
+): File | null {
355
+ const [rawHeader, payload] = attachment.dataUrl.split(",", 2);
356
const header = rawHeader ?? "";
357
if (!payload) {
358
return null;
@@ -359,15 +364,17 @@ function fileFromDataUrl(dataUrl: string, name: string, mimeType: string): File
364
const inferredMimeType =
365
header.startsWith("data:") && header.includes(";")
366
? header.slice("data:".length, header.indexOf(";"))
362
- : mimeType;
363
- return new File([decodedText], name, { type: inferredMimeType || mimeType });
367
+ : attachment.mimeType;
368
+ return new File([decodedText], attachment.name, {
369
+ type: inferredMimeType || attachment.mimeType,
370
+ });
371
}
372
const binary = atob(payload);
373
const bytes = new Uint8Array(binary.length);
374
for (let index = 0; index < binary.length; index += 1) {
375
bytes[index] = binary.charCodeAt(index);
376
}
370
- return new File([bytes], name, { type: mimeType });
377
+ return new File([bytes], attachment.name, { type: attachment.mimeType });
378
} catch {
379
return null;
380
}
@@ -377,10 +384,9 @@ function hydrateImagesFromPersisted(
384
attachments: PersistedComposerImageAttachment[],
385
): ComposerImageAttachment[] {
386
return attachments.flatMap((attachment) => {
380
- const file = fileFromDataUrl(attachment.dataUrl, attachment.name, attachment.mimeType);
381
- if (!file) {
382
- return [];
383
- }
387
+ const file = hydreatePersistedComposerImageAttachment(attachment);
388
+ if (!file) return [];
389
+
390
return [
391
{
392
type: "image" as const,
@@ -409,52 +415,6 @@ function toHydratedThreadDraft(
415
};
416
}
417
412
-const composerDraftStorage = createJSONStorage(() => ({
413
- getItem: (name: string): string | null => {
414
- if (typeof window === "undefined") {
415
- persistedDraftStateCache = EMPTY_PERSISTED_DRAFT_STORE_STATE;
416
- persistedDraftStorageChars = null;
417
- return null;
418
- }
419
- try {
420
- const raw = window.localStorage.getItem(name);
421
- persistedDraftStateCache = parsePersistedDraftStateRaw(raw);
422
- persistedDraftStorageChars = raw?.length ?? 0;
423
- return raw;
424
- } catch {
425
- persistedDraftStateCache = EMPTY_PERSISTED_DRAFT_STORE_STATE;
426
- persistedDraftStorageChars = null;
427
- return null;
428
- }
429
- },
430
- setItem: (name: string, value: string): void => {
431
- if (typeof window === "undefined") {
432
- return;
433
- }
434
- try {
435
- window.localStorage.setItem(name, value);
436
- persistedDraftStateCache = parsePersistedDraftStateRaw(value);
437
- persistedDraftStorageChars = value.length;
438
- } catch {
439
- // Quota/storage errors are expected for large drafts. Keep last successful snapshot.
440
- }
441
- },
442
- removeItem: (name: string): void => {
443
- if (typeof window === "undefined") {
444
- persistedDraftStateCache = EMPTY_PERSISTED_DRAFT_STORE_STATE;
445
- persistedDraftStorageChars = null;
446
- return;
447
- }
448
- try {
449
- window.localStorage.removeItem(name);
450
- } catch {
451
- // Ignore remove failures; clear in-memory cache regardless.
452
- }
453
- persistedDraftStateCache = EMPTY_PERSISTED_DRAFT_STORE_STATE;
454
- persistedDraftStorageChars = 0;
455
- },
456
-}));
457
-
418
export const useComposerDraftStore = create<ComposerDraftStoreState>()(
419
persist(
420
(set, get) => ({
@@ -890,7 +850,7 @@ export const useComposerDraftStore = create<ComposerDraftStoreState>()(
850
return { draftsByThreadId: nextDraftsByThreadId };
851
});
852
Promise.resolve().then(() => {
893
- const persistedIdSet = new Set(readPersistedAttachmentIdsFromCache(threadId));
853
+ const persistedIdSet = new Set(readPersistedAttachmentIdsFromStorage(threadId));
854
set((state) => {
855
const current = state.draftsByThreadId[threadId];
856
if (!current) {
@@ -983,7 +943,7 @@ export const useComposerDraftStore = create<ComposerDraftStoreState>()(
943
{
944
name: COMPOSER_DRAFT_STORAGE_KEY,
945
version: 1,
986
- storage: composerDraftStorage,
946
+ storage: createJSONStorage(() => localStorage),
947
partialize: (state) => {
948
const persistedDraftsByThreadId: PersistedComposerDraftStoreState["draftsByThreadId"] = {};
949
for (const [threadId, draft] of Object.entries(state.draftsByThreadId)) {
@@ -1019,7 +979,6 @@ export const useComposerDraftStore = create<ComposerDraftStoreState>()(
979
},
980
merge: (persistedState, currentState) => {
981
const normalizedPersisted = normalizePersistedComposerDraftState(persistedState);
1022
- persistedDraftStateCache = normalizedPersisted;
982
const draftsByThreadId = Object.fromEntries(
983
Object.entries(normalizedPersisted.draftsByThreadId).map(([threadId, draft]) => [
984
threadId,
@@ -1040,7 +999,3 @@ export const useComposerDraftStore = create<ComposerDraftStoreState>()(
999
export function useComposerThreadDraft(threadId: ThreadId): ComposerThreadDraftState {
1000
return useComposerDraftStore((state) => state.draftsByThreadId[threadId] ?? EMPTY_THREAD_DRAFT);
1001
}
1043
-
1044
-export function readComposerDraftPersistStorageChars(): number | null {
1045
- return persistedDraftStorageChars;
1046
-}