Implemented basic playlist editing

This commit is contained in:
Ben committed 2026-07-03 13:21:35 +02:00
1 parent fceaab6d78
commit 3ace90179b
20 files changed
+1523 -105

No files matched your search

+50
View File
@@ -96,12 +96,42 @@ export interface TrackQuery {
offset?: number
}
export interface EditState {
dirty: boolean
nml_path: string
}
export interface SaveResult {
saved: boolean
backup: string | null
playlists: number
}
async function getJSON<T>(url: string): Promise<T> {
const res = await fetch(url)
if (!res.ok) throw new Error(`${res.status} ${res.statusText} — ${url}`)
return res.json() as Promise<T>
}
async function send<T>(method: string, url: string, body?: unknown): Promise<T> {
const res = await fetch(url, {
method,
headers: body ? { 'Content-Type': 'application/json' } : undefined,
body: body ? JSON.stringify(body) : undefined,
})
if (!res.ok) {
let detail = `${res.status} ${res.statusText}`
try {
const j = await res.json()
if (j.detail) detail = j.detail
} catch {
/* ignore */
}
throw new Error(detail)
}
return res.json() as Promise<T>
}
function qs(params: Record<string, unknown>): string {
const p = new URLSearchParams()
for (const [k, v] of Object.entries(params)) {
@@ -119,4 +149,24 @@ export const api = {
getJSON<TrackPage>(`/api/tracks${qs(query as Record<string, unknown>)}`),
playlistTracks: (id: string) =>
getJSON<Track[]>(`/api/playlists/${id}/tracks`),
// ---- writes ----
state: () => getJSON<EditState>('/api/state'),
createPlaylist: (name: string, parentId?: string) =>
send<PlaylistNode>('POST', '/api/playlists', { name, parent_id: parentId ?? null }),
renamePlaylist: (uuid: string, name: string) =>
send<{ status: string }>('PATCH', `/api/playlists/${uuid}`, { name }),
deletePlaylist: (uuid: string) =>
send<{ status: string }>('DELETE', `/api/playlists/${uuid}`),
setEntries: (uuid: string, trackIds: string[]) =>
send<{ status: string; count: number }>('PUT', `/api/playlists/${uuid}/entries`, {
track_ids: trackIds,
}),
addEntries: (uuid: string, trackIds: string[]) =>
send<{ status: string; added: number; count: number }>(
'POST',
`/api/playlists/${uuid}/add`,
{ track_ids: trackIds },
),
save: () => send<SaveResult>('POST', '/api/save'),
}