import React, { useContext, useEffect, useState } from 'react'; import Loading from './Loading'; import { Alert, Col, Row } from 'react-bootstrap'; import { fetchImport } from '../../core/imports'; import { InfinityMintProjectAsset, InfinityMintProjectPath, } from 'infinitymint/dist/app/interfaces'; import { Project } from '../../core/project'; import { ClientContext } from '../../contexts/clientProvider'; import { cutLongString, log } from '../../utils/helpers'; let currentExecution = false; const awaitCurrentExecution = () => { let interval; return new Promise((resolve, reject) => { interval = setInterval(() => { if (!currentExecution) { clearInterval(interval); resolve(true); } }, 100); }); }; export default function PathImage({ path, project = null, waitForOthers = true, maxHeight = 256, }: { path: InfinityMintProjectPath | InfinityMintProjectAsset; project?: Project; waitForOthers?: boolean; maxHeight?: number; forceHeight?: boolean; }) { const [loaded, setLoaded] = useState(false); const [rendered, setRendered] = useState(false); const [base64Encoded, setBase64Encoded] = useState(null); const client = useContext(ClientContext); useEffect(() => { setLoaded(false); }, []); useEffect(() => { if (!path || !client.loaded) return; let currentProject = project || client.project; const main = async () => { setLoaded(false); if (currentExecution && waitForOthers) { log('Waiting for other fetch to finish...'); await awaitCurrentExecution(); } if (waitForOthers) currentExecution = true; let blob = await fetchImport( currentProject.deployedProject, path.export, null, false, client.controller.apiAccess ); let ext = currentProject.deployedProject.bundles.imports[ currentProject.deployedProject.imports[path.export.key] ].extension.substring(1); let encoding = 'image/' + ext; if (ext === 'svg') encoding = 'image/svg+xml'; setBase64Encoded( `data:${encoding};base64,` + Buffer.from(await blob.arrayBuffer()).toString('base64') ); setLoaded(true); if (waitForOthers) currentExecution = false; }; main(); }, [path, project, client, waitForOthers]); return (
{!loaded ? ( 🚀

Loading
{cutLongString( path.export.key.split('@')?.[1] || path.export.key, 30 )}

) : ( {!rendered ? (
) : ( <> )} {'path { console.log('test'); }} onLoad={(e) => { setRendered(true); e.currentTarget.style.display = 'block'; }} />
)}
); }