import { tr } from '../lib/i18n'; import { useRef, useState } from 'react'; import { FloatingMenu } from './FloatingMenu'; import { Icon } from './ui'; import { artifactIcon, artifactLabel, safePreviewUrl, type PreviewArtifact } from '../lib/artifacts'; import { api } from '../lib/client'; export function ArtifactCard({ artifact: a, onPreview }: { artifact: PreviewArtifact; onPreview: (a: PreviewArtifact) => void; }) { const [menu, setMenu] = useState(false), [busy, setBusy] = useState(false), [error, setError] = useState(''); const trigger = useRef(null); const url = a.url ? safePreviewUrl(a.url) : null; const localPath = url && new URL(url).origin === location.origin && new URL(url).pathname.startsWith('/examples/') ? new URL(url).pathname : null; const canReveal = !!localPath || a.content !== undefined || !!url?.startsWith('blob:'); async function open(action: 'open' | 'reveal') { setMenu(false); setError(''); if (!canReveal && url && action === 'open') { // The app window has no handler for a new window, so window.open does nothing there: the host opens the address in the default browser (#4721). api.post('/api/artifacts/open', { action: 'open', url }).catch(() => { window.open(url, '_blank', 'noopener,noreferrer'); }); return; } setBusy(true); try { const body: Record = { action, name: a.title }; if (localPath) body.path = localPath; else if (a.content !== undefined) body.content = a.content; else if (url?.startsWith('blob:')) { const blob = await (await fetch(url)).blob(); body.base64 = await new Promise((resolve, reject) => { const r = new FileReader(); r.onload = () => resolve(String(r.result).split(',')[1]); r.onerror = () => reject(new Error('Could not read this file.')); r.readAsDataURL(blob); }); } else throw new Error('No local file is available.'); await api.post('/api/artifacts/open', body); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setBusy(false); } } return
{tr(error ?

{tr(error)}

: null)}{tr(menu ? setMenu(false)}>{tr(!canReveal ?

{tr("Websites open in your default browser.")}

: a.content !== undefined || a.url?.startsWith('blob:') ?

{tr("Opens a local copy of this preview.")}

: null)}
: null)}
; }