import { createPortal } from 'react-dom' import { useEffect, useState } from 'react' import { useQuery, useQueryClient } from '@tanstack/react-query' import { api, type StemTarget } from '../api' import { askConfirm } from '../lib/confirm' import { FolderPicker } from './FolderPicker' import { size } from './ConvertStemsDialog' /** * Settings → Stems (decided contents): the engine (status, download, install * from files for offline machines, remove), the compute device, the conversion * defaults the Convert dialog remembers, and the originals awaiting Save. */ interface Props { onClose: () => void } export function StemsSettings({ onClose }: Props) { const qc = useQueryClient() const engine = useQuery({ queryKey: ['stem-engine'], queryFn: api.stemEngine }) const state = useQuery({ queryKey: ['state'], queryFn: api.state, retry: false }) const prefs = useQuery({ queryKey: ['prefs'], queryFn: api.getPrefs }) const [busy, setBusy] = useState(false) const [error, setError] = useState(null) const [picking, setPicking] = useState(null) const st = engine.data const installJob = st?.install_job ?? null const install = useQuery({ queryKey: ['job', installJob], queryFn: () => api.job(installJob!), enabled: !!installJob, refetchInterval: (q) => (q.state.data && q.state.data.state !== 'running' ? false : 500), }) useEffect(() => { if (install.data && install.data.state !== 'running') { if (install.data.state === 'failed') setError(install.data.error || 'The download failed') qc.invalidateQueries({ queryKey: ['stem-engine'] }) } }, [install.data, qc]) useEffect(() => { const onKey = (e: KeyboardEvent) => e.key === 'Escape' && !picking && onClose() window.addEventListener('keydown', onKey) return () => window.removeEventListener('keydown', onKey) }, [onClose, picking]) const act = async (fn: () => Promise) => { setBusy(true) setError(null) try { await fn() qc.invalidateQueries({ queryKey: ['stem-engine'] }) } catch (e) { setError((e as Error).message) } finally { setBusy(false) } } const device = st?.device ?? 'auto' const setDevice = (d: string) => api.patchPrefs({ stemDevice: d }).then((p) => { qc.setQueryData(['prefs'], p) qc.invalidateQueries({ queryKey: ['stem-engine'] }) }) const defaults = (prefs.data as { stemConvert?: { mode?: string; destination?: string; collection?: string } }) ?.stemConvert const pending = state.data?.pending_stems const downloading = install.data?.state === 'running' const targetLabel = (t: StemTarget) => t === 'macos-arm64' ? 'Apple silicon (GPU)' : t === 'windows-x64-cuda' ? 'NVIDIA GPU' : 'processor only' return createPortal( <> {picking && ( void act(() => api.stemSideload(path, picking))} onClose={() => setPicking(null)} /> )}
e.stopPropagation()} >

Stems

Stem engine

{!st ? (
Loading…
) : !st.supported ? (
There is no stem engine for this computer.
) : ( <>
{st.installed ? `Installed — version ${st.installed.version}, ${targetLabel(st.installed.target)}${ st.installed.size ? `, ${size(st.installed.size)}` : '' }` : 'Not installed'} {' · '} {st.weights.installed ? `model installed (${size(st.weights.size)})` : 'model not installed'}
{downloading && install.data && (
{install.data.message} {size(install.data.done)} of {size(install.data.total)}
)}
{!(st.installed && st.weights.installed) && !downloading && st.offered_targets.map((t) => ( ))} {(st.installed || st.weights.installed) && ( )}
{!st.manifest_available && !st.installed && (
The download cannot be reached right now.
)} )}

Compute device

{[ ['auto', 'Automatic'], ['gpu', 'GPU'], ['cpu', 'Processor only'], ].map(([value, label]) => ( ))}
Automatic uses the fastest available. The GPU is several times faster; without one, conversion runs on the processor.

Conversion defaults

{defaults ? defaults.mode === 'destination' ? `Save to ${defaults.destination ?? 'a folder'}, ${ defaults.collection === 'add' ? 'added as new tracks' : 'the track pointed at its stem file' }` : 'Replace the originals' : 'Replace the originals'}
The Convert to Stems dialog remembers what you chose last.
{defaults && ( )}

Originals awaiting Save

{pending && pending.parked ? `${pending.parked} original file${pending.parked === 1 ? '' : 's'} (${size(pending.bytes)}) kept beside their stem files. Saving deletes them; Discard puts them back.` : 'None.'}
{error &&
{error}
}
, document.body, ) }