okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1import { tr } from '../lib/i18n' ; 2import { useRef , useState } from 'react' ; 3import { FloatingMenu } from './FloatingMenu' ; 4import { Icon } from './ui' ; 5import { artifactIcon , artifactLabel , safePreviewUrl , type PreviewArtifact } from '../lib/artifacts' ; 6import { api } from '../lib/client' ; 7export function ArtifactCard ({ artifact :a , onPreview} :{ 8artifact :PreviewArtifact ; 9onPreview :( a :PreviewArtifact ) => void ; 10}) { 11const [ menu , setMenu ] = useState ( false ), [ busy , setBusy ] = useState ( false ), [ error , setError ] = useState ( '' ); 12const trigger = useRef < HTMLButtonElement >( null ); 13const url = a . url ?safePreviewUrl ( a . url ) :null ; 14const localPath = url && new URL ( url ). origin === location . origin && new URL ( url ). pathname . startsWith ( '/examples/' ) ?new URL ( url ). pathname :null ; 15const canReveal = !! localPath || a . content !== undefined || !! url ?. startsWith ( 'blob:' ); 16async function open ( action :'open' | 'reveal' ) { 17setMenu ( false ); 18setError ( '' ); 19if ( ! canReveal && url && action === 'open' ) { 20// 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). 21api . post ( '/api/artifacts/open' , { action :'open' , url}). catch (() => { window . open ( url , '_blank' , 'noopener,noreferrer' ); }); 22return ; 23} 24setBusy ( true ); 25try { 26const body :Record < string , string > = { action, name :a . title }; 27if ( localPath ) 28body . path = localPath ; 29else if ( a . content !== undefined ) 30body . content = a . content ; 31else if ( url ?. startsWith ( 'blob:' )) { 32const blob = await ( await fetch ( url )). blob (); 33body . base64 = await new Promise < string >(( 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 ); }); 34} 35else 36throw new Error ( 'No local file is available.' ); 37await api . post ( '/api/artifacts/open' , body ); 38} 39catch ( e ) { 40setError ( e instanceof Error ?e . message :String ( e )); 41} 42finally { 43setBusy ( false ); 44} 45} 46return < div className = "artifact-card-wrap" >< div className = "artifact-card" >< button className = "artifact-card__open" onClick = {() => onPreview ( a )} aria-label = { tr ( "Open {0} in sidebar" , [ a . title ])}>< span className = "artifact-card__icon" >< Icon name = { artifactIcon ( a )} size = { 22 }/></ span >< span >< strong >{ a . title }</ strong >< small >{ tr ( artifactLabel ( a . kind ))}{ tr ( " \u00B7 Open in sidebar" )}</ small ></ span ></ button >< button ref = { trigger } className = "st-btn st-btn--icon" aria-label = { tr ( "Options for {0}" , [ a . title ])} title = { tr ( "Options for {0}" , [ a . title ])} aria-haspopup = "menu" aria-expanded = { menu } disabled = { busy } onClick = {() => setMenu ( v => ! v )}>< Icon name = "ellipsis" size = { 16 }/></ button ></ div >{ tr ( error ?< p className = "xs inline-error" role = "alert" >{ tr ( error )}</ p > :null )}{ tr ( menu ?< FloatingMenu anchor = { trigger . current } title = { tr ( "Open {0}" , [ a . title ])} onClose = {() => setMenu ( false )}>< button role = "menuitem" className = "menu__item" onClick = {() => { setMenu ( false ); onPreview ( a ); }}>< Icon name = "panel" />{ tr ( "Open in sidebar" )}</ button >< button role = "menuitem" className = "menu__item" disabled = { ! canReveal } title = { tr ( ! canReveal ?'This website has no local file' :undefined )} onClick = {() => void open ( 'reveal' )}>< Icon name = "folder" />{ tr ( "Show in file explorer" )}</ button >< button role = "menuitem" className = "menu__item" onClick = {() => void open ( 'open' )}>< Icon name = "computer" />{ tr ( "Open in default app" )}</ button >{ tr ( ! canReveal ?< p className = "composer-menu-note xs muted" >{ tr ( "Websites open in your default browser." )}</ p > :a . content !== undefined || a . url ?. startsWith ( 'blob:' ) ?< p className = "composer-menu-note xs muted" >{ tr ( "Opens a local copy of this preview." )}</ p > :null )}</ FloatingMenu > :null )}</ div >; 47}