| 1 |
import { stampImageBlob } from '@shared/lib/ai-label'; |
| 2 |
import { useEffect, useState } from '@wordpress/element'; |
| 3 |
|
| 4 |
// Reuses the importer's drawing so the preview can't drift from the import. |
| 5 |
export const useStampedPreview = (url, disclose) => { |
| 6 |
const [stamped, setStamped] = useState(null); |
| 7 |
|
| 8 |
useEffect(() => { |
| 9 |
setStamped(null); |
| 10 |
if (!url || !disclose) return; |
| 11 |
|
| 12 |
let objectUrl = null; |
| 13 |
let cancelled = false; |
| 14 |
stampImageBlob(url).then((blob) => { |
| 15 |
if (!blob) return; |
| 16 |
objectUrl = URL.createObjectURL(blob); |
| 17 |
if (cancelled) return URL.revokeObjectURL(objectUrl); |
| 18 |
setStamped(objectUrl); |
| 19 |
}); |
| 20 |
|
| 21 |
return () => { |
| 22 |
cancelled = true; |
| 23 |
if (objectUrl) URL.revokeObjectURL(objectUrl); |
| 24 |
}; |
| 25 |
}, [url, disclose]); |
| 26 |
|
| 27 |
return stamped ?? url; |
| 28 |
}; |
| 29 |
|