@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 50 lines 1.99 KB
Raw
1 import type { FilePreviewItem, FilePreviewSource } from "../components/ui/FilePreviewModal.types";
2
3 export function filePreviewKey(source: FilePreviewItem): string {
4 if ("resource" in source)
5 return JSON.stringify([
6 source.environmentId,
7 Object.entries(source.resource)
8 .filter(([key]) => key !== "mimeType" && key !== "fileName")
9 .sort(([a], [b]) => a.localeCompare(b)),
10 ]);
11 if ("attachment" in source) return `attachment:${source.attachment.id}`;
12 return source.uri;
13 }
14
15 /** The thumbnail a gallery page opens from and closes into, including pages that were not tapped. */
16 export function filePreviewSourceIdentifier(source: FilePreviewItem): string {
17 return source.sourceIdentifier ?? `preview:${filePreviewKey(source)}`;
18 }
19
20 export function filePreviewGallery(source: FilePreviewSource, group: readonly FilePreviewItem[]) {
21 if (source.kind !== "image") return [source];
22 const selectedKey = filePreviewKey(source);
23 const seen = new Set<string>();
24 const items: FilePreviewItem[] = [];
25 for (const item of source.gallery ?? group) {
26 if (item.kind !== "image") continue;
27 const key = filePreviewKey(item);
28 if (seen.has(key)) continue;
29 seen.add(key);
30 items.push(key === selectedKey ? source : item);
31 }
32 if (!seen.has(selectedKey)) items.unshift(source);
33 return items;
34 }
35
36 export function filePreviewName(source: FilePreviewItem): string {
37 const uriName = "uri" in source ? source.uri.split(/[?#]/)[0]?.split("/").pop() : undefined;
38 const name =
39 source.name ?? ("attachment" in source ? source.attachment.name : uriName) ?? "Preview";
40 const extension = /\.(png|jpe?g|gif|webp|heic|heif|avif|svg|pdf|docx?|xlsx?|pptx?|txt|rtf)$/i;
41 const filename =
42 source.kind !== "image" || extension.test(name)
43 ? name
44 : uriName && extension.test(uriName)
45 ? uriName
46 : name.replace(/\./g, "_");
47 return Array.from(filename, (character) =>
48 character.charCodeAt(0) < 32 || "/:\\".includes(character) ? "_" : character,
49 ).join("");
50 }