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

main
ts 110 lines 4.12 KB
Raw
1 import type {
2 EnvironmentContextRecord,
3 PreviewAnnotationPayload,
4 ThreadContextRecord,
5 } from "@t3tools/contracts";
6 import { useEffect, useState } from "react";
7
8 import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore";
9 import { fileContextReference, previewAnnotationContextId } from "../../lib/composerContextRecords";
10 import type { TerminalContextDraft } from "../../lib/terminalContext";
11 import type { ReviewCommentContext } from "../../reviewCommentContext";
12
13 interface RetainedPreviewAnnotation {
14 annotation: PreviewAnnotationPayload;
15 image: ComposerImageAttachment | undefined;
16 }
17
18 export interface RetainedAttachmentContextPayloads {
19 files: Map<string, ComposerFileAttachment>;
20 previewAnnotations: Map<string, RetainedPreviewAnnotation>;
21 }
22
23 export interface RetainedContextPayloads extends RetainedAttachmentContextPayloads {
24 terminals: Map<string, TerminalContextDraft>;
25 reviewComments: Map<string, ReviewCommentContext>;
26 threads: Map<string, ThreadContextRecord>;
27 environments: Map<string, EnvironmentContextRecord>;
28 }
29
30 function createRetainedContextPayloads(): RetainedContextPayloads {
31 return {
32 terminals: new Map(),
33 reviewComments: new Map(),
34 threads: new Map(),
35 environments: new Map(),
36 files: new Map(),
37 previewAnnotations: new Map(),
38 };
39 }
40
41 // Deleted chips keep their payloads until their draft's undo history ends.
42 export function useRetainedContextPayloads(draftTargetKey: string): RetainedContextPayloads {
43 const [draft, setDraft] = useState(() => ({
44 targetKey: draftTargetKey,
45 payloads: createRetainedContextPayloads(),
46 }));
47 if (draft.targetKey !== draftTargetKey) {
48 setDraft({ targetKey: draftTargetKey, payloads: createRetainedContextPayloads() });
49 }
50 const retained = draft.payloads;
51 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- clears the retained payload maps when the draft target changes or the composer unmounts
52 useEffect(
53 () => () => {
54 for (const payloads of Object.values(retained)) payloads.clear();
55 },
56 [retained],
57 );
58 return retained;
59 }
60
61 export function reconcileAttachmentContextReferences(input: {
62 referencedContextIds: ReadonlySet<string>;
63 files: ReadonlyArray<ComposerFileAttachment>;
64 images: ReadonlyArray<ComposerImageAttachment>;
65 previewAnnotations: ReadonlyArray<PreviewAnnotationPayload>;
66 retained: RetainedAttachmentContextPayloads;
67 }): {
68 filesToRemove: string[];
69 filesToRestore: ComposerFileAttachment[];
70 annotationIdsToRemove: string[];
71 annotationsToRestore: RetainedPreviewAnnotation[];
72 } {
73 const liveFileContextIds = new Set<string>(
74 input.files.map((file) => fileContextReference(file).contextId),
75 );
76 const filesToRemove: string[] = [];
77 for (const file of input.files) {
78 const contextId = fileContextReference(file).contextId;
79 if (input.referencedContextIds.has(contextId)) continue;
80 input.retained.files.set(contextId, file);
81 filesToRemove.push(file.id);
82 }
83 const filesToRestore = [...input.referencedContextIds].flatMap((contextId) => {
84 if (liveFileContextIds.has(contextId)) return [];
85 const file = input.retained.files.get(contextId);
86 return file ? [file] : [];
87 });
88
89 const imagesById = new Map(input.images.map((image) => [image.id, image]));
90 const liveAnnotationContextIds = new Set<string>(
91 input.previewAnnotations.map((annotation) => previewAnnotationContextId(annotation.id)),
92 );
93 const annotationIdsToRemove: string[] = [];
94 for (const annotation of input.previewAnnotations) {
95 const contextId = previewAnnotationContextId(annotation.id);
96 if (input.referencedContextIds.has(contextId)) continue;
97 input.retained.previewAnnotations.set(contextId, {
98 annotation,
99 image: imagesById.get(annotation.id),
100 });
101 annotationIdsToRemove.push(annotation.id);
102 }
103 const annotationsToRestore = [...input.referencedContextIds].flatMap((contextId) => {
104 if (liveAnnotationContextIds.has(contextId)) return [];
105 const retained = input.retained.previewAnnotations.get(contextId);
106 return retained ? [retained] : [];
107 });
108
109 return { filesToRemove, filesToRestore, annotationIdsToRemove, annotationsToRestore };
110 }