refactor(web): share track target helpers between home and explore

Both pages reimplemented the same accessors over the target union and a
byte-identical queue-item mapper. Moves the domain type (renamed
ContextMenuTarget -> TrackTarget, since tiles and the queue use it too)
and its helpers to $lib/tracks.ts; TrackContextMenu keeps only its menu
geometry. Home drops from 341 to ~230 lines.

Unifies two divergences the duplication was hiding: home compared the
active tile against track.id while the queue is keyed by
queueIdForTrack, so YouTube-sourced tracks never highlighted; and it
hardcoded explicit to false for local tracks, hiding the badge.

Also cancels in-flight youtube filler results when the effect re-runs,
so a slow response cannot overwrite newer ones.
This commit is contained in:
2026-07-20 07:28:12 +02:00
parent c8bc26a5e0
commit db71c4a331
4 changed files with 167 additions and 199 deletions
@@ -1,20 +1,12 @@
<script lang="ts" module>
import type { ApiTrackLike } from '$lib/player/player.svelte';
import type { YouTubeSong } from '$lib/types';
import type { TrackTarget } from '$lib/tracks';
export type ContextMenuTarget =
| { kind: 'youtube'; song: YouTubeSong }
| { kind: 'local'; track: ApiTrackLike };
export type ContextMenuState = ContextMenuTarget & { x: number; y: number; openLeft: boolean };
export type ContextMenuState = TrackTarget & { x: number; y: number; openLeft: boolean };
const MENU_WIDTH = 208;
const SUBMENU_WIDTH = 224;
export function contextMenuStateFor(
event: MouseEvent,
target: ContextMenuTarget
): ContextMenuState {
export function contextMenuStateFor(event: MouseEvent, target: TrackTarget): ContextMenuState {
event.preventDefault();
return {
...target,
+79
View File
@@ -0,0 +1,79 @@
import {
player,
queueIdForTrack,
toQueueItems,
type ApiTrackLike,
type QueueItem
} from '$lib/player/player.svelte';
import type { YouTubeSong } from '$lib/types';
export type TrackTarget =
| { kind: 'youtube'; song: YouTubeSong }
| { kind: 'local'; track: ApiTrackLike };
export function targetId(target: TrackTarget): string | number {
return target.kind === 'youtube' ? target.song.videoId : target.track.id;
}
export function targetTitle(target: TrackTarget): string {
return target.kind === 'youtube' ? target.song.title : target.track.title;
}
export function targetArtist(target: TrackTarget): string | undefined {
return target.kind === 'youtube' ? target.song.artist : target.track.artist;
}
export function targetExplicit(target: TrackTarget): boolean {
return target.kind === 'youtube' ? target.song.isExplicit : (target.track.isExplicit ?? false);
}
export function targetCoverSrc(target: TrackTarget): string | undefined {
return target.kind === 'youtube' ? target.song.thumbnailUrl : undefined;
}
export function queueIdForTarget(target: TrackTarget): string | number {
return target.kind === 'youtube' ? target.song.videoId : queueIdForTrack(target.track);
}
export function isTargetCurrent(target: TrackTarget): boolean {
return player.current.id === queueIdForTarget(target);
}
export function queueItemForTarget(target: TrackTarget): QueueItem {
return target.kind === 'youtube'
? {
id: target.song.videoId,
title: target.song.title,
artist: target.song.artist,
source: 'youtube',
coverUrl: target.song.thumbnailUrl,
explicit: target.song.isExplicit
}
: toQueueItems([target.track])[0];
}
export function targetForQueueItem(item: QueueItem): TrackTarget {
return item.source === 'youtube'
? {
kind: 'youtube',
song: {
videoId: String(item.id),
title: item.title,
artist: item.artist ?? '',
album: '',
durationSeconds: 0,
thumbnailUrl: item.coverUrl ?? '',
isExplicit: item.explicit ?? false
}
}
: {
kind: 'local',
track: {
id: item.id,
title: item.title,
artist: item.artist,
source: 'Local',
isExplicit: item.explicit
}
};
}