1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
use dioxus::prelude::*;
use hooks::db_reactivity::Table;
use hooks::use_db_queries::{use_playlists, use_tracks_by_keys};
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
use rfd::AsyncFileDialog;
use std::collections::HashSet;
use std::path::PathBuf;
use tracing::Instrument;
/// Wall-clock seconds since the epoch — the playlist-pull staleness stamp.
fn unix_secs() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0)
}
/// Wall-clock millis since the epoch — one reconcile's sweep epoch token.
fn unix_millis() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis() as i64)
.unwrap_or(0)
}
#[component]
#[tracing::instrument(name = "render.playlist_detail", skip_all)]
pub fn PlaylistDetail(
playlist_id: String,
config: Signal<config::AppConfig>,
on_close: EventHandler<()>,
on_download_all: Option<EventHandler<()>>,
on_delete_all: Option<EventHandler<()>>,
on_download_track: Option<EventHandler<usize>>,
#[props(default = false)] is_downloading_all: bool,
) -> Element {
let mut tracks = use_signal(Vec::<reader::models::Track>::new);
let mut has_loaded_remote = use_signal(|| false);
let gens = hooks::db_reactivity::use_generations();
let active_source = use_context::<Signal<::server::source::ActiveSource>>();
let playlists_res = use_playlists();
let cover_for = hooks::use_db_queries::use_cover_resolver(512);
// Seed = the stored playlist's track refs, resolved from the ACTIVE source's
// partition (the store only holds the active source's playlists).
let pid_for_seed = playlist_id.clone();
let seed_refs = use_memo(move || {
let store = playlists_res.read().clone().unwrap_or_default();
store
.playlists
.iter()
.find(|p| p.id == pid_for_seed)
.map(|p| p.tracks.clone())
.unwrap_or_default()
});
let active_partition = use_memo(move || config.read().active_source.clone());
let seed_tracks_res = use_tracks_by_keys(active_partition, seed_refs);
// Affordances are capability-driven, not source-kind-driven: tag-edit and
// delete-from-disk are local-only, downloads server-only, reorder per the
// playlists cap (YT's InnerTube has no reorder mutation). Reading the caps is
// also more correct than `is_server` — e.g. a creds-less offline server has
// downloads=false.
let caps = active_source.read().capabilities();
let can_reorder = caps.playlists == ::server::source::PlaylistOps::Reorder;
// Initial tracks with no network round-trip: resolve the playlist's refs from
// the active source's cached/local rows. A server's live entries (below)
// replace this once they arrive; local has no remote entries, so this stands.
use_effect(move || {
if !*has_loaded_remote.read() {
tracks.set(seed_tracks_res.read().clone().unwrap_or_default());
}
});
let pid = playlist_id.clone();
// Only sources with remote entries reconcile; a local playlist's tracks live
// entirely in the seed, and the end-of-walk sweep would wipe them.
let remote_entries = caps.sync;
use_effect(move || {
if *has_loaded_remote.read() {
return;
}
if !remote_entries {
return;
}
let pid_clone = pid.clone();
let load_span = tracing::info_span!("playlist.reconcile", playlist_id = %pid_clone);
let source = active_source.peek().clone();
let read_db = consume_context::<hooks::ReadDb>();
spawn(
async move {
// Staleness gate (mirrors the favorites pull): the cached seed shows
// instantly; only re-walk the remote when the last reconcile is
// older than 15 min. First visit (last_pull == 0) always pulls.
let now = unix_secs();
let last_pull: u64 = read_db
.meta_get("pl_pull", &pid_clone)
.await
.ok()
.flatten()
.and_then(|s| s.parse().ok())
.unwrap_or(0);
if last_pull <= now && now - last_pull < 15 * 60 {
return;
}
// Stream the entries page by page under one epoch. Each page upserts
// its tracks + positions into the cache (and grows the visible list);
// the end sweep drops entries the remote no longer has. The visible
// list only grows mid-walk, so re-reconciling an already-cached
// playlist never blinks shorter — removals/reorders land at the end.
let epoch = unix_millis();
let mut cursor: Option<String> = None;
let mut seen: HashSet<String> = HashSet::new();
let mut acc: Vec<reader::models::Track> = Vec::new();
let mut position: i64 = 0;
let mut completed = true;
loop {
let page = match source
.fetch_playlist_entries_page(&pid_clone, cursor.clone())
.await
{
Ok(p) => p,
Err(e) => {
tracing::warn!(error = %e, "playlist page fetch failed");
completed = false;
break;
}
};
let next = page.next.clone();
// YT repeats tracks at page boundaries — dedup across the walk.
let fresh: Vec<reader::models::Track> = page
.tracks
.into_iter()
.filter(|t| {
let k = t.id.key().to_string();
!k.is_empty() && seen.insert(k)
})
.collect();
if fresh.is_empty() {
match next {
Some(n) => {
cursor = Some(n);
continue;
}
None => break,
}
}
let page_refs: Vec<String> =
fresh.iter().map(|t| t.id.key().to_string()).collect();
let start = position;
position += fresh.len() as i64;
for chunk in fresh.chunks(100) {
let _ = source.upsert_tracks(chunk).await;
}
let _ = source
.upsert_playlist_tracks_page(&pid_clone, &page_refs, start, epoch)
.await;
acc.extend(fresh);
// Grow-only: never shrink the visible list mid-walk.
if acc.len() > tracks.peek().len() {
tracks.set(acc.clone());
}
has_loaded_remote.set(true);
gens.bump_coalesced(Table::Tracks);
match next {
Some(n) => cursor = Some(n),
None => break,
}
}
if completed {
tracing::debug!(count = acc.len(), "playlist reconciled");
tracks.set(acc);
let _ = source.sweep_playlist_tracks(&pid_clone, epoch).await;
let _ = source
.set_meta("pl_pull", &pid_clone, &unix_secs().to_string())
.await;
gens.bump(Table::Playlists);
gens.bump(Table::Tracks);
}
}
.instrument(load_span),
);
});
let store_loading = playlists_res.read().is_none();
let store = playlists_res.read().clone().unwrap_or_default();
let (playlist_name, playlist_custom_cover, playlist_image_tag) =
if let Some(p) = store.playlists.iter().find(|p| p.id == playlist_id) {
(p.name.clone(), p.cover_path.clone(), p.image_tag.clone())
} else if store_loading {
return rsx! { div {} };
} else {
return rsx! { div { "{i18n::t(\"playlist_not_found\")}" } };
};
let tracks_val = tracks.read().clone();
// A custom (locally-picked) cover wins; then a server playlist's remote image
// tag; then the first track's cover via the source-agnostic seam.
let playlist_cover = playlist_custom_cover
.as_ref()
.and_then(|p| utils::format_artwork_url(Some(p)))
.or_else(|| {
let tag = playlist_image_tag.as_ref()?;
let conf = config.read();
let server = conf.server.as_ref()?;
Some(std::sync::Arc::from(
utils::jellyfin_image::jellyfin_image_url(
&server.url,
&playlist_id,
Some(tag.as_str()),
server.access_token.as_deref(),
512,
90,
)
.as_str(),
))
})
.or_else(|| tracks_val.first().and_then(&cover_for));
let pid_for_remove = playlist_id.clone();
let pid_for_move_up = playlist_id.clone();
let pid_for_move_down = playlist_id.clone();
let pid_for_cover = playlist_id.clone();
let name_for_cover = playlist_name.clone();
let tag_for_cover = playlist_image_tag.clone();
rsx! {
crate::track_list_view::TrackListView {
name: playlist_name.clone(),
description: String::new(),
cover_url: playlist_cover,
back_label: i18n::t("back_to_playlists").to_string(),
tracks: tracks_val,
on_close,
enable_metadata: caps.edit_tags,
on_cover_click: move |_| {
let _ = &pid_for_cover;
let _ = &name_for_cover;
let _ = &tag_for_cover;
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
{
let pid = pid_for_cover.clone();
let pl_name = name_for_cover.clone();
let pl_tag = tag_for_cover.clone();
let source = active_source.peek().clone();
spawn(async move {
let file = AsyncFileDialog::new()
.add_filter("Images", &["jpg", "jpeg", "png", "webp"])
.pick_file()
.await;
if let Some(file) = file {
let path = file.path().to_path_buf();
// The source decides what "set a cover" means — Jellyfin
// pushes the image upstream, everyone else just records
// the local path.
if source
.set_playlist_cover(&pid, &pl_name, &path, pl_tag.as_deref())
.await
.is_ok()
{
gens.bump(Table::Playlists);
}
}
});
}
},
on_delete_track: move |idx: usize| {
if caps.delete_from_disk
&& let Some(t) = tracks.read().get(idx).cloned() {
#[cfg(not(target_arch = "wasm32"))]
if let Some(del_path) = t.id.local_path()
&& std::fs::remove_file(del_path).is_ok()
{
let source = consume_context::<Signal<::server::source::ActiveSource>>().peek().clone();
let key = t.id.key().into_owned();
spawn(async move {
if source.delete_tracks(&[key]).await.is_ok() {
gens.bump(Table::Tracks);
}
});
}
}
},
on_selection_delete: move |paths: Vec<PathBuf>| {
if caps.delete_from_disk {
#[cfg(not(target_arch = "wasm32"))]
{
let mut keys = Vec::new();
for path in &paths {
if std::fs::remove_file(path).is_ok() {
keys.push(path.to_string_lossy().into_owned());
}
}
if !keys.is_empty() {
let source = consume_context::<Signal<::server::source::ActiveSource>>().peek().clone();
spawn(async move {
if source.delete_tracks(&keys).await.is_ok() {
gens.bump(Table::Tracks);
}
});
}
}
}
},
on_remove_from_playlist: move |idx: usize| {
if let Some(t) = tracks.read().get(idx).cloned() {
let pid = pid_for_remove.clone();
let source = active_source.peek().clone();
spawn(async move {
if source.remove_from_playlist(&pid, &t, idx).await.is_ok() {
let mut tw = tracks.write();
if idx < tw.len() {
tw.remove(idx);
}
gens.bump(Table::Playlists);
}
});
}
},
is_reorderable: can_reorder,
on_move_up: move |idx: usize| {
if idx == 0 || !can_reorder {
return;
}
tracks.write().swap(idx - 1, idx);
let mut refs = {
let store = playlists_res.read();
let Some(pl) = store
.as_ref()
.and_then(|s| s.playlists.iter().find(|p| p.id == pid_for_move_up))
else {
return;
};
if idx >= pl.tracks.len() {
return;
}
pl.tracks.clone()
};
refs.swap(idx - 1, idx);
let Some(moved) = tracks.read().get(idx - 1).cloned() else {
return;
};
let pid = pid_for_move_up.clone();
let source = active_source.peek().clone();
spawn(async move {
if source
.reorder_playlist(&pid, &refs, &moved, idx - 1)
.await
.is_ok()
{
gens.bump(Table::Playlists);
}
});
},
on_move_down: move |idx: usize| {
let len = tracks.read().len();
if idx + 1 >= len || !can_reorder {
return;
}
tracks.write().swap(idx, idx + 1);
let mut refs = {
let store = playlists_res.read();
let Some(pl) = store
.as_ref()
.and_then(|s| s.playlists.iter().find(|p| p.id == pid_for_move_down))
else {
return;
};
if idx + 1 >= pl.tracks.len() {
return;
}
pl.tracks.clone()
};
refs.swap(idx, idx + 1);
let Some(moved) = tracks.read().get(idx + 1).cloned() else {
return;
};
let pid = pid_for_move_down.clone();
let source = active_source.peek().clone();
spawn(async move {
if source
.reorder_playlist(&pid, &refs, &moved, idx + 1)
.await
.is_ok()
{
gens.bump(Table::Playlists);
}
});
},
on_download_all: if caps.downloads { on_download_all } else { None },
on_download_track: if caps.downloads { on_download_track } else { None },
on_delete_all: if caps.downloads { on_delete_all } else { None },
is_downloading_all,
show_delete_in_selection: caps.delete_from_disk,
}
}
}