| 1 | import { DownloadIcon, ExternalLinkIcon } from "lucide-react"; |
| 2 | |
| 3 | import { resolveExternalWebLinkHost } from "../chat/externalLinkContextMenu"; |
| 4 | import { Button } from "../ui/button"; |
| 5 | import { resolveProtocolRelativeMediaUrl } from "./mediaContent"; |
| 6 | |
| 7 | /** Navigates directly so the browser handles video playback and downloads, without fetching bytes. */ |
| 8 | export function OpenMediaLink(props: { |
| 9 | readonly originalUrl?: string | undefined; |
| 10 | readonly src?: string | null | undefined; |
| 11 | readonly fileName?: string | undefined; |
| 12 | readonly className?: string | undefined; |
| 13 | }) { |
| 14 | const originalUrl = |
| 15 | resolveExternalWebLinkHost(props.originalUrl) !== null ? props.originalUrl : undefined; |
| 16 | const source = originalUrl ?? props.src; |
| 17 | if (!source) return null; |
| 18 | const url = resolveProtocolRelativeMediaUrl(source); |
| 19 | let isBlob = false; |
| 20 | try { |
| 21 | const protocol = new URL(url).protocol; |
| 22 | if (protocol !== "http:" && protocol !== "https:" && protocol !== "blob:") return null; |
| 23 | isBlob = protocol === "blob:"; |
| 24 | } catch { |
| 25 | return null; |
| 26 | } |
| 27 | return ( |
| 28 | <Button |
| 29 | size="sm" |
| 30 | variant="secondary" |
| 31 | className={props.className} |
| 32 | render={ |
| 33 | <a |
| 34 | href={url} |
| 35 | target={isBlob ? undefined : "_blank"} |
| 36 | download={isBlob ? props.fileName || true : undefined} |
| 37 | rel="noopener noreferrer" |
| 38 | /> |
| 39 | } |
| 40 | > |
| 41 | {isBlob ? <DownloadIcon /> : <ExternalLinkIcon />} |
| 42 | {originalUrl ? "Open original" : isBlob ? "Download video" : "Open in browser"} |
| 43 | </Button> |
| 44 | ); |
| 45 | } |