Repository navigation
Expand file tree
/
Copy pathCoverMedia.astro
More file actions
85 lines (80 loc) · 1.98 KB
/
Copy pathCoverMedia.astro
File metadata and controls
85 lines (80 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
---
import { stegaClean } from "@sanity/client/stega";
import type { SanityImageSource } from "@sanity/image-url";
import CoverImage from "@/components/CoverImage.astro";
import PtYouTube from "@/components/portable-text/PtYouTube.astro";
/**
* Picks the hero medium for a content page, in the Next app's precedence:
* uploaded video, then YouTube, then the cover image.
*/
interface Props {
image?: (SanityImageSource & { alt?: string }) | null;
video?: { asset?: { _ref?: string } } | null;
youtube?: string | null;
width?: number;
height?: number;
sizes?: string;
class?: string;
}
const {
image,
video,
youtube,
width,
height,
sizes = "(max-width: 1280px) 100vw, 1280px",
class: className,
} = Astro.props;
/**
* Sanity file refs are `file-{hash}-{ext}`. The Next version read the project
* id and dataset from `process.env.NEXT_PUBLIC_*` at module scope, which does
* not exist on Workers — resolved from the per-request config instead.
*/
function fileUrl(ref: string | undefined) {
if (!ref?.startsWith("file-")) {
return undefined;
}
const parts = ref.split("-");
const ext = parts.pop();
const hash = parts.slice(1).join("-");
if (!ext || !hash) {
return undefined;
}
const { projectId, dataset } = Astro.locals.sanity.config;
return `https://cdn.sanity.io/files/${projectId}/${dataset}/${hash}.${ext}`;
}
const videoUrl = fileUrl(stegaClean(video?.asset?._ref));
const youtubeUrl = stegaClean(youtube);
---
{
videoUrl ? (
<div class="shadow-md transition-shadow duration-200 sm:mx-0">
<video
class:list={["w-full h-auto rounded-md", className]}
width={1920}
height={1080}
controls
preload="metadata"
>
<source src={videoUrl} />
</video>
</div>
) : youtubeUrl ? (
<PtYouTube
node={{ _type: "youtube", youtube: youtubeUrl }}
coverImage={image}
priority
index={0}
isInline={false}
/>
) : (
<CoverImage
image={image}
width={width}
height={height}
priority
sizes={sizes}
class={className}
/>
)
}