Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composerContextUndo.ts

103 lines3.9 KB

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