import { useState } from 'react'; import { Upload } from 'lucide-react'; import { useQueryClient } from '@tanstack/react-query'; import { api } from '../api/client'; import { ErrorNotice } from './ui'; import type { Asset } from '../api/types'; export function AssetUpload({ kind, onUploaded, }: { kind: 'image' | 'voice'; onUploaded: (asset: Asset) => void; }) { const [file, setFile] = useState(null), [rights, setRights] = useState('original'), [note, setNote] = useState(''), [consent, setConsent] = useState(false), [busy, setBusy] = useState(false), [error, setError] = useState(); const query = useQueryClient(); async function upload() { if (!file) return; setBusy(true); setError(null); const form = new FormData(); form.append('file', file); form.append('kind', kind); form.append('rights', rights); form.append('rights_note', note); form.append('consent', String(consent)); try { const asset = await api.upload(form); await query.invalidateQueries({ queryKey: ['assets'] }); onUploaded(asset); setFile(null); } catch (e) { setError(e); } finally { setBusy(false); } } return (
{file && (
)}
); }