Repository Public
1"use client";2import { useEffect, useState } from "react";3import Link from "next/link";4import { usePathname, useRouter } from "next/navigation";5import { CalendarClock, Clapperboard, Globe2, HardDrive, History, Home, Layers, ListVideo, LogOut, Menu, Search, Settings, SlidersHorizontal, Star, Trophy, Tv, X } from "lucide-react";6import { api, cn } from "@/lib/client";7import { PlayerProvider, usePlayer } from "./player";8import { VodProvider } from "./vod-player";910const NAV = [11 { href: "/", label: "Accueil", icon: Home },12 { href: "/chaines", label: "TV en direct", short: "Direct", icon: Tv },13 { href: "/sports", label: "Sports", icon: Trophy },14 { href: "/films", label: "Films", icon: Clapperboard },15 { href: "/series", label: "Séries", icon: Layers },16 { href: "/documentaires", label: "Documentaires", icon: Globe2 },17 { href: "/bibliotheque", label: "Ma bibliothèque", icon: HardDrive },18];19const NAV2 = [20 { href: "/favoris", label: "Favoris", icon: Star },21 { href: "/continuer", label: "Continuer à regarder", icon: History },22 { href: "/guide", label: "Guide TV", icon: CalendarClock },23 { href: "/playlists", label: "Playlists M3U", icon: ListVideo },24 { href: "/recherche", label: "Recherche", icon: Search },25 { href: "/parametres", label: "Paramètres", icon: SlidersHorizontal },26 { href: "/admin", label: "Administration", icon: Settings },27];28const TABS = [NAV[0], NAV[1], NAV[3], NAV[4]];2930const FOCUSABLE = 'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea, [tabindex="0"]';3132/** Directional focus for remotes and keyboards: arrows move to the nearest control in that direction. */33function useSpatialNav() {34 const router = useRouter();35 useEffect(() => {36 const onKey = (e: KeyboardEvent) => {37 if (e.defaultPrevented || e.altKey || e.ctrlKey || e.metaKey) return;38 const dir = ({ ArrowLeft: "l", ArrowRight: "r", ArrowUp: "u", ArrowDown: "d" } as Record<string, "l" | "r" | "u" | "d">)[e.key];39 const el = document.activeElement as HTMLElement | null;40 const tag = el?.tagName;41 if (!dir) {42 // Back key of TV remotes outside the player: previous page.43 if ((e.key === "BrowserBack" || e.key === "GoBack") && !document.querySelector('[data-player="full"]')) { e.preventDefault(); router.back(); }44 return;45 }46 if (tag === "TEXTAREA" || (tag === "INPUT" && (el as HTMLInputElement).type === "range")) return;47 if (tag === "SELECT" && (dir === "u" || dir === "d")) return; // ↑/↓ change the value; ←/→ move on48 if (tag === "INPUT" && (dir === "l" || dir === "r") && (el as HTMLInputElement).value) return;49 const scope: ParentNode = document.querySelector('[data-player="full"]') ?? document;50 const all = [...scope.querySelectorAll<HTMLElement>(FOCUSABLE)].filter((c) => { if (c === el || c.closest("[aria-hidden='true'],[hidden]")) return false; const r = c.getBoundingClientRect(); return r.width > 0 && r.height > 0; });51 if (!all.length) return;52 e.preventDefault();53 if (!el || el === document.body || !scope.contains(el)) { (scope.querySelector<HTMLElement>("main [data-nav]") ?? all[0]).focus(); return; }54 const a = el.getBoundingClientRect();55 let best: HTMLElement | null = null, bestScore = Infinity;56 for (const c of all) {57 const b = c.getBoundingClientRect();58 const horizontal = dir === "l" || dir === "r";59 const ahead = dir === "r" ? b.left - a.right : dir === "l" ? a.left - b.right : dir === "d" ? b.top - a.bottom : a.top - b.bottom;60 if (ahead < -Math.min(horizontal ? a.width : a.height, horizontal ? b.width : b.height) / 2 - 1) continue;61 const centre = horizontal ? Math.abs(b.left + b.width / 2 - (a.left + a.width / 2)) : Math.abs(b.top + b.height / 2 - (a.top + a.height / 2));62 if (centre < 2 && ahead < 0) continue;63 const overlap = horizontal ? Math.min(a.bottom, b.bottom) - Math.max(a.top, b.top) : Math.min(a.right, b.right) - Math.max(a.left, b.left);64 const off = overlap > 0 ? 0 : horizontal ? Math.min(Math.abs(b.top - a.bottom), Math.abs(a.top - b.bottom)) : Math.min(Math.abs(b.left - a.right), Math.abs(a.left - b.right));65 if (ahead < 0 && overlap > 0) continue; // same band, not actually further in that direction66 const score = Math.max(0, ahead) + off * 4 + (overlap > 0 ? 0 : 60);67 if (score < bestScore) { bestScore = score; best = c; }68 }69 if (best) { best.focus({ preventScroll: true }); best.scrollIntoView({ block: "nearest", inline: "nearest", behavior: "smooth" }); }70 };71 addEventListener("keydown", onKey); return () => removeEventListener("keydown", onKey);72 }, [router]);73}7475function Vod({ children }: { children: React.ReactNode }) {76 const { mode, close } = usePlayer();77 return <VodProvider onStart={() => { if (mode !== "closed") close(); }}>{children}</VodProvider>;78}7980export function Shell({ children }: { children: React.ReactNode }) {81 const path = usePathname();82 const [more, setMore] = useState(false);83 useSpatialNav();84 useEffect(() => setMore(false), [path]);85 const active = (href: string) => (href === "/" ? path === "/" : path.startsWith(href));86 const logout = async () => { await api("/auth/logout", { method: "POST" }).catch(() => {}); location.href = "/connexion"; };87 const item = (n: { href: string; label: string; icon: typeof Home }) => (88 <Link key={n.href} href={n.href} aria-current={active(n.href) ? "page" : undefined} className={cn("flex h-10 items-center gap-3 rounded-xl px-3 text-[14.5px] font-medium text-dim transition hover:bg-white/5 hover:text-ink", active(n.href) && "bg-white/[0.09] text-ink")}>89 <n.icon strokeWidth={active(n.href) ? 2.2 : 1.8} className="h-[1.15rem] w-[1.15rem]" />{n.label}90 </Link>91 );92 return (93 <PlayerProvider>94 <Vod>95 <div className="lg:pl-64">96 <aside className="fixed inset-y-0 left-0 z-30 hidden w-64 flex-col border-r border-line bg-bg/80 p-4 backdrop-blur-xl lg:flex">97 <Link href="/" className="mb-6 flex items-center gap-3 rounded-xl px-2 py-2"><img src="/icons/icon.svg" alt="" className="h-9 w-9 rounded-[0.7rem]" /><span className="text-[1.15rem] font-semibold tracking-tight">SP <span className="grad-text">TV</span></span></Link>98 <nav className="flex flex-1 flex-col gap-0.5 overflow-y-auto" aria-label="Navigation principale">99 {NAV.map(item)}100 <span className="my-3 border-t border-line" />101 {NAV2.map(item)}102 </nav>103 <button onClick={logout} className="mt-2 flex h-10 items-center gap-3 rounded-xl px-3 text-[14.5px] font-medium text-faint hover:bg-white/5 hover:text-ink"><LogOut strokeWidth={1.8} className="h-[1.15rem] w-[1.15rem]" />Se déconnecter</button>104 </aside>105106 {/* phone / tablet top bar */}107 <header className="glass sticky top-0 z-30 flex h-14 items-center gap-2 border-b border-line px-4 pt-[env(safe-area-inset-top)] lg:hidden" style={{ height: "calc(3.25rem + env(safe-area-inset-top))" }}>108 <Link href="/" className="flex flex-1 items-center gap-2.5"><img src="/icons/icon.svg" alt="" className="h-7 w-7 rounded-[0.55rem]" /><span className="text-[1.05rem] font-semibold tracking-tight">SP <span className="grad-text">TV</span></span></Link>109 <Link href="/recherche" aria-label="Recherche" className="grid h-10 w-10 place-items-center rounded-full bg-white/[0.07] active:bg-white/15"><Search strokeWidth={2} className="h-[1.1rem] w-[1.1rem]" /></Link>110 </header>111112 <main className="min-h-dvh pb-[calc(6.75rem+env(safe-area-inset-bottom))] lg:pb-10">{children}</main>113114 <nav aria-label="Navigation principale" className="pointer-events-none fixed inset-x-0 bottom-0 z-30 px-3 pb-[max(0.6rem,env(safe-area-inset-bottom))] lg:hidden">115 <div className="tabbar pointer-events-auto mx-auto flex max-w-md gap-0.5 rounded-[1.6rem] p-1.5">116 {TABS.map((n) => (117 <Link key={n.href} href={n.href} aria-current={active(n.href) ? "page" : undefined} className={cn("flex h-[3.25rem] flex-1 flex-col items-center justify-center gap-[3px] rounded-[1.2rem] text-[10.5px] font-medium text-faint transition-colors", active(n.href) && "bg-white/[0.11] text-ink")}>118 <n.icon strokeWidth={active(n.href) ? 2.3 : 1.8} className="h-[1.35rem] w-[1.35rem]" />{"short" in n && n.short ? n.short : n.label}119 </Link>120 ))}121 <button onClick={() => setMore(true)} aria-haspopup="dialog" aria-expanded={more} className={cn("flex h-[3.25rem] flex-1 flex-col items-center justify-center gap-[3px] rounded-[1.2rem] text-[10.5px] font-medium text-faint transition-colors", [NAV[2], ...NAV.slice(5), ...NAV2].some((n) => active(n.href)) && "bg-white/[0.11] text-ink")}><Menu strokeWidth={1.8} className="h-[1.35rem] w-[1.35rem]" />Plus</button>122 </div>123 </nav>124125 {more && (126 <div className="fixed inset-0 z-40 flex items-end bg-black/60 lg:hidden" onClick={() => setMore(false)}>127 <div role="dialog" aria-label="Plus" className="sheet glass w-full rounded-t-[1.75rem] p-3 pb-[max(1rem,env(safe-area-inset-bottom))] ring-1 ring-line" onClick={(e) => e.stopPropagation()}>128 <div className="mx-auto mb-2 h-1 w-9 rounded-full bg-white/20" aria-hidden />129 <div className="mb-1 flex items-center justify-between px-3 py-1"><h2 className="text-base font-semibold">Plus</h2><button aria-label="Fermer" onClick={() => setMore(false)} className="grid h-10 w-10 place-items-center rounded-full bg-white/10"><X className="h-5 w-5" /></button></div>130 <div className="grid grid-cols-2 gap-2">131 {[NAV[2], ...NAV.slice(5), ...NAV2].map((n) => (132 <Link key={n.href} href={n.href} className={cn("flex h-[3.25rem] items-center gap-3 rounded-2xl bg-white/[0.06] px-4 text-sm font-medium active:bg-white/15", active(n.href) && "bg-white/15")}><n.icon strokeWidth={1.8} className="h-5 w-5 text-dim" />{n.label}</Link>133 ))}134 <button onClick={logout} className="flex h-[3.25rem] items-center gap-3 rounded-2xl bg-white/[0.06] px-4 text-sm font-medium text-dim"><LogOut strokeWidth={1.8} className="h-5 w-5" />Se déconnecter</button>135 </div>136 </div>137 </div>138 )}139 </div>140 </Vod>141 </PlayerProvider>142 );143}144