Open History to see the last commit
1import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
2import { SOURCE_CONTROL_MEDIA_ASSET, type EnvironmentId } from "@t3tools/contracts";
3import type { FilePreviewItem } from "../../components/ui/FilePreviewModal.types";
4import type { AssetUrlState } from "../../state/asset-url-state";
5
6export interface PullRequestMediaLocation {
7 readonly environmentId: EnvironmentId;
8 readonly cwd: string;
9 readonly repository?: string;
10}
11
12/** Keep the original resource identity so previewers can resolve and renew its capability. */
13export function pullRequestMediaSource(url: string, location: PullRequestMediaLocation | null) {
14 const fetchUrl = sourceControlClients.mediaUrl(url)?.fetchUrl;
15 return location && fetchUrl
16 ? {
17 environmentId: location.environmentId,
18 resource: {
19 _tag: SOURCE_CONTROL_MEDIA_ASSET,
20 cwd: location.cwd,
21 url: fetchUrl,
22 ...(location.repository ? { repository: location.repository } : {}),
23 },
24 }
25 : { uri: url };
26}
27
28export function pullRequestImageSource(
29 url: string,
30 location: PullRequestMediaLocation | null,
31): FilePreviewItem {
32 return { kind: "image", ...pullRequestMediaSource(url, location) };
33}
34
35/**
36 * A connected server that refuses to sign the URL cannot fetch that host (an older release, or a
37 * host turned off), so load it directly as the web does: public media still shows.
38 */
39export function pullRequestMediaUri(
40 url: string,
41 proxied: boolean,
42 asset: AssetUrlState,
43): { readonly uri: string | null; readonly proxied: boolean; readonly unavailable: boolean } {
44 if (!proxied || (asset._tag === "Failure" && asset.reason === "failed")) {
45 return { uri: url, proxied: false, unavailable: false };
46 }
47 return {
48 uri: asset._tag === "Success" ? asset.url : null,
49 proxied: true,
50 unavailable: asset._tag === "Failure",
51 };
52}