feat(web/playlists): Discover refresh button (detail + home kebab)

Operator can refresh their Discover playlist without waiting for
the next daily cron tick. Two surfaces, both gated on
system_variant === 'discover':

- Detail page header: "Refresh" button next to the cover art / meta.
- Home Playlists row tile kebab: "Refresh Discover" menu item.

Both call POST /api/playlists/system/discover/refresh, show a
confirmation toast, and invalidate the playlist + playlists queries
so the new content lands without a manual page reload.

Extends Playlist.system_variant union with 'discover'. Adds
refreshDiscover() typed client. Tests cover client behaviour
(success + empty-library) plus conditional rendering on both
surfaces.
This commit is contained in:
2026-05-07 08:44:33 -04:00
parent 0ba31c7816
commit b20738f925
7 changed files with 309 additions and 8 deletions
+82 -2
View File
@@ -1,15 +1,37 @@
<script lang="ts">
import { Play } from 'lucide-svelte';
import { Play, MoreVertical, RefreshCcw } from 'lucide-svelte';
import { useQueryClient } from '@tanstack/svelte-query';
import type { Playlist, PlaylistTrack, TrackRef } from '$lib/api/types';
import { user } from '$lib/auth/store.svelte';
import { getPlaylist } from '$lib/api/playlists';
import { getPlaylist, refreshDiscover } from '$lib/api/playlists';
import { qk } from '$lib/api/queries';
import { playQueue } from '$lib/player/store.svelte';
let { playlist }: { playlist: Playlist } = $props();
const isOwn = $derived(user.value?.id === playlist.user_id);
const isDiscover = $derived(playlist.system_variant === 'discover');
const queryClient = useQueryClient();
let starting = $state(false);
let menuOpen = $state(false);
// --- Toast ---
let toast = $state<string | null>(null);
let toastTimer: ReturnType<typeof setTimeout> | null = null;
function showToast(msg: string) {
if (toastTimer) clearTimeout(toastTimer);
toast = msg;
toastTimer = setTimeout(() => { toast = null; }, 5000);
}
function toggleMenu(e: MouseEvent) {
e.preventDefault();
e.stopPropagation();
menuOpen = !menuOpen;
}
// Map PlaylistTrack rows to TrackRef shape and drop tracks whose upstream
// file has been removed (track_id is null). Mirrors PlaylistTrackRow's
@@ -47,9 +69,67 @@
starting = false;
}
}
async function onRefreshDiscover(e: MouseEvent) {
e.preventDefault();
e.stopPropagation();
menuOpen = false;
try {
await refreshDiscover();
showToast('Discover refreshed.');
await queryClient.invalidateQueries({ queryKey: qk.playlist(playlist.id) });
await queryClient.invalidateQueries({ queryKey: qk.playlists() });
} catch (err: unknown) {
showToast(`Refresh failed: ${(err as { code?: string })?.code ?? 'unknown'}`);
}
}
</script>
<svelte:window onclick={() => { menuOpen = false; }} />
{#if toast}
<div
role="status"
aria-live="polite"
class="fixed bottom-4 left-1/2 z-50 -translate-x-1/2 rounded-md border border-border bg-surface px-4 py-2 text-sm text-text-primary shadow-md"
>
{toast}
</div>
{/if}
<div class="card relative">
{#if isDiscover}
<div class="kebab-wrap absolute right-1 top-1 z-10">
<button
type="button"
aria-label="Playlist actions"
aria-haspopup="menu"
aria-expanded={menuOpen}
onclick={toggleMenu}
class="rounded p-1 text-text-muted hover:bg-surface-hover hover:text-text-primary"
>
<MoreVertical size={16} strokeWidth={1} />
</button>
{#if menuOpen}
<div
role="menu"
tabindex="-1"
class="absolute right-0 top-full z-20 mt-1 w-44 rounded-md border border-border bg-surface p-1 shadow-lg"
onclick={(e) => e.stopPropagation()}
>
<button
type="button"
role="menuitem"
onclick={onRefreshDiscover}
class="flex w-full items-center gap-2 rounded px-2 py-1.5 text-sm text-text-primary hover:bg-surface-hover"
>
<RefreshCcw size={14} strokeWidth={1} />
Refresh Discover
</button>
</div>
{/if}
</div>
{/if}
<a
href={`/playlists/${playlist.id}`}
class="group flex w-full flex-col items-start gap-2 rounded-md p-2 text-left hover:bg-surface-hover"