Files
Musify/web-player/src/lib/player/liked.svelte.ts
T
Admin 5ae9955bd0 Remove YouTube integration from web-player; unify Home track rows
Drops every YouTube-backed route, proxy and type (external
albums/tracks, thumbnail proxy, youtube.ts) and simplifies
player/track/mix types to the local-only catalog now that the
backend contract dropped Source/ExternalId. Genre tiles on Explore
and "Populares esta semana" on Home stay as deliberate client-only
placeholders per product decision, just without the YouTube backing.

Also finishes the design-system unification pass: Home's three
hand-rolled track rows (Continuar escuchando, Playlist destacada,
Populares esta semana) now reuse the shared TrackTitleCell component
instead of duplicating cover+title+artist markup, and the orphaned
Rail.svelte (no importers) is removed.
2026-09-15 21:17:14 +02:00

82 lines
1.9 KiB
TypeScript

import { toQueueItems, type ApiTrackLike } from './player.svelte';
export interface LikedTrack {
id: string;
title: string;
artist?: string;
explicit?: boolean;
ownerUserId?: string | number | null;
likedAt: number;
}
type LikeToggleInput = {
id: string | number;
title: string;
artist?: string;
explicit?: boolean;
ownerUserId?: string | number | null;
};
class LikedStore {
entries = $state<Record<string, LikedTrack>>({});
private hydrated = false;
count = $derived(Object.keys(this.entries).length);
list = $derived(Object.values(this.entries).sort((a, b) => b.likedAt - a.likedAt));
hydrate(tracks: (ApiTrackLike & { createdAt: string })[]) {
if (this.hydrated) return;
this.hydrated = true;
const items = toQueueItems(tracks);
const next: Record<string, LikedTrack> = {};
items.forEach((item, i) => {
next[String(item.id)] = {
id: String(item.id),
title: item.title,
artist: item.artist,
explicit: item.explicit,
ownerUserId: item.ownerUserId,
likedAt: new Date(tracks[i].createdAt).getTime()
};
});
this.entries = next;
}
isLiked(id: string | number): boolean {
return String(id) in this.entries;
}
async toggle(track: LikeToggleInput) {
const id = String(track.id);
const before = this.entries;
const wasLiked = id in before;
const next = { ...before };
if (wasLiked) delete next[id];
else {
next[id] = {
id,
title: track.title,
artist: track.artist,
explicit: track.explicit,
ownerUserId: track.ownerUserId,
likedAt: Date.now()
};
}
this.entries = next;
try {
const res = await fetch('/api/likes', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ trackId: id })
});
if (!res.ok) throw new Error('Failed to toggle like');
} catch {
this.entries = before;
}
}
}
export const liked = new LikedStore();