import { useId, useState } from 'react' import { OUTPUT_FORMATS, convertImage, renamed, saveBlob, type OutputType } from '../../lib/convertImage.ts' /* A Media library image saved as JPG, PNG or WebP, converted in the browser (src/lib/convertImage.ts). */ export default function DownloadAs({ url, name }: { url: string; name: string }) { const id = useId() const [state, setState] = useState<'idle' | 'busy' | string>('idle') async function download(type: OutputType) { setState('busy') try { const response = await fetch(url) if (!response.ok) throw new Error(`Couldn't fetch the picture (HTTP ${response.status}).`) saveBlob(await convertImage(await response.blob(), type), renamed(name, type)) setState('idle') } catch (err) { setState(err instanceof Error ? err.message : "Couldn't convert it.") } } return ( {state !== 'idle' && state !== 'busy' && {state}} ) }