mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Begun implementing collection history system
This commit is contained in:
1 parent
75deded209
commit
e8d2c66e06
19 files changed
+744
-54
No files matched your search
@@ -0,0 +1,192 @@
|
||||
import { useState } from 'react'
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { api, type HistoryEntry } from '../api'
|
||||
import type { ToastMsg } from './Toast'
|
||||
|
||||
interface Props {
|
||||
onClose: () => void
|
||||
onNotify: (kind: ToastMsg['kind'], text: string) => void
|
||||
onError: (msg: string) => void
|
||||
}
|
||||
|
||||
function formatWhen(iso: string): string {
|
||||
const d = new Date(iso)
|
||||
if (Number.isNaN(d.getTime())) return iso
|
||||
return d.toLocaleString(undefined, {
|
||||
year: 'numeric',
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Version history browser for the current collection. Every save is a commit in
|
||||
* a purely-local git repo (see backend history.py); this lists them newest-first
|
||||
* and lets the user restore any past version (written back as a new forward save)
|
||||
* or wipe the entire history. Restore overwrites the collection on disk, so the
|
||||
* user must close Traktor first (it rewrites the .nml on exit).
|
||||
*/
|
||||
export function HistoryPanel({ onClose, onNotify, onError }: Props) {
|
||||
const qc = useQueryClient()
|
||||
const history = useQuery({ queryKey: ['history'], queryFn: api.history })
|
||||
const [confirmingId, setConfirmingId] = useState<string | null>(null)
|
||||
const [clearing, setClearing] = useState(false)
|
||||
|
||||
const restore = useMutation({
|
||||
mutationFn: (id: string) => api.restoreVersion(id),
|
||||
onSuccess: () => {
|
||||
// A restore swaps the whole collection — refetch everything.
|
||||
qc.invalidateQueries()
|
||||
onNotify('success', 'Restored — the library now reflects that version.')
|
||||
setConfirmingId(null)
|
||||
onClose()
|
||||
},
|
||||
onError: (e: Error) => {
|
||||
onError(e.message)
|
||||
setConfirmingId(null)
|
||||
},
|
||||
})
|
||||
|
||||
const clear = useMutation({
|
||||
mutationFn: () => api.clearHistory(),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: ['history'] })
|
||||
onNotify('success', 'Version history deleted.')
|
||||
setClearing(false)
|
||||
},
|
||||
onError: (e: Error) => {
|
||||
onError(e.message)
|
||||
setClearing(false)
|
||||
},
|
||||
})
|
||||
|
||||
const entries = history.data ?? []
|
||||
|
||||
return (
|
||||
<div
|
||||
className="fixed inset-0 z-40 flex items-center justify-center bg-black/60 p-6"
|
||||
onClick={onClose}
|
||||
>
|
||||
<div
|
||||
className="flex max-h-[85vh] w-full max-w-xl flex-col overflow-hidden rounded-xl border border-line bg-ink-900 shadow-2xl"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<div className="border-b border-line px-5 py-4">
|
||||
<div className="text-[15px] font-semibold tracking-tight">Version History</div>
|
||||
<div className="text-xs text-muted">
|
||||
Every save is a restorable version of this collection. Close Traktor before
|
||||
restoring — it overwrites the collection on exit.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="min-h-0 flex-1 overflow-y-auto px-5 py-4">
|
||||
{history.isLoading ? (
|
||||
<div className="py-8 text-center text-sm text-faint">Loading…</div>
|
||||
) : entries.length === 0 ? (
|
||||
<div className="py-8 text-center text-sm text-faint">
|
||||
No versions yet. Saving a change records the first one.
|
||||
</div>
|
||||
) : (
|
||||
<ul className="space-y-1.5">
|
||||
{entries.map((e: HistoryEntry, i: number) => {
|
||||
const isCurrent = i === 0
|
||||
const isConfirming = confirmingId === e.id
|
||||
return (
|
||||
<li
|
||||
key={e.id}
|
||||
className="group flex items-center gap-3 rounded-md border border-line bg-ink-850 px-3 py-2"
|
||||
>
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="truncate text-sm text-text">{e.summary}</span>
|
||||
{isCurrent && (
|
||||
<span className="shrink-0 rounded bg-mint/15 px-1.5 py-0.5 text-[10px] font-medium text-mint">
|
||||
current
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="text-[11px] text-faint">
|
||||
{formatWhen(e.timestamp)} · {e.id.slice(0, 8)}
|
||||
</div>
|
||||
</div>
|
||||
{isConfirming ? (
|
||||
<div className="flex shrink-0 items-center gap-1.5">
|
||||
<button
|
||||
onClick={() => restore.mutate(e.id)}
|
||||
disabled={restore.isPending}
|
||||
className="rounded-md bg-gold px-2.5 py-1 text-xs font-medium text-ink-950 hover:brightness-110 disabled:opacity-50"
|
||||
>
|
||||
{restore.isPending ? 'Restoring…' : 'Restore'}
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setConfirmingId(null)}
|
||||
className="rounded-md px-2 py-1 text-xs text-muted hover:bg-ink-800 hover:text-text"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<button
|
||||
onClick={() => setConfirmingId(e.id)}
|
||||
disabled={isCurrent}
|
||||
title={isCurrent ? 'This is the current version' : 'Restore this version'}
|
||||
className="shrink-0 rounded-md border border-line px-2.5 py-1 text-xs text-muted opacity-0 transition-opacity hover:bg-ink-800 hover:text-text group-hover:opacity-100 disabled:cursor-not-allowed disabled:opacity-0"
|
||||
>
|
||||
Restore
|
||||
</button>
|
||||
)}
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Destructive: wipe the whole history. */}
|
||||
<div className="border-t border-line px-5 py-3">
|
||||
{clearing ? (
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<span className="text-[11px] text-gold">
|
||||
Permanently delete ALL {entries.length} version
|
||||
{entries.length === 1 ? '' : 's'}? This can't be undone.
|
||||
</span>
|
||||
<div className="flex shrink-0 items-center gap-1.5">
|
||||
<button
|
||||
onClick={() => clear.mutate()}
|
||||
disabled={clear.isPending}
|
||||
className="rounded-md bg-gold px-2.5 py-1 text-xs font-medium text-ink-950 hover:brightness-110 disabled:opacity-50"
|
||||
>
|
||||
{clear.isPending ? 'Deleting…' : 'Yes, delete all'}
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setClearing(false)}
|
||||
className="rounded-md px-2 py-1 text-xs text-muted hover:bg-ink-800 hover:text-text"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex items-center justify-between">
|
||||
<button
|
||||
onClick={() => setClearing(true)}
|
||||
disabled={entries.length === 0}
|
||||
className="rounded-md border border-gold/50 px-2.5 py-1 text-xs text-gold hover:bg-gold/10 disabled:opacity-40"
|
||||
>
|
||||
Delete all history…
|
||||
</button>
|
||||
<button
|
||||
onClick={onClose}
|
||||
className="rounded-md px-3 py-1.5 text-sm text-muted hover:bg-ink-800 hover:text-text"
|
||||
>
|
||||
Close
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -87,7 +87,7 @@ export function PathMappingDialog({ onClose, onNotify, onError }: Props) {
|
||||
onNotify(
|
||||
'success',
|
||||
`Rewrote ${res.rewritten} track path${res.rewritten === 1 ? '' : 's'}.` +
|
||||
(res.backup ? ` Backup: ${res.backup.split('/').pop()}` : ''),
|
||||
(res.commit ? ` Version: ${res.commit.slice(0, 8)}` : ''),
|
||||
)
|
||||
onClose()
|
||||
}
|
||||
@@ -195,8 +195,8 @@ export function PathMappingDialog({ onClose, onNotify, onError }: Props) {
|
||||
<div className="mt-2 space-y-2 rounded-md border border-gold/40 bg-gold/5 px-3 py-3">
|
||||
<div className="text-xs font-semibold text-gold">Make permanent</div>
|
||||
<p className="text-[11px] leading-relaxed text-muted">
|
||||
Rewrites the matching file paths directly in the collection (a backup is
|
||||
written first). Use this only when you've moved the library for good — it
|
||||
Rewrites the matching file paths directly in the collection (saved to
|
||||
version history). Use this only when you've moved the library for good — it
|
||||
bakes in <span className="text-text">this machine's</span> paths and will
|
||||
break the collection on other machines/OSes.
|
||||
</p>
|
||||
|
||||
@@ -7,7 +7,7 @@ interface Props {
|
||||
}
|
||||
|
||||
// Bottom-of-sidebar save control. Shows unsaved-changes state and writes to the
|
||||
// NML (backup-first). On-disk saving is gated server-side by KONDUKTOR_ALLOW_WRITE.
|
||||
// NML; every save is recorded in the collection's version history.
|
||||
export function SaveBar({ onError }: Props) {
|
||||
const qc = useQueryClient()
|
||||
const [justSaved, setJustSaved] = useState<string | null>(null)
|
||||
@@ -17,9 +17,9 @@ export function SaveBar({ onError }: Props) {
|
||||
mutationFn: api.save,
|
||||
onSuccess: (res) => {
|
||||
qc.invalidateQueries({ queryKey: ['state'] })
|
||||
if (res.saved && res.backup) {
|
||||
const name = res.backup.split('/').pop()
|
||||
setJustSaved(`Saved · backup ${name}`)
|
||||
qc.invalidateQueries({ queryKey: ['history'] })
|
||||
if (res.saved && res.commit) {
|
||||
setJustSaved(`Saved · version ${res.commit.slice(0, 8)}`)
|
||||
setTimeout(() => setJustSaved(null), 6000)
|
||||
}
|
||||
},
|
||||
|
||||
@@ -9,6 +9,7 @@ interface Props {
|
||||
source: Source
|
||||
onSelect: (s: Source) => void
|
||||
onError: (msg: string) => void
|
||||
onOpenHistory: () => void
|
||||
}
|
||||
|
||||
const icons: Record<string, string> = {
|
||||
@@ -157,7 +158,7 @@ function NodeRow({
|
||||
)
|
||||
}
|
||||
|
||||
export function Sidebar({ source, onSelect, onError }: Props) {
|
||||
export function Sidebar({ source, onSelect, onError, onOpenHistory }: Props) {
|
||||
const qc = useQueryClient()
|
||||
const [creating, setCreating] = useState(false)
|
||||
const [newName, setNewName] = useState('')
|
||||
@@ -245,6 +246,14 @@ export function Sidebar({ source, onSelect, onError }: Props) {
|
||||
))}
|
||||
</div>
|
||||
|
||||
<button
|
||||
onClick={onOpenHistory}
|
||||
className="flex items-center gap-2 border-t border-line px-4 py-2 text-left text-xs text-muted hover:bg-ink-800 hover:text-text"
|
||||
>
|
||||
<span className="text-[11px] text-faint">🕑</span>
|
||||
Version history
|
||||
</button>
|
||||
|
||||
<SaveBar onError={onError} />
|
||||
</aside>
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user