|
| 1 | +import React, { useRef, useEffect, useState } from "react"; |
| 2 | +import { useTranslation } from "react-i18next"; |
| 3 | +import { useSelector } from "react-redux"; |
| 4 | +import { getFilenameFromURL } from "@/utils/base.ts"; |
| 5 | +import { AlertCircle, Copy, Eye, Link, Loader2 } from "lucide-react"; |
| 6 | +import { Skeleton } from "@/components/ui/skeleton.tsx"; |
| 7 | +import { cn } from "@/components/ui/lib/utils.ts"; |
| 8 | +import { |
| 9 | + Dialog, |
| 10 | + DialogContent, |
| 11 | + DialogHeader, |
| 12 | + DialogTitle, |
| 13 | + DialogTrigger, |
| 14 | +} from "@/components/ui/dialog.tsx"; |
| 15 | +import { useClipboard } from "@/utils/dom.ts"; |
| 16 | +import { Button } from "@/components/ui/button.tsx"; |
| 17 | +import { openWindow } from "@/utils/device.ts"; |
| 18 | +import { RootState } from "@/store/index.ts"; |
| 19 | + |
| 20 | +export enum VideoState { |
| 21 | + Loading = "loading", |
| 22 | + Loaded = "loaded", |
| 23 | + Error = "error", |
| 24 | +} |
| 25 | +export type VideoStateType = (typeof VideoState)[keyof typeof VideoState]; |
| 26 | + |
| 27 | +type VideoProps = React.VideoHTMLAttributes<HTMLVideoElement> & { |
| 28 | + alt?: string; |
| 29 | +}; |
| 30 | + |
| 31 | +export default function Video({ |
| 32 | + src, |
| 33 | + alt, |
| 34 | + className, |
| 35 | + ...props |
| 36 | +}: VideoProps) { |
| 37 | + const { t } = useTranslation(); |
| 38 | + const copy = useClipboard(); |
| 39 | + const token = useSelector((state: RootState) => state.auth.token); |
| 40 | + |
| 41 | + const filename = getFilenameFromURL(src) || "unknown"; |
| 42 | + const description = alt || filename; |
| 43 | + |
| 44 | + const videoRef = useRef<HTMLVideoElement>(null); |
| 45 | + const [state, setState] = useState<VideoStateType>(VideoState.Loading); |
| 46 | + const [videoUrl, setVideoUrl] = useState<string | null>(null); |
| 47 | + const blobUrlRef = useRef<string | null>(null); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + if (!src) { |
| 51 | + setState(VideoState.Error); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + const fetchVideo = async () => { |
| 56 | + try { |
| 57 | + setState(VideoState.Loading); |
| 58 | + const headers: HeadersInit = { |
| 59 | + "Content-Type": "video/mp4", |
| 60 | + }; |
| 61 | + |
| 62 | + if (token) { |
| 63 | + headers["Authorization"] = token; |
| 64 | + } |
| 65 | + |
| 66 | + const response = await fetch(src, { |
| 67 | + method: "GET", |
| 68 | + headers, |
| 69 | + }); |
| 70 | + |
| 71 | + if (!response.ok) { |
| 72 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 73 | + } |
| 74 | + |
| 75 | + const blob = await response.blob(); |
| 76 | + const blobUrl = URL.createObjectURL(blob); |
| 77 | + |
| 78 | + if (blobUrlRef.current) { |
| 79 | + URL.revokeObjectURL(blobUrlRef.current); |
| 80 | + } |
| 81 | + |
| 82 | + blobUrlRef.current = blobUrl; |
| 83 | + setVideoUrl(blobUrl); |
| 84 | + setState(VideoState.Loaded); |
| 85 | + } catch (error) { |
| 86 | + console.error("[Video] Failed to load video:", error); |
| 87 | + setState(VideoState.Error); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + fetchVideo(); |
| 92 | + |
| 93 | + return () => { |
| 94 | + if (blobUrlRef.current) { |
| 95 | + URL.revokeObjectURL(blobUrlRef.current); |
| 96 | + blobUrlRef.current = null; |
| 97 | + } |
| 98 | + }; |
| 99 | + }, [src, token]); |
| 100 | + |
| 101 | + const isLoading = state === VideoState.Loading; |
| 102 | + const isError = state === VideoState.Error; |
| 103 | + const isLoaded = state === VideoState.Loaded; |
| 104 | + |
| 105 | + return ( |
| 106 | + <Dialog> |
| 107 | + <DialogTrigger asChild> |
| 108 | + <div className={`flex flex-col items-center cursor-pointer`}> |
| 109 | + {isLoading && ( |
| 110 | + <Skeleton |
| 111 | + className={`relative rounded-md w-80 h-44 mx-auto my-1 flex items-center justify-center`} |
| 112 | + > |
| 113 | + <Loader2 className={`w-6 h-6 animate-spin`} /> |
| 114 | + </Skeleton> |
| 115 | + )} |
| 116 | + |
| 117 | + {isError && ( |
| 118 | + <div |
| 119 | + className={`flex flex-col items-center text-center border rounded-md py-6 px-8 mx-auto my-1`} |
| 120 | + > |
| 121 | + <AlertCircle className={`h-5 w-5 text-secondary mb-1`} /> |
| 122 | + <span |
| 123 | + className={`text-secondary mb-0 select-none text-sm whitespace-pre-wrap`} |
| 124 | + > |
| 125 | + {t("renderer.videoLoadFailed", { src: filename })} |
| 126 | + </span> |
| 127 | + </div> |
| 128 | + )} |
| 129 | + |
| 130 | + {videoUrl && ( |
| 131 | + <video |
| 132 | + className={cn( |
| 133 | + className, |
| 134 | + "select-none outline-none rounded-md max-w-[20rem] max-h-[20rem]", |
| 135 | + !isLoaded && `hidden`, |
| 136 | + )} |
| 137 | + src={videoUrl} |
| 138 | + ref={videoRef} |
| 139 | + controls |
| 140 | + preload="metadata" |
| 141 | + onAbort={() => setState(VideoState.Error)} |
| 142 | + onError={() => setState(VideoState.Error)} |
| 143 | + {...props} |
| 144 | + /> |
| 145 | + )} |
| 146 | + <span |
| 147 | + className={`text-secondary text-sm mt-1 select-none max-w-[20rem] text-center truncate`} |
| 148 | + > |
| 149 | + {description} |
| 150 | + </span> |
| 151 | + </div> |
| 152 | + </DialogTrigger> |
| 153 | + <DialogContent className={`flex-dialog`} couldFullScreen> |
| 154 | + <DialogHeader> |
| 155 | + <DialogTitle className={`flex flex-row items-center`}> |
| 156 | + <Eye className={`h-4 w-4 mr-1.5 translate-y-[1px]`} /> |
| 157 | + {t("renderer.viewVideo")} |
| 158 | + </DialogTitle> |
| 159 | + </DialogHeader> |
| 160 | + <div className={`flex flex-row mb-2 items-center`}> |
| 161 | + <div className={`grow`} /> |
| 162 | + <Button |
| 163 | + size={`icon`} |
| 164 | + variant={`outline`} |
| 165 | + className={`ml-2`} |
| 166 | + onClick={() => copy(src || "")} |
| 167 | + > |
| 168 | + <Copy className={`h-4 w-4`} /> |
| 169 | + </Button> |
| 170 | + <Button |
| 171 | + size={`icon`} |
| 172 | + variant={`outline`} |
| 173 | + className={`ml-2`} |
| 174 | + onClick={() => openWindow(src || "")} |
| 175 | + disabled={isError} |
| 176 | + > |
| 177 | + <Link className={`h-4 w-4`} /> |
| 178 | + </Button> |
| 179 | + </div> |
| 180 | + <div className={`flex flex-col items-center`}> |
| 181 | + {videoUrl && ( |
| 182 | + <video |
| 183 | + className={cn(className, "rounded-md select-none outline-none max-w-full max-h-[80vh]")} |
| 184 | + src={videoUrl} |
| 185 | + controls |
| 186 | + preload="auto" |
| 187 | + {...props} |
| 188 | + /> |
| 189 | + )} |
| 190 | + <span |
| 191 | + className={`text-secondary text-sm mt-2.5 text-center break-all whitespace-pre-wrap`} |
| 192 | + > |
| 193 | + <button |
| 194 | + onClick={() => copy(src || "")} |
| 195 | + className={`h-4 w-4 inline-block mr-1 outline-none translate-y-[2px]`} |
| 196 | + > |
| 197 | + <Copy className={`h-3.5 w-3.5`} /> |
| 198 | + </button> |
| 199 | + {src || ''} |
| 200 | + </span> |
| 201 | + </div> |
| 202 | + </DialogContent> |
| 203 | + </Dialog> |
| 204 | + ); |
| 205 | +} |
0 commit comments