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

ExpandedImagePreview.tsx

209 lines6.5 KB

1import type { SnapShotSource } from "@t3tools/contracts";
2​
3import type { ComposerFileAttachment } from "../../composerDraftStore";
4import { type ChatFileAttachment, type ChatImageAttachment, isVideoAttachment } from "../../types";
5import type {
6 AssetCreateUrlResult,
7 AssetResource,
8 EnvironmentId,
9 ScopedThreadRef,
10} from "@t3tools/contracts";
11import { videoMimeType } from "@t3tools/shared/video";
12import { resolveMediaSource } from "@t3tools/client-runtime/media-source";
13import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets";
14import {
15 squashAtomCommandFailure,
16 type AtomCommandResult,
17} from "@t3tools/client-runtime/state/runtime";
18import { resolveExternalWebLinkHost } from "./externalLinkContextMenu";
19import type { MediaActionSource } from "../media/MediaActions";
20import { resolveProtocolRelativeMediaUrl } from "../media/mediaContent";
21​
22export interface ExpandedImageItem {
23 src: string | null;
24 name: string;
25 mimeType?: string;
26 type?: "video";
27 /** Revoke this owned object URL; other blob URLs belong to the timeline. */
28 ownsSrc?: boolean;
29 source?: SnapShotSource;
30 autoPlay?: boolean;
31 originalUrl?: string;
32 srcFragment?: string;
33 actionsSource?: MediaActionSource;
35​
36export interface ExpandedImagePreview {
37 images: ExpandedImageItem[];
38 index: number;
40​
41export async function resolveMarkdownMediaPreview(input: {
42 source: string;
43 resolvedFilePath?: string | undefined;
44 cwd?: string | undefined;
45 threadRef?: ScopedThreadRef | undefined;
46 httpBaseUrl?: string | undefined;
47 onOpenFile?: ((relativePath: string) => void) | undefined;
48 createAssetUrl: (input: {
49 environmentId: EnvironmentId;
50 input: { resource: AssetResource };
51 }) => Promise<AtomCommandResult<AssetCreateUrlResult, unknown>>;
52}): Promise<ExpandedImagePreview | null> {
53 const media = resolveMediaSource(input.source, {
54 threadId: input.threadRef?.threadId,
55 workspaceRoot: input.cwd,
56 resolvedFilePath: input.resolvedFilePath,
57 });
58 if (media === null) return null;
59 const { kind, name, reference } = media;
60 const relativePath = reference?.kind === "file" ? reference.relativePath : undefined;
61​
62 let src: string;
63 let asset: MediaActionSource["asset"];
64 if (media.access === "direct") {
65 src = resolveProtocolRelativeMediaUrl(media.uri);
66 } else {
67 if (media.access === "unavailable" || !input.threadRef || !input.httpBaseUrl) {
68 throw new Error("Reconnect to this environment and open the media again.");
69 }
70 asset = { environmentId: input.threadRef.environmentId, resource: media.resource };
71 const result = await input.createAssetUrl({
72 environmentId: asset.environmentId,
73 input: { resource: asset.resource },
74 });
75 if (result._tag === "Failure") throw squashAtomCommandFailure(result);
76 const assetUrl = resolveAssetUrl(input.httpBaseUrl, result.value.relativeUrl);
77 if (assetUrl === null) throw new Error("The environment returned an invalid media URL.");
78 src = assetUrl + media.srcFragment;
79 }
80 return {
81 images: [
82 {
83 src,
84 name,
85 ...(kind === "video" ? { type: "video", autoPlay: false } : {}),
86 ...(media.access === "direct" && resolveExternalWebLinkHost(media.uri) !== null
87 ? { originalUrl: media.uri }
88 : {}),
89 ...(media.srcFragment ? { srcFragment: media.srcFragment } : {}),
90 actionsSource: {
91 kind,
92 name,
93 src,
94 ...(reference ? { reference } : {}),
95 ...(asset ? { asset } : {}),
96 ...(relativePath && input.onOpenFile
97 ? { onOpenFile: () => input.onOpenFile?.(relativePath) }
98 : {}),
99 },
100 },
101 ],
102 index: 0,
103 };
105​
106export function buildAttachmentVideoAsset(
107 environmentId: EnvironmentId,
108 attachment: Pick<ChatFileAttachment, "id" | "name" | "mimeType">,
109): NonNullable<MediaActionSource["asset"]> {
110 return {
111 environmentId,
112 resource: {
113 _tag: "attachment" as const,
114 attachmentId: attachment.id,
115 fileName: attachment.name,
116 mimeType: videoMimeType(attachment) ?? attachment.mimeType,
117 },
118 };
120​
121export function buildAttachmentVideoPreview(
122 environmentId: EnvironmentId,
123 attachment: ChatFileAttachment,
124): ExpandedImagePreview | null {
125 if (!isVideoAttachment(attachment)) return null;
126 const src = attachment.previewUrl ?? null;
127 const asset =
128 attachment.downloadable === false
129 ? undefined
130 : buildAttachmentVideoAsset(environmentId, attachment);
131 if (src === null && asset === undefined) return null;
132 return {
133 images: [
134 {
135 src,
136 name: attachment.name,
137 type: "video",
138 actionsSource: {
139 kind: "video",
140 name: attachment.name,
141 src,
142 ...(asset ? { asset } : {}),
143 },
144 },
145 ],
146 index: 0,
147 };
149​
150export function expandedImageKey(preview: ExpandedImagePreview): string {
151 const item = preview.images[preview.index];
152 const asset = item?.actionsSource?.asset;
153 return `${item?.src ?? (asset ? JSON.stringify([asset.environmentId, asset.resource]) : "image")}:${preview.index}`;
155​
156export function attachVideoThumbnail(video: HTMLVideoElement, file: File): () => void {
157 const url = URL.createObjectURL(file);
158 video.src = url;
159 return () => URL.revokeObjectURL(url);
161​
162export function buildExpandedImagePreview(
163 images: ReadonlyArray<ChatImageAttachment | ComposerFileAttachment>,
164 selectedImageId: string,
165): ExpandedImagePreview | null {
166 const selected = images.find((image) => image.id === selectedImageId);
167 if (selected?.type === "file" && selected.file && isVideoAttachment(selected)) {
168 return {
169 images: [
170 {
171 src: URL.createObjectURL(selected.file),
172 name: selected.name,
173 type: "video",
174 ownsSrc: true,
175 },
176 ],
177 index: 0,
178 };
179 }
180 const previewableImages = images.flatMap((image) =>
181 image.type === "image" && image.previewUrl
182 ? [
183 {
184 id: image.id,
185 src: image.previewUrl,
186 name: image.name,
187 mimeType: image.mimeType,
188 source: image.source,
189 },
190 ]
191 : [],
192 );
193 if (previewableImages.length === 0) {
194 return null;
195 }
196 const selectedIndex = previewableImages.findIndex((image) => image.id === selectedImageId);
197 if (selectedIndex < 0) {
198 return null;
199 }
200 return {
201 images: previewableImages.map((image) => ({
202 src: image.src,
203 name: image.name,
204 mimeType: image.mimeType,
205 ...(image.source?.kind === "snap-shot" ? { source: image.source } : {}),
206 })),
207 index: selectedIndex,
208 };