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

queuedMessageEdit.ts

76 lines3.0 KB

1import { resolveQueuedEditExit } from "@t3tools/client-runtime/queued-run-edit";
2import type { ChatAttachment } from "@t3tools/contracts";
3import {
4 composerDraftHasUserContent,
5 useComposerDraftStore,
6 type ComposerFileAttachment,
7 type ComposerImageAttachment,
8 type ComposerThreadTarget,
9} from "../../composerDraftStore";
10import { releaseDraftAttachments } from "../../lib/attachmentUploadQueue";
11​
12/** Staged attachments upload as soon as they are added, so a dropped edit must release them. */
13export function discardQueuedEditDraft(target: ComposerThreadTarget): void {
14 const store = useComposerDraftStore.getState();
15 const draft = store.getComposerDraft(target);
16 if (draft) {
17 releaseDraftAttachments([...draft.images, ...draft.files]);
18 for (const image of draft.images) {
19 if (image.previewUrl.startsWith("blob:")) URL.revokeObjectURL(image.previewUrl);
20 }
21 }
22 store.clearComposerContent(target);
24​
25export function recoverQueuedMessageEdit(input: {
26 readonly editTarget: ComposerThreadTarget;
27 readonly threadTarget: ComposerThreadTarget;
28 readonly originalText: string;
29 readonly removedExistingAttachment?: boolean;
30}): "kept" | "discarded" | "clean" {
31 const store = useComposerDraftStore.getState();
32 const draft = store.getComposerDraft(input.editTarget);
33 const recovery = resolveQueuedEditExit({
34 editChanged:
35 input.removedExistingAttachment === true ||
36 (draft !== null &&
37 (draft.prompt !== input.originalText || draft.images.length > 0 || draft.files.length > 0)),
38 // A cleared edit has nothing to put back, so it can only be discarded.
39 threadDraftEmpty:
40 composerDraftHasUserContent(draft) &&
41 !composerDraftHasUserContent(store.getComposerDraft(input.threadTarget)),
42 });
43 if (recovery === "kept") {
44 store.moveComposerContent(input.editTarget, input.threadTarget);
45 store.clearComposerContent(input.editTarget);
46 } else {
47 discardQueuedEditDraft(input.editTarget);
48 }
49 return recovery;
51​
52export async function prepareQueuedEditAttachments(input: {
53 readonly existingAttachments: ReadonlyArray<ChatAttachment>;
54 readonly images: ReadonlyArray<ComposerImageAttachment>;
55 readonly files: ReadonlyArray<ComposerFileAttachment>;
56 readonly uploadFiles: (
57 files: ReadonlyArray<ComposerFileAttachment>,
58 ) => Promise<ReadonlyArray<ChatAttachment>>;
59 readonly readImage: (file: File) => Promise<string>;
60}) {
61 const files = input.files.length === 0 ? [] : await input.uploadFiles(input.files);
62 if (files.length !== input.files.length)
63 throw new Error("Retry or remove failed uploads before saving.");
64 const images = await Promise.all(
65 input.images.map(async (image) => ({
66 type: "image" as const,
67 id: image.id,
68 name: image.name,
69 mimeType: image.mimeType,
70 sizeBytes: image.sizeBytes,
71 dataUrl: await input.readImage(image.file),
72 ...(image.source ? { source: image.source } : {}),
73 })),
74 );
75 return [...input.existingAttachments, ...images, ...files];