mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Implemented basic playlist editing
This commit is contained in:
1 parent
fceaab6d78
commit
3ace90179b
20 files changed
+1523
-105
No files matched your search
+87
-27
@@ -1,11 +1,14 @@
|
||||
import { useMemo, useState } from 'react'
|
||||
import { useCallback, useMemo, useState } from 'react'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { SortingState } from '@tanstack/react-table'
|
||||
import { api, type Track } from './api'
|
||||
import { Sidebar, type Source } from './components/Sidebar'
|
||||
import { Toolbar, emptyFilters, type Filters } from './components/Toolbar'
|
||||
import { TrackTable } from './components/TrackTable'
|
||||
import { PlaylistEditor } from './components/PlaylistEditor'
|
||||
import { SelectionBar } from './components/SelectionBar'
|
||||
import { StatusBar } from './components/StatusBar'
|
||||
import { Toast, type ToastMsg } from './components/Toast'
|
||||
|
||||
function applyFilters(tracks: Track[], f: Filters): Track[] {
|
||||
const q = f.search.trim().toLowerCase()
|
||||
@@ -31,57 +34,114 @@ export default function App() {
|
||||
const [source, setSource] = useState<Source>({ kind: 'all' })
|
||||
const [filters, setFilters] = useState<Filters>(emptyFilters)
|
||||
const [sorting, setSorting] = useState<SortingState>([])
|
||||
const [selected, setSelected] = useState<Set<string>>(new Set())
|
||||
const [toast, setToast] = useState<ToastMsg | null>(null)
|
||||
|
||||
const notify = useCallback((kind: ToastMsg['kind'], text: string) => {
|
||||
setToast({ id: Date.now(), kind, text })
|
||||
}, [])
|
||||
const onError = useCallback((msg: string) => notify('error', msg), [notify])
|
||||
|
||||
const allTracks = useQuery({
|
||||
queryKey: ['tracks', 'all'],
|
||||
queryFn: () => api.tracks({ limit: 20000, sort: 'artist' }),
|
||||
enabled: source.kind === 'all',
|
||||
})
|
||||
|
||||
const playlistTracks = useQuery({
|
||||
queryKey: ['playlist', source.kind === 'playlist' ? source.id : null],
|
||||
queryFn: () => api.playlistTracks((source as { id: string }).id),
|
||||
enabled: source.kind === 'playlist',
|
||||
})
|
||||
|
||||
const tracks: Track[] =
|
||||
source.kind === 'all' ? (allTracks.data?.items ?? []) : (playlistTracks.data ?? [])
|
||||
const loading = source.kind === 'all' ? allTracks.isLoading : playlistTracks.isLoading
|
||||
const isAll = source.kind === 'all'
|
||||
const tracks: Track[] = isAll ? (allTracks.data?.items ?? []) : (playlistTracks.data ?? [])
|
||||
const loading = isAll ? allTracks.isLoading : playlistTracks.isLoading
|
||||
const filtered = useMemo(() => (isAll ? applyFilters(tracks, filters) : tracks), [
|
||||
isAll,
|
||||
tracks,
|
||||
filters,
|
||||
])
|
||||
|
||||
const filtered = useMemo(() => applyFilters(tracks, filters), [tracks, filters])
|
||||
const selectSource = (s: Source) => {
|
||||
setSource(s)
|
||||
setSorting([])
|
||||
setSelected(new Set())
|
||||
}
|
||||
|
||||
const sourceName = source.kind === 'all' ? 'All Tracks' : source.name
|
||||
const toggle = (id: string) =>
|
||||
setSelected((prev) => {
|
||||
const next = new Set(prev)
|
||||
next.has(id) ? next.delete(id) : next.add(id)
|
||||
return next
|
||||
})
|
||||
const toggleAll = () =>
|
||||
setSelected((prev) =>
|
||||
prev.size === filtered.length ? new Set() : new Set(filtered.map((t) => t.id)),
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="flex h-screen w-screen overflow-hidden bg-ink-950">
|
||||
<Sidebar
|
||||
source={source}
|
||||
onSelect={(s) => {
|
||||
setSource(s)
|
||||
// Playlists carry a deliberate order; reset sort to preserve it.
|
||||
setSorting([])
|
||||
}}
|
||||
/>
|
||||
<main className="flex min-w-0 flex-1 flex-col">
|
||||
<Toolbar filters={filters} onChange={setFilters} />
|
||||
<Toast toast={toast} onClose={() => setToast(null)} />
|
||||
<Sidebar source={source} onSelect={selectSource} onError={onError} />
|
||||
|
||||
<main className="relative flex min-w-0 flex-1 flex-col">
|
||||
{isAll ? (
|
||||
<Toolbar filters={filters} onChange={setFilters} />
|
||||
) : (
|
||||
<div className="flex items-center gap-3 border-b border-line bg-ink-900 px-4 py-3">
|
||||
<span className="text-[11px] text-accent">♫</span>
|
||||
<span className="font-semibold text-text">{source.name}</span>
|
||||
<span className="text-xs text-faint">
|
||||
{tracks.length} tracks · drag ⠿ to reorder · × to remove
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="min-h-0 flex-1">
|
||||
{loading ? (
|
||||
<div className="flex h-full items-center justify-center text-muted">
|
||||
Loading library…
|
||||
</div>
|
||||
) : filtered.length === 0 ? (
|
||||
<div className="flex h-full flex-col items-center justify-center gap-1 text-muted">
|
||||
<div className="text-lg">No tracks match</div>
|
||||
<div className="text-sm text-faint">Try clearing some filters.</div>
|
||||
</div>
|
||||
<div className="flex h-full items-center justify-center text-muted">Loading…</div>
|
||||
) : isAll ? (
|
||||
filtered.length === 0 ? (
|
||||
<div className="flex h-full flex-col items-center justify-center gap-1 text-muted">
|
||||
<div className="text-lg">No tracks match</div>
|
||||
<div className="text-sm text-faint">Try clearing some filters.</div>
|
||||
</div>
|
||||
) : (
|
||||
<TrackTable
|
||||
tracks={filtered}
|
||||
sorting={sorting}
|
||||
onSortingChange={setSorting}
|
||||
selection={{
|
||||
selected,
|
||||
onToggle: toggle,
|
||||
onToggleAll: toggleAll,
|
||||
allSelected: selected.size > 0 && selected.size === filtered.length,
|
||||
}}
|
||||
/>
|
||||
)
|
||||
) : (
|
||||
<TrackTable tracks={filtered} sorting={sorting} onSortingChange={setSorting} />
|
||||
<PlaylistEditor
|
||||
uuid={(source as { id: string }).id}
|
||||
tracks={tracks}
|
||||
onError={onError}
|
||||
/>
|
||||
)}
|
||||
|
||||
{isAll && (
|
||||
<SelectionBar
|
||||
count={selected.size}
|
||||
trackIds={[...selected]}
|
||||
onClear={() => setSelected(new Set())}
|
||||
onDone={(msg) => notify('success', msg)}
|
||||
onError={onError}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<StatusBar
|
||||
showing={filtered.length}
|
||||
total={tracks.length}
|
||||
sourceName={sourceName}
|
||||
sourceName={isAll ? 'All Tracks' : source.name}
|
||||
loading={loading}
|
||||
/>
|
||||
</main>
|
||||
|
||||
Reference in new issue
Block a user