Playlist.tsx
⎇
Raw
1import {
2 createSelector,
3 type Accessor,
4 type Setter,
5 createSignal,
6 type ResourceActions,
7 createEffect,
8 onMount,
9 Show,
10 on,
11} from "solid-js";
12import "./Playlist.css";
13import { getItemName, type PlayerState, type PlaylistItem, type AppOptions } from "../types";
14import { formatTime, LocalStorageValues, toast } from "../utils";
15import { ClearSVG, DownloadSVG, DownSVG, DragSVG, LinkSVG, SaveSVG, SyncSVG, UpSVG } from "./SVG";
16import { savePlaylist, download, downloadStatusMap, getStoredFiles } from "../offline";
17import { DraggableList } from "./DraggableList";
18import type { FileListing } from "music-server-shared/types";
19
20interface PlaylistProps {
21 currentDir: Accessor<string>;
22 playlist: Accessor<PlaylistItem[]>;
23 setPlaylist: Setter<PlaylistItem[]>;
24 playerState: PlayerState;
25 setPlayerState: Setter<PlayerState>;
26 playTrack: (item: PlaylistItem) => void;
27 playNext: () => void;
28 audioElement: HTMLAudioElement;
29 options: AppOptions;
30 fetchFiles: ResourceActions<FileListing>;
31}
32
33export default function Playlist(props: PlaylistProps) {
34 const [showActionButtons, setShowActionButtons] = createSignal(true);
35 function removeFromPlaylist(id: string) {
36 props.playNext();
37 const newPlaylist = props.playlist().filter((i) => i.id !== id);
38 props.setPlaylist(newPlaylist);
39 }
40
41 async function playlistDownload(download: boolean) {
42 try {
43 const playlist = props.playlist();
44 const filePaths = playlist.map((item) => item.path);
45 const response = await fetch("/prepare-playlist", { body: JSON.stringify(filePaths), method: "POST" });
46 if (!response.ok) throw new Error(`Generating playlist id not successful: ${await response.text()}`);
47 const id = await response.text();
48 if (download) {
49 toast("Started playlist download", "info");
50 const a = document.createElement("a");
51 a.href = `/download-playlist/${id}`;
52 document.body.appendChild(a);
53 a.click();
54 document.body.removeChild(a);
55 } else {
56 toast("Copied playlist link to clipboard", "info");
57 const urlPath = location.href.split(/\?|#/)[0];
58 navigator.clipboard.writeText(`${urlPath}${urlPath.endsWith("/") ? "" : "/"}download-playlist/${id}`);
59 }
60 } catch (e) {
61 toast("Playlist items download failed", "error", e);
62 }
63 }
64
65 const [syncing, setSyncing] = createSignal(false);
66 async function syncPlaylist() {
67 if (syncing()) return;
68 setSyncing(true);
69 const progressToast = toast("Syncing files in playlist...", "progress");
70 try {
71 const itemsToSync: PlaylistItem[] = [];
72 const playlist = props.playlist();
73 const unique = playlist.filter((item) => playlist.some((other) => item.path !== other.path));
74 const storedFiles = await getStoredFiles();
75 for (const item of unique) {
76 if (!storedFiles.includes(item.path)) itemsToSync.push(item);
77 }
78 const downloadPromises = itemsToSync.map((item) => {
79 const basePath = item.path.split("/").slice(0, -1).join("/");
80 const fileName = item.path.split("/").toReversed()[0];
81 return download(fileName, item, basePath, props.currentDir, props.options, props.fetchFiles);
82 });
83 await Promise.allSettled(downloadPromises);
84 toast("Playlist sync completed", "success");
85 } catch (e) {
86 toast("Playlist sync failed", "error", e);
87 } finally {
88 progressToast.hideToast();
89 setSyncing(false);
90 }
91 }
92
93 let dialogElement!: HTMLDialogElement;
94 let playlistNameInput!: HTMLInputElement;
95 let allowOverwriteCheckbox!: HTMLInputElement;
96
97 async function save() {
98 try {
99 if (!playlistNameInput.reportValidity()) return;
100 await savePlaylist({ name: playlistNameInput.value, items: props.playlist() }, allowOverwriteCheckbox.checked);
101 closeDialog();
102 } catch (e) {
103 if (e instanceof Error && e.name === "ConstraintError") {
104 toast("Playlist with this name already exists", "error");
105 } else {
106 toast("Saving playlist failed", "error", e);
107 }
108 }
109 }
110
111 function closeDialog() {
112 dialogElement.close();
113 playlistNameInput.value = "";
114 allowOverwriteCheckbox.checked = false;
115 }
116
117 const [syncedCount, setSyncedCount] = createSignal(0);
118 createEffect(
119 on(
120 () => props.playlist().map((item) => downloadStatusMap[item.path]),
121 async () => {
122 const playlist = props.playlist();
123 const storedFiles = await getStoredFiles();
124 setSyncedCount(playlist.filter((item) => storedFiles.includes(item.path)).length);
125 },
126 ),
127 );
128
129 onMount(() => {
130 const savedPlaylistButtons = localStorage.getItem(LocalStorageValues.playlistButtons);
131 if (savedPlaylistButtons !== null) setShowActionButtons(savedPlaylistButtons === "true");
132 createEffect(() => localStorage.setItem(LocalStorageValues.playlistButtons, showActionButtons().toString()));
133 });
134
135 return (
136 <div class="playlist-container">
137 <dialog class="save-dialog" ref={dialogElement} onclick={(e) => e.target.nodeName === "DIALOG" && closeDialog()}>
138 <div>
139 <input ref={playlistNameInput} required type="text" placeholder="Playlist name" />
140 <div class="save-buttons">
141 <label for="overwrite-checkbox">allow overwrite</label>
142 <input ref={allowOverwriteCheckbox} id="overwrite-checkbox" type="checkbox" />
143 <button onClick={save}>Save</button>
144 <button onClick={closeDialog}>Cancel</button>
145 </div>
146 </div>
147 </dialog>
148
149 <div class="playlist-header">
150 <div class="playlist-header-title">
151 <span>
152 Playlist ({props.playlist().length} tracks, {syncedCount()} synced)
153 </span>
154 <button title="Toggle playlist actions" onclick={() => setShowActionButtons(!showActionButtons())}>
155 {showActionButtons() ? <UpSVG /> : <DownSVG />}
156 </button>
157 </div>
158 <Show when={showActionButtons()}>
159 <div class="button-container">
160 <button title="Save playlist" onClick={() => dialogElement.showModal()}>
161 <SaveSVG />
162 </button>
163 <button title="Sync all unsynced files in playlist" onClick={syncPlaylist} disabled={syncing()}>
164 <SyncSVG />
165 </button>
166 <button
167 title="Copies and 1-hour valid link for the same action as the download button"
168 onClick={() => playlistDownload(false)}
169 >
170 <LinkSVG />
171 </button>
172 <button
173 title="Download all files in the playlist in their original format as a .tar file, or without archive if it contains just a single item"
174 onClick={() => playlistDownload(true)}
175 >
176 <DownloadSVG />
177 </button>
178 <button title="Clear Playlist" onClick={() => props.setPlaylist([])}>
179 <ClearSVG />
180 </button>
181 </div>
182 </Show>
183 </div>
184
185 <DraggableList<PlaylistItem> items={props.playlist} setItems={props.setPlaylist} id="playlist">
186 {(item, _, { dragHandlers, isDragging, dropPosition }) => {
187 const isActive = createSelector(
188 () => props.playerState.currentTrack?.id,
189 (itemId, currentId) => currentId === itemId,
190 );
191 return (
192 <div
193 classList={{
194 "playlist-item": true,
195 "draggable-item": true,
196 active: isActive(item.id),
197 "drop-target-top-border": dropPosition() === "before",
198 "drop-target-bottom-border": dropPosition() === "after",
199 }}
200 style={{ opacity: isDragging() ? 0.5 : 1 }}
201 onClick={() => props.playTrack(item)}
202 >
203 <div class="playlist-drag-btn" {...dragHandlers}>
204 <DragSVG />
205 </div>
206 <span>{getItemName(item)}</span>
207 <div class="spacer" />
208 {formatTime(item.metadata.duration)}
209 <button
210 title={`Remove ${getItemName(item)}`}
211 onClick={(e) => {
212 e.stopPropagation();
213 removeFromPlaylist(item.id);
214 }}
215 >
216 <ClearSVG />
217 </button>
218 </div>
219 );
220 }}
221 </DraggableList>
222 </div>
223 );
224}
225