import React, { ReactNode, useEffect, useRef, useState } from "react" import { FileManager } from "./formContext" export interface FileLinkComponentProps { fileManager: FileManager fileId: string /** MIME type of the file, used to render supported inline previews. */ mimetype?: string children: ReactNode } /** Renders a download link for a file whose URL is resolved asynchronously via FileManager.getFileUrl. * Local (pending) files are shown as plain text since they don't need to be downloadable. * Video files are previewed inline with native browser controls. * Remote URLs use InAppBrowser on Cordova to open in the system browser. */ export default function FileLinkComponent(props: FileLinkComponentProps) { const { fileManager, fileId, mimetype, children } = props const [url, setUrl] = useState(null) const videoRef = useRef(null) useEffect(() => { let cancelled = false fileManager.getFileUrl(fileId).then((resolved) => { if (!cancelled) setUrl(resolved) }) return () => { cancelled = true } }, [fileManager, fileId]) if (!url) { return {children} } const isLocal = url.startsWith("file://") || url.startsWith("/") if (isLocal) { return {children} } const handleClick = (e: React.MouseEvent) => { if ((window as any).cordova?.InAppBrowser) { e.preventDefault() ;(window as any).cordova.InAppBrowser.open(url, "_blank", "location=yes") } } if (mimetype?.startsWith("video/")) { /** Seeks the inline video by a relative number of seconds. */ const seekVideo = (seconds: number) => { if (videoRef.current) { videoRef.current.currentTime = Math.max(0, videoRef.current.currentTime + seconds) } } return (
{children}
) } return {children} }