Add discover UI with new song suggestions
Each mix rebuild now also finds songs that aren't in the library: the user's Listenbrainz recommendations they've never played, plus an LB Radio prompt per mix when a Listenbrainz token is configured. Each candidate goes to the mix whose genre profile it fits best and is skipped if it's already in the library or was played recently. discover/ is a new React app to listen to the mixes: library songs stream from Subsonic and suggested songs are found on YouTube with yt-dlp, both proxied through playlistd with Range support for seeking. Suggestions can be liked (kept in a "Liked new songs" list) or hidden. - discover: candidate gathering, genre fit scoring, library filtering - web: /api/discover routes, Subsonic/YouTube streaming proxy - youtube: yt-dlp search (skipping hour-long results) and audio URLs - mix tags now hold library genre names, for display in the UI - config: listenbrainz.token, discover.ui, discover.ytdlp, discover.format Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018opPJMJjLWr4RqdG6vuK6p
This commit is contained in:
32 files changed
+4628
-36
No files matched your search
@@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
dist
|
||||
@@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="color-scheme" content="light dark" />
|
||||
<title>Discover</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
Generated
+1757
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "playlistd-discover",
|
||||
"private": true,
|
||||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "tsc --noEmit && vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "18.3.1",
|
||||
"react-dom": "18.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.9.0",
|
||||
"@types/react": "18.3.12",
|
||||
"@types/react-dom": "18.3.1",
|
||||
"@vitejs/plugin-react": "4.3.3",
|
||||
"typescript": "5.6.3",
|
||||
"vite": "5.4.10"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { api, MixDetail, MixSummary, mixTitle, Rule, Suggestion, SuggestionStatus } from "./api";
|
||||
import { usePlayer } from "./usePlayer";
|
||||
import { MixView, hue } from "./MixView";
|
||||
import { LikedView } from "./LikedView";
|
||||
import { PlayerBar } from "./PlayerBar";
|
||||
import * as Icon from "./icons";
|
||||
|
||||
type Route = { view: "mix"; id?: string } | { view: "liked" };
|
||||
|
||||
function parseHash(): Route {
|
||||
const [, view, id] = window.location.hash.split("/");
|
||||
return view === "liked" ? { view: "liked" } : { view: "mix", id: id || undefined };
|
||||
}
|
||||
|
||||
type Toast = { message: string; undo?: () => void };
|
||||
|
||||
function storedRule() {
|
||||
try {
|
||||
return localStorage.getItem("discover.rule");
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function App() {
|
||||
const [rules, setRules] = useState<Rule[] | null>(null);
|
||||
const [ruleId, setRuleId] = useState<string | null>(storedRule);
|
||||
const [mixes, setMixes] = useState<MixSummary[] | null>(null);
|
||||
const [detail, setDetail] = useState<MixDetail | null>(null);
|
||||
const [liked, setLiked] = useState<Suggestion[]>([]);
|
||||
const [route, setRoute] = useState<Route>(parseHash);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [toast, setToast] = useState<Toast | null>(null);
|
||||
|
||||
const player = usePlayer((item) => setToast({ message: `Couldn't play “${item.title}”` }));
|
||||
const rule = rules?.find((r) => r.id === ruleId) ?? rules?.[0];
|
||||
|
||||
useEffect(() => {
|
||||
const onHash = () => setRoute(parseHash());
|
||||
window.addEventListener("hashchange", onHash);
|
||||
return () => window.removeEventListener("hashchange", onHash);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!toast) return;
|
||||
const t = window.setTimeout(() => setToast(null), toast.undo ? 6000 : 3500);
|
||||
return () => window.clearTimeout(t);
|
||||
}, [toast]);
|
||||
|
||||
const loadRules = useCallback(() => {
|
||||
api.rules().then(setRules, (e) => setError(`Couldn't reach playlistd (${e.message})`));
|
||||
}, []);
|
||||
useEffect(loadRules, [loadRules]);
|
||||
|
||||
const loadMixes = useCallback(() => {
|
||||
if (!rule) return;
|
||||
api.mixes(rule.id).then(setMixes, (e) => setError(e.message));
|
||||
api.liked(rule.id).then(setLiked, () => undefined);
|
||||
}, [rule?.id]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
useEffect(loadMixes, [loadMixes]);
|
||||
|
||||
// mixes are built by the daemon; check back while they're pending
|
||||
const pending = !!rule && (!rule.last_run || mixes?.length === 0);
|
||||
useEffect(() => {
|
||||
if (!pending) return;
|
||||
const t = window.setInterval(() => {
|
||||
loadRules();
|
||||
loadMixes();
|
||||
}, 10000);
|
||||
return () => window.clearInterval(t);
|
||||
}, [pending, loadRules, loadMixes]);
|
||||
|
||||
const mixId = route.view === "mix" ? route.id ?? mixes?.[0]?.id : undefined;
|
||||
const loadDetail = useCallback(
|
||||
(quiet = false) => {
|
||||
if (!mixId) return;
|
||||
if (!quiet) setDetail((d) => (d?.id === mixId ? d : null));
|
||||
api.mix(mixId).then(setDetail, (e) => !quiet && setError(`Couldn't load mix (${e.message})`));
|
||||
},
|
||||
[mixId],
|
||||
);
|
||||
useEffect(() => loadDetail(), [loadDetail]);
|
||||
|
||||
// pick up YouTube links once a new song has been found and starts playing
|
||||
useEffect(() => {
|
||||
if (player.status === "playing" && player.current?.source === "youtube") {
|
||||
loadDetail(true);
|
||||
if (rule) api.liked(rule.id).then(setLiked, () => undefined);
|
||||
}
|
||||
}, [player.status, player.current?.key]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
const updateLocal = (id: string, status: SuggestionStatus, previous?: Suggestion) => {
|
||||
setDetail((d) => {
|
||||
if (!d) return d;
|
||||
const suggestions =
|
||||
status === "dismissed"
|
||||
? d.suggestions.filter((s) => s.id !== id)
|
||||
: d.suggestions.some((s) => s.id === id)
|
||||
? d.suggestions.map((s) => (s.id === id ? { ...s, status } : s))
|
||||
: previous && previous.mix_id === d.id
|
||||
? [...d.suggestions, { ...previous, status }].sort((a, b) => b.score - a.score)
|
||||
: d.suggestions;
|
||||
return { ...d, suggestions };
|
||||
});
|
||||
setLiked((l) => {
|
||||
const without = l.filter((s) => s.id !== id);
|
||||
const item = previous ?? l.find((s) => s.id === id);
|
||||
return status === "liked" && item ? [{ ...item, status }, ...without] : without;
|
||||
});
|
||||
};
|
||||
|
||||
const onStatus = (s: Suggestion, status: SuggestionStatus) => {
|
||||
updateLocal(s.id, status, s);
|
||||
api.setStatus(s.id, status).then(
|
||||
() => {
|
||||
loadMixes();
|
||||
if (status === "dismissed") {
|
||||
setToast({
|
||||
message: `Hid “${s.title}”`,
|
||||
undo: () => {
|
||||
updateLocal(s.id, s.status, s);
|
||||
api.setStatus(s.id, s.status).then(loadMixes);
|
||||
setToast(null);
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
() => {
|
||||
updateLocal(s.id, s.status, s);
|
||||
setToast({ message: "Couldn't save that, try again" });
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
const rebuild = () => {
|
||||
if (!rule) return;
|
||||
api.rebuild(rule.id).then(() => {
|
||||
setToast({ message: "Rebuilding mixes. This takes a minute or two." });
|
||||
loadRules();
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={`app ${player.current ? "with-player" : ""}`}>
|
||||
<nav className="sidebar" aria-label="Mixes">
|
||||
<div className="brand">
|
||||
<span className="brand-mark"><Icon.Sparkle size={16} /></span>
|
||||
Discover
|
||||
</div>
|
||||
{rules && rules.length > 1 && (
|
||||
<select
|
||||
className="rule-select"
|
||||
value={rule?.id}
|
||||
aria-label="Listenbrainz user"
|
||||
onChange={(e) => {
|
||||
setRuleId(e.target.value);
|
||||
setMixes(null);
|
||||
try {
|
||||
localStorage.setItem("discover.rule", e.target.value);
|
||||
} catch {
|
||||
/* not essential */
|
||||
}
|
||||
window.location.hash = "";
|
||||
}}
|
||||
>
|
||||
{rules.map((r) => <option key={r.id} value={r.id}>{r.tracking_user}</option>)}
|
||||
</select>
|
||||
)}
|
||||
{rule && (
|
||||
<p className="rule-info">
|
||||
{rule.tracking_user} · last {rule.months} months
|
||||
<button className="icon-button small" onClick={rebuild} title="Rebuild mixes" aria-label="Rebuild mixes">
|
||||
<Icon.Refresh size={15} />
|
||||
</button>
|
||||
</p>
|
||||
)}
|
||||
<ul className="mix-list">
|
||||
{mixes?.map((m) => (
|
||||
<li key={m.id}>
|
||||
<a href={`#/mix/${m.id}`} className={mixId === m.id ? "selected" : ""}
|
||||
style={{ ["--hue" as string]: hue(m.tags.join()) }}>
|
||||
<span className="mix-dot" aria-hidden="true" />
|
||||
<span className="mix-name">{mixTitle(m)}</span>
|
||||
{m.new_count > 0 && <span className="count" title={`${m.new_count} new songs`}>{m.new_count}</span>}
|
||||
</a>
|
||||
</li>
|
||||
))}
|
||||
<li className="liked-link">
|
||||
<a href="#/liked" className={route.view === "liked" ? "selected" : ""} style={{ ["--hue" as string]: 340 }}>
|
||||
<span className="mix-dot heart" aria-hidden="true"><Icon.Heart size={12} filled /></span>
|
||||
<span className="mix-name">Liked new songs</span>
|
||||
{liked.length > 0 && <span className="count">{liked.length}</span>}
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
<main className="content">
|
||||
{error && <div className="notice error">{error}</div>}
|
||||
{rules && rules.length === 0 && (
|
||||
<div className="notice">
|
||||
<h1>No mix rules yet</h1>
|
||||
<p>Create one, and playlistd will cluster your listens into genre mixes:</p>
|
||||
<pre>{`curl -X POST /api/playlist/mix -H 'Content-Type: application/json' \\\n -d '{"tracking_user": "your-listenbrainz-name"}'`}</pre>
|
||||
</div>
|
||||
)}
|
||||
{pending && mixes?.length === 0 && (
|
||||
<div className="notice"><span className="spinner" /> Building mixes from your recent listens…</div>
|
||||
)}
|
||||
{route.view === "liked" ? (
|
||||
<LikedView liked={liked} player={player} onStatus={onStatus} />
|
||||
) : detail ? (
|
||||
<MixView mix={detail} player={player} onStatus={onStatus} />
|
||||
) : (
|
||||
mixId && <div className="notice"><span className="spinner" /> Loading…</div>
|
||||
)}
|
||||
</main>
|
||||
|
||||
<PlayerBar player={player} />
|
||||
|
||||
{toast && (
|
||||
<div className="toast" role="status">
|
||||
{toast.message}
|
||||
{toast.undo && <button onClick={toast.undo}>Undo</button>}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import { useMemo } from "react";
|
||||
import { Suggestion, SuggestionStatus } from "./api";
|
||||
import { fromSuggestion } from "./queue";
|
||||
import { Player } from "./usePlayer";
|
||||
import { TrackRow } from "./TrackRow";
|
||||
import { SuggestionActions } from "./MixView";
|
||||
import * as Icon from "./icons";
|
||||
|
||||
type Props = {
|
||||
liked: Suggestion[];
|
||||
player: Player;
|
||||
onStatus: (s: Suggestion, status: SuggestionStatus) => void;
|
||||
};
|
||||
|
||||
export function LikedView({ liked, player, onStatus }: Props) {
|
||||
const items = useMemo(() => liked.map(fromSuggestion), [liked]);
|
||||
return (
|
||||
<article className="mix liked-view" style={{ ["--hue" as string]: 340 }}>
|
||||
<header className="mix-header">
|
||||
<div className="mix-art" aria-hidden="true"><Icon.Heart size={56} filled /></div>
|
||||
<div className="mix-info">
|
||||
<p className="eyebrow">Collection</p>
|
||||
<h1>Liked new songs</h1>
|
||||
<p className="mix-counts">Songs you liked that aren't in your library yet: a shopping list.</p>
|
||||
<div className="mix-buttons">
|
||||
<button className="primary" onClick={() => player.playAt(items, 0)} disabled={!items.length}>
|
||||
<Icon.Play size={16} /> Play all
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
<section>
|
||||
{items.length === 0 ? (
|
||||
<p className="empty">Nothing yet. Tap the heart on a new song to keep it here.</p>
|
||||
) : (
|
||||
<ol className="tracks">
|
||||
{liked.map((s, i) => (
|
||||
<TrackRow key={s.id} item={items[i]} number={i + 1} active={player.current?.key === items[i].key}
|
||||
status={player.status} onPlay={() => player.playAt(items, i)}
|
||||
actions={<SuggestionActions s={s} onStatus={onStatus} />} />
|
||||
))}
|
||||
</ol>
|
||||
)}
|
||||
</section>
|
||||
</article>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
import { useMemo } from "react";
|
||||
import { MixDetail, mixTitle, Suggestion, SuggestionStatus } from "./api";
|
||||
import { fromSong, fromSuggestion, interleave, QueueItem } from "./queue";
|
||||
import { Player } from "./usePlayer";
|
||||
import { TrackRow } from "./TrackRow";
|
||||
import * as Icon from "./icons";
|
||||
|
||||
export function hue(text: string) {
|
||||
let h = 0;
|
||||
for (const c of text) h = (h * 31 + c.charCodeAt(0)) % 360;
|
||||
return h;
|
||||
}
|
||||
|
||||
export function SuggestionActions({ s, onStatus }: { s: Suggestion; onStatus: (s: Suggestion, status: SuggestionStatus) => void }) {
|
||||
const liked = s.status === "liked";
|
||||
return (
|
||||
<>
|
||||
<button className={`icon-button ${liked ? "liked" : ""}`} onClick={() => onStatus(s, liked ? "new" : "liked")}
|
||||
aria-label={liked ? "Unlike" : "Like"} aria-pressed={liked} title={liked ? "Unlike" : "Like"}>
|
||||
<Icon.Heart filled={liked} />
|
||||
</button>
|
||||
{s.youtube_id ? (
|
||||
<a className="icon-button" href={`https://www.youtube.com/watch?v=${s.youtube_id}`} target="_blank" rel="noreferrer"
|
||||
aria-label="Open on YouTube" title="Open on YouTube"><Icon.External /></a>
|
||||
) : (
|
||||
<a className="icon-button" href={`https://musicbrainz.org/recording/${s.recording_mbid}`} target="_blank" rel="noreferrer"
|
||||
aria-label="Open on MusicBrainz" title="Open on MusicBrainz"><Icon.External /></a>
|
||||
)}
|
||||
{s.status !== "liked" && (
|
||||
<button className="icon-button" onClick={() => onStatus(s, "dismissed")} aria-label="Not for me" title="Not for me">
|
||||
<Icon.Close />
|
||||
</button>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
type Props = {
|
||||
mix: MixDetail;
|
||||
player: Player;
|
||||
onStatus: (s: Suggestion, status: SuggestionStatus) => void;
|
||||
};
|
||||
|
||||
export function MixView({ mix, player, onStatus }: Props) {
|
||||
const fresh = useMemo(() => mix.suggestions.map(fromSuggestion), [mix.suggestions]);
|
||||
const library = useMemo(() => mix.songs.map(fromSong), [mix.songs]);
|
||||
// clicking a row plays through everything shown, new songs first
|
||||
const all = useMemo(() => [...fresh, ...library], [fresh, library]);
|
||||
const title = mixTitle(mix);
|
||||
const h = hue(mix.tags.join());
|
||||
|
||||
const play = (items: QueueItem[], at = 0) => items.length && player.playAt(items, at);
|
||||
const isActive = (item: QueueItem) => player.current?.key === item.key;
|
||||
|
||||
return (
|
||||
<article className="mix" style={{ ["--hue" as string]: h }}>
|
||||
<header className="mix-header">
|
||||
<div className="mix-art" aria-hidden="true">
|
||||
<span>{title.split(" / ")[0]}</span>
|
||||
</div>
|
||||
<div className="mix-info">
|
||||
<p className="eyebrow">Mix</p>
|
||||
<h1>{title}</h1>
|
||||
<ul className="tags">
|
||||
{mix.tags.map((t) => <li key={t}>{t}</li>)}
|
||||
</ul>
|
||||
<p className="mix-counts">
|
||||
{mix.songs.length} from your library · {mix.suggestions.length} new
|
||||
</p>
|
||||
<div className="mix-buttons">
|
||||
<button className="primary" onClick={() => play(interleave(library, fresh))} disabled={!all.length}>
|
||||
<Icon.Shuffle size={16} /> Play mix
|
||||
</button>
|
||||
<button onClick={() => play(fresh)} disabled={!fresh.length}>
|
||||
<Icon.Sparkle size={16} /> Only new songs
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<section aria-labelledby="new-heading">
|
||||
<h2 id="new-heading"><Icon.Sparkle size={16} /> New for you</h2>
|
||||
{fresh.length === 0 ? (
|
||||
<p className="empty">No new songs for this mix yet. They're found when the mixes are rebuilt.</p>
|
||||
) : (
|
||||
<ol className="tracks">
|
||||
{mix.suggestions.map((s, i) => (
|
||||
<TrackRow
|
||||
key={s.id}
|
||||
item={fresh[i]}
|
||||
number={i + 1}
|
||||
active={isActive(fresh[i])}
|
||||
status={player.status}
|
||||
onPlay={() => play(all, i)}
|
||||
meta={<span className="match" title="How well its genres fit this mix">{Math.round(s.score * 100)}% match</span>}
|
||||
actions={<SuggestionActions s={s} onStatus={onStatus} />}
|
||||
/>
|
||||
))}
|
||||
</ol>
|
||||
)}
|
||||
</section>
|
||||
|
||||
<section aria-labelledby="library-heading">
|
||||
<h2 id="library-heading"><Icon.Library size={16} /> From your library</h2>
|
||||
<ol className="tracks">
|
||||
{mix.songs.map((song, i) => (
|
||||
<TrackRow
|
||||
key={song.id}
|
||||
item={library[i]}
|
||||
number={i + 1}
|
||||
active={isActive(library[i])}
|
||||
status={player.status}
|
||||
onPlay={() => play(all, fresh.length + i)}
|
||||
duration={song.duration}
|
||||
/>
|
||||
))}
|
||||
</ol>
|
||||
</section>
|
||||
</article>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { Player } from "./usePlayer";
|
||||
import { Cover, formatTime } from "./TrackRow";
|
||||
import * as Icon from "./icons";
|
||||
|
||||
export function PlayerBar({ player }: { player: Player }) {
|
||||
const { current, status, time, duration } = player;
|
||||
if (!current) return null;
|
||||
const label =
|
||||
status === "loading"
|
||||
? current.source === "youtube" ? "Finding on YouTube…" : "Loading…"
|
||||
: status === "error" ? "Couldn't play this one, skipping" : null;
|
||||
|
||||
return (
|
||||
<footer className="player" aria-label="Player">
|
||||
<div className="player-track">
|
||||
<Cover src={current.cover} label={current.title} size={52} />
|
||||
<div className="track-text">
|
||||
<div className="track-title">{current.title}</div>
|
||||
<div className="track-sub">{current.artist}</div>
|
||||
</div>
|
||||
<span className={`badge ${current.source}`}>
|
||||
{current.source === "youtube" ? "YouTube" : "Library"}
|
||||
</span>
|
||||
</div>
|
||||
<div className="player-main">
|
||||
<div className="player-buttons">
|
||||
<button className="icon-button" onClick={player.previous} aria-label="Previous"><Icon.Prev /></button>
|
||||
<button className="play-button" onClick={player.toggle} aria-label={status === "playing" ? "Pause" : "Play"}>
|
||||
{status === "loading" ? <span className="spinner light" /> : status === "playing" ? <Icon.Pause size={20} /> : <Icon.Play size={20} />}
|
||||
</button>
|
||||
<button className="icon-button" onClick={player.next} aria-label="Next"><Icon.Next /></button>
|
||||
</div>
|
||||
<div className="player-progress">
|
||||
<span>{formatTime(time) || "0:00"}</span>
|
||||
<input
|
||||
type="range"
|
||||
min={0}
|
||||
max={duration || 1}
|
||||
step={0.5}
|
||||
value={Math.min(time, duration || 1)}
|
||||
disabled={!duration}
|
||||
onChange={(e) => player.seek(Number(e.target.value))}
|
||||
aria-label="Seek"
|
||||
style={{ ["--progress" as string]: `${duration ? (time / duration) * 100 : 0}%` }}
|
||||
/>
|
||||
<span>{formatTime(duration)}</span>
|
||||
</div>
|
||||
{label && <div className={`player-status ${status}`}>{label}</div>}
|
||||
</div>
|
||||
<div className="player-queue">
|
||||
{player.index + 1} / {player.queue.length}
|
||||
</div>
|
||||
</footer>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { ReactNode } from "react";
|
||||
import { QueueItem } from "./queue";
|
||||
import { PlayerStatus } from "./usePlayer";
|
||||
import * as Icon from "./icons";
|
||||
|
||||
export function formatTime(seconds?: number) {
|
||||
if (!seconds || !Number.isFinite(seconds)) return "";
|
||||
const s = Math.floor(seconds);
|
||||
return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
|
||||
}
|
||||
|
||||
export function Cover({ src, label, size = 44 }: { src?: string | null; label: string; size?: number }) {
|
||||
return (
|
||||
<div className="cover" style={{ width: size, height: size }} aria-hidden="true">
|
||||
<span>{label.slice(0, 1).toUpperCase()}</span>
|
||||
{src && <img src={src} alt="" loading="lazy" onError={(e) => (e.currentTarget.style.display = "none")} />}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
type Props = {
|
||||
item: QueueItem;
|
||||
number: number;
|
||||
active: boolean;
|
||||
status: PlayerStatus;
|
||||
onPlay: () => void;
|
||||
meta?: ReactNode;
|
||||
actions?: ReactNode;
|
||||
duration?: number;
|
||||
};
|
||||
|
||||
export function TrackRow({ item, number, active, status, onPlay, meta, actions, duration }: Props) {
|
||||
const loading = active && status === "loading";
|
||||
const playing = active && status === "playing";
|
||||
return (
|
||||
<li className={`track ${active ? "active" : ""}`}>
|
||||
<button className="track-play" onClick={onPlay} aria-label={`Play ${item.title} by ${item.artist}`}>
|
||||
<span className="track-number">{number}</span>
|
||||
<span className="track-icon">
|
||||
{loading ? <span className="spinner" /> : playing ? <span className="bars"><i /><i /><i /></span> : <Icon.Play size={14} />}
|
||||
</span>
|
||||
</button>
|
||||
<Cover src={item.cover} label={item.title} />
|
||||
<div className="track-text">
|
||||
<div className="track-title">{item.title}</div>
|
||||
<div className="track-sub">
|
||||
{item.artist}
|
||||
{item.album && <span className="track-album"> · {item.album}</span>}
|
||||
</div>
|
||||
</div>
|
||||
{meta && <div className="track-meta">{meta}</div>}
|
||||
<div className="track-duration">{formatTime(duration)}</div>
|
||||
{actions && <div className="track-actions">{actions}</div>}
|
||||
</li>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
export type Rule = {
|
||||
id: string;
|
||||
tracking_user: string;
|
||||
months: number;
|
||||
playlist_size: number;
|
||||
discovery_count: number;
|
||||
last_run: string | null;
|
||||
};
|
||||
|
||||
export type MixSummary = {
|
||||
id: string;
|
||||
playlist_id: string;
|
||||
name: string | null;
|
||||
tags: string[];
|
||||
position: number;
|
||||
new_count: number;
|
||||
};
|
||||
|
||||
export type Song = {
|
||||
id: string;
|
||||
title: string;
|
||||
artist: string;
|
||||
album: string;
|
||||
duration?: number;
|
||||
coverArt?: string;
|
||||
};
|
||||
|
||||
export type SuggestionStatus = "new" | "liked" | "dismissed";
|
||||
|
||||
export type Suggestion = {
|
||||
id: string;
|
||||
mix_id: string | null;
|
||||
recording_mbid: string;
|
||||
title: string;
|
||||
artist: string;
|
||||
release: string | null;
|
||||
cover_url: string | null;
|
||||
score: number;
|
||||
status: SuggestionStatus;
|
||||
youtube_id: string | null;
|
||||
};
|
||||
|
||||
export type MixDetail = MixSummary & { songs: Song[]; suggestions: Suggestion[] };
|
||||
|
||||
async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
const response = await fetch(`/api/discover${path}`, init);
|
||||
if (!response.ok) {
|
||||
throw new Error(`${response.status} ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export const api = {
|
||||
rules: () => request<Rule[]>("/rules"),
|
||||
mixes: (ruleId: string) => request<MixSummary[]>(`/rules/${ruleId}/mixes`),
|
||||
mix: (mixId: string) => request<MixDetail>(`/mixes/${mixId}`),
|
||||
liked: (ruleId: string) => request<Suggestion[]>(`/rules/${ruleId}/liked`),
|
||||
setStatus: (id: string, status: SuggestionStatus) =>
|
||||
request<Suggestion>(`/suggestions/${id}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ status }),
|
||||
}),
|
||||
rebuild: (ruleId: string) =>
|
||||
fetch(`/api/discover/rules/${ruleId}/rebuild`, { method: "POST" }),
|
||||
};
|
||||
|
||||
export const coverUrl = (coverArt: string, size = 120) =>
|
||||
`/api/discover/cover/${encodeURIComponent(coverArt)}?size=${size}`;
|
||||
export const streamUrl = (songId: string) => `/api/discover/stream/${encodeURIComponent(songId)}`;
|
||||
export const youtubeUrl = (suggestionId: string) => `/api/discover/youtube/${suggestionId}`;
|
||||
|
||||
/** "Mix: Pop Punk / Emo" -> "Pop Punk / Emo" */
|
||||
export const mixTitle = (mix: MixSummary) =>
|
||||
(mix.name ?? mix.tags.join(" / ")).replace(/^mix:\s*/i, "");
|
||||
@@ -0,0 +1,16 @@
|
||||
type P = { size?: number };
|
||||
const svg = (path: JSX.Element, size = 18) => (
|
||||
<svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2}
|
||||
strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">{path}</svg>
|
||||
);
|
||||
export const Play = ({ size }: P) => svg(<path d="M7 4.5v15l12-7.5z" fill="currentColor" stroke="none" />, size);
|
||||
export const Pause = ({ size }: P) => svg(<><rect x="6" y="5" width="4" height="14" rx="1" fill="currentColor" stroke="none" /><rect x="14" y="5" width="4" height="14" rx="1" fill="currentColor" stroke="none" /></>, size);
|
||||
export const Next = ({ size }: P) => svg(<><path d="M5 5v14l10-7z" fill="currentColor" stroke="none" /><path d="M18 5v14" /></>, size);
|
||||
export const Prev = ({ size }: P) => svg(<><path d="M19 5v14L9 12z" fill="currentColor" stroke="none" /><path d="M6 5v14" /></>, size);
|
||||
export const Heart = ({ size, filled }: P & { filled?: boolean }) => svg(<path d="M12 20s-7-4.4-9.2-8.6C1.2 8.3 3 4.5 6.6 4.5c2.1 0 3.6 1.2 5.4 3.2 1.8-2 3.3-3.2 5.4-3.2 3.6 0 5.4 3.8 3.8 6.9C19 15.6 12 20 12 20z" fill={filled ? "currentColor" : "none"} />, size);
|
||||
export const Close = ({ size }: P) => svg(<path d="M6 6l12 12M18 6L6 18" />, size);
|
||||
export const External = ({ size }: P) => svg(<><path d="M14 4h6v6" /><path d="M20 4l-9 9" /><path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5" /></>, size);
|
||||
export const Refresh = ({ size }: P) => svg(<><path d="M20 11a8 8 0 1 0-2.3 5.7" /><path d="M20 4v7h-7" /></>, size);
|
||||
export const Sparkle = ({ size }: P) => svg(<path d="M12 3l1.8 5.2L19 10l-5.2 1.8L12 17l-1.8-5.2L5 10l5.2-1.8z" fill="currentColor" stroke="none" />, size);
|
||||
export const Library = ({ size }: P) => svg(<><path d="M4 4v16M9 4v16" /><path d="M14 5l5 14" /></>, size);
|
||||
export const Shuffle = ({ size }: P) => svg(<><path d="M4 7h3c4 0 6 10 10 10h3" /><path d="M4 17h3c1.6 0 2.8-1.6 3.9-3.6M13.1 9.6C14.2 8 15.4 7 17 7h3" /><path d="M18 4l3 3-3 3M18 14l3 3-3 3" /></>, size);
|
||||
@@ -0,0 +1,10 @@
|
||||
import { StrictMode } from "react";
|
||||
import { createRoot } from "react-dom/client";
|
||||
import { App } from "./App";
|
||||
import "./styles.css";
|
||||
|
||||
createRoot(document.getElementById("root")!).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>,
|
||||
);
|
||||
@@ -0,0 +1,47 @@
|
||||
import { coverUrl, Song, streamUrl, Suggestion, youtubeUrl } from "./api";
|
||||
|
||||
export type QueueItem = {
|
||||
key: string;
|
||||
source: "library" | "youtube";
|
||||
src: string;
|
||||
title: string;
|
||||
artist: string;
|
||||
album?: string | null;
|
||||
cover?: string | null;
|
||||
suggestionId?: string;
|
||||
};
|
||||
|
||||
export const fromSong = (song: Song): QueueItem => ({
|
||||
key: `song:${song.id}`,
|
||||
source: "library",
|
||||
src: streamUrl(song.id),
|
||||
title: song.title,
|
||||
artist: song.artist,
|
||||
album: song.album,
|
||||
cover: song.coverArt ? coverUrl(song.coverArt, 300) : null,
|
||||
});
|
||||
|
||||
export const fromSuggestion = (s: Suggestion): QueueItem => ({
|
||||
key: `suggestion:${s.id}`,
|
||||
source: "youtube",
|
||||
src: youtubeUrl(s.id),
|
||||
title: s.title,
|
||||
artist: s.artist,
|
||||
album: s.release,
|
||||
cover: s.cover_url,
|
||||
suggestionId: s.id,
|
||||
});
|
||||
|
||||
/** Spread new songs evenly through the library songs */
|
||||
export function interleave(library: QueueItem[], fresh: QueueItem[]): QueueItem[] {
|
||||
if (fresh.length === 0) return library;
|
||||
const out: QueueItem[] = [];
|
||||
const every = Math.max(1, Math.round((library.length + fresh.length) / fresh.length));
|
||||
let l = 0;
|
||||
let f = 0;
|
||||
for (let i = 0; l < library.length || f < fresh.length; i++) {
|
||||
const takeFresh = (i % every === every - 1 && f < fresh.length) || l >= library.length;
|
||||
out.push(takeFresh ? fresh[f++] : library[l++]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -0,0 +1,824 @@
|
||||
:root {
|
||||
--bg: #f6f5f2;
|
||||
--surface: #ffffff;
|
||||
--surface-2: #efede8;
|
||||
--border: #e2dfd8;
|
||||
--text: #1d1b18;
|
||||
--muted: #6d675e;
|
||||
--accent: #5b3df5;
|
||||
--accent-text: #ffffff;
|
||||
--youtube: #d4351c;
|
||||
--library: #2f7d4f;
|
||||
--heart: #e0245e;
|
||||
--shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 8px 24px rgb(0 0 0 / 6%);
|
||||
--art-l: 62%;
|
||||
--art-s: 70%;
|
||||
--radius: 12px;
|
||||
font-family: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
|
||||
font-size: 15px;
|
||||
line-height: 1.45;
|
||||
color: var(--text);
|
||||
background: var(--bg);
|
||||
-webkit-font-smoothing: antialiased;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--bg: #121214;
|
||||
--surface: #1b1b1f;
|
||||
--surface-2: #26262b;
|
||||
--border: #2e2e34;
|
||||
--text: #f1f0ee;
|
||||
--muted: #a09d98;
|
||||
--accent: #8b74ff;
|
||||
--accent-text: #120d26;
|
||||
--youtube: #ff6b52;
|
||||
--library: #5fcf8d;
|
||||
--heart: #ff5c8a;
|
||||
--shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 8px 24px rgb(0 0 0 / 30%);
|
||||
--art-l: 45%;
|
||||
--art-s: 55%;
|
||||
}
|
||||
}
|
||||
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
background: var(--bg);
|
||||
}
|
||||
|
||||
button,
|
||||
select,
|
||||
input {
|
||||
font: inherit;
|
||||
color: inherit;
|
||||
}
|
||||
|
||||
a {
|
||||
color: inherit;
|
||||
}
|
||||
|
||||
:focus-visible {
|
||||
outline: 2px solid var(--accent);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
/* ---------- layout ---------- */
|
||||
|
||||
.app {
|
||||
display: grid;
|
||||
grid-template-columns: 272px minmax(0, 1fr);
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.sidebar {
|
||||
position: sticky;
|
||||
top: 0;
|
||||
height: 100vh;
|
||||
overflow-y: auto;
|
||||
padding: 20px 12px;
|
||||
border-right: 1px solid var(--border);
|
||||
background: var(--surface);
|
||||
}
|
||||
|
||||
.with-player .sidebar {
|
||||
height: calc(100vh - 84px);
|
||||
}
|
||||
|
||||
.content {
|
||||
padding: 28px clamp(16px, 4vw, 48px) 48px;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.with-player .content {
|
||||
padding-bottom: 120px;
|
||||
}
|
||||
|
||||
.brand {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
padding: 0 10px 16px;
|
||||
font-weight: 700;
|
||||
font-size: 1.15rem;
|
||||
letter-spacing: -0.01em;
|
||||
}
|
||||
|
||||
.brand-mark {
|
||||
display: grid;
|
||||
place-items: center;
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border-radius: 8px;
|
||||
background: var(--accent);
|
||||
color: var(--accent-text);
|
||||
}
|
||||
|
||||
.rule-select {
|
||||
width: calc(100% - 20px);
|
||||
margin: 0 10px 8px;
|
||||
padding: 6px 8px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
background: var(--surface-2);
|
||||
}
|
||||
|
||||
.rule-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin: 0 0 8px;
|
||||
padding: 0 4px 0 10px;
|
||||
color: var(--muted);
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.mix-list {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.mix-list a {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
padding: 8px 10px;
|
||||
border-radius: 8px;
|
||||
text-decoration: none;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.mix-list a:hover {
|
||||
background: var(--surface-2);
|
||||
}
|
||||
|
||||
.mix-list a.selected {
|
||||
background: hsl(var(--hue) var(--art-s) var(--art-l) / 18%);
|
||||
}
|
||||
|
||||
.mix-dot {
|
||||
flex: none;
|
||||
width: 12px;
|
||||
height: 12px;
|
||||
border-radius: 4px;
|
||||
background: linear-gradient(135deg, hsl(var(--hue) var(--art-s) var(--art-l)), hsl(calc(var(--hue) + 40) var(--art-s) calc(var(--art-l) - 12%)));
|
||||
}
|
||||
|
||||
.mix-dot.heart {
|
||||
display: grid;
|
||||
place-items: center;
|
||||
background: none;
|
||||
color: var(--heart);
|
||||
}
|
||||
|
||||
.mix-name {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
font-size: 0.93rem;
|
||||
}
|
||||
|
||||
.count {
|
||||
flex: none;
|
||||
min-width: 22px;
|
||||
padding: 1px 7px;
|
||||
border-radius: 999px;
|
||||
background: var(--accent);
|
||||
color: var(--accent-text);
|
||||
font-size: 0.75rem;
|
||||
font-weight: 600;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.liked-link {
|
||||
margin-top: 10px;
|
||||
padding-top: 10px;
|
||||
border-top: 1px solid var(--border);
|
||||
}
|
||||
|
||||
/* ---------- mix header ---------- */
|
||||
|
||||
.mix-header {
|
||||
display: flex;
|
||||
gap: 28px;
|
||||
align-items: flex-end;
|
||||
margin-bottom: 32px;
|
||||
}
|
||||
|
||||
.mix-art {
|
||||
flex: none;
|
||||
display: grid;
|
||||
place-items: end start;
|
||||
width: 200px;
|
||||
height: 200px;
|
||||
padding: 18px;
|
||||
border-radius: 16px;
|
||||
background:
|
||||
radial-gradient(circle at 80% 15%, hsl(calc(var(--hue) + 60) 90% 75% / 70%), transparent 55%),
|
||||
linear-gradient(135deg, hsl(var(--hue) var(--art-s) var(--art-l)), hsl(calc(var(--hue) + 40) var(--art-s) calc(var(--art-l) - 18%)));
|
||||
color: #fff;
|
||||
box-shadow: var(--shadow);
|
||||
font-weight: 800;
|
||||
font-size: 1.5rem;
|
||||
line-height: 1.1;
|
||||
letter-spacing: -0.02em;
|
||||
text-shadow: 0 1px 8px rgb(0 0 0 / 25%);
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.liked-view .mix-art {
|
||||
place-items: center;
|
||||
}
|
||||
|
||||
.mix-info {
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.eyebrow {
|
||||
margin: 0 0 4px;
|
||||
color: var(--muted);
|
||||
font-size: 0.78rem;
|
||||
font-weight: 600;
|
||||
letter-spacing: 0.08em;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin: 0 0 12px;
|
||||
font-size: clamp(1.6rem, 3.4vw, 2.6rem);
|
||||
line-height: 1.1;
|
||||
letter-spacing: -0.025em;
|
||||
}
|
||||
|
||||
.tags {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
margin: 0 0 12px;
|
||||
padding: 0;
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
.tags li {
|
||||
padding: 2px 10px;
|
||||
border-radius: 999px;
|
||||
background: hsl(var(--hue) var(--art-s) var(--art-l) / 16%);
|
||||
font-size: 0.82rem;
|
||||
}
|
||||
|
||||
.mix-counts {
|
||||
margin: 0 0 16px;
|
||||
color: var(--muted);
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.mix-buttons {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.mix-buttons button {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 9px 16px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 999px;
|
||||
background: var(--surface);
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.mix-buttons button.primary {
|
||||
border-color: transparent;
|
||||
background: var(--accent);
|
||||
color: var(--accent-text);
|
||||
}
|
||||
|
||||
.mix-buttons button:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
/* ---------- track lists ---------- */
|
||||
|
||||
section + section {
|
||||
margin-top: 32px;
|
||||
}
|
||||
|
||||
h2 {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
margin: 0 0 8px;
|
||||
font-size: 1.05rem;
|
||||
}
|
||||
|
||||
.empty {
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.tracks {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
.track {
|
||||
display: grid;
|
||||
grid-template-columns: 36px 44px minmax(0, 1fr) auto auto auto;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
padding: 6px 8px;
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
.track:hover,
|
||||
.track.active {
|
||||
background: var(--surface);
|
||||
}
|
||||
|
||||
.track.active {
|
||||
box-shadow: inset 3px 0 0 var(--accent);
|
||||
}
|
||||
|
||||
.track-play {
|
||||
display: grid;
|
||||
place-items: center;
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
border-radius: 50%;
|
||||
background: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.track-icon {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.track:hover .track-number,
|
||||
.track.active .track-number {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.track:hover .track-icon,
|
||||
.track.active .track-icon,
|
||||
.track-play:focus-visible .track-icon {
|
||||
display: grid;
|
||||
place-items: center;
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.track-play:focus-visible .track-number {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.track-number {
|
||||
color: var(--muted);
|
||||
font-variant-numeric: tabular-nums;
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.cover {
|
||||
position: relative;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
overflow: hidden;
|
||||
border-radius: 6px;
|
||||
background: var(--surface-2);
|
||||
color: var(--muted);
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.cover img {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.track-text {
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.track-title,
|
||||
.track-sub {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.track-title {
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.track.active .track-title {
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.track-sub {
|
||||
color: var(--muted);
|
||||
font-size: 0.88rem;
|
||||
}
|
||||
|
||||
.match {
|
||||
color: var(--muted);
|
||||
font-size: 0.82rem;
|
||||
font-variant-numeric: tabular-nums;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.track-duration {
|
||||
color: var(--muted);
|
||||
font-size: 0.85rem;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
.track-actions {
|
||||
display: flex;
|
||||
gap: 2px;
|
||||
}
|
||||
|
||||
.icon-button {
|
||||
display: inline-grid;
|
||||
place-items: center;
|
||||
width: 34px;
|
||||
height: 34px;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
border-radius: 50%;
|
||||
background: none;
|
||||
color: var(--muted);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.icon-button.small {
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
}
|
||||
|
||||
.icon-button:hover {
|
||||
background: var(--surface-2);
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.icon-button.liked {
|
||||
color: var(--heart);
|
||||
}
|
||||
|
||||
/* ---------- player ---------- */
|
||||
|
||||
.player {
|
||||
position: fixed;
|
||||
inset: auto 0 0 0;
|
||||
z-index: 10;
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr) minmax(0, 1fr);
|
||||
align-items: center;
|
||||
gap: 20px;
|
||||
height: 84px;
|
||||
padding: 0 20px;
|
||||
border-top: 1px solid var(--border);
|
||||
background: color-mix(in srgb, var(--surface) 92%, transparent);
|
||||
backdrop-filter: blur(12px);
|
||||
}
|
||||
|
||||
.player-track {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.badge {
|
||||
flex: none;
|
||||
padding: 1px 8px;
|
||||
border-radius: 999px;
|
||||
border: 1px solid currentColor;
|
||||
font-size: 0.72rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.badge.youtube {
|
||||
color: var(--youtube);
|
||||
}
|
||||
|
||||
.badge.library {
|
||||
color: var(--library);
|
||||
}
|
||||
|
||||
.player-main {
|
||||
position: relative;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.player-buttons {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.play-button {
|
||||
display: grid;
|
||||
place-items: center;
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
border: 0;
|
||||
border-radius: 50%;
|
||||
background: var(--text);
|
||||
color: var(--bg);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.player-progress {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
width: 100%;
|
||||
color: var(--muted);
|
||||
font-size: 0.75rem;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
.player-progress span {
|
||||
width: 38px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.player-progress input {
|
||||
flex: 1;
|
||||
height: 4px;
|
||||
appearance: none;
|
||||
border-radius: 2px;
|
||||
background: linear-gradient(to right, var(--accent) var(--progress), var(--border) var(--progress));
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.player-progress input::-webkit-slider-thumb {
|
||||
appearance: none;
|
||||
width: 12px;
|
||||
height: 12px;
|
||||
border-radius: 50%;
|
||||
background: var(--text);
|
||||
}
|
||||
|
||||
.player-progress input::-moz-range-thumb {
|
||||
width: 12px;
|
||||
height: 12px;
|
||||
border: 0;
|
||||
border-radius: 50%;
|
||||
background: var(--text);
|
||||
}
|
||||
|
||||
.player-status {
|
||||
position: absolute;
|
||||
top: -26px;
|
||||
padding: 2px 10px;
|
||||
border-radius: 999px;
|
||||
background: var(--surface-2);
|
||||
font-size: 0.78rem;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.player-status.error {
|
||||
color: var(--youtube);
|
||||
}
|
||||
|
||||
.player-queue {
|
||||
justify-self: end;
|
||||
color: var(--muted);
|
||||
font-size: 0.85rem;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
/* ---------- feedback ---------- */
|
||||
|
||||
.spinner {
|
||||
display: inline-block;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
border: 2px solid var(--border);
|
||||
border-top-color: var(--accent);
|
||||
border-radius: 50%;
|
||||
animation: spin 0.8s linear infinite;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.spinner.light {
|
||||
border-color: color-mix(in srgb, var(--bg) 35%, transparent);
|
||||
border-top-color: var(--bg);
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
.bars {
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
gap: 2px;
|
||||
height: 14px;
|
||||
}
|
||||
|
||||
.bars i {
|
||||
width: 3px;
|
||||
background: var(--accent);
|
||||
animation: bar 0.9s ease-in-out infinite;
|
||||
}
|
||||
|
||||
.bars i:nth-child(2) {
|
||||
animation-delay: -0.3s;
|
||||
}
|
||||
|
||||
.bars i:nth-child(3) {
|
||||
animation-delay: -0.6s;
|
||||
}
|
||||
|
||||
@keyframes bar {
|
||||
0%,
|
||||
100% {
|
||||
height: 30%;
|
||||
}
|
||||
50% {
|
||||
height: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.bars i,
|
||||
.spinner {
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
|
||||
.notice {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
margin-bottom: 24px;
|
||||
padding: 16px 20px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
background: var(--surface);
|
||||
}
|
||||
|
||||
.notice.error {
|
||||
border-color: var(--youtube);
|
||||
color: var(--youtube);
|
||||
}
|
||||
|
||||
.notice pre {
|
||||
overflow-x: auto;
|
||||
margin: 0;
|
||||
padding: 12px;
|
||||
border-radius: 8px;
|
||||
background: var(--surface-2);
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.toast {
|
||||
position: fixed;
|
||||
left: 50%;
|
||||
bottom: 104px;
|
||||
z-index: 20;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 14px;
|
||||
max-width: calc(100vw - 32px);
|
||||
padding: 10px 16px;
|
||||
border-radius: 10px;
|
||||
background: var(--text);
|
||||
color: var(--bg);
|
||||
box-shadow: var(--shadow);
|
||||
transform: translateX(-50%);
|
||||
}
|
||||
|
||||
.app:not(.with-player) .toast {
|
||||
bottom: 24px;
|
||||
}
|
||||
|
||||
.toast button {
|
||||
border: 0;
|
||||
background: none;
|
||||
color: var(--accent);
|
||||
font-weight: 700;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
/* ---------- small screens ---------- */
|
||||
|
||||
@media (max-width: 800px) {
|
||||
.app {
|
||||
grid-template-columns: minmax(0, 1fr);
|
||||
}
|
||||
|
||||
.sidebar,
|
||||
.with-player .sidebar {
|
||||
position: static;
|
||||
height: auto;
|
||||
padding: 14px 16px 10px;
|
||||
border-right: 0;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.brand {
|
||||
padding: 0 0 10px;
|
||||
}
|
||||
|
||||
.rule-info {
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.mix-list {
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
overflow-x: auto;
|
||||
margin: 0 -16px;
|
||||
padding: 0 16px 4px;
|
||||
scrollbar-width: none;
|
||||
}
|
||||
|
||||
.mix-list a {
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 999px;
|
||||
padding: 6px 12px;
|
||||
}
|
||||
|
||||
.mix-name {
|
||||
overflow: visible;
|
||||
}
|
||||
|
||||
.liked-link {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
}
|
||||
|
||||
.content {
|
||||
padding: 20px 16px 40px;
|
||||
}
|
||||
|
||||
.mix-header {
|
||||
flex-direction: column;
|
||||
align-items: stretch;
|
||||
gap: 18px;
|
||||
}
|
||||
|
||||
.mix-art {
|
||||
width: 100%;
|
||||
height: 140px;
|
||||
}
|
||||
|
||||
.track {
|
||||
grid-template-columns: 32px 40px minmax(0, 1fr) auto;
|
||||
gap: 10px;
|
||||
padding: 6px 0;
|
||||
}
|
||||
|
||||
.track-meta,
|
||||
.track-duration,
|
||||
.track-album {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.player {
|
||||
grid-template-columns: minmax(0, 1fr) auto;
|
||||
height: auto;
|
||||
padding: 10px 16px;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.player-main {
|
||||
flex-direction: row-reverse;
|
||||
}
|
||||
|
||||
.player-progress,
|
||||
.player-queue,
|
||||
.player .badge {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.player-status {
|
||||
right: 0;
|
||||
top: -38px;
|
||||
}
|
||||
|
||||
.with-player .content {
|
||||
padding-bottom: 110px;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { QueueItem } from "./queue";
|
||||
|
||||
export type PlayerStatus = "idle" | "loading" | "playing" | "paused" | "error";
|
||||
|
||||
export type Player = ReturnType<typeof usePlayer>;
|
||||
|
||||
export function usePlayer(onError: (item: QueueItem) => void) {
|
||||
const audio = useRef<HTMLAudioElement | null>(null);
|
||||
if (audio.current === null && typeof Audio !== "undefined") {
|
||||
audio.current = new Audio();
|
||||
audio.current.preload = "auto";
|
||||
}
|
||||
|
||||
const [queue, setQueue] = useState<QueueItem[]>([]);
|
||||
const [index, setIndex] = useState(-1);
|
||||
const [status, setStatus] = useState<PlayerStatus>("idle");
|
||||
const [time, setTime] = useState(0);
|
||||
const [duration, setDuration] = useState(0);
|
||||
const current = index >= 0 ? queue[index] : undefined;
|
||||
|
||||
// keep the latest values for the audio element's event handlers
|
||||
const state = useRef({ queue, index, onError });
|
||||
state.current = { queue, index, onError };
|
||||
|
||||
const playAt = useCallback((items: QueueItem[], at: number) => {
|
||||
setQueue(items);
|
||||
setIndex(at);
|
||||
}, []);
|
||||
|
||||
const next = useCallback(() => {
|
||||
const { queue, index } = state.current;
|
||||
if (index + 1 < queue.length) setIndex(index + 1);
|
||||
else {
|
||||
audio.current?.pause();
|
||||
setStatus("paused");
|
||||
}
|
||||
}, []);
|
||||
|
||||
const previous = useCallback(() => {
|
||||
const el = audio.current;
|
||||
if (el && el.currentTime > 3) el.currentTime = 0;
|
||||
else setIndex((i) => Math.max(0, i - 1));
|
||||
}, []);
|
||||
|
||||
const toggle = useCallback(() => {
|
||||
const el = audio.current;
|
||||
if (!el || !state.current.queue.length) return;
|
||||
if (el.paused) void el.play().catch(() => undefined);
|
||||
else el.pause();
|
||||
}, []);
|
||||
|
||||
const seek = useCallback((seconds: number) => {
|
||||
if (!audio.current) return;
|
||||
audio.current.currentTime = seconds;
|
||||
// update now, or the controlled slider snaps back and its change event seeks back too
|
||||
setTime(seconds);
|
||||
}, []);
|
||||
|
||||
// load the current item
|
||||
useEffect(() => {
|
||||
const el = audio.current;
|
||||
if (!el || !current) return;
|
||||
setTime(0);
|
||||
setDuration(0);
|
||||
setStatus("loading");
|
||||
el.src = current.src;
|
||||
void el.play().catch(() => undefined);
|
||||
}, [current?.key]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
useEffect(() => {
|
||||
const el = audio.current;
|
||||
if (!el) return;
|
||||
let errorTimer: number | undefined;
|
||||
const handlers: Record<string, () => void> = {
|
||||
playing: () => setStatus("playing"),
|
||||
pause: () => setStatus((s) => (s === "error" ? s : "paused")),
|
||||
waiting: () => setStatus("loading"),
|
||||
timeupdate: () => setTime(el.currentTime),
|
||||
durationchange: () => setDuration(Number.isFinite(el.duration) ? el.duration : 0),
|
||||
ended: () => next(),
|
||||
error: () => {
|
||||
if (!el.src) return;
|
||||
setStatus("error");
|
||||
const { queue, index, onError } = state.current;
|
||||
if (queue[index]) onError(queue[index]);
|
||||
errorTimer = window.setTimeout(next, 1500);
|
||||
},
|
||||
};
|
||||
for (const [event, handler] of Object.entries(handlers)) el.addEventListener(event, handler);
|
||||
return () => {
|
||||
window.clearTimeout(errorTimer);
|
||||
for (const [event, handler] of Object.entries(handlers)) el.removeEventListener(event, handler);
|
||||
};
|
||||
}, [next]);
|
||||
|
||||
// OS media controls and keyboard
|
||||
useEffect(() => {
|
||||
if (!("mediaSession" in navigator) || !current) return;
|
||||
navigator.mediaSession.metadata = new MediaMetadata({
|
||||
title: current.title,
|
||||
artist: current.artist,
|
||||
album: current.album ?? undefined,
|
||||
artwork: current.cover ? [{ src: current.cover }] : [],
|
||||
});
|
||||
navigator.mediaSession.setActionHandler("play", toggle);
|
||||
navigator.mediaSession.setActionHandler("pause", toggle);
|
||||
navigator.mediaSession.setActionHandler("nexttrack", next);
|
||||
navigator.mediaSession.setActionHandler("previoustrack", previous);
|
||||
}, [current, toggle, next, previous]);
|
||||
|
||||
useEffect(() => {
|
||||
const onKey = (e: KeyboardEvent) => {
|
||||
const target = e.target as HTMLElement;
|
||||
if (e.code !== "Space" || ["INPUT", "TEXTAREA", "BUTTON"].includes(target.tagName)) return;
|
||||
e.preventDefault();
|
||||
toggle();
|
||||
};
|
||||
window.addEventListener("keydown", onKey);
|
||||
return () => window.removeEventListener("keydown", onKey);
|
||||
}, [toggle]);
|
||||
|
||||
return { queue, index, current, status, time, duration, playAt, next, previous, toggle, seek };
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"jsx": "react-jsx",
|
||||
"strict": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"skipLibCheck": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true
|
||||
},
|
||||
"include": ["src", "vite.config.ts"]
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { defineConfig } from "vite";
|
||||
import react from "@vitejs/plugin-react";
|
||||
|
||||
// During development, API requests go to playlistd (PLAYLISTD_URL, default localhost:8000)
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
server: {
|
||||
proxy: {
|
||||
"/api": process.env.PLAYLISTD_URL ?? "http://localhost:8000",
|
||||
},
|
||||
},
|
||||
});
|
||||
Reference in new issue
Block a user