From 7b2d6de4119f504c34e337250eeba7d311586845 Mon Sep 17 00:00:00 2001 From: Benjamin Hardy Date: Thu, 15 Oct 2026 10:48:14 +0200 Subject: [PATCH] Implemented new set playlist position mode --- CLAUDE.md | 9 ++- frontend/src/App.tsx | 22 ++++--- frontend/src/components/SetPositionDialog.tsx | 64 +++++++++++++++++-- frontend/src/components/TrackTable.tsx | 5 +- frontend/src/lib/playlistOrder.test.ts | 19 ++++++ frontend/src/lib/playlistOrder.ts | 36 +++++++++-- 6 files changed, 129 insertions(+), 26 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index a64fe0f..9af126d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -996,9 +996,12 @@ Two independent apps that talk over HTTP: column chooser uses those; "Add to" lists playlists + exports and acts on the whole selection; in an editable playlist "Set Position…" (`SetPositionDialog`, `lib/playlistOrder.ts`) - moves the selection as ONE block in playlist order so its first track - lands at #N — allowed while sorted/filtered, unlike drag, because it - reorders the FULL entry list; "Remove ▸" holds + moves the selection as ONE block so its first track lands at #N — + allowed while sorted/filtered, unlike drag, because it reorders the FULL + entry list. While the table is sorted the dialog asks the block's Order: + "As sorted" (default; the menu's ids arrive in VIEW order, so the sort is + written into the playlist and survives clearing it) or playlist order; + "Remove ▸" holds From this playlist / From this export / Grids / Hotcues / From collection, each behind a `ConfirmDialog` — as is the playlist Delete key. Enter confirms unless a button has focus, so Tab-to-Cancel + Enter cancels) + `EditTagsDialog` diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 616d368..a7d1605 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -898,11 +898,16 @@ export default function App() { // TrackTable retargets the selection to a right-clicked row outside it // before calling this, but that state update has not landed yet — so the - // same rule is applied here to decide what the menu acts on. - const openMenu = (track: Track, x: number, y: number, selectable: boolean) => + // same rule is applied here to decide what the menu acts on. The ids come + // in the order the view shows them (Set Position can keep that order); a + // selected track the view hides follows. + const openMenu = (track: Track, x: number, y: number, selectable: boolean, viewIds: string[]) => setMenu({ track, - ids: selectable && selected.has(track.id) ? [...selected] : [track.id], + ids: + selectable && selected.has(track.id) + ? [...new Set([...viewIds.filter((id) => selected.has(id)), ...selected])] + : [track.id], x, y, }) @@ -1321,6 +1326,7 @@ export default function App() { t.id)} ids={positioning} + sorting={sorting} onMove={reorderPlaylist} onClose={() => setPositioning(null)} /> @@ -1603,7 +1609,7 @@ export default function App() { sorting={sorting} onSortingChange={setSorting} selection={{ selected, onChange: setSelected }} - onRowContextMenu={(track, x, y) => openMenu(track, x, y, true)} + onRowContextMenu={(track, x, y, viewIds) => openMenu(track, x, y, true, viewIds)} onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })} onPlay={playTrack} onEditField={canEdit ? editField : undefined} @@ -1644,7 +1650,7 @@ export default function App() { selection={{ selected, onChange: setSelected }} marked={{ ids: heldInFolder, title: 'Already in your collection' }} dragFrom={{ origin: 'folder' }} - onRowContextMenu={(track, x, y) => openMenu(track, x, y, true)} + onRowContextMenu={(track, x, y, viewIds) => openMenu(track, x, y, true, viewIds)} onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })} onPlay={playTrack} onEditField={canEdit ? folderEditField : undefined} @@ -1680,7 +1686,7 @@ export default function App() { tracks={filtered} sorting={sorting} onSortingChange={setSorting} - onRowContextMenu={(track, x, y) => openMenu(track, x, y, false)} + onRowContextMenu={(track, x, y, viewIds) => openMenu(track, x, y, false, viewIds)} dragFrom={{ origin: 'device' }} onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })} onPlay={playTrack} @@ -1705,7 +1711,7 @@ export default function App() { sorting={sorting} onSortingChange={setSorting} selection={{ selected, onChange: setSelected }} - onRowContextMenu={(track, x, y) => openMenu(track, x, y, true)} + onRowContextMenu={(track, x, y, viewIds) => openMenu(track, x, y, true, viewIds)} onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })} onPlay={playTrack} onEditField={canEdit ? editField : undefined} @@ -1747,7 +1753,7 @@ export default function App() { origin: 'collection', playlistId: playlistEditable && source.kind === 'playlist' ? source.id : undefined, }} - onRowContextMenu={(track, x, y) => openMenu(track, x, y, true)} + onRowContextMenu={(track, x, y, viewIds) => openMenu(track, x, y, true, viewIds)} onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })} onPlay={playTrack} onEditField={canEdit ? editField : undefined} diff --git a/frontend/src/components/SetPositionDialog.tsx b/frontend/src/components/SetPositionDialog.tsx index 4269702..2fef983 100644 --- a/frontend/src/components/SetPositionDialog.tsx +++ b/frontend/src/components/SetPositionDialog.tsx @@ -1,24 +1,41 @@ import { createPortal } from 'react-dom' -import { useEffect, useRef, useState } from 'react' +import { useEffect, useRef, useState, type ReactNode } from 'react' +import type { SortingState } from '@tanstack/react-table' import { maxPosition, moveToPosition } from '../lib/playlistOrder' +import { COLUMN_MENU } from '../lib/trackColumns' interface Props { /** The playlist's full entry order — never the sorted or filtered view, so * the move is safe whatever the table is showing. */ order: string[] - /** The tracks to move; they keep their playlist order as one block. */ + /** The tracks to move, in the order the table shows them. */ ids: string[] + /** The table's sort. While there is one, the block may keep that order + * instead of its playlist order — which writes the sort into the playlist. */ + sorting: SortingState onMove: (order: string[]) => void onClose: () => void } -/** "Set Position…": type where the first selected track should end up. */ -export function SetPositionDialog({ order, ids, onMove, onClose }: Props) { +/** "Key" / "Key descending, then BPM" — the sort as the Order choice names it. */ +function describeSort(sorting: SortingState): string { + const label = (id: string) => COLUMN_MENU.find((c) => c.id === id)?.label ?? id + return sorting.map((s) => `${label(s.id)}${s.desc ? ' descending' : ''}`).join(', then ') +} + +/** "Set Position…": type where the first selected track should end up, and — + * while the table is sorted — whether the block keeps its playlist order or + * takes the sorted one. */ +export function SetPositionDialog({ order, ids, sorting, onMove, onClose }: Props) { const moving = new Set(ids) const count = order.filter((id) => moving.has(id)).length const max = maxPosition(order, moving) const current = order.findIndex((id) => moving.has(id)) + 1 const [value, setValue] = useState(String(current)) + // With one track there is no order to choose; unsorted, both are the same. + const canSort = sorting.length > 0 && count > 1 + const [mode, setMode] = useState<'playlist' | 'sorted'>('sorted') + const sorted = canSort && mode === 'sorted' const input = useRef(null) useEffect(() => input.current?.select(), []) @@ -28,7 +45,7 @@ export function SetPositionDialog({ order, ids, onMove, onClose }: Props) { const submit = () => { if (position === null) return - const next = moveToPosition(order, moving, position) + const next = moveToPosition(order, moving, position, sorted ? ids : undefined) if (next.some((id, i) => id !== order[i])) onMove(next) onClose() } @@ -41,6 +58,26 @@ export function SetPositionDialog({ order, ids, onMove, onClose }: Props) { return () => window.removeEventListener('keydown', onKey) }, [onClose]) + const option = (value: 'playlist' | 'sorted', label: string, detail: ReactNode) => ( + + ) + const span = (p: number) => (count > 1 ? `#${p}–${p + count - 1}` : `#${p}`) return createPortal( @@ -79,9 +116,24 @@ export function SetPositionDialog({ order, ids, onMove, onClose }: Props) { {position === null ? 'Type a position.' : count > 1 - ? `Will occupy ${span(position)}` + ? `Will occupy ${span(position)}${canSort ? '' : ', in the order they have in the playlist.'}` : `Now ${span(current)} · will be ${span(position)}`} + {canSort && ( +
+
Order
+ {option( + 'sorted', + `As sorted: ${describeSort(sorting)}`, + 'In the order shown here. The playlist keeps this order when you clear the sort.', + )} + {option( + 'playlist', + 'Playlist order', + 'In the order they have in the playlist now, whatever the sort shows.', + )} +
+ )}