import { useState } from 'react' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { api } from '../api' interface Props { onError: (msg: string) => void } // Bottom-of-sidebar save control. Shows unsaved-changes state and writes to the // NML; every save is recorded in the collection's version history. export function SaveBar({ onError }: Props) { const qc = useQueryClient() const [justSaved, setJustSaved] = useState(null) const { data: state } = useQuery({ queryKey: ['state'], queryFn: api.state }) const save = useMutation({ mutationFn: api.save, onSuccess: (res) => { qc.invalidateQueries({ queryKey: ['state'] }) qc.invalidateQueries({ queryKey: ['history'] }) if (res.saved && res.commit) { setJustSaved(`Saved · version ${res.commit.slice(0, 8)}`) setTimeout(() => setJustSaved(null), 6000) } }, onError: (e: Error) => onError(e.message), }) const dirty = state?.dirty ?? false return (
{dirty && (
⚠ Close Traktor before saving — it overwrites the collection on exit.
)} {justSaved && (
{justSaved}
)}
) }