Crea src/lib/actions/pressable.ts y clickOutside.ts (Svelte actions; role/tabindex se mantienen estáticos en el markup por accesibilidad SSR, la acción solo añade click + Enter/Espacio) y src/lib/toggle.svelte.ts (createToggle), usado por playlists.svelte.ts y queuePanel.svelte.ts. format.ts: añade plural(n, uno, muchos); migra las 15 pluralizaciones manuales del repo, unificando a minúscula (Δ visible en Playlists, Inicio, liked, Explorar). player.svelte.ts: añade toQueueItem, isQueueCurrent, playAllOrToggle, playShuffled; renombra addToQueue → playNextItem (el nombre anterior confundía: inserta a continuación, no al final). Migra los 5 sitios que repetían el patrón isCurrentQueue/playAll (álbum, playlist, liked, mezcla, Inicio). Simplifica liked.svelte.ts y recentlyPlayed.ts con object spread en vez de reconstruir cada campo a mano. navigation.svelte.ts: añade searchHref (migra el buildHref duplicado de TopBar y Explorar) y appNavLinks (creado; su adopción en Sidebar/ TabsBar queda para P12, como indica esa parte). menus.svelte.ts: createTrackMenu() sustituye el estado manual de menú contextual (contextMenu + contextMenuStateFor + addToQueue) en Inicio, Explorar y Mezcla. Pendiente a propósito: la tarea 5 (eliminar el envoltorio TrackTarget de tracks.ts y sus 8 accesores) se difiere a P13 — es el cambio de mayor riesgo del plan, toca tipos de forma transversal en Inicio/ Explorar/Mezcla/menús, y no se pudo verificar visualmente sin Docker en esta sesión. Verificado con npm run check/build. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
75 lines
1.8 KiB
TypeScript
75 lines
1.8 KiB
TypeScript
import { toQueueItems, type ApiTrackLike } from './player.svelte';
|
|
|
|
export interface LikedTrack {
|
|
id: string;
|
|
title: string;
|
|
artist?: string;
|
|
explicit?: boolean;
|
|
ownerUserId?: string | number | null;
|
|
listensCount?: number | string;
|
|
duration?: number | string;
|
|
likedAt: number;
|
|
}
|
|
|
|
type LikeToggleInput = {
|
|
id: string | number;
|
|
title: string;
|
|
artist?: string;
|
|
explicit?: boolean;
|
|
ownerUserId?: string | number | null;
|
|
duration?: number | string;
|
|
};
|
|
|
|
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; duration: number | string })[]) {
|
|
if (this.hydrated) return;
|
|
this.hydrated = true;
|
|
|
|
const items = toQueueItems(tracks);
|
|
const next: Record<string, LikedTrack> = {};
|
|
items.forEach((item, i) => {
|
|
const id = String(item.id);
|
|
next[id] = {
|
|
...item,
|
|
id,
|
|
duration: tracks[i].duration,
|
|
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] = { ...track, id, 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();
|