mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-01 03:45:53 -05:00
23 lines
534 B
TypeScript
23 lines
534 B
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
/** A URL for previewing a picked file, revoked once the file changes or the component unmounts. */
|
|
export function useObjectUrl(file: File | null): string | null {
|
|
const [url, setUrl] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!file) {
|
|
setUrl(null);
|
|
|
|
return;
|
|
}
|
|
|
|
const created = URL.createObjectURL(file);
|
|
|
|
setUrl(created);
|
|
|
|
return () => URL.revokeObjectURL(created);
|
|
}, [file]);
|
|
|
|
return url;
|
|
}
|