Open History to see the last commit
1import { memo, useCallback, useEffect, useState } from "react";
2import { ChevronLeftIcon, ChevronRightIcon, DownloadIcon, XIcon } from "lucide-react";
3import { Button } from "../ui/button";
4import { downloadVideoPreview, type ExpandedImagePreview } from "./ExpandedImagePreview";
5
6interface ExpandedImageDialogProps {
7 preview: ExpandedImagePreview;
8 onClose: () => void;
9}
10
11export const ExpandedImageDialog = memo(function ExpandedImageDialog({
12 preview,
13 onClose,
14}: ExpandedImageDialogProps) {
15 const [imageOffset, setImageOffset] = useState(0);
16 const [failedVideoSrc, setFailedVideoSrc] = useState<string | null>(null);
17 const [downloadingVideoSrc, setDownloadingVideoSrc] = useState<string | null>(null);
18 const [downloadFailedVideoSrc, setDownloadFailedVideoSrc] = useState<string | null>(null);
19 const index = (preview.index + imageOffset + preview.images.length) % preview.images.length;
20
21 const navigateImage = useCallback((direction: -1 | 1) => {
22 setImageOffset((current) => current + direction);
23 }, []);
24
25 const downloadVideo = async (src: string, name: string) => {
26 setDownloadFailedVideoSrc(null);
27 setDownloadingVideoSrc(src);
28 try {
29 await downloadVideoPreview(src, name);
30 } catch {
31 setDownloadFailedVideoSrc(src);
32 } finally {
33 setDownloadingVideoSrc((current) => (current === src ? null : current));
34 }
35 };
36
37 useEffect(() => {
38 const onKeyDown = (event: globalThis.KeyboardEvent) => {
39 if (event.key === "Escape") {
40 event.preventDefault();
41 event.stopPropagation();
42 onClose();
43 return;
44 }
45 if (preview.images.length <= 1) return;
46 if (event.key === "ArrowLeft") {
47 event.preventDefault();
48 event.stopPropagation();
49 navigateImage(-1);
50 return;
51 }
52 if (event.key !== "ArrowRight") return;
53 event.preventDefault();
54 event.stopPropagation();
55 navigateImage(1);
56 };
57 window.addEventListener("keydown", onKeyDown);
58 return () => window.removeEventListener("keydown", onKeyDown);
59 }, [navigateImage, onClose, preview.images.length]);
60
61 const item = preview.images[index];
62 if (!item) return null;
63 const mediaLabel = item.type === "video" ? "video" : "image";
64
65 const isDownloadingVideo = downloadingVideoSrc === item.src;
66 const videoDownloadFailed = downloadFailedVideoSrc === item.src;
67 return (
68 <div
69 className="fixed inset-0 z-[60] flex items-center justify-center bg-black/75 px-4 py-6 [-webkit-app-region:no-drag]"
70 role="dialog"
71 aria-modal="true"
72 aria-label={`Expanded ${mediaLabel} preview`}
73 >
74 <button
75 type="button"
76 className="absolute inset-0 z-0 cursor-zoom-out"
77 aria-label={`Close ${mediaLabel} preview`}
78 onClick={onClose}
79 />
80 {preview.images.length > 1 && (
81 <Button
82 type="button"
83 size="icon"
84 variant="ghost"
85 className="absolute left-2 top-1/2 z-20 -translate-y-1/2 text-white/90 hover:bg-white/10 hover:text-white sm:left-6"
86 aria-label="Previous image"
87 onClick={() => navigateImage(-1)}
88 >
89 <ChevronLeftIcon className="size-5" />
90 </Button>
91 )}
92 <div className="relative isolate z-10 max-h-[92vh] max-w-[92vw]">
93 <Button
94 type="button"
95 size="icon-xs"
96 variant="ghost"
97 className="absolute right-2 top-2"
98 onClick={onClose}
99 aria-label={`Close ${mediaLabel} preview`}
100 >
101 <XIcon />
102 </Button>
103 {item.type === "video" && failedVideoSrc === item.src ? (
104 <div className="flex h-48 w-[min(92vw,32rem)] flex-col items-center justify-center gap-3 rounded-lg border border-border/70 bg-black px-6 text-center text-white shadow-2xl">
105 <p className="text-sm">
106 {videoDownloadFailed
107 ? "Could not download this video."
108 : "This video format cannot be played here."}
109 </p>
110 <Button
111 size="sm"
112 variant="secondary"
113 aria-busy={isDownloadingVideo || undefined}
114 aria-disabled={isDownloadingVideo || undefined}
115 onClick={() => {
116 if (isDownloadingVideo) return;
117 void downloadVideo(item.src, item.name);
118 }}
119 >
120 <DownloadIcon />
121 {isDownloadingVideo ? "Downloading…" : "Download video"}
122 </Button>
123 </div>
124 ) : item.type === "video" ? (
125 <video
126 src={item.src}
127 aria-label={item.name}
128 autoPlay
129 controls
130 playsInline
131 onError={() => setFailedVideoSrc(item.src)}
132 className="max-h-[86vh] max-w-[92vw] rounded-lg border border-border/70 bg-black object-contain shadow-2xl"
133 />
134 ) : (
135 <img
136 src={item.src}
137 alt={item.name}
138 className="max-h-[86vh] max-w-[92vw] select-none rounded-lg border border-border/70 bg-background object-contain shadow-2xl"
139 draggable={false}
140 />
141 )}
142 <p className="mt-2 max-w-[92vw] truncate text-center text-xs text-muted-foreground/80">
143 {item.name}
144 {preview.images.length > 1 ? ` (${index + 1}/${preview.images.length})` : ""}
145 </p>
146 </div>
147 {preview.images.length > 1 && (
148 <Button
149 type="button"
150 size="icon"
151 variant="ghost"
152 className="absolute right-2 top-1/2 z-20 -translate-y-1/2 text-white/90 hover:bg-white/10 hover:text-white sm:right-6"
153 aria-label="Next image"
154 onClick={() => navigateImage(1)}
155 >
156 <ChevronRightIcon className="size-5" />
157 </Button>
158 )}
159 </div>
160 );
161});