diff --git a/apps/web/src/components/series/ready-carousel.tsx b/apps/web/src/components/series/ready-carousel.tsx new file mode 100644 index 0000000..0d08d1c --- /dev/null +++ b/apps/web/src/components/series/ready-carousel.tsx @@ -0,0 +1,82 @@ +import type { SeriesCard } from "@snug/shared"; +import { useEffect, useRef, useState } from "react"; +import { cx } from "@/components/ui"; +import { useT } from "@/i18n"; +import { ReadyCard } from "./ready-card"; + +/** + * Pages of two rows of cards, swiped one whole page at a time. The page on screen is read from the scroll position + * only, so it stays right when cards come and go or the screen turns; `onPage` reports it for the section's title. + */ +export function ReadyCarousel({ + cards, + columns, + onPage, +}: { + cards: SeriesCard[]; + columns: number; + onPage: (page: number, total: number) => void; +}) { + const t = useT(); + const scroller = useRef(null); + const [page, setPage] = useState(0); + const perPage = columns * 2; + const pages: SeriesCard[][] = []; + for (let i = 0; i < cards.length; i += perPage) pages.push(cards.slice(i, i + perPage)); + + const readPage = () => { + const element = scroller.current; + if (element && element.clientWidth > 0) setPage(Math.round(element.scrollLeft / element.clientWidth)); + }; + + // A resize or a page that disappears moves the scroll position without a scroll event. + useEffect(() => { + const element = scroller.current; + if (!element) return; + const observer = new ResizeObserver(readPage); + observer.observe(element); + return () => observer.disconnect(); + }, []); + useEffect(readPage, [pages.length]); + useEffect(() => onPage(page, pages.length), [onPage, page, pages.length]); + + const goTo = (index: number) => { + const element = scroller.current; + element?.scrollTo({ left: index * element.clientWidth, behavior: "smooth" }); + }; + + return ( + <> +
+ {pages.map((items, index) => ( +
+ {items.map((card) => ( + + ))} +
+ ))} +
+ {pages.length > 1 && ( +
+ {pages.map((_page, index) => ( + + ))} +
+ )} + + ); +} diff --git a/apps/web/src/components/series/ready-layout.tsx b/apps/web/src/components/series/ready-layout.tsx new file mode 100644 index 0000000..3ee457d --- /dev/null +++ b/apps/web/src/components/series/ready-layout.tsx @@ -0,0 +1,59 @@ +import { GalleryHorizontal, LayoutGrid, List } from "lucide-react"; +import { useState } from "react"; +import { cx } from "@/components/ui"; +import { useT } from "@/i18n"; + +export type ReadyLayout = "cards" | "list" | "carousel"; + +const LAYOUTS: ReadyLayout[] = ["cards", "list", "carousel"]; +const LAYOUT_KEY = "snug.readyLayout"; + +/** The last layout, per device: a list on the phone, cards on the iPad. */ +function storedLayout(): ReadyLayout { + try { + const stored = localStorage.getItem(LAYOUT_KEY); + return LAYOUTS.find((layout) => layout === stored) ?? "cards"; + } catch { + return "cards"; + } +} + +export function useReadyLayout() { + const [layout, setLayout] = useState(storedLayout); + const choose = (next: ReadyLayout) => { + setLayout(next); + try { + localStorage.setItem(LAYOUT_KEY, next); + } catch {} + }; + return [layout, choose] as const; +} + +const ICONS = { cards: LayoutGrid, list: List, carousel: GalleryHorizontal }; + +/** Three icon buttons, the current one lit: how "Ready to watch" is laid out. */ +export function LayoutSwitch({ value, onChange }: { value: ReadyLayout; onChange: (layout: ReadyLayout) => void }) { + const t = useT(); + return ( +
+ {LAYOUTS.map((layout) => { + const Icon = ICONS[layout]; + const current = layout === value; + return ( + + ); + })} +
+ ); +} diff --git a/apps/web/src/components/series/ready-row.tsx b/apps/web/src/components/series/ready-row.tsx new file mode 100644 index 0000000..50298fe --- /dev/null +++ b/apps/web/src/components/series/ready-row.tsx @@ -0,0 +1,65 @@ +import type { SeriesCard } from "@snug/shared"; +import { Check } from "lucide-react"; +import { Link } from "wouter"; +import { PlayButton, useOpenPlayers } from "@/components/play"; +import { useWatchNext } from "@/components/series-actions"; +import { Backdrop } from "@/components/ui"; +import { useT } from "@/i18n"; +import { episodeLabel } from "@/lib/format"; + +/** One line of the compact list: thumbnail, the full title, the next episode and the one-tap ✓. */ +export function ReadyRow({ card }: { card: SeriesCard }) { + const watchNext = useWatchNext(); + const players = useOpenPlayers(); + const t = useT(); + const next = card.next; + const code = next && episodeLabel(t, next, card.absoluteNumbering); + const left = card.airing.kind === "ready" ? card.airing.left : 0; + const flagged = card.saving || card.fresh; + return ( +
  • + +
    + +
    +
    +
    +
    +
    +

    + {flagged && ( + + {card.saving ? t.home.savedUp : t.home.new} + + )} + {card.name} +

    +

    + {code && {code}} + {code && " · "} + {t.home.left(left)} +

    +
    + + {players.length > 0 && next && ( + + )} + +
  • + ); +} diff --git a/apps/web/src/components/use-columns.ts b/apps/web/src/components/use-columns.ts new file mode 100644 index 0000000..b8d2e01 --- /dev/null +++ b/apps/web/src/components/use-columns.ts @@ -0,0 +1,25 @@ +import { useSyncExternalStore } from "react"; + +// Tailwind's `md` and `lg` breakpoints (default theme, not overridden in styles.css). +const MEDIUM = matchMedia("(min-width: 768px)"); +const LARGE = matchMedia("(min-width: 1024px)"); + +function subscribe(onChange: () => void) { + MEDIUM.addEventListener("change", onChange); + LARGE.addEventListener("change", onChange); + return () => { + MEDIUM.removeEventListener("change", onChange); + LARGE.removeEventListener("change", onChange); + }; +} + +function columns() { + if (LARGE.matches) return 4; + if (MEDIUM.matches) return 3; + return 2; +} + +/** Columns of the Ready cards grid at the current width (`grid-cols-2 md:grid-cols-3 lg:grid-cols-4`). */ +export function useColumns() { + return useSyncExternalStore(subscribe, columns); +} diff --git a/apps/web/src/i18n/en.tsx b/apps/web/src/i18n/en.tsx index 61399dc..f92dd35 100644 --- a/apps/web/src/i18n/en.tsx +++ b/apps/web/src/i18n/en.tsx @@ -101,6 +101,15 @@ export const en = { watched: (code: string) => `${code} watched`, savingOut: (out: number, needed: number) => `${out}/${needed} out`, complete: (when: string) => `Complete ${when}`, + layout: { + label: "Layout", + cards: "Cards", + list: "List", + carousel: "Carousel", + page: (current: number, total: number) => `${current} / ${total}`, + slide: "page", + pageOf: (current: number, total: number) => `Page ${current} of ${total}`, + }, pick: { trigger: (minutes: number) => `I have ${minutes} min`, title: "I have…", diff --git a/apps/web/src/i18n/fr.tsx b/apps/web/src/i18n/fr.tsx index fd41247..f4892b1 100644 --- a/apps/web/src/i18n/fr.tsx +++ b/apps/web/src/i18n/fr.tsx @@ -95,6 +95,15 @@ export const fr: Messages = { watched: (code) => `${code} vu`, savingOut: (out, needed) => `${out}/${needed} sortis`, complete: (when) => `Complète ${when}`, + layout: { + label: "Affichage", + cards: "Cartes", + list: "Liste", + carousel: "Carrousel", + page: (current, total) => `${current} / ${total}`, + slide: "page", + pageOf: (current, total) => `Page ${current} sur ${total}`, + }, pick: { trigger: (minutes) => `J'ai ${minutes} min`, title: "J'ai…", diff --git a/apps/web/src/pages/home.tsx b/apps/web/src/pages/home.tsx index b56d515..accb315 100644 --- a/apps/web/src/pages/home.tsx +++ b/apps/web/src/pages/home.tsx @@ -1,11 +1,16 @@ import { useQuery } from "@tanstack/react-query"; import { Coffee } from "lucide-react"; +import { type ReactNode, useCallback, useState } from "react"; import { Link } from "wouter"; import { usePreferences } from "@/components/preferences"; import { PickSheet } from "@/components/series/pick-sheet"; import { ReadyCard } from "@/components/series/ready-card"; +import { ReadyCarousel } from "@/components/series/ready-carousel"; +import { LayoutSwitch, useReadyLayout } from "@/components/series/ready-layout"; +import { ReadyRow } from "@/components/series/ready-row"; import { UpcomingTile } from "@/components/series/upcoming-tile"; import { HomeSkeleton } from "@/components/skeletons"; +import { useColumns } from "@/components/use-columns"; import { Button, Empty, PageTitle, POSTER_GRID, SectionTitle } from "@/components/ui"; import { useT } from "@/i18n"; import { greeting } from "@/lib/format"; @@ -15,6 +20,10 @@ export function HomePage() { const { data, isPending } = useQuery(homeQuery); const t = useT(); const { showPick } = usePreferences(); + const [chosenLayout, setLayout] = useReadyLayout(); + const [carousel, setCarousel] = useState({ page: 0, total: 1 }); + const onPage = useCallback((page: number, total: number) => setCarousel({ page, total }), []); + const columns = useColumns(); if (isPending) return ; if (!data || (!data.ready.length && !data.upcoming.length)) { @@ -33,20 +42,56 @@ export function HomePage() { ); } + // A computer screen fits the cards grid well: no switch there, the phone's choice stays for the phone. + const wide = columns === 4; + const layout = wide ? "cards" : chosenLayout; + + // Wide: the Pick beside the title, as before the switch. Carousel: where you are, "2 / 3" (the dots say it to screen readers). + let titleAction: ReactNode = null; + if (wide) titleAction = showPick && ; + else if (layout === "carousel" && carousel.total > 1) { + titleAction = ( + + {t.home.layout.page(carousel.page + 1, carousel.total)} + + ); + } + return ( <> {greeting(t)} {data.ready.length > 0 && (
    - }> + {t.home.ready} -
    - {data.ready.map((card) => ( - - ))} -
    + {!wide && ( +
    + {showPick && } +
    + +
    +
    + )} + {layout === "cards" && ( +
    + {data.ready.map((card) => ( + + ))} +
    + )} + {layout === "list" && ( +
      + {data.ready.map((card) => ( + + ))} +
    + )} + {layout === "carousel" && }
    )} @@ -63,3 +108,4 @@ export function HomePage() { ); } +