import { useState } from 'react' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { api, type PlaylistKind, type PlaylistNode } from '../api' import { useCaps } from '../lib/capabilities' import { SaveBar } from './SaveBar' import { DevicesSection } from './DevicesSection' /** * Which view the main table is showing. * * `device*` variants are a browsed import SOURCE — a plugged-in stick — rather * than the loaded collection. They are a view like any other, so the table, the * toolbar and the deck need no special case; what differs is that a device is * read-only, and that is carried by its CAPABILITIES rather than by this type, * so no component has to ask "am I looking at a device?" before deciding what to * offer. */ export type Source = | { kind: 'all' } | { kind: 'playlist'; id: string; name: string } | { kind: 'device' } | { kind: 'device-playlist'; id: string; name: string } interface Props { source: Source onSelect: (s: Source) => void onError: (msg: string) => void onOpenHistory: () => void onImport: () => void } // Kind picks the icon; every behavioural question is answered by the node's own // flags, so nothing here infers what a node can do from what it is called. const icons: Record = { folder: '▸', playlist: '♫', smart: '✦', } function NodeRow({ node, depth, source, onSelect, onError, }: { node: PlaylistNode depth: number source: Source onSelect: (s: Source) => void onError: (msg: string) => void }) { const qc = useQueryClient() const [open, setOpen] = useState(true) const [renaming, setRenaming] = useState(false) const [draft, setDraft] = useState(node.name) const isFolder = node.kind === 'folder' const selected = source.kind === 'playlist' && source.id === node.id const selectable = node.selectable const invalidate = () => { qc.invalidateQueries({ queryKey: ['playlists'] }) qc.invalidateQueries({ queryKey: ['state'] }) } const rename = useMutation({ mutationFn: (name: string) => api.renamePlaylist(node.id, name), onSuccess: (_d, name) => { invalidate() if (source.kind === 'playlist' && source.id === node.id) onSelect({ kind: 'playlist', id: node.id, name }) }, onError: (e: Error) => onError(e.message), }) const del = useMutation({ mutationFn: () => api.deletePlaylist(node.id), onSuccess: () => { invalidate() if (source.kind === 'playlist' && source.id === node.id) onSelect({ kind: 'all' }) }, onError: (e: Error) => onError(e.message), }) return (
{/* Each action gates on ITS OWN flag: a platform may allow renaming but not deleting. The count shows either way — it is information, not an action, and hiding it on a read-only library loses real data. */} {!renaming && node.can_rename && ( )} {!renaming && node.can_delete && ( )} {!renaming && !isFolder && ( {node.count} )}
{isFolder && open && node.children.length > 0 && (
{node.children.map((c) => ( ))}
)}
) } export function Sidebar({ source, onSelect, onError, onOpenHistory, onImport }: Props) { const qc = useQueryClient() // There is no per-node flag for "you may create a NEW playlist" — the node // flags describe existing nodes — so this is the library-level gate. const canCreate = useCaps().writable const [creating, setCreating] = useState(false) const [newName, setNewName] = useState('') const { data: playlists, isLoading } = useQuery({ queryKey: ['playlists'], queryFn: api.playlists, }) const create = useMutation({ mutationFn: (name: string) => api.createPlaylist(name), onSuccess: (pl) => { qc.invalidateQueries({ queryKey: ['playlists'] }) qc.invalidateQueries({ queryKey: ['state'] }) onSelect({ kind: 'playlist', id: pl.id, name: pl.name }) }, onError: (e: Error) => onError(e.message), }) const submitNew = () => { setCreating(false) if (newName.trim()) create.mutate(newName.trim()) setNewName('') } return ( ) }