SPB Git

Repository Public

page.tsx 5.7 KB 109 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Music SP8 * File             : src/app/(app)/bibliotheque/page.tsx9 * Module           : app10 * Repository       : https://git.spboucher.ai/sp-music11 * Project Website  : https://music.spboucher.ai12 * Created          : 2026-11-1713 * Last Modified    : 2026-11-1714 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243"use client";44import { useRouter, useSearchParams } from "next/navigation";45import { Suspense, useEffect, useState } from "react";46import { api } from "@/lib/client-api";47import type { Album, Artist, Paged, Track } from "@/lib/types";48import { useT } from "@/i18n";49import type { Key } from "@/i18n/messages";50import { AlbumCard, ArtistCard, Async, Empty, Grid, GridSkeleton, PageHeader, TrackList, useApi, useErrorText, useToast, type Loaded } from "@/components/ui";51import { ISpinner } from "@/components/icons";5253const TABS = ["albums", "artists", "tracks"] as const;54type Tab = (typeof TABS)[number];5556/** Appends the next page to an already loaded list. */57function More<T>({ res, path }: { res: Loaded<Paged<T>>; path: string }) {58  const t = useT(); const toast = useToast(); const text = useErrorText();59  const [busy, setBusy] = useState(false);60  const d = res.data;61  if (!d || d.items.length >= d.total) return null;62  const load = async () => {63    setBusy(true);64    try { const next = await api<Paged<T>>(`${path}${path.includes("?") ? "&" : "?"}offset=${d.items.length}`); res.setData((cur) => ({ ...cur, items: [...cur.items, ...next.items], total: next.total })); }65    catch (e) { toast(text(e), "error"); } finally { setBusy(false); }66  };67  return <div className="mt-6 flex justify-center"><button className="btn" onClick={load} disabled={busy}>{busy && <ISpinner size={16} />}{t("common.loadMore", { shown: d.items.length, total: d.total })}</button></div>;68}6970function Albums() {71  const t = useT();72  const [sort, setSort] = useState("artist");73  const path = `/api/v1/library/albums?sort=${sort}`;74  const res = useApi<Paged<Album>>(path);75  return (<>76    <div className="mb-4 flex items-center gap-2"><label htmlFor="sort" className="text-xs font-semibold text-muted">{t("lib.sort")}</label>77      <select id="sort" className="input !min-h-9 !w-auto !text-sm" value={sort} onChange={(e) => setSort(e.target.value)}>78        {(["artist", "title", "recent", "year"] as const).map((s) => <option key={s} value={s}>{t(`lib.sort.${s}` as Key)}</option>)}</select></div>79    <Async res={res} skeleton={<GridSkeleton n={12} />}>{(d) => d.items.length === 0 ? <Empty title={t("lib.empty")} /> : <Grid>{d.items.map((a) => <AlbumCard key={a.id} album={a} />)}</Grid>}</Async>80    <More res={res} path={path} />81  </>);82}83function Artists() {84  const t = useT(); const path = "/api/v1/library/artists"; const res = useApi<Paged<Artist>>(path);85  return (<><Async res={res} skeleton={<GridSkeleton n={12} />}>{(d) => d.items.length === 0 ? <Empty title={t("lib.empty")} /> : <Grid>{d.items.map((a) => <ArtistCard key={a.id} artist={a} />)}</Grid>}</Async><More res={res} path={path} /></>);86}87function Tracks() {88  const t = useT(); const path = "/api/v1/library/tracks?sort=artist"; const res = useApi<Paged<Track>>(path);89  return (<><Async res={res}>{(d) => d.items.length === 0 ? <Empty title={t("lib.empty")} /> : <TrackList tracks={d.items} />}</Async><More res={res} path={path} /></>);90}9192function Library() {93  const t = useT(); const router = useRouter(); const params = useSearchParams();94  const fromUrl = params.get("tab") as Tab | null;95  const [tab, setTab] = useState<Tab>(fromUrl && TABS.includes(fromUrl) ? fromUrl : "albums");96  useEffect(() => { if (fromUrl && TABS.includes(fromUrl)) setTab(fromUrl); }, [fromUrl]);97  const pick = (k: Tab) => { setTab(k); router.replace(`/bibliotheque?tab=${k}`, { scroll: false }); };98  return (99    <>100      <PageHeader title={t("lib.title")} />101      <div className="mb-5 inline-flex rounded-full border border-line bg-surface p-1" role="tablist" aria-label={t("lib.title")}>102        {TABS.map((k) => <button key={k} role="tab" id={`tab-${k}`} aria-selected={tab === k} aria-controls="lib-panel" onClick={() => pick(k)} className={`rounded-full px-4 py-1.5 text-sm font-bold ${tab === k ? "bg-ink text-bg" : "text-muted hover:text-ink"}`}>{t(`lib.${k}` as Key)}</button>)}103      </div>104      <div id="lib-panel" role="tabpanel" aria-labelledby={`tab-${tab}`}>{tab === "albums" ? <Albums /> : tab === "artists" ? <Artists /> : <Tracks />}</div>105    </>106  );107}108export default function LibraryPage() { return <Suspense><Library /></Suspense>; }109
Files main
Menu

Theme