Files
konduktor/frontend/src/components/Sidebar.tsx
T

796 lines
31 KiB
TypeScript

import { useEffect, useRef, useState } from 'react'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { api, type PlaylistKind, type PlaylistNode, type TrackOrigin } from '../api'
import type { AddTarget } from './AddFilesDialog'
import { useCaps } from '../lib/capabilities'
import { SaveBar } from './SaveBar'
import { confirmDiscardUnsaved } from '../lib/unsaved'
import { SettingsMenu } from './SettingsMenu'
import { DevicesSection } from './DevicesSection'
import { ExportsSection } from './ExportsSection'
import { ConfirmDialog, type ConfirmRequest } from './ConfirmDialog'
import { ContextMenu, type MenuItem } from './ContextMenu'
import { Icon, type IconName } from '../lib/icons'
import { PlatformIcon } from '../lib/platformIcons'
import { addTracksToPlaylist } from '../lib/addTracks'
import { useDragPayload, usePressToDrag, useDropTarget, type DragPayload } from '../lib/drag'
import { nodeIdsUnder, playlistIdsUnder } from '../lib/playlistTree'
/**
* 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 }
// A folder of loose files on any drive, browsed from a Files tree. Its tracks
// are not in any library until added, so the view is read-only.
| { kind: 'folder'; path: string; name: string }
// An export set. `export` is its root — every track it would ship, deduped —
// and `export-playlist` is one referenced playlist inside it, read live from
// the library rather than from the set, which is the reference doing its job.
| { kind: 'export'; id: string; name: string }
| { kind: 'export-playlist'; exportId: string; id: string; name: string }
// The loose tracks — those added individually rather than via a playlist.
// A view of its own, NOT the export root: the root is everything the export
// would ship, which is a different question and a different answer.
| { kind: 'export-other'; exportId: string; name: string }
interface Props {
source: Source
onSelect: (s: Source) => void
onError: (msg: string) => void
onOpenHistory: () => void
onImport: () => void
onSwitchLibrary: () => void
/** Open a drive's library as THE library (Devices' "Open for editing…"). */
onOpenLibrary: (path: string) => void
onDone: (msg: string) => void
/** Tracks not in the collection yet (a device's, a folder's) dropped on a
* playlist or export: open the dialog that adds them, target filled in. */
onAddForeign: (origin: TrackOrigin, ids: string[], target: AddTarget) => void
onOpenPathMapping: () => void
/** After unsaved edits were discarded (SaveBar). */
onDiscarded?: () => 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.
/** The confirm wording for deleting a node. A folder says what goes with it,
* since the whole subtree is deleted and the row only shows the folder. */
function deleteRequest(node: PlaylistNode) {
if (node.kind !== 'folder')
return {
title: 'Delete playlist',
body: `Delete "${node.name}"? Its tracks stay in your collection.`,
confirmLabel: 'Delete playlist',
}
const playlists = playlistIdsUnder(node).length
const inside =
playlists === 0
? 'It is empty.'
: `The ${playlists === 1 ? 'playlist' : `${playlists} playlists`} inside it will be deleted too; their tracks stay in your collection.`
return {
title: 'Delete folder',
body: `Delete "${node.name}"? ${inside}`,
confirmLabel: 'Delete folder',
}
}
const icons: Record<PlaylistKind, IconName> = {
folder: 'chevronRight',
playlist: 'playlist',
smart: 'smart',
}
/** What the sidebar is naming in place: a new node, and the folder it goes in
* (`null` = the top level). Nothing is created until the name is committed. */
type Draft = { kind: 'playlist' | 'folder'; parentId: string | null }
/** Actions a row can ask of the sidebar. They live there, not in the row, so
* the right-click menu and the hover buttons run the very same code. */
interface RowActions {
onContextMenu: (e: React.MouseEvent, node: PlaylistNode) => void
onRename: (node: PlaylistNode, name: string) => void
onDelete: (node: PlaylistNode) => void
onAddToExport: (node: PlaylistNode, setId: string) => void
/** The row's export button: the export list as a menu under it. */
onPickExport: (e: React.MouseEvent<HTMLElement>, node: PlaylistNode) => void
onCommitDraft: (name: string) => void
onCancelDraft: () => void
/** Whether a drag of tracks may drop on this playlist. */
acceptsTracks: (node: PlaylistNode, p: Extract<DragPayload, { kind: 'tracks' }>) => boolean
/** Tracks dropped on a playlist; `alt` (Option) moves them out of the
* playlist they came from. */
onDropTracks: (node: PlaylistNode, p: Extract<DragPayload, { kind: 'tracks' }>, alt: boolean) => void
/** Whether a node may be dragged elsewhere in the tree. */
canMove: (node: PlaylistNode) => boolean
/** A node dropped in the tree: `index` among `parentId`'s children,
* counted without it (what `move_playlist` takes). */
onMoveNode: (node: PlaylistNode, parentId: string | null, index: number, into: PlaylistNode | null) => void
}
/** Where a dragged node would land, relative to the row under the pointer:
* `before`/`after` it, `into` it (a folder; appended), or `first` inside it —
* the lower edge of an EXPANDED folder, where the line sits above its first
* child. `tracks` is a track drop on a playlist; `peek` is tracks held over a
* collapsed folder, which opens it and drops nothing. */
type RowHint =
| { zone: 'tracks' | 'peek' }
| { zone: 'before' | 'after' | 'into' | 'first'; parentId: string | null; index: number }
/** The inline name field for a node being created. Enter or clicking away
* commits a non-empty name; Esc (or an empty name) creates nothing. */
function DraftRow({
draft,
depth,
actions,
}: {
draft: Draft
depth: number
actions: RowActions
}) {
const [name, setName] = useState('')
const done = useRef(false)
const finish = (commit: boolean) => {
if (done.current) return
done.current = true
if (commit && name.trim()) actions.onCommitDraft(name.trim())
else actions.onCancelDraft()
}
return (
<div
className="flex items-center gap-2 py-1 pr-1 text-sm"
style={{ paddingLeft: `${8 + depth * 14}px` }}
>
<span
className={`flex w-4 shrink-0 justify-center ${
draft.kind === 'folder' ? 'text-faint' : 'text-accent'
}`}
>
<Icon name={icons[draft.kind]} size={draft.kind === 'folder' ? 12 : 15} />
</span>
<input
autoFocus
value={name}
placeholder={draft.kind === 'folder' ? 'Folder name…' : 'Playlist name…'}
onChange={(e) => setName(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') finish(true)
else if (e.key === 'Escape') finish(false)
}}
onBlur={() => finish(true)}
className="min-w-0 flex-1 rounded-md bg-well px-1 py-0 text-sm text-text outline-none ring-1 ring-accent"
/>
</div>
)
}
function NodeRow({
node,
parentId,
siblings,
depth,
source,
onSelect,
renamingId,
setRenamingId,
draft,
exportSets,
actions,
}: {
node: PlaylistNode
/** The folder this node sits in; null = the top level. */
parentId: string | null
/** That folder's children, this node included, in order. */
siblings: PlaylistNode[]
depth: number
source: Source
onSelect: (s: Source) => void
renamingId: string | null
setRenamingId: (id: string | null) => void
draft: Draft | null
exportSets: { id: string; name: string }[]
actions: RowActions
}) {
const [open, setOpen] = useState(true)
const renaming = renamingId === node.id
const [renameDraft, setRenameDraft] = useState(node.name)
const isFolder = node.kind === 'folder'
const selected = source.kind === 'playlist' && source.id === node.id
const selectable = node.selectable
// A folder being created into shows its children, or the draft would be
// typed into a collapsed folder the user cannot see.
const draftHere = draft !== null && draft.parentId === node.id
const expanded = isFolder && (open || draftHere)
const startRename = () => {
setRenameDraft(node.name)
setRenamingId(node.id)
}
// A row is a drag SOURCE (onto an export, or elsewhere in the tree) and a
// drop TARGET for tracks. Whether it takes them is the node's own
// `can_add_tracks` plus the library being writable, decided by the sidebar.
const startDrag = usePressToDrag(() => (renaming ? null : { kind: 'node', node, parentId }))
const moves = (p: DragPayload, alt: boolean) =>
p.kind === 'tracks' && alt && !!p.fromPlaylist && p.fromPlaylist !== node.id
// A node held over this row: which zone, and where that puts it. Never into
// itself or anything nested in it; a drop that would leave it exactly where
// it is shows nothing.
const placeNode = (p: Extract<DragPayload, { kind: 'node' }>, y: number, el: HTMLElement): RowHint | null => {
if (!actions.canMove(p.node) || nodeIdsUnder(p.node).has(node.id)) return null
const r = el.getBoundingClientRect()
const f = (y - r.top) / r.height
const opened = expanded && node.children.length > 0
const zone: 'before' | 'after' | 'into' | 'first' = node.can_contain_children
? f < 0.25 ? 'before' : f > 0.75 ? (opened ? 'first' : 'after') : 'into'
: f < 0.5 ? 'before' : 'after'
let target: string | null
let index: number
let current: PlaylistNode[]
if (zone === 'into' || zone === 'first') {
target = node.id
current = node.children
index = zone === 'first' ? 0 : node.children.filter((c) => c.id !== p.node.id).length
} else {
target = parentId
current = siblings
const i = siblings.filter((c) => c.id !== p.node.id).findIndex((c) => c.id === node.id)
index = zone === 'before' ? i : i + 1
}
if (target === p.parentId && current.findIndex((c) => c.id === p.node.id) === index) return null
return { zone, parentId: target, index }
}
const [dropRef, over] = useDropTarget<RowHint>({
hint: (p, at, el) => {
if (p.kind === 'node') return placeNode(p, at.y, el)
if (actions.acceptsTracks(node, p)) return { zone: 'tracks' }
return isFolder && !expanded ? { zone: 'peek' } : null
},
label: (p, h, at) => {
if (h.zone === 'tracks') {
if (p.kind === 'tracks' && p.origin === 'device') return `Import into ${node.name}…`
if (p.kind === 'tracks' && p.origin === 'folder') return `Add to ${node.name}…`
return `${moves(p, at.alt) ? 'Move' : 'Add'} to ${node.name}`
}
if (h.zone === 'peek') return null
return h.zone === 'into' || h.zone === 'first' ? `Move into ${node.name}` : `Move ${h.zone} ${node.name}`
},
onDrop: (p, h, at) => {
if (p.kind === 'tracks') {
if (h.zone === 'tracks') actions.onDropTracks(node, p, at.alt)
} else if ('parentId' in h) {
actions.onMoveNode(p.node, h.parentId, h.index, h.zone === 'into' && !expanded ? node : null)
}
},
})
// Holding a drag over a collapsed folder opens it, so a playlist nested in
// it can be reached without letting go.
const opening = over?.zone === 'into' || over?.zone === 'peek'
useEffect(() => {
if (!opening || expanded) return
const t = setTimeout(() => setOpen(true), 600)
return () => clearTimeout(t)
}, [opening, expanded])
const ringed = over?.zone === 'tracks' || over?.zone === 'into'
const dragging = useDragPayload()
const beingMoved = dragging?.kind === 'node' && dragging.node.id === node.id
const line = over?.zone === 'before' || over?.zone === 'after' || over?.zone === 'first' ? over.zone : null
return (
<div className={beingMoved ? 'opacity-40' : undefined}>
<div
ref={dropRef}
data-node-row
onPointerDown={startDrag}
onContextMenu={(e) => actions.onContextMenu(e, node)}
className={`group relative flex items-center gap-1 rounded-[10px] pr-1 text-sm transition-colors ${
ringed
? 'bg-accent-soft/40 text-text ring-1 ring-accent'
: selected
? 'is-selected font-medium text-text'
: 'text-muted hover:bg-ink-800 hover:text-text'
}`}
>
{line && (
<span
aria-hidden
className={`pointer-events-none absolute right-1 h-0.5 rounded-full bg-accent ${
line === 'before' ? '-top-px' : '-bottom-px'
}`}
style={{ left: 8 + (line === 'first' ? depth + 1 : depth) * 14 }}
/>
)}
<button
onClick={() => {
if (isFolder) setOpen((o) => !o)
else if (selectable) onSelect({ kind: 'playlist', id: node.id, name: node.name })
}}
className={`flex min-w-0 flex-1 items-center gap-2 py-1.5 pl-2 text-left ${
!selectable && !isFolder ? 'cursor-default opacity-70' : ''
}`}
style={{ paddingLeft: `${8 + depth * 14}px` }}
>
<span
className={`flex w-4 shrink-0 justify-center ${
isFolder ? 'text-faint' : node.kind === 'smart' ? 'text-pink' : selected ? 'text-accent' : 'text-muted'
} ${expanded ? 'rotate-90' : ''} transition-transform`}
>
<Icon
name={icons[node.kind]}
size={isFolder ? 12 : 15}
strokeWidth={isFolder ? 2.4 : 1.8}
/>
</span>
{isFolder && (
<span className="-ml-1 flex shrink-0 text-muted">
<Icon name={expanded ? 'folderOpen' : 'folder'} size={15} />
</span>
)}
{renaming ? (
<input
autoFocus
value={renameDraft}
onChange={(e) => setRenameDraft(e.target.value)}
onClick={(e) => e.stopPropagation()}
onKeyDown={(e) => {
if (e.key === 'Enter') {
setRenamingId(null)
if (renameDraft.trim() && renameDraft.trim() !== node.name)
actions.onRename(node, renameDraft.trim())
} else if (e.key === 'Escape') {
setRenamingId(null)
}
}}
onBlur={() => setRenamingId(null)}
className="min-w-0 flex-1 rounded-md bg-well px-1 py-0 text-sm text-text outline-none ring-1 ring-accent"
/>
) : (
<span className="flex-1 truncate">{node.name}</span>
)}
</button>
{/* 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. */}
{/* Add this playlist (or folder) to an export. Not gated on the
library being writable: an export set is Konduktor's own curation
and adding to one changes nothing in the user's library. */}
{!renaming && exportSets.length > 0 && (
<button
title="Add to an export"
onClick={(e) => actions.onPickExport(e, node)}
className="hidden shrink-0 rounded px-1 text-faint hover:text-gold group-hover:block"
>
<Icon name="export" size={13} />
</button>
)}
{!renaming && node.can_rename && (
<button
title="Rename"
onClick={startRename}
className="hidden shrink-0 rounded px-1 text-faint hover:text-text group-hover:block"
>
<Icon name="pencil" size={13} />
</button>
)}
{!renaming && node.can_delete && (
<button
title={isFolder ? 'Delete folder' : 'Delete playlist'}
onClick={() => actions.onDelete(node)}
className="hidden shrink-0 rounded px-1 text-faint hover:text-pink group-hover:block"
>
<Icon name="close" size={13} />
</button>
)}
{!renaming && !isFolder && (
<span
className={`shrink-0 px-1.5 font-mono text-[11px] text-faint ${
node.can_rename || node.can_delete ? 'group-hover:hidden' : ''
}`}
>
{node.count}
</span>
)}
</div>
{expanded && (
<div>
{draftHere && <DraftRow draft={draft} depth={depth + 1} actions={actions} />}
{node.children.map((c) => (
<NodeRow
key={c.id}
node={c}
parentId={node.id}
siblings={node.children}
depth={depth + 1}
source={source}
onSelect={onSelect}
renamingId={renamingId}
setRenamingId={setRenamingId}
draft={draft}
exportSets={exportSets}
actions={actions}
/>
))}
</div>
)}
</div>
)
}
export function Sidebar({
source,
onSelect,
onError,
onOpenHistory,
onImport,
onSwitchLibrary,
onOpenLibrary,
onDone,
onAddForeign,
onOpenPathMapping,
onDiscarded,
}: Props) {
const qc = useQueryClient()
// Shares a cache entry with App, so the header never disagrees with what is
// loaded.
const library = useQuery({ queryKey: ['collection'], queryFn: api.collection }).data?.library
// Switching library throws away every unsaved edit — the adapter holds them in
// its native model, and opening another library replaces it. Worth a confirm:
// until now there was no way to switch at all, so this hazard is new.
const switchLibrary = async () => {
if (await confirmDiscardUnsaved()) onSwitchLibrary()
}
// 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 caps = useCaps()
const canCreate = caps.writable
const foldersSupported = caps.playlists.folders
const { data: playlists, isLoading } = useQuery({
queryKey: ['playlists'],
queryFn: api.playlists,
})
const exportSets = useQuery({ queryKey: ['exports'], queryFn: api.exports }).data ?? []
const [draft, setDraft] = useState<Draft | null>(null)
const [renamingId, setRenamingId] = useState<string | null>(null)
const [menu, setMenu] = useState<{ x: number; y: number; items: MenuItem[] } | null>(null)
const [confirmReq, setConfirmReq] = useState<ConfirmRequest | null>(null)
const invalidate = () => {
qc.invalidateQueries({ queryKey: ['playlists'] })
qc.invalidateQueries({ queryKey: ['state'] })
}
const create = useMutation({
mutationFn: ({ kind, parentId, name }: Draft & { name: string }) =>
kind === 'folder'
? api.createFolder(name, parentId ?? undefined)
: api.createPlaylist(name, parentId ?? undefined),
onSuccess: (node) => {
invalidate()
if (node.kind === 'playlist') onSelect({ kind: 'playlist', id: node.id, name: node.name })
},
onError: (e: Error) => onError(e.message),
})
const rename = useMutation({
mutationFn: ({ node, name }: { node: PlaylistNode; name: string }) =>
api.renamePlaylist(node.id, name),
onSuccess: (_d, { node, 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: (node: PlaylistNode) => api.deletePlaylist(node.id),
onSuccess: (_d, node) => {
invalidate()
// A folder takes its nested playlists with it, so the open view may be
// one of those rather than the node itself.
if (source.kind === 'playlist' && playlistIdsUnder(node).concat(node.id).includes(source.id))
onSelect({ kind: 'all' })
},
onError: (e: Error) => onError(e.message),
})
// A FOLDER contributes its nested playlists; a playlist contributes itself.
// Folders are flattened here rather than stored as a folder reference: the
// export stores playlist ids, and a folder is a shape in the tree, not a
// thing with tracks.
const addToExport = useMutation({
mutationFn: ({ node, setId }: { node: PlaylistNode; setId: string }) =>
api.addToExport(setId, { playlist_ids: playlistIdsUnder(node) }),
onSuccess: (_r, { setId }) => {
qc.invalidateQueries({ queryKey: ['export-contents', setId] })
qc.invalidateQueries({ queryKey: ['export-tracks', setId] })
},
onError: (e: Error) => onError(e.message),
})
const startDraft = (kind: Draft['kind'], parentId: string | null) => {
setRenamingId(null)
setDraft({ kind, parentId })
}
// Creating is the library-level gate (there is no per-node "may create"
// flag); WHERE is the node's own `can_contain_children`; and a folder also
// needs the platform to have folders at all.
const createItems = (parentId: string | null): MenuItem[] =>
canCreate
? [
{ label: 'New Playlist', icon: <Icon name="playlist" size={13} />, onClick: () => startDraft('playlist', parentId) },
...(foldersSupported
? [{ label: 'New Folder', icon: <Icon name="folder" size={13} />, onClick: () => startDraft('folder', parentId) }]
: []),
]
: []
const confirmDelete = (node: PlaylistNode) =>
setConfirmReq({ ...deleteRequest(node), onConfirm: async () => void (await del.mutateAsync(node)) })
const nodeItems = (node: PlaylistNode): MenuItem[] => {
const groups: MenuItem[][] = [
node.can_contain_children ? createItems(node.id) : [],
exportSets.length > 0
? [
{
label: 'Add to export',
submenu: exportSets.map((set) => ({
label: set.name,
icon: <Icon name="export" size={13} />,
onClick: () => addToExport.mutate({ node, setId: set.id }),
})),
},
]
: [],
[
...(node.can_rename
? [{ label: 'Rename', onClick: () => { setDraft(null); setRenamingId(node.id) } }]
: []),
...(node.can_delete
? [
{
label: node.kind === 'folder' ? 'Delete Folder…' : 'Delete Playlist…',
danger: true,
onClick: () => confirmDelete(node),
},
]
: []),
],
]
return groups
.filter((g) => g.length > 0)
.flatMap((g, i) => (i === 0 ? g : [{ separator: true } as MenuItem, ...g]))
}
const openMenu = (e: React.MouseEvent, items: MenuItem[]) => {
e.preventDefault()
e.stopPropagation()
if (items.length > 0) setMenu({ x: e.clientX, y: e.clientY, items })
}
const actions: RowActions = {
onContextMenu: (e, node) => openMenu(e, nodeItems(node)),
onRename: (node, name) => rename.mutate({ node, name }),
onDelete: confirmDelete,
onAddToExport: (node, setId) => addToExport.mutate({ node, setId }),
// Opened as the shared ContextMenu rather than a popover inside the row: the
// playlist list scrolls, and a popover inside it was clipped by its edges.
onPickExport: (e, node) => {
e.stopPropagation()
const r = e.currentTarget.getBoundingClientRect()
setMenu({
x: r.left,
y: r.bottom + 4,
items: [
{ heading: 'Add to export' },
...exportSets.map((set) => ({
label: set.name,
icon: <Icon name="export" size={13} />,
onClick: () => addToExport.mutate({ node, setId: set.id }),
})),
],
})
},
onCommitDraft: (name) => {
if (draft) create.mutate({ ...draft, name })
setDraft(null)
},
onCancelDraft: () => setDraft(null),
// A device's or a folder's tracks are not in the collection yet, so they
// can only come if the collection can ADD tracks — via the add dialog.
acceptsTracks: (node, p) =>
caps.writable &&
node.can_add_tracks &&
(p.origin === 'collection' ? p.fromPlaylist !== node.id : caps.tracks.addable),
canMove: (node) => caps.writable && node.can_move,
onMoveNode: async (node, parentId, index, into) => {
try {
await api.movePlaylist(node.id, parentId, index)
invalidate()
// Into a COLLAPSED folder the node vanishes from view, so say where.
if (into) onDone(`Moved “${node.name}” into ${into.name}`)
} catch (e) {
onError((e as Error).message)
}
},
onDropTracks: async (node, p, alt) => {
if (p.origin !== 'collection') {
onAddForeign(p.origin, p.ids, { kind: 'playlist', id: node.id, name: node.name })
return
}
try {
const msg = await addTracksToPlaylist({
qc,
playlist: node,
ids: p.ids,
duplicatesAllowed: caps.playlists.duplicates,
moveFrom: alt ? p.fromPlaylist : undefined,
})
if (msg) onDone(msg)
} catch (e) {
onError((e as Error).message)
}
},
}
// The empty space below the rows: a node dropped there goes to the END of
// the top level. Only the space itself — over a row that declined (a folder
// into its own subtree), nothing happens rather than this.
const roots = playlists ?? []
const [rootDropRef, rootOver] = useDropTarget<{ index: number }>({
hint: (p, at) => {
if (p.kind !== 'node' || !actions.canMove(p.node)) return null
if (document.elementFromPoint(at.x, at.y)?.closest('[data-node-row]')) return null
const index = roots.filter((n) => n.id !== p.node.id).length
if (p.parentId === null && roots.findIndex((n) => n.id === p.node.id) === index) return null
return { index }
},
label: () => 'Move to the top level',
onDrop: (p, h) => p.kind === 'node' && actions.onMoveNode(p.node, null, h.index, null),
})
return (
<aside className="glass flex h-full w-64 shrink-0 flex-col">
{/* Which library is open — and the way to open a different one. Until
this existed the picker was a one-way door: it ran once at startup and
nothing could summon it again, so changing library meant restarting.
It doubles as the only place the app says what it currently has open. */}
<button
onClick={switchLibrary}
title={library ? `${library.path} — click to open a different library` : undefined}
className="btn-glass group m-2.5 mb-0 flex shrink-0 items-center gap-2.5 rounded-[14px] px-2.5 py-2 text-left"
>
<span
aria-hidden
className="flex h-8 w-8 shrink-0 items-center justify-center rounded-lg text-white shadow-[inset_0_1px_0_rgb(255_255_255/0.4)]"
style={{ background: 'linear-gradient(135deg, var(--amb-1), var(--amb-3))' }}
>
<PlatformIcon platform={library?.platform ?? ''} size={20} />
</span>
<div className="min-w-0 flex-1">
<div className="truncate text-sm font-medium text-text">
{library?.display_name ?? 'No library'}
</div>
<div className="truncate text-[11px] text-faint">
{library
? `${library.name}${library.version ? ` ${library.version}` : ''}`
: 'Choose one'}
</div>
</div>
<span className="shrink-0 text-faint transition-colors group-hover:text-accent">
<Icon name="switch" size={14} strokeWidth={2} />
</span>
</button>
{/* Everything between the library header and the footer scrolls as one
when the window is too short for it. Each section keeps its own
minimum (the playlists 6 rows, Devices 7) and inner scroll, so on a
tall window nothing changes; on a short one the sections stop
shrinking at those minimums and this takes over, instead of the
sidebar's contents running off the bottom of its panel. */}
<div className="flex min-h-0 flex-1 flex-col overflow-y-auto">
<div className="shrink-0 px-2 pt-3">
<button
onClick={() => onSelect({ kind: 'all' })}
className={`flex w-full items-center gap-2 rounded-[10px] px-2 py-1.5 text-left text-sm transition-colors ${
source.kind === 'all'
? 'is-selected font-medium text-text'
: 'text-muted hover:bg-ink-800 hover:text-text'
}`}
>
<span className={`flex w-4 justify-center ${source.kind === 'all' ? 'text-accent' : ''}`}>
<Icon name="music" size={15} />
</span>
<span className="flex-1">All Tracks</span>
</button>
</div>
<div className="mt-3 shrink-0 px-4">
<span className="text-[11px] font-semibold uppercase tracking-wider text-faint">
Playlists
</span>
</div>
{/* Right-click here (below the rows) creates at the top level; rows
stop the event so they get their own menu. */}
<div
ref={rootDropRef}
data-drag-scroll=""
className="mt-1 min-h-24 flex-1 overflow-y-auto px-2 pb-4"
onContextMenu={(e) => openMenu(e, createItems(null))}
>
{draft?.parentId === null && <DraftRow draft={draft} depth={0} actions={actions} />}
{isLoading && <div className="px-2 py-2 text-sm text-faint">Loading…</div>}
{playlists?.map((n) => (
<NodeRow
key={n.id}
node={n}
parentId={null}
siblings={playlists}
depth={0}
source={source}
onSelect={onSelect}
renamingId={renamingId}
setRenamingId={setRenamingId}
draft={draft}
exportSets={exportSets}
actions={actions}
/>
))}
{rootOver && <div aria-hidden className="mx-2 mt-0.5 h-0.5 rounded-full bg-accent" />}
</div>
{menu && <ContextMenu {...menu} onClose={() => setMenu(null)} />}
{confirmReq && <ConfirmDialog {...confirmReq} onClose={() => setConfirmReq(null)} />}
<ExportsSection
source={source}
onSelect={onSelect}
onDone={onDone}
onError={onError}
onAddForeign={onAddForeign}
/>
<DevicesSection
source={source}
onSelect={onSelect}
onError={onError}
onImport={onImport}
onOpenLibrary={onOpenLibrary}
/>
</div>
<button
onClick={onOpenHistory}
className="flex shrink-0 items-center justify-center gap-2 border-t border-line px-4 py-2 text-center text-xs text-muted hover:bg-ink-800 hover:text-text"
>
<Icon name="history" size={13} />
Collection Version History
</button>
<SaveBar
onError={onError}
onDiscarded={onDiscarded}
trailing={<SettingsMenu up onOpenPathMapping={onOpenPathMapping} />}
/>
</aside>
)
}