@devries-cameron20 / t3code Mirror · read-only
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 21, 04:45 UTC

main
ts 33 lines 1.08 KB
Raw
1 import type {
2 ComposerImageAttachment,
3 PersistedComposerImageAttachment,
4 } from "../../composerDraftStore";
5 import { readFileAsDataUrl } from "../../lib/imageCompression";
6
7 export async function serializeComposerImages(
8 images: ReadonlyArray<ComposerImageAttachment>,
9 persistedAttachments: ReadonlyArray<PersistedComposerImageAttachment>,
10 ): Promise<PersistedComposerImageAttachment[]> {
11 const existingPersistedById = new Map(
12 persistedAttachments.map((attachment) => [attachment.id, attachment]),
13 );
14 const staged = await Promise.all(
15 images.map(async (image) => {
16 try {
17 const dataUrl =
18 existingPersistedById.get(image.id)?.dataUrl ?? (await readFileAsDataUrl(image.file));
19 return {
20 id: image.id,
21 name: image.name,
22 mimeType: image.mimeType,
23 sizeBytes: image.sizeBytes,
24 dataUrl,
25 ...(image.source ? { source: image.source } : {}),
26 };
27 } catch {
28 return existingPersistedById.get(image.id);
29 }
30 }),
31 );
32 return staged.filter((attachment) => attachment !== undefined);
33 }