Chat / frontend /src /components /stillwave /Collection.tsx
kishankrish19's picture
Deploy Stillwave fullstack clean
b0798af
Raw History Blame Contribute Delete
10.4 kB
"use client";
import { useEffect, useMemo, useState } from "react";
import Link from "next/link";
import {
Search,
Plus,
Music2,
ArrowUpRight,
Heart,
LayoutGrid,
List,
Folder,
} from "lucide-react";
import { api, apiRequest, Track, Playlist, token } from "@/lib/api/apiClient";
import Artwork from "./Artwork";
import BackupStatus from "./BackupStatus";
import TrackList from "./TrackList";
import { usePlayerStore } from "@/lib/store/playerStore";
export default function Collection() {
const [tracks, setTracks] = useState<Track[]>([]),
[playlists, setPlaylists] = useState<Playlist[]>([]),
[tab, setTab] = useState("songs"),
[query, setQuery] = useState(""),
[sort, setSort] = useState("recent"),
[grid, setGrid] = useState(false),
[error, setError] = useState(""),
[loading, setLoading] = useState(true),
[create, setCreate] = useState(false),
[name, setName] = useState(""),
[saving, setSaving] = useState(false),
[syncing, setSyncing] = useState(false);
const load = () => {
setLoading(true);
api
.getTracks()
.then(setTracks)
.catch((e) => setError(e.message))
.finally(() => setLoading(false));
if (token())
api
.playlists()
.then(setPlaylists)
.catch((e) => setError(e.message));
};
useEffect(() => {
const refresh = () => {
api
.getTracks()
.then(setTracks)
.catch((e) => setError(e.message))
.finally(() => setLoading(false));
if (token())
api
.playlists()
.then(setPlaylists)
.catch((e) => setError(e.message));
};
refresh();
window.addEventListener("library-updated", refresh);
return () => window.removeEventListener("library-updated", refresh);
}, []);
const filtered = useMemo(
() =>
tracks
.filter(
(t) =>
(tab !== "liked" || t.liked) &&
(tab !== "uploads" || t.source === "upload") &&
`${t.title} ${t.artist_name} ${t.album_title}`
.toLowerCase()
.includes(query.toLowerCase()),
)
.sort((a, b) =>
sort === "title"
? a.title.localeCompare(b.title)
: sort === "artist"
? a.artist_name.localeCompare(b.artist_name)
: String(b.created_at).localeCompare(String(a.created_at)),
),
[tracks, tab, query, sort],
);
return (
<div className="page-enter">
<div className="page-title-row">
<div>
<span className="eyebrow">A SOUND OF YOUR OWN</span>
<h1>
Your collection<span className="title-period">.</span>
</h1>
<p>{tracks.length} songs. Every one has a place.</p>
</div>
<Link className="button" href="/uploads">
<Plus size={17} /> Import music
</Link>
</div>
<div className="collection-toolbar">
<span className="muted">
Your uploads and connected dataset, together.
</span>
<button
className="button small secondary"
disabled={syncing}
onClick={async () => {
setSyncing(true);
setError("");
try {
await apiRequest("/library/refresh", { method: "POST" });
load();
window.dispatchEvent(new Event("library-updated"));
} catch (e) {
setError((e as Error).message);
} finally {
setSyncing(false);
}
}}
>
{syncing ? "Refreshing…" : "Refresh dataset"}
</button>
</div>
<div className="tabs">
{[
["songs", "All songs"],
["liked", "Favorites"],
["uploads", "Your uploads"],
["playlists", "Playlists"],
].map(([id, label]) => (
<button
key={id}
className={tab === id ? "active" : ""}
onClick={() => setTab(id)}
>
{id === "liked" && <Heart size={14} />} {label}
</button>
))}
</div>
{error && (
<div className="notice error" role="alert">
{error}
<button onClick={load}>Retry</button>
</div>
)}
{tab !== "playlists" && (
<div className="backup-legend" aria-label="Cloud backup symbols">
<BackupStatus status="replicated" showLabel />
<BackupStatus status="pending" showLabel />
<BackupStatus status="failed" showLabel />
</div>
)}
{tab === "playlists" ? (
<>
<div className="collection-toolbar">
<span className="muted">Made by you, for any moment.</span>
<button
className="button secondary small"
onClick={() => setCreate(true)}
>
<Plus size={15} /> New playlist
</button>
</div>
<div className="album-grid">
{playlists.map((p) => (
<Link
className="album-card"
href={"/playlist/" + p.id}
key={p.id}
>
<Artwork id={p.id} />
<h3>{p.title}</h3>
<p>{p.track_ids.length} songs</p>
</Link>
))}
</div>
{!playlists.length && (
<div className="empty-state">
<Folder />
<h3>Every mood deserves a playlist</h3>
<p>Collect the songs that belong together.</p>
<button
className="button secondary"
onClick={() => setCreate(true)}
>
Create a playlist
</button>
</div>
)}
</>
) : (
<>
<div className="collection-toolbar">
<label className="search-field">
<Search size={16} />
<input
placeholder="Find something in your collection"
aria-label="Filter collection"
value={query}
onChange={(e) => setQuery(e.target.value)}
/>
</label>
<div className="toolbar-actions">
<select
aria-label="Sort songs"
value={sort}
onChange={(e) => setSort(e.target.value)}
>
<option value="recent">Recently added</option>
<option value="title">Song title</option>
<option value="artist">Artist</option>
</select>
<button
className="icon-button"
aria-label={grid ? "Switch to list" : "Switch to artwork grid"}
onClick={() => setGrid(!grid)}
>
{grid ? <List size={18} /> : <LayoutGrid size={18} />}
</button>
</div>
</div>
{loading ? (
<div className="loading-block">
<span className="spinner" /> Opening your collection…
</div>
) : filtered.length ? (
grid ? (
<div className="album-grid">
{filtered.map((t) => (
<button
className="album-card"
key={t.id}
onClick={() => usePlayerStore.getState().start(t, filtered)}
>
<Artwork
hasArtwork={t.has_artwork}
id={t.id}
title={t.title}
/>
<h3 className="song-title">
<span>{t.title}</span>
<BackupStatus id={t.id} status={t.backup_status} />
</h3>
<p>{t.artist_name}</p>
</button>
))}
</div>
) : (
<TrackList tracks={filtered} onChange={load} />
)
) : (
<div className="empty-state">
<Music2 />
<h3>
{query
? "Nothing here just yet"
: tab === "liked"
? "Keep your favorites close"
: "A little space for your songs"}
</h3>
<p>
{query
? "Try another title or artist."
: tab === "liked"
? "Tap the heart beside any song to find it here."
: "Import music to make this collection your own."}
</p>
<Link className="text-link" href="/uploads">
Import music <ArrowUpRight size={16} />
</Link>
</div>
)}
</>
)}
{create && (
<div className="modal-backdrop">
<form
className="modal-card"
role="dialog"
aria-modal="true"
aria-label="Create playlist"
onSubmit={async (e) => {
e.preventDefault();
setSaving(true);
try {
await api.createPlaylist(name);
setCreate(false);
setName("");
load();
} catch (e) {
setError((e as Error).message);
setCreate(false);
} finally {
setSaving(false);
}
}}
>
<button
type="button"
className="close-button"
aria-label="Close"
onClick={() => setCreate(false)}
>
×
</button>
<span className="eyebrow">COLLECT A FEELING</span>
<h2>Name your playlist</h2>
<label className="field">
Playlist name
<input
required
autoFocus
maxLength={100}
placeholder="Sunday, unhurried"
value={name}
onChange={(e) => setName(e.target.value)}
/>
</label>
<button className="button" disabled={saving || !name.trim()}>
{saving ? "Creating…" : "Create playlist"}
</button>
</form>
</div>
)}
</div>
);
}