import { useEffect, useState } from 'react'; /** * The bytes as an object URL that lives as long as the component. * @param {Uint8Array|null|undefined} bytes * @param {string} mediaType * @returns {string} '' until the URL exists */ export function useObjectUrl(bytes, mediaType) { const [url, setUrl] = useState(''); useEffect(() => { if (!bytes) return undefined; const next = URL.createObjectURL( new Blob([/** @type {BlobPart} */ (bytes)], { type: mediaType }), ); setUrl(next); return () => URL.revokeObjectURL(next); }, [bytes, mediaType]); return url; }