From 7ba71981c81f90c719b2b8fc4d8647fdeede2159 Mon Sep 17 00:00:00 2001 From: Benjamin Hardy Date: Tue, 6 Oct 2026 22:51:34 +0200 Subject: [PATCH] Implemented library rearrangement --- frontend/src/components/Sidebar.tsx | 132 +++++++++++++++++++++++++--- frontend/src/lib/drag.tsx | 20 +++-- frontend/src/lib/playlistTree.ts | 12 +++ 3 files changed, 149 insertions(+), 15 deletions(-) diff --git a/frontend/src/components/Sidebar.tsx b/frontend/src/components/Sidebar.tsx index 23f91a7..9673562 100644 --- a/frontend/src/components/Sidebar.tsx +++ b/frontend/src/components/Sidebar.tsx @@ -1,4 +1,4 @@ -import { useRef, useState } from 'react' +import { useEffect, useRef, useState } from 'react' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { api, type PlaylistKind, type PlaylistNode } from '../api' import { useCaps } from '../lib/capabilities' @@ -12,8 +12,8 @@ import { ContextMenu, type MenuItem } from './ContextMenu' import { Icon, type IconName } from '../lib/icons' import { PlatformIcon } from '../lib/platformIcons' import { addTracksToPlaylist } from '../lib/addTracks' -import { usePressToDrag, useDropTarget, type DragPayload } from '../lib/drag' -import { playlistIdsUnder } from '../lib/playlistTree' +import { useDragPayload, usePressToDrag, useDropTarget, type DragPayload } from '../lib/drag' +import { nodeIdsUnder, playlistIdsUnder } from '../lib/playlistTree' /** * Which view the main table is showing. @@ -108,8 +108,22 @@ interface RowActions { /** Tracks dropped on a playlist; `alt` (Option) moves them out of the * playlist they came from. */ onDropTracks: (node: PlaylistNode, p: Extract, 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({ @@ -160,6 +174,7 @@ function DraftRow({ function NodeRow({ node, parentId, + siblings, depth, source, onSelect, @@ -172,6 +187,8 @@ function NodeRow({ 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 @@ -203,26 +220,90 @@ function NodeRow({ 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 - const [dropRef, over] = useDropTarget({ - hint: (p) => (p.kind === 'tracks' && actions.acceptsTracks(node, p) ? true : null), - label: (p, _h, at) => `${moves(p, at.alt) ? 'Move' : 'Add'} to ${node.name}`, - onDrop: (p, _h, at) => p.kind === 'tracks' && actions.onDropTracks(node, p, at.alt), + + // 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, 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({ + 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') 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 ( -
+
actions.onContextMenu(e, node)} - className={`group flex items-center gap-1 rounded-[10px] pr-1 text-sm transition-colors ${ - over + 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 && ( + + )}