Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Music SP8 * File : src/components/player-ui.tsx9 * Module : components10 * 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 Link from "next/link";45import { useEffect, useState } from "react";46import { audioInfo, fmtTime } from "@/lib/format";47import { useLocale, useT } from "@/i18n";48import type { Key } from "@/i18n/messages";49import { useCurrentTrack, usePlayer, usePlayerActions, useProgress, type PlayerError } from "./player";50import { Artwork, FavButton, NowBars, useFavorite } from "./ui";51import { LyricsView } from "./lyrics-view";52import { ILyrics } from "./icons";53import { IAlert, IChevronDown, IClose, IDown, INext, IPause, IPlay, IPrev, IQueue, IRepeat, IRepeatOne, IShuffle, ISpinner, IUp, IVolume } from "./icons";5455export type PlayerTab = "now" | "lyrics" | "queue";56const ERR: Record<Exclude<PlayerError, null>, Key> = { network: "player.err.network", decode: "player.err.decode", unsupported: "player.err.unsupported", unavailable: "player.err.unavailable", preparing_failed: "player.err.preparing" };5758function Seek({ compact }: { compact?: boolean }) {59 const t = useT();60 const { positionMs, durationMs } = useProgress();61 const actions = usePlayerActions();62 const [drag, setDrag] = useState<number | null>(null);63 const pos = drag ?? positionMs;64 const max = Math.max(1, durationMs);65 const commit = () => { if (drag != null) { actions.seek(drag); setDrag(null); } };66 return (67 <div className={compact ? "flex items-center gap-2" : ""}>68 {compact && <span className="w-10 text-right text-[0.7rem] tabular-nums text-faint">{fmtTime(pos)}</span>}69 <input type="range" className="range" min={0} max={max} step={1000} value={Math.min(pos, max)} aria-label={t("player.position")} aria-valuetext={`${fmtTime(pos)} / ${fmtTime(durationMs)}`}70 style={{ "--fill": `${(Math.min(pos, max) / max) * 100}%` } as React.CSSProperties} disabled={!durationMs}71 onChange={(e) => setDrag(Number(e.target.value))} onPointerUp={commit} onKeyUp={commit} onBlur={commit} />72 {compact ? <span className="w-10 text-[0.7rem] tabular-nums text-faint">{fmtTime(durationMs)}</span>73 : <div className="-mt-1 flex justify-between text-[0.72rem] tabular-nums text-faint"><span>{fmtTime(pos)}</span><span>−{fmtTime(Math.max(0, durationMs - pos))}</span></div>}74 </div>75 );76}7778function PlayPause({ size = 22, big }: { size?: number; big?: boolean }) {79 const t = useT(); const s = usePlayer(); const a = usePlayerActions();80 const busy = s.loading || s.preparing;81 return (82 <button onClick={a.toggle} aria-label={t(s.playing ? "player.pause" : "player.play")} disabled={s.preparing}83 className={big ? "flex h-16 w-16 flex-none items-center justify-center rounded-full bg-ink text-bg transition-transform active:scale-95" : "icon-btn"}>84 {busy && !s.playing ? <ISpinner size={size} /> : s.playing ? <IPause size={size} /> : <IPlay size={size} />}85 </button>86 );87}8889function ModeButtons() {90 const t = useT(); const s = usePlayer(); const a = usePlayerActions();91 return (<>92 <button className="icon-btn" aria-pressed={s.shuffle} aria-label={t("player.shuffleMode")} title={t("player.shuffleMode")} onClick={() => a.setShuffle(!s.shuffle)}><IShuffle size={20} /></button>93 <button className="icon-btn" aria-pressed={s.repeat !== "off"} aria-label={t(s.repeat === "one" ? "player.repeatOne" : s.repeat === "all" ? "player.repeatAll" : "player.repeatOff")} title={t("player.repeat")} onClick={a.cycleRepeat}>{s.repeat === "one" ? <IRepeatOne size={20} /> : <IRepeat size={20} />}</button>94 </>);95}9697function Volume() {98 const t = useT(); const s = usePlayer(); const a = usePlayerActions();99 if (!s.canSetVolume) return null;100 return (101 <div className="flex items-center gap-2 text-muted">102 <IVolume size={18} />103 <input type="range" className="range w-24" min={0} max={1} step={0.02} value={s.volume} aria-label={t("player.volume")} style={{ "--fill": `${s.volume * 100}%` } as React.CSSProperties} onChange={(e) => a.setVolume(Number(e.target.value))} />104 </div>105 );106}107108function ErrorLine() {109 const t = useT(); const s = usePlayer(); const a = usePlayerActions();110 if (s.preparing) return <p className="flex items-center justify-center gap-2 text-xs text-warn" role="status"><ISpinner size={14} />{t("player.preparing")}</p>;111 if (!s.error) return null;112 return (113 <p className="flex flex-wrap items-center justify-center gap-2 text-xs text-danger" role="alert">114 <IAlert size={14} /><span>{t(ERR[s.error])}</span>115 {s.error !== "unsupported" && <button className="font-bold underline" onClick={a.retry}>{t("common.retry")}</button>}116 </p>117 );118}119120/** Compact bar: phone mini-player (above the tab bar) and desktop player bar (full width, bottom). */121export function PlayerBar({ onOpen }: { onOpen: (tab: PlayerTab) => void }) {122 const t = useT(); const locale = useLocale();123 const s = usePlayer(); const a = usePlayerActions(); const tr = useCurrentTrack();124 const { positionMs, durationMs } = useProgress();125 const fav = useFavorite();126 if (!tr) return null;127 const pct = durationMs ? Math.min(100, (positionMs / durationMs) * 100) : 0;128 return (129 <>130 {/* Phone */}131 <div className="glass fixed inset-x-2 z-40 overflow-hidden rounded-2xl border border-line shadow-2xl lg:hidden" style={{ bottom: "calc(var(--tabbar-h) + var(--safe-b) + 8px)", height: "var(--mini-h)" }} role="region" aria-label={t("player.region")}>132 <div className="flex h-full items-center gap-3 pl-2 pr-1">133 <button className="flex min-w-0 flex-1 items-center gap-3 text-left" onClick={() => onOpen("now")} aria-label={t("player.openNow")}>134 <Artwork artworkId={tr.album.artworkId} seed={tr.album.title} size={96} className="w-11 flex-none" rounded="rounded-lg" priority />135 <span className="min-w-0">136 <span className="block truncate text-sm font-semibold">{tr.title}</span>137 <span className={`block truncate text-xs ${s.error ? "text-danger" : "text-muted"}`}>{s.preparing ? t("player.preparing") : s.error ? t(ERR[s.error]) : tr.artist.name}</span>138 </span>139 </button>140 <PlayPause />141 <button className="icon-btn" onClick={a.next} aria-label={t("player.next")}><INext size={20} /></button>142 </div>143 <div className="absolute inset-x-0 bottom-0 h-[2px] bg-line" aria-hidden><div className="h-full bg-accent" style={{ width: `${pct}%` }} /></div>144 </div>145 {/* Desktop */}146 <div className="glass fixed inset-x-0 bottom-0 z-40 hidden border-t border-line lg:block" style={{ height: "var(--playerbar-h)" }} role="region" aria-label={t("player.region")}>147 <div className="grid h-full grid-cols-[minmax(0,1fr)_minmax(0,1.4fr)_minmax(0,1fr)] items-center gap-4 px-5">148 <div className="flex min-w-0 items-center gap-3">149 <button onClick={() => onOpen("now")} aria-label={t("player.openNow")} className="flex-none"><Artwork artworkId={tr.album.artworkId} seed={tr.album.title} size={96} className="w-14" rounded="rounded-lg" priority /></button>150 <div className="min-w-0">151 <div className="truncate text-sm font-semibold">{tr.title}</div>152 <div className="truncate text-xs text-muted"><Link href={`/artiste/${tr.artist.id}`} className="hover:underline">{tr.artist.name}</Link> — <Link href={`/album/${tr.album.id}`} className="hover:underline">{tr.album.title}</Link></div>153 <div className="truncate text-[0.68rem] text-faint">{audioInfo(tr, locale)}{s.variant === "compat" ? ` → ${t("player.compat")}` : ""}</div>154 </div>155 <FavButton on={tr.favorite} size={18} label={t(tr.favorite ? "fav.remove" : "fav.add")} onToggle={async () => { const on = !tr.favorite; a.patchTrack(tr.id, { favorite: on }); if (!(await fav("track", tr.id, on))) a.patchTrack(tr.id, { favorite: !on }); }} />156 </div>157 <div>158 <div className="flex items-center justify-center gap-2">159 <ModeButtons />160 <button className="icon-btn" onClick={a.prev} aria-label={t("player.prev")}><IPrev size={20} /></button>161 <button onClick={a.toggle} disabled={s.preparing} aria-label={t(s.playing ? "player.pause" : "player.play")} className="flex h-10 w-10 items-center justify-center rounded-full bg-ink text-bg transition-transform active:scale-95">162 {(s.loading || s.preparing) && !s.playing ? <ISpinner size={18} /> : s.playing ? <IPause size={18} /> : <IPlay size={18} />}</button>163 <button className="icon-btn" onClick={a.next} aria-label={t("player.next")}><INext size={20} /></button>164 </div>165 {s.error || s.preparing ? <ErrorLine /> : <Seek compact />}166 </div>167 <div className="flex items-center justify-end gap-2">168 <Volume />169 <button className="icon-btn" onClick={() => onOpen("lyrics")} aria-label={t("lyrics.title")} title={t("lyrics.title")}><ILyrics size={20} /></button>170 <button className="icon-btn" onClick={() => onOpen("queue")} aria-label={t("queue.title")} title={t("queue.title")}><IQueue size={20} /></button>171 </div>172 </div>173 </div>174 </>175 );176}177178function QueueList() {179 const t = useT(); const s = usePlayer(); const a = usePlayerActions();180 const upcoming = s.queue.length - s.index - 1;181 return (182 <div>183 <div className="mb-2 flex items-center justify-between">184 <h3 className="text-sm font-bold uppercase tracking-wider text-faint">{t("queue.upNext")} · {upcoming}</h3>185 {upcoming > 0 && <button className="btn btn-sm" onClick={a.clearUpcoming}>{t("queue.clear")}</button>}186 </div>187 {s.queue.length === 0 ? <p className="py-6 text-center text-sm text-muted">{t("queue.empty")}</p> : (188 <ol>189 {s.queue.map((tr, i) => {190 const cur = i === s.index;191 return (192 <li key={`${tr.id}-${i}`} className={`flex items-center gap-2 rounded-xl px-1 py-1.5 ${cur ? "bg-raised" : i < s.index ? "opacity-50" : ""}`}>193 <button className="flex min-w-0 flex-1 items-center gap-3 text-left" onClick={() => (cur ? a.toggle() : a.jumpTo(i))} aria-current={cur ? "true" : undefined} aria-label={t("player.playTrack", { title: tr.title })}>194 <span className="relative w-10 flex-none"><Artwork artworkId={tr.album.artworkId} seed={tr.album.title} size={96} rounded="rounded-md" />195 {cur && <span className="absolute inset-0 flex items-center justify-center rounded-md bg-black/55"><NowBars paused={!s.playing} /></span>}</span>196 <span className="min-w-0"><span className={`block truncate text-sm font-medium ${cur ? "text-accent" : ""}`}>{tr.title}</span><span className="block truncate text-xs text-muted">{tr.artist.name}</span></span>197 </button>198 {i > s.index && <>199 <button className="icon-btn !h-8 !w-8 text-muted" disabled={i <= s.index + 1} onClick={() => a.move(i, i - 1)} aria-label={t("queue.moveUp", { title: tr.title })}><IUp size={16} /></button>200 <button className="icon-btn !h-8 !w-8 text-muted" disabled={i >= s.queue.length - 1} onClick={() => a.move(i, i + 1)} aria-label={t("queue.moveDown", { title: tr.title })}><IDown size={16} /></button>201 </>}202 {!cur && <button className="icon-btn !h-8 !w-8 text-muted" onClick={() => a.removeAt(i)} aria-label={t("queue.remove", { title: tr.title })}><IClose size={16} /></button>}203 </li>204 );205 })}206 </ol>207 )}208 </div>209 );210}211212/** Full-screen "now playing" with the queue as a second tab. */213export function NowPlaying({ open, tab, onTab, onClose }: { open: boolean; tab: PlayerTab; onTab: (t: PlayerTab) => void; onClose: () => void }) {214 const t = useT(); const locale = useLocale();215 const s = usePlayer(); const a = usePlayerActions(); const tr = useCurrentTrack();216 const fav = useFavorite();217 useEffect(() => {218 if (!open) return;219 const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") onClose(); };220 document.addEventListener("keydown", onKey);221 const o = document.body.style.overflow; document.body.style.overflow = "hidden";222 return () => { document.removeEventListener("keydown", onKey); document.body.style.overflow = o; };223 }, [open, onClose]);224 if (!open || !tr) return null;225 return (226 <div className="sheet-up fixed inset-0 z-[60] flex flex-col bg-bg" role="dialog" aria-modal="true" aria-label={t("player.nowPlaying")}>227 <div className="pointer-events-none absolute inset-0 opacity-40" aria-hidden style={{ background: "radial-gradient(120% 70% at 50% 0%, color-mix(in srgb, var(--accent) 38%, transparent), transparent 70%)" }} />228 <div className="relative flex items-center justify-between px-3 pb-1" style={{ paddingTop: "calc(var(--safe-t) + 10px)" }}>229 <button className="icon-btn" onClick={onClose} aria-label={t("common.close")} autoFocus><IChevronDown size={24} /></button>230 <div className="flex rounded-full border border-line bg-surface p-1" role="tablist">231 {(["now", "lyrics", "queue"] as const).map((k) => <button key={k} role="tab" aria-selected={tab === k} onClick={() => onTab(k)} className={`rounded-full px-3.5 py-1.5 text-xs font-bold ${tab === k ? "bg-ink text-bg" : "text-muted"}`}>{t(k === "now" ? "player.nowShort" : k === "lyrics" ? "lyrics.title" : "queue.title")}</button>)}232 </div>233 <span className="w-10" />234 </div>235 {tab === "queue" ? (236 <div className="relative mx-auto w-full max-w-xl flex-1 overflow-y-auto px-4 py-3" style={{ paddingBottom: "calc(1rem + var(--safe-b))" }}><QueueList /></div>237 ) : tab === "lyrics" ? (238 <div className="relative mx-auto flex min-h-0 w-full max-w-2xl flex-1 flex-col px-4">239 <div className="min-h-0 flex-1"><LyricsView track={tr} /></div>240 {/* A compact transport stays under the lyrics. */}241 <div className="glass -mx-4 border-t border-line px-5 pt-2" style={{ paddingBottom: "calc(0.6rem + var(--safe-b))" }}>242 <p className="truncate text-center text-xs text-muted"><span className="font-semibold text-ink">{tr.title}</span> — {tr.artist.name}</p>243 <Seek compact />244 <div className="flex items-center justify-center gap-4">245 <button className="icon-btn" onClick={a.prev} aria-label={t("player.prev")}><IPrev size={22} /></button>246 <PlayPause size={24} />247 <button className="icon-btn" onClick={a.next} aria-label={t("player.next")}><INext size={22} /></button>248 </div>249 </div>250 </div>251 ) : (252 <div className="relative mx-auto flex w-full max-w-md flex-1 flex-col justify-between gap-4 overflow-y-auto px-7 pt-3" style={{ paddingBottom: "calc(1.25rem + var(--safe-b))" }}>253 <div className="mx-auto w-full max-w-[min(100%,44dvh)]"><Artwork artworkId={tr.album.artworkId} seed={tr.album.title} size={640} rounded="rounded-2xl" className="shadow-2xl shadow-black/60" priority /></div>254 <div>255 <div className="flex items-start justify-between gap-3">256 <div className="min-w-0">257 <h2 className="truncate text-xl font-extrabold tracking-tight">{tr.title}</h2>258 <p className="truncate text-muted"><Link href={`/artiste/${tr.artist.id}`} onClick={onClose} className="hover:underline">{tr.artist.name}</Link> — <Link href={`/album/${tr.album.id}`} onClick={onClose} className="hover:underline">{tr.album.title}</Link></p>259 </div>260 <FavButton on={tr.favorite} size={24} label={t(tr.favorite ? "fav.remove" : "fav.add")} onToggle={async () => { const on = !tr.favorite; a.patchTrack(tr.id, { favorite: on }); if (!(await fav("track", tr.id, on))) a.patchTrack(tr.id, { favorite: !on }); }} />261 </div>262 <div className="mt-3"><Seek /></div>263 <div className="mt-1 min-h-5"><ErrorLine /></div>264 <div className="mt-2 flex items-center justify-between">265 <button className="icon-btn" aria-pressed={s.shuffle} aria-label={t("player.shuffleMode")} onClick={() => a.setShuffle(!s.shuffle)}><IShuffle size={22} /></button>266 <button className="icon-btn !h-14 !w-14" onClick={a.prev} aria-label={t("player.prev")}><IPrev size={30} /></button>267 <PlayPause big size={28} />268 <button className="icon-btn !h-14 !w-14" onClick={a.next} aria-label={t("player.next")}><INext size={30} /></button>269 <button className="icon-btn" aria-pressed={s.repeat !== "off"} aria-label={t(s.repeat === "one" ? "player.repeatOne" : s.repeat === "all" ? "player.repeatAll" : "player.repeatOff")} onClick={a.cycleRepeat}>{s.repeat === "one" ? <IRepeatOne size={22} /> : <IRepeat size={22} />}</button>270 </div>271 <div className="mt-3 flex items-center justify-between gap-3">272 <span className="chip">{audioInfo(tr, locale)}{s.variant === "compat" ? ` → ${t("player.compat")}` : ""}</span>273 <Volume />274 <span className="flex"><button className="icon-btn" onClick={() => onTab("lyrics")} aria-label={t("lyrics.title")}><ILyrics size={22} /></button>275 <button className="icon-btn" onClick={() => onTab("queue")} aria-label={t("queue.title")}><IQueue size={22} /></button></span>276 </div>277 </div>278 </div>279 )}280 </div>281 );282}283