|
1 | | -import { useEffect, useState } from 'react'; |
2 | | -import { ArrowLeft, Search, X } from 'lucide-react'; |
| 1 | +import { useEffect, useMemo, useState } from 'react'; |
| 2 | +import { ArrowLeft, Check, Plus, Search, X } from 'lucide-react'; |
3 | 3 | import { hasTmdb } from '../lib/config.js'; |
4 | | -import { filmography, searchMovies, searchPeople } from '../lib/tmdb.js'; |
| 4 | +import { comingSoon, filmography, searchMovies, searchPeople } from '../lib/tmdb.js'; |
| 5 | +import { predictStars, tasteProfile } from '../lib/suggest.js'; |
5 | 6 | import { MemberDots, MovieRow, Notice } from './ui.jsx'; |
6 | 7 |
|
7 | | -const MODES = [['movies', 'Movies'], ['people', 'Directors']]; |
| 8 | +const MODES = [['movies', 'Movies'], ['people', 'Directors'], ['soon', 'Coming soon']]; |
| 9 | +const opens = (date) => new Date(`${date}T12:00:00`).toLocaleDateString(undefined, { month: 'short', day: 'numeric' }); |
8 | 10 |
|
9 | 11 | /** Find one specific movie, or look up a director (or writer) and browse what they made. */ |
10 | | -export default function Browse({ entries, members, onOpen }) { |
| 12 | +export default function Browse({ entries, members, me, onWant, onOpen }) { |
11 | 13 | const [mode, setMode] = useState('movies'); |
12 | 14 | const [query, setQuery] = useState(''); |
13 | 15 | const [results, setResults] = useState([]); |
14 | 16 | const [state, setState] = useState('idle'); // idle | loading | ready | error |
15 | 17 | const [error, setError] = useState(''); |
| 18 | + const [soon, setSoon] = useState({ state: 'idle', list: [] }); // movies opening in theatres soon |
| 19 | + const [soonSort, setSoonSort] = useState('fit'); // fit | date |
16 | 20 | const [person, setPerson] = useState(null); // the chosen director, with { directed, written } once loaded |
17 | 21 |
|
18 | 22 | useEffect(() => { |
| 23 | + if (mode !== 'soon' || soon.state !== 'idle') return; |
| 24 | + setSoon({ state: 'loading', list: [] }); |
| 25 | + comingSoon().then((list) => setSoon({ state: 'ready', list })).catch((e) => setSoon({ state: 'error', list: [], error: e.message })); |
| 26 | + }, [mode]); // eslint-disable-line react-hooks/exhaustive-deps |
| 27 | + const taste = useMemo(() => tasteProfile(entries, me), [entries, me]); |
| 28 | + const upcoming = useMemo(() => soon.list |
| 29 | + .map((movie) => ({ movie, fit: Math.round(predictStars(taste, movie) * 20) })) |
| 30 | + .sort((a, b) => (soonSort === 'date' ? a.movie.release.localeCompare(b.movie.release) : b.fit - a.fit))) // ties keep TMDB's order: most anticipated first, [soon.list, taste, soonSort]); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + if (mode === 'soon') return undefined; |
19 | 34 | const q = query.trim(); |
20 | 35 | if (!q) { setState('idle'); setResults([]); return undefined; } |
21 | 36 | let live = true; |
@@ -71,12 +86,39 @@ export default function Browse({ entries, members, onOpen }) { |
71 | 86 |
|
72 | 87 | return ( |
73 | 88 | <div className="flex flex-col gap-4"> |
74 | | - <div className="grid grid-cols-2 gap-1 rounded-full bg-seat p-1" role="tablist" aria-label="Search for"> |
| 89 | + <div className="grid grid-cols-3 gap-1 rounded-full bg-seat p-1" role="tablist" aria-label="Search for"> |
75 | 90 | {MODES.map(([id, label]) => ( |
76 | 91 | <button key={id} type="button" role="tab" aria-selected={mode === id} onClick={() => switchMode(id)} className={`rounded-full py-2 text-sm font-bold transition ${mode === id ? 'bg-gold text-house' : 'text-dim hover:text-cream'}`}>{label}</button> |
77 | 92 | ))} |
78 | 93 | </div> |
79 | 94 |
|
| 95 | + {mode === 'soon' && ( |
| 96 | + <section className="flex flex-col gap-3"> |
| 97 | + <div className="flex flex-wrap items-end justify-between gap-2"> |
| 98 | + <p className="text-sm text-dim">Opening in US theatres over the next five months. Tap <span className="font-bold text-cream">Want to see</span> and it joins your Want to list, then shows up on Tonight once it's out.</p> |
| 99 | + <div className="flex gap-2" role="group" aria-label="Sort by"> |
| 100 | + {[['fit', 'Best fit for me'], ['date', 'Soonest']].map(([key, label]) => <button key={key} type="button" aria-pressed={soonSort === key} onClick={() => setSoonSort(key)} className={`rounded-full border px-3 py-1.5 text-sm font-bold ${soonSort === key ? 'border-gold bg-gold text-house' : 'border-aisle text-cream'}`}>{label}</button>)} |
| 101 | + </div> |
| 102 | + </div> |
| 103 | + {!hasTmdb && <Notice>Coming soon needs the TMDB key.</Notice>} |
| 104 | + {soon.state === 'loading' && <p className="py-10 text-center text-sm text-dim">Finding what's coming…</p>} |
| 105 | + {soon.state === 'error' && <Notice>{soon.error}</Notice>} |
| 106 | + {soon.state === 'ready' && hasTmdb && upcoming.length === 0 && <p className="py-10 text-center text-sm text-dim">Nothing announced yet.</p>} |
| 107 | + <ol className="-mx-2 flex flex-col divide-y divide-aisle/60 lg:grid lg:grid-cols-2 lg:gap-x-8 lg:divide-y-0"> |
| 108 | + {upcoming.map(({ movie, fit }) => { |
| 109 | + const mine = entries[movie.id]?.ratings?.[me]; |
| 110 | + const wanted = mine?.status === 'unseen'; |
| 111 | + return ( |
| 112 | + <MovieRow key={movie.id} movie={entries[movie.id]?.movie || movie} onOpen={onOpen} |
| 113 | + note={<span><span className="font-bold text-gold">Opens {opens(movie.release)}</span>{taste.genres.length ? ` · ${fit}% your kind of movie` : ''}</span>} |
| 114 | + action={<button type="button" onClick={() => onWant(movie, !wanted)} aria-pressed={wanted} className={`mr-1 inline-flex shrink-0 items-center gap-1 rounded-full border px-3 py-1.5 text-xs font-bold ${wanted ? 'border-gold bg-gold text-house' : 'border-aisle text-cream'}`}>{wanted ? <><Check size={13} /> Wanted</> : <><Plus size={13} /> Want to see</>}</button>} /> |
| 115 | + ); |
| 116 | + })} |
| 117 | + </ol> |
| 118 | + </section> |
| 119 | + )} |
| 120 | + |
| 121 | + {mode !== 'soon' && <> |
80 | 122 | <div className="relative"> |
81 | 123 | <Search size={18} className="pointer-events-none absolute left-3.5 top-1/2 -translate-y-1/2 text-dim" /> |
82 | 124 | <input id="movie-search" type="search" autoComplete="off" value={query} onChange={(e) => setQuery(e.target.value)} placeholder={mode === 'people' ? 'Search a director or writer' : hasTmdb ? 'Search any movie' : 'Search 150 built-in movies'} aria-label={mode === 'people' ? 'Search directors and writers' : 'Search movies'} className="w-full rounded-full border border-aisle bg-seat py-3 pl-11 pr-10 text-cream placeholder:text-dim focus:border-gold focus:outline-none" /> |
@@ -106,6 +148,7 @@ export default function Browse({ entries, members, onOpen }) { |
106 | 148 | ))} |
107 | 149 | </ul> |
108 | 150 | )} |
| 151 | + </>} |
109 | 152 | </div> |
110 | 153 | ); |
111 | 154 | } |
0 commit comments