Repository Public
1"use client";2import Link from "next/link";3import { usePathname } from "next/navigation";4import { useCallback, useEffect, useState, type ReactNode } from "react";5import { setCsrf } from "@/lib/client-api";6import type { Me } from "@/lib/types";7import { I18nProvider, useT } from "@/i18n";8import type { Key } from "@/i18n/messages";9import { PlayerProvider, useCurrentTrack, usePlayerActions } from "./player";10import { NowPlaying, PlayerBar, type PlayerTab } from "./player-ui";11import { MeProvider, ToastProvider, TrackMenuProvider } from "./ui";12import { IGear, IHeart, IHome, ILibrary, IList, ILogo, ISearch, IUser } from "./icons";1314const NAV: { href: string; key: Key; icon: (p: { size?: number }) => ReactNode; match: RegExp }[] = [15 { href: "/", key: "nav.listen", icon: IHome, match: /^\/$/ },16 { href: "/recherche", key: "nav.search", icon: ISearch, match: /^\/recherche/ },17 { href: "/bibliotheque", key: "nav.library", icon: ILibrary, match: /^\/(bibliotheque|album|artiste)/ },18 { href: "/listes", key: "nav.playlists", icon: IList, match: /^\/listes/ },19 { href: "/favoris", key: "nav.favorites", icon: IHeart, match: /^\/favoris/ },20];2122function Chrome({ me, children }: { me: Me; children: ReactNode }) {23 const t = useT();24 const path = usePathname();25 const track = useCurrentTrack();26 const actions = usePlayerActions();27 const [sheet, setSheet] = useState<{ open: boolean; tab: PlayerTab }>({ open: false, tab: "now" });28 const close = useCallback(() => setSheet((s) => ({ ...s, open: false })), []);29 useEffect(() => { close(); }, [path, close]);30 // Space bar toggles playback when no control or field has the focus.31 useEffect(() => {32 const onKey = (e: KeyboardEvent) => {33 const el = e.target as HTMLElement;34 if (e.code !== "Space" || e.metaKey || e.ctrlKey || e.altKey || /^(INPUT|TEXTAREA|SELECT|BUTTON|A)$/.test(el.tagName) || el.isContentEditable) return;35 e.preventDefault(); actions.toggle();36 };37 window.addEventListener("keydown", onKey);38 return () => window.removeEventListener("keydown", onKey);39 }, [actions]);40 const hasPlayer = !!track;41 return (42 <div className="min-h-dvh" style={{ "--toast-b": hasPlayer ? "150px" : "80px" } as React.CSSProperties}>43 <a href="#main" className="sr-only focus:not-sr-only focus:fixed focus:left-3 focus:top-3 focus:z-[90] focus:rounded-lg focus:bg-ink focus:px-3 focus:py-2 focus:text-bg">{t("nav.skip")}</a>4445 {/* Desktop sidebar */}46 <aside className="fixed inset-y-0 left-0 z-30 hidden flex-col border-r border-line bg-surface lg:flex" style={{ width: "var(--sidebar-w)", paddingBottom: hasPlayer ? "var(--playerbar-h)" : 0 }}>47 <Link href="/" className="flex items-center gap-2.5 px-5 py-5"><ILogo size={30} /><span className="text-lg font-extrabold tracking-tight">Music <span className="brand-text">SP</span></span></Link>48 <nav className="flex-1 space-y-0.5 px-3" aria-label={t("nav.main")}>49 {NAV.map((n) => { const on = n.match.test(path); return (50 <Link key={n.href} href={n.href} aria-current={on ? "page" : undefined} className={`flex items-center gap-3 rounded-xl px-3 py-2.5 text-sm font-semibold ${on ? "bg-raised text-accent" : "text-muted hover:bg-hover hover:text-ink"}`}><n.icon size={20} />{t(n.key)}</Link>); })}51 </nav>52 <div className="space-y-0.5 border-t border-line p-3">53 {me.role === "admin" && <Link href="/admin" aria-current={path.startsWith("/admin") ? "page" : undefined} className={`flex items-center gap-3 rounded-xl px-3 py-2.5 text-sm font-semibold ${path.startsWith("/admin") ? "bg-raised text-accent" : "text-muted hover:bg-hover hover:text-ink"}`}><IGear size={20} />{t("nav.admin")}</Link>}54 <Link href="/compte" aria-current={path.startsWith("/compte") ? "page" : undefined} className={`flex items-center gap-3 rounded-xl px-3 py-2.5 text-sm font-semibold ${path.startsWith("/compte") ? "bg-raised text-accent" : "text-muted hover:bg-hover hover:text-ink"}`}><IUser size={20} /><span className="truncate">{me.displayName}</span></Link>55 </div>56 </aside>5758 {/* Phone header */}59 <header className="glass sticky top-0 z-30 flex items-center justify-between border-b border-line px-4 lg:hidden" style={{ paddingTop: "var(--safe-t)", height: "calc(52px + var(--safe-t))" }}>60 <Link href="/" className="flex items-center gap-2"><ILogo size={26} /><span className="text-base font-extrabold tracking-tight">Music <span className="brand-text">SP</span></span></Link>61 <div className="flex items-center">62 {me.role === "admin" && <Link href="/admin" className="icon-btn" aria-label={t("nav.admin")}><IGear size={21} /></Link>}63 <Link href="/compte" className="icon-btn" aria-label={t("nav.account")}><IUser size={21} /></Link>64 </div>65 </header>6667 <main id="main" tabIndex={-1} className="mx-auto max-w-[1400px] px-4 pt-5 outline-none sm:px-6 lg:pt-8"68 style={{ paddingBottom: `calc(${hasPlayer ? "var(--mini-h) + 28px + " : "12px + "}var(--tabbar-h) + var(--safe-b))` }}>69 <div className="lg:pl-[var(--sidebar-w)]"><div className="lg:pl-4">{children}</div></div>70 <div className="hidden lg:block" style={{ height: hasPlayer ? "var(--playerbar-h)" : 0 }} />71 </main>7273 <PlayerBar onOpen={(tab) => setSheet({ open: true, tab })} />74 <NowPlaying open={sheet.open} tab={sheet.tab} onTab={(tab) => setSheet({ open: true, tab })} onClose={close} />7576 {/* Phone tab bar */}77 <nav className="glass fixed inset-x-0 bottom-0 z-40 grid grid-cols-5 border-t border-line lg:hidden" style={{ height: "calc(var(--tabbar-h) + var(--safe-b))", paddingBottom: "var(--safe-b)" }} aria-label={t("nav.main")}>78 {NAV.map((n) => { const on = n.match.test(path); return (79 <Link key={n.href} href={n.href} aria-current={on ? "page" : undefined} className={`flex flex-col items-center justify-center gap-0.5 text-[0.66rem] font-semibold ${on ? "text-accent" : "text-muted"}`}><n.icon size={22} /><span>{t(n.key)}</span></Link>); })}80 </nav>81 </div>82 );83}8485export function AppShell({ me, csrfToken, children }: { me: Me; csrfToken: string; children: ReactNode }) {86 setCsrf(csrfToken);87 return (88 <I18nProvider locale={me.locale}>89 <MeProvider value={me}>90 <ToastProvider>91 <PlayerProvider>92 <TrackMenuProvider>93 <Chrome me={me}>{children}</Chrome>94 </TrackMenuProvider>95 </PlayerProvider>96 </ToastProvider>97 </MeProvider>98 </I18nProvider>99 );100}101