import { tr } from '../lib/i18n'; import { useState } from 'react'; import { Btn, ErrorNotice, Icon, Select } from './ui'; import type { ImageModelChoice, ImageModelLibrary } from '../lib/types'; import { cancelImageModel, installImageModel, selectImageModel } from '../lib/store'; export function RunImageModel({ library, disabled, onChange }: { library: ImageModelLibrary | null; disabled: boolean; onChange: () => void; }) { const [pending, setPending] = useState(false); const [error, setError] = useState(''); const version = library?.variants.find(v => v.id === library.selected); const downloading = library?.variants.find(v => v.state === 'downloading'); async function perform(action: () => Promise) { setPending(true); setError(''); try { await action(); } catch (reason) { setError(reason instanceof Error ? reason.message : String(reason)); } finally { setPending(false); } } return
{tr("Image model ")}{tr("Optional")}{tr(library?.family ?? 'Qwen-Image-2.1-Turbo')}
ariaLabel={tr("Image model version")} value={library?.selected ?? 'off'} disabled={disabled || pending || !!downloading || !library} menuWidth={240} menuSize="comfortable" options={[{ value: 'off', label: 'Off' }, ...(library?.variants.map(v => ({ value: v.id, label: `${v.label} · ${v.downloadGb.toFixed(1)} GB`, title: `${v.quant}${v.state === 'installed' ? ' · Downloaded' : ''}` })) ?? [])]} onChange={choice => { onChange(); void perform(() => selectImageModel(choice)); }}/>
{tr(downloading ?
{tr(downloading.progress?.stage ?? 'Downloading')} {tr(downloading.label)} {tr(Math.floor(downloading.progress?.pct ?? 0))}% void perform(() => cancelImageModel(downloading.id))}>{tr("Cancel")}
: version ?
{tr(version.state === 'installed' ? <>{tr(library?.generationAvailable ? 'Downloaded' : 'Downloaded · Image engine required')} : 'Download this version to use it.')} {tr(version.state !== 'installed' ? void perform(() => installImageModel(version.id))}>{tr(version.state === 'error' ? 'Retry download' : `Download ${version.label}`)} : null)}
: null)} {tr(version?.error && !error ? : null)} {tr(error ? : null)}
; }