- Featured event badge: above the event's card from lg up, left out on phones - Version names from the tag (The Region Cards Update) in the site and admin footers - Tools -> Image converter (JPG, PNG, WebP in the browser) and Download as in Media - People directory sections take several tags, a tab each (migration 078) - Tab blocks split a blog post into tabs; contents follow the open tab (migration 079) - The side nav sun fades out as the page scrolls into the footer Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
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 (
|
|
<span className="inline-flex flex-wrap items-center gap-1">
|
|
<label htmlFor={id} className="sr-only">
|
|
Download {name} as
|
|
</label>
|
|
<select
|
|
id={id}
|
|
value=""
|
|
disabled={state === 'busy'}
|
|
onChange={(e) => e.target.value && download(e.target.value as OutputType)}
|
|
className="border border-muted/25 bg-surface px-1 py-0.5 text-xs text-link"
|
|
>
|
|
<option value="">{state === 'busy' ? 'Converting…' : 'Download as…'}</option>
|
|
{OUTPUT_FORMATS.map((format) => (
|
|
<option key={format.type} value={format.type}>
|
|
{format.label}
|
|
</option>
|
|
))}
|
|
</select>
|
|
{state !== 'idle' && state !== 'busy' && <span className="text-xs text-danger">{state}</span>}
|
|
</span>
|
|
)
|
|
}
|