Open History to see the last commit
1import type { AssetResource, ChatFileAttachment, EnvironmentId } from "@t3tools/contracts";
2import { videoMimeType } from "@t3tools/shared/video";
3
4import type { DraftComposerFileAttachment } from "./composerImages";
5import type { MediaActionsSource } from "./mediaActionsSource";
6
7export type MediaVideoPreviewSource = {
8 readonly type: "media";
9 readonly name: string;
10 readonly mimeType: string;
11 readonly sourceIdentifier?: string;
12 readonly srcFragment?: string;
13 readonly actionsSource?: MediaActionsSource;
14} & (
15 | { readonly uri: string }
16 | {
17 readonly environmentId: EnvironmentId;
18 readonly resource: Extract<
19 AssetResource,
20 { readonly _tag: "attachment" | "media-file" | "draft-workspace-file" | "github-media" }
21 >;
22 }
23);
24
25export function mediaVideoPreviewUri(
26 source: MediaVideoPreviewSource,
27 assetUrl: string | null,
28): string | null {
29 if ("uri" in source) return source.uri;
30 return assetUrl === null ? null : assetUrl + (source.srcFragment ?? "");
31}
32
33export function mediaVideoThumbnailKey(source: MediaVideoPreviewSource): string {
34 return JSON.stringify(
35 "uri" in source
36 ? ["media-video", source.uri]
37 : source.resource._tag === "attachment"
38 ? ["media-video", source.environmentId, "attachment", source.resource.attachmentId]
39 : source.resource._tag === "media-file"
40 ? [
41 "media-video",
42 "media-file",
43 source.environmentId,
44 source.resource.threadId,
45 source.resource.path,
46 source.srcFragment ?? "",
47 ]
48 : source.resource._tag === "github-media"
49 ? [
50 "media-video",
51 "github-media",
52 source.environmentId,
53 source.resource.cwd,
54 source.resource.url,
55 ]
56 : [
57 "media-video",
58 "draft-workspace-file",
59 source.environmentId,
60 source.resource.cwd,
61 source.resource.path,
62 source.srcFragment ?? "",
63 ],
64 );
65}
66
67export interface LocalVideoPreviewSource {
68 readonly type: "local";
69 readonly attachment: DraftComposerFileAttachment;
70 readonly sourceIdentifier?: string;
71}
72
73export type VideoPreviewSource = LocalVideoPreviewSource | MediaVideoPreviewSource;
74
75export function attachmentVideoPreviewSource(
76 environmentId: EnvironmentId,
77 attachment: ChatFileAttachment,
78 sourceIdentifier?: string,
79): MediaVideoPreviewSource {
80 const mimeType = videoMimeType(attachment) ?? attachment.mimeType;
81 const resource = {
82 _tag: "attachment" as const,
83 attachmentId: attachment.id,
84 fileName: attachment.name,
85 mimeType,
86 };
87 return {
88 type: "media",
89 name: attachment.name,
90 mimeType,
91 ...(sourceIdentifier ? { sourceIdentifier } : {}),
92 environmentId,
93 resource,
94 actionsSource: {
95 name: attachment.name,
96 mimeType,
97 ...(sourceIdentifier ? { sourceIdentifier } : {}),
98 environmentId,
99 resource,
100 },
101 };
102}