import { createPortal } from 'react-dom' import { useState } from 'react' import { useQuery } from '@tanstack/react-query' import { api, type ExportSet } from '../api' import { FolderPicker } from './FolderPicker' /** * Create or edit an export: its name, its target platforms and where it lands. * * Several platforms can be ticked. They share the destination and ONE copy of * the audio (under `Contents/`), each writing its own library beside it — so a * stick can carry a Traktor collection and a OneLibrary library at once. * * All three are settled up front, and the destination especially is NOT a * detail deferred to export time. A Traktor `` is a volume name plus * a volume-relative path — both derived from where the export is written — so * the library file genuinely cannot be built until the destination is known. */ interface Props { /** The set being edited, or null to create a new one. */ editing: ExportSet | null onClose: () => void onSaved: (set: ExportSet, created: boolean) => void onError: (msg: string) => void } export function ExportDialog({ editing, onClose, onSaved, onError }: Props) { const [name, setName] = useState(editing?.name ?? '') const [chosen, setChosen] = useState(editing?.targets ?? ['traktor']) const [destination, setDestination] = useState(editing?.destination ?? '') const [browsing, setBrowsing] = useState(false) const [busy, setBusy] = useState(false) // Every platform, with `installed` standing for "can be an export target". // The unsupported ones are shown DISABLED with a reason rather than hidden: // an absent option reads as a missing feature, a disabled one as a roadmap. const targets = useQuery({ queryKey: ['export-targets'], queryFn: api.exportTargets }) // Another set writing to the same folder would have its output wiped, because // an export clears its destination before writing. Caught while the folder is // being chosen — at run time a warning would already be too late. const conflict = useQuery({ queryKey: ['export-conflict', editing?.id ?? null], queryFn: () => api.exportContents(editing!.id), enabled: !!editing, }).data?.destination_conflict // Some targets are only found at a drive's root (a CDJ looks for `PIONEER/` // there and nowhere else). Warned, not refused: the other targets are fine in // any folder. Checked against the same polled drive list the sidebar uses. const drives = useQuery({ queryKey: ['drives'], queryFn: api.drives }) const trim = (p: string) => p.replace(/[\\/]+$/, '') || p const atDriveRoot = destination.trim() !== '' && (drives.data ?? []).some((d) => trim(d.path) === trim(destination.trim())) const needsRoot = (targets.data ?? []).filter( (p) => p.drive_root && chosen.includes(p.platform), ) // A computer-only library ticked beside a stick format: harmless, but it does // nothing on the stick, which is easy to mistake for a broken export. const computerOnly = (targets.data ?? []).filter( (p) => p.computer_only && chosen.includes(p.platform), ) const toggle = (platform: string, on: boolean) => setChosen((prev) => on ? [...prev.filter((t) => t !== platform), platform] : prev.filter((t) => t !== platform), ) const valid = name.trim() !== '' && destination.trim() !== '' && chosen.length > 0 const save = async () => { setBusy(true) try { const body = { name: name.trim(), targets: chosen, destination: destination.trim() } const saved = editing ? await api.updateExport(editing.id, body) : await api.createExport(body) onSaved(saved, !editing) } catch (e) { onError((e as Error).message) } finally { setBusy(false) } } return createPortal( <> {browsing && ( setBrowsing(false)} /> )}

{editing ? `Edit “${editing.name}”` : 'New export'}

Name setName(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter' && valid && !busy) save() }} className="w-full rounded-md well px-2 py-1.5 text-sm text-text outline-none focus:ring-1 focus:ring-accent" />
For
{(targets.data ?? []).map((p) => ( ))}
{computerOnly.length > 0 && needsRoot.length > 0 && (
{computerOnly.map((p) => p.name).join(' and ')} is only read on a computer, never from a USB drive, so it adds nothing to {needsRoot.map((p) => p.name).join(' and ')}. It replaces a collection on this computer and only works on this machine.
)}
Export to
{destination || 'Choose a folder…'}
The folder gets a library for each platform, plus one copy of every track under{' '} Contents/, in your own folder structure.
{needsRoot.length > 0 && destination.trim() !== '' && !atDriveRoot && (
{needsRoot.map((p) => p.name).join(' and ')} is only found at the top of a drive. Choose the drive itself rather than a folder on it, or players won’t see this export.
)} {conflict && (
“{conflict}” already exports here. Exporting replaces whatever the other one wrote.
)}
, document.body, ) }