Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions apps/web/src/components/series/ready-carousel.tsx
Original file line number Diff line number Diff line change
@@ -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<HTMLDivElement>(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 (
<>
<div ref={scroller} onScroll={readPage} className="no-scrollbar flex snap-x snap-mandatory overflow-x-auto">
{pages.map((items, index) => (
<div
key={index}
role="group"
aria-roledescription={t.home.layout.slide}
aria-label={t.home.layout.pageOf(index + 1, pages.length)}
className="grid w-full shrink-0 snap-start snap-always grid-cols-2 content-start gap-3 md:grid-cols-3"
>
{items.map((card) => (
<ReadyCard key={card.seriesId} card={card} />
))}
</div>
))}
</div>
{pages.length > 1 && (
<div className="mt-2 flex justify-center">
{pages.map((_page, index) => (
<button
key={index}
aria-label={t.home.layout.pageOf(index + 1, pages.length)}
aria-current={index === page ? "step" : undefined}
onClick={() => goTo(index)}
className="flex size-7 items-center justify-center"
>
<span className={cx("h-1.5 rounded-full transition-all", index === page ? "w-4.5 bg-accent" : "w-1.5 bg-line")} />
</button>
))}
</div>
)}
</>
);
}
59 changes: 59 additions & 0 deletions apps/web/src/components/series/ready-layout.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div role="group" aria-label={t.home.layout.label} className="flex shrink-0 gap-0.5 rounded-full bg-raised p-0.5">
{LAYOUTS.map((layout) => {
const Icon = ICONS[layout];
const current = layout === value;
return (
<button
key={layout}
aria-label={t.home.layout[layout]}
aria-pressed={current}
onClick={() => onChange(layout)}
className={cx(
"flex h-8 w-10 items-center justify-center rounded-full transition",
current ? "bg-accent text-accent-ink" : "text-muted hover:text-text",
)}
>
<Icon className="size-4" />
</button>
);
})}
</div>
);
}
65 changes: 65 additions & 0 deletions apps/web/src/components/series/ready-row.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<li className="flex items-center gap-1 pr-0.5">
<Link href={`/series/${card.seriesId}`} className="flex min-w-0 flex-1 items-center gap-2.5 py-2.5 pl-2.5">
<div className="relative h-7.5 w-13 shrink-0 overflow-hidden rounded-md">
<Backdrop backdropPath={card.backdropPath} posterPath={card.posterPath} sizes="52px" className="h-full w-full" />
<div className="absolute inset-x-0 bottom-0 h-0.75 bg-bg/70">
<div className="h-full bg-accent" style={{ width: `${card.airedCount ? (card.watchedCount / card.airedCount) * 100 : 0}%` }} />
</div>
</div>
<div className="min-w-0">
<h3 className="flex items-center gap-1.5 text-base leading-tight">
{flagged && (
<span className="size-1.75 shrink-0 rounded-full bg-accent" title={card.saving ? t.home.savedUp : t.home.new}>
<span className="sr-only">{card.saving ? t.home.savedUp : t.home.new}</span>
</span>
)}
<span className="truncate">{card.name}</span>
</h3>
<p className="truncate text-xs text-muted">
{code && <span className="font-bold text-accent">{code}</span>}
{code && " · "}
{t.home.left(left)}
</p>
</div>
</Link>
{players.length > 0 && next && (
<PlayButton
seriesId={card.seriesId}
episode={next}
label={code ?? undefined}
player={players.length === 1 ? players[0] : undefined}
className="flex size-11 shrink-0 items-center justify-center rounded-full text-text transition hover:text-accent"
/>
)}
<button
aria-label={code ? t.home.markWatched(code) : t.home.markNextWatched}
disabled={watchNext.isPending}
onClick={() => watchNext.mutate(card)}
className="group flex size-11 shrink-0 items-center justify-center disabled:opacity-60"
>
<span className="flex size-8 items-center justify-center rounded-full bg-accent text-accent-ink transition group-hover:bg-accent-strong group-active:scale-90">
<Check className="size-4" strokeWidth={3} />
</span>
</button>
</li>
);
}
25 changes: 25 additions & 0 deletions apps/web/src/components/use-columns.ts
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 9 additions & 0 deletions apps/web/src/i18n/en.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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…",
Expand Down
9 changes: 9 additions & 0 deletions apps/web/src/i18n/fr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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…",
Expand Down
58 changes: 52 additions & 6 deletions apps/web/src/pages/home.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 <HomeSkeleton />;
if (!data || (!data.ready.length && !data.upcoming.length)) {
Expand All @@ -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 && <PickSheet ready={data.ready} />;
else if (layout === "carousel" && carousel.total > 1) {
titleAction = (
<span aria-hidden className="text-sm font-bold text-muted">
{t.home.layout.page(carousel.page + 1, carousel.total)}
</span>
);
}

return (
<>
<PageTitle>{greeting(t)}</PageTitle>

{data.ready.length > 0 && (
<section className="mb-10">
<SectionTitle count={data.ready.length} action={showPick && <PickSheet ready={data.ready} />}>
<SectionTitle
count={data.ready.length}
action={titleAction}
>
{t.home.ready}
</SectionTitle>
<div className="grid grid-cols-2 gap-3 md:grid-cols-3 lg:grid-cols-4">
{data.ready.map((card) => (
<ReadyCard key={card.seriesId} card={card} />
))}
</div>
{!wide && (
<div className="mb-4 flex items-center justify-between gap-3">
{showPick && <PickSheet ready={data.ready} />}
<div className="ml-auto">
<LayoutSwitch value={layout} onChange={setLayout} />
</div>
</div>
)}
{layout === "cards" && (
<div className="grid grid-cols-2 gap-3 md:grid-cols-3 lg:grid-cols-4">
{data.ready.map((card) => (
<ReadyCard key={card.seriesId} card={card} />
))}
</div>
)}
{layout === "list" && (
<ul className="divide-y divide-line/60 overflow-hidden rounded-2xl border border-line/60 bg-surface">
{data.ready.map((card) => (
<ReadyRow key={card.seriesId} card={card} />
))}
</ul>
)}
{layout === "carousel" && <ReadyCarousel cards={data.ready} columns={columns} onPage={onPage} />}
</section>
)}

Expand All @@ -63,3 +108,4 @@ export function HomePage() {
</>
);
}

Loading