diff --git a/app/(site)/person/[id]/loading.tsx b/app/(site)/person/[id]/loading.tsx new file mode 100644 index 0000000..8aec056 --- /dev/null +++ b/app/(site)/person/[id]/loading.tsx @@ -0,0 +1,5 @@ +import PersonSkeleton from '@/components/Person/components/Skeleton'; + +const Loading = () => ; + +export default Loading; diff --git a/app/(site)/person/[id]/page.tsx b/app/(site)/person/[id]/page.tsx new file mode 100644 index 0000000..617ce1e --- /dev/null +++ b/app/(site)/person/[id]/page.tsx @@ -0,0 +1,53 @@ +import { FC } from 'react'; +import { dehydrate, HydrationBoundary } from '@tanstack/react-query'; +import { Metadata } from 'next'; +import { redirect } from 'next/navigation'; + +import Person from '@/components/Person'; +import personQuery from '@/data/queries/person'; +import { SITE_URL, TITLE } from '@/env'; +import getQueryClient from '@/lib/queryClient'; + +interface Props { + params: { id: string }; +} + +export const generateMetadata = async ({ params: { id } }: Props): Promise => { + const queryClient = getQueryClient(); + + try { + const { name, biography, photo } = await queryClient.fetchQuery(personQuery({ personId: id })); + + const description = biography || `Movies and series starring ${name}.`; + + return { + title: `${name} | ${TITLE}`, + description, + openGraph: { + title: name, + description, + images: photo ? [photo] : [], + type: 'profile', + url: `${SITE_URL}/person/${id}` + } + }; + } catch (_) { + return { title: TITLE }; + } +}; + +const PersonPage: FC = async ({ params: { id } }) => { + const queryClient = getQueryClient(); + + if (!id) return redirect('/'); + + await queryClient.prefetchQuery(personQuery({ personId: id })); + + return ( + + + + ); +}; + +export default PersonPage; diff --git a/app/(site)/starring/container.tsx b/app/(site)/starring/container.tsx deleted file mode 100644 index 758f95f..0000000 --- a/app/(site)/starring/container.tsx +++ /dev/null @@ -1,31 +0,0 @@ -'use client'; - -import { useSearchParams } from 'next/navigation'; - -import List from '@/components/List'; -import NoResults from '@/components/NoResults'; - -import useStarring from './useStarring'; - -const Container = () => { - const params = useSearchParams(); - - const title = params.get('title') as string; - const actorId = params.get('actorId') as string; - - const { shows, fetchNextPage, isFetched } = useStarring({ actorId }); - - return ( - <> - {Boolean(title) && ( - - Starring: {decodeURIComponent(title || '')} - - )} - - {isFetched && !shows.length && } - > - ); -}; - -export default Container; diff --git a/app/(site)/starring/page.tsx b/app/(site)/starring/page.tsx deleted file mode 100644 index add1fd0..0000000 --- a/app/(site)/starring/page.tsx +++ /dev/null @@ -1,32 +0,0 @@ -import { FC } from 'react'; -import { dehydrate, HydrationBoundary } from '@tanstack/react-query'; - -import popularQuery from '@/data/queries/popular'; -import getQueryClient from '@/lib/queryClient'; - -import Container from './container'; - -interface Props { - searchParams: Record; -} - -const Starring: FC = ({ searchParams }) => { - const queryClient = getQueryClient(); - - const { actorId } = searchParams || {}; - - queryClient.prefetchInfiniteQuery( - popularQuery({ - sortBy: 'vote_average.desc', - starring: actorId - }) - ); - - return ( - - - - ); -}; - -export default Starring; diff --git a/app/(site)/starring/useStarring.ts b/app/(site)/starring/useStarring.ts deleted file mode 100644 index be79129..0000000 --- a/app/(site)/starring/useStarring.ts +++ /dev/null @@ -1,9 +0,0 @@ -'use client'; - -import popularQuery from '@/data/queries/popular'; -import useInfiniteList from '@/hooks/useInfiniteList'; - -const useStarring = ({ actorId }: { actorId?: string }) => - useInfiniteList(popularQuery({ sortBy: 'vote_average.desc', starring: actorId })); - -export default useStarring; diff --git a/components/Person/components/Biography/index.tsx b/components/Person/components/Biography/index.tsx new file mode 100644 index 0000000..4707184 --- /dev/null +++ b/components/Person/components/Biography/index.tsx @@ -0,0 +1,65 @@ +'use client'; + +import { FC, useEffect, useRef, useState } from 'react'; + +import { cn } from '@/lib/utils'; + +interface Props { + text: string; +} + +const Biography: FC = ({ text }) => { + const [open, setOpen] = useState(false); + const [overflows, setOverflows] = useState(false); + + const textRef = useRef(null); + + useEffect(() => { + const element = textRef.current; + + if (!element) return; + + const measure = () => setOverflows(element.scrollHeight > element.clientHeight + 1); + + measure(); + + const observer = new ResizeObserver(measure); + + observer.observe(element); + + return () => observer.disconnect(); + }, [text]); + + if (!text) return null; + + return ( + pointerType === 'mouse' && overflows && setOpen(true)} + onPointerLeave={({ pointerType }) => pointerType === 'mouse' && setOpen(false)} + > + + + {text} + + {overflows && ( + + )} + + {overflows && ( + + {text} + + )} + + ); +}; + +export default Biography; diff --git a/components/Person/components/Skeleton/index.tsx b/components/Person/components/Skeleton/index.tsx new file mode 100644 index 0000000..d47544d --- /dev/null +++ b/components/Person/components/Skeleton/index.tsx @@ -0,0 +1,52 @@ +import { FC } from 'react'; + +import { Skeleton } from '@/components/ui/skeleton'; +import { cn } from '@/lib/utils'; + +const PARAGRAPHS = [ + ['w-full', 'w-full', 'w-10/12'], + ['w-full', 'w-full', 'w-full', 'w-4/5'], + ['w-full', 'w-full', 'w-3/4'], + ['w-full', 'w-2/3'] +]; + +const POSTERS = Array.from({ length: 8 }, (_, index) => index); + +const PersonSkeleton: FC = () => ( + + + + + + + + + + + + + + + {PARAGRAPHS.map((lines, paragraph) => ( + + {lines.map((width, line) => ( + + ))} + + ))} + + + + + + + + {POSTERS.map((index) => ( + = 2 })} /> + ))} + + + +); + +export default PersonSkeleton; diff --git a/components/Person/index.tsx b/components/Person/index.tsx new file mode 100644 index 0000000..57ccb77 --- /dev/null +++ b/components/Person/index.tsx @@ -0,0 +1,65 @@ +'use client'; + +import { FC } from 'react'; + +import List from '@/components/List'; +import NoResults from '@/components/NoResults'; +import { formatDate } from '@/lib/utils'; + +import Biography from './components/Biography'; +import PersonSkeleton from './components/Skeleton'; +import useProfile from './useProfile'; + +interface Props { + personId: string; +} + +const lifespan = ({ birthday, deathday }: Profile) => { + if (!birthday) return ''; + + return deathday ? `${formatDate(birthday)} — ${formatDate(deathday)}` : formatDate(birthday); +}; + +const Person: FC = ({ personId }) => { + const { data: profile, isError } = useProfile({ personId }); + + if (isError) return ; + + if (!profile) return ; + + const dates = lifespan(profile); + + return ( + + + {Boolean(profile.photo) && ( + + + + )} + + {profile.name} + + + {Boolean(profile.department) && {profile.department}} + {Boolean(dates) && {dates}} + {Boolean(profile.birthplace) && {profile.birthplace}} + + + + + + + {Boolean(profile.shows.length) && ( + + Filmography + + + )} + + {!profile.shows.length && } + + ); +}; + +export default Person; diff --git a/components/Person/useProfile.ts b/components/Person/useProfile.ts new file mode 100644 index 0000000..fc0e1ec --- /dev/null +++ b/components/Person/useProfile.ts @@ -0,0 +1,7 @@ +import { useQuery } from '@tanstack/react-query'; + +import personQuery, { PersonQueryProps } from '@/data/queries/person'; + +const useProfile = ({ personId }: PersonQueryProps) => useQuery(personQuery({ personId })); + +export default useProfile; diff --git a/components/Preview/components/Availability/index.tsx b/components/Preview/components/Availability/index.tsx index 0a4ab6d..2266687 100644 --- a/components/Preview/components/Availability/index.tsx +++ b/components/Preview/components/Availability/index.tsx @@ -39,11 +39,7 @@ const Availability: FC = ({ availability, className }) => { const Icon = ICONS[state]; return ( - + ); diff --git a/components/Preview/components/Credits/index.tsx b/components/Preview/components/Credits/index.tsx index a3f40d1..59949ab 100644 --- a/components/Preview/components/Credits/index.tsx +++ b/components/Preview/components/Credits/index.tsx @@ -52,11 +52,7 @@ const components: VirtuosoProps['components'] = { style={{ width: context?.width, height: context?.height }} className='relative shrink-0 cursor-pointer snap-start self-end bg-card text-card-foreground transition-shadow hover:shadow-lg' > - context?.onClick?.(actor.id.toString())} - > + context?.onClick?.(actor.id.toString())}> diff --git a/components/ui/hint.tsx b/components/ui/hint.tsx index ca88da0..37bf270 100644 --- a/components/ui/hint.tsx +++ b/components/ui/hint.tsx @@ -8,21 +8,17 @@ interface Props { lines: string[]; children: ReactNode; className?: string; - triggerClassName?: string; } -const Hint: FC = ({ lines, children, className, triggerClassName }) => { +const Hint: FC = ({ lines, children, className }) => { const [open, setOpen] = useState(false); - // an animation that lands on a transform turns this wrapper into a stacking context, - // so the tooltip can only reach above the following rows by raising the wrapper itself return ( pointerType === 'mouse' && setOpen(true)} onPointerLeave={({ pointerType }) => pointerType === 'mouse' && setOpen(false)} onClick={() => setOpen((value) => !value)} diff --git a/components/ui/skeleton.tsx b/components/ui/skeleton.tsx new file mode 100644 index 0000000..f0a2781 --- /dev/null +++ b/components/ui/skeleton.tsx @@ -0,0 +1,7 @@ +import { cn } from '@/lib/utils'; + +function Skeleton({ className, ...props }: React.HTMLAttributes) { + return ; +} + +export { Skeleton }; diff --git a/data/clients/movieAdvisor.ts b/data/clients/movieAdvisor.ts index d697f32..3c22cf1 100644 --- a/data/clients/movieAdvisor.ts +++ b/data/clients/movieAdvisor.ts @@ -4,8 +4,10 @@ import { SITE_URL } from '@/env'; import Http from './Http'; -const ROOT = isServer ? `${SITE_URL?.startsWith('http') ? SITE_URL : `https://${SITE_URL}`}` : ''; +const PUBLIC_ORIGIN = `${SITE_URL?.startsWith('http') ? SITE_URL : `https://${SITE_URL}`}`; -const movieAdvisor = new Http(`${ROOT}/api/tmdb`); +const ROOT = process.env.NODE_ENV === 'development' ? `http://127.0.0.1:${process.env.PORT || 3000}` : PUBLIC_ORIGIN; + +const movieAdvisor = new Http(`${isServer ? ROOT : ''}/api/tmdb`); export default movieAdvisor; diff --git a/data/dto/Airing.ts b/data/dto/Airing.ts index b4511e9..8a44e7e 100644 --- a/data/dto/Airing.ts +++ b/data/dto/Airing.ts @@ -6,7 +6,6 @@ export const mapDetailsToAiring = ({ number_of_seasons, status }: TMDBSeriesDetails): Airing | undefined => { - // before a premiere there is nothing aired yet, the upcoming episode is the only position we have const current = last_episode_to_air || next_episode_to_air; if (!current) return undefined; diff --git a/data/dto/Availability.ts b/data/dto/Availability.ts index 259f6ab..68fbcf8 100644 --- a/data/dto/Availability.ts +++ b/data/dto/Availability.ts @@ -1,4 +1,3 @@ -// TMDB release types: 1 premiere, 2 limited theatrical, 3 theatrical, 4 digital, 5 physical, 6 TV const STATE_BY_RELEASE_TYPE: Record = { 1: 'theatre', 2: 'theatre', @@ -7,11 +6,9 @@ const STATE_BY_RELEASE_TYPE: Record = { 5: 'bluray' }; -/** How long a movie is assumed to still be running in theatres when TMDB has no digital/physical date yet */ const THEATRICAL_WINDOW = 120 * 24 * 60 * 60 * 1000; export const mapReleaseDatesToAvailability = (results?: TMDBCountryReleaseDates[]): Availability | undefined => { - // TMDB reports dates per country, it doesn't matter where a window opened first — only when const availability = (results || []).reduce((dates, { release_dates }) => { (release_dates || []).forEach(({ type, release_date }) => { const state = STATE_BY_RELEASE_TYPE[type]; @@ -37,8 +34,6 @@ export const getAvailabilityState = ({ theatre, stream, bluray }: Availability): if (isUpcoming(theatre)) return 'theatre'; if (!theatre) return undefined; - // Still in theatres only while the run lasts or while a digital/physical date is announced but not reached, - // otherwise TMDB simply never got the digital entry and the movie is long out of cinemas const isRunning = Date.now() - Date.parse(theatre) < THEATRICAL_WINDOW; return isRunning || isUpcoming(stream) || isUpcoming(bluray) ? 'theatre' : 'stream'; diff --git a/data/dto/Person.ts b/data/dto/Person.ts new file mode 100644 index 0000000..2eb9ce2 --- /dev/null +++ b/data/dto/Person.ts @@ -0,0 +1,31 @@ +import { mapMovieSeriesToShow } from './Show'; + +const unique = (credits: TMDBPersonCredit[]) => { + const titles = new Map(); + + credits.forEach((credit) => { + const key = `${credit.media_type}-${credit.id}`; + + if (credit.poster_path && ['movie', 'tv'].includes(credit.media_type) && !titles.has(key)) { + titles.set(key, credit); + } + }); + + return Array.from(titles.values()); +}; + +const mapPersonToProfile = ({ combined_credits, ...person }: TMDBPersonDetails): Profile => ({ + id: person.id, + name: person.name, + photo: person.profile_path ? `https://image.tmdb.org/t/p/w500${person.profile_path}` : '', + biography: person.biography || '', + department: person.known_for_department || '', + birthday: person.birthday || undefined, + deathday: person.deathday || undefined, + birthplace: person.place_of_birth || undefined, + shows: unique([...(combined_credits?.cast || []), ...(combined_credits?.crew || [])]) + .sort((a, b) => b.vote_count - a.vote_count) + .map((credit) => mapMovieSeriesToShow(credit, credit.media_type)) +}); + +export default mapPersonToProfile; diff --git a/data/queries/credits.ts b/data/queries/credits.ts index eb263cb..d6cd2ee 100644 --- a/data/queries/credits.ts +++ b/data/queries/credits.ts @@ -7,7 +7,6 @@ export type CreditsQueryProps = { const photo = (path: string) => `https://image.tmdb.org/t/p/w185${path}`; -// a series has no single director, so the one behind most episodes stands in for it const findDirector = (crew: Crew[] = []) => crew .filter( diff --git a/data/queries/person.ts b/data/queries/person.ts new file mode 100644 index 0000000..7d7efbb --- /dev/null +++ b/data/queries/person.ts @@ -0,0 +1,21 @@ +import movieAdvisor from '../clients/movieAdvisor'; +import mapPersonToProfile from '../dto/Person'; + +export type PersonQueryProps = { + personId?: string; +}; + +const personQuery = ({ personId }: PersonQueryProps) => ({ + enabled: Boolean(personId), + queryKey: ['person', personId], + queryFn: () => + movieAdvisor + .get(`/person/${personId}`, { + params: { append_to_response: 'combined_credits' }, + preventCache: false + }) + .then(mapPersonToProfile) + .catch((error) => Promise.reject(error)) +}); + +export default personQuery; diff --git a/data/queries/popular.ts b/data/queries/popular.ts index 1b109f4..f4b632b 100644 --- a/data/queries/popular.ts +++ b/data/queries/popular.ts @@ -3,11 +3,10 @@ import mapMoviesSeriesResponseToShows from '../dto/Show'; export type PopularQueryProps = { sortBy?: string; - starring?: string; }; -const popularQuery = ({ sortBy = 'popularity.desc', starring }: PopularQueryProps = {}) => ({ - queryKey: ['popular', sortBy, starring], +const popularQuery = ({ sortBy = 'popularity.desc' }: PopularQueryProps = {}) => ({ + queryKey: ['popular', sortBy], initialPageParam: '1', queryFn: ({ pageParam = '1' }: { pageParam?: string }) => movieAdvisor @@ -15,8 +14,6 @@ const popularQuery = ({ sortBy = 'popularity.desc', starring }: PopularQueryProp params: { page: pageParam, sort_by: sortBy, - // with_people covers cast and crew, with_cast on top of it would AND directors away - with_people: starring, 'vote_count.gte': 300, 'vote_average.lte': 10, 'vote_average.gte': 5 diff --git a/lib/utils.ts b/lib/utils.ts index f10ab7f..a75cc71 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -3,7 +3,6 @@ import { twMerge } from 'tailwind-merge'; export const cn = (...inputs: ClassValue[]) => twMerge(clsx(inputs)); -// TMDB dates are UTC midnight, formatting them in the local zone would shift them a day back export const formatDate = (date: string) => new Intl.DateTimeFormat('en-GB', { day: '2-digit', month: '2-digit', year: 'numeric', timeZone: 'UTC' }) .format(new Date(date)) diff --git a/types.d.ts b/types.d.ts index 1382f8b..a647743 100644 --- a/types.d.ts +++ b/types.d.ts @@ -36,13 +36,11 @@ interface TMDBSeriesDetails { status?: string; } -/** Where a series currently stands — the latest aired episode plus what follows it */ interface Airing { season: number; episode: number; seasons: number; ended: boolean; - /** air date of the next episode, ISO string, only when TMDB knows one */ next?: string; } @@ -61,7 +59,6 @@ interface TMDBCountryReleaseDates { type AvailabilityState = 'theatre' | 'stream' | 'bluray'; -/** Earliest worldwide release date per window, ISO strings */ type Availability = Partial>; interface Movie { @@ -127,14 +124,12 @@ interface Person { popularity: number; profile_path: string; credit_id: string; - // crew members carry neither cast_id?: number; order?: number; } interface Crew extends Person { department: string; - /** movie credits carry a single job, tv aggregate credits carry one entry per job */ job?: string; jobs?: Array<{ credit_id: string; job: string; episode_count: number }>; total_episode_count?: number; @@ -145,6 +140,32 @@ interface Actor extends Person { photoUrl: string; } +type TMDBPersonCredit = (Movie | Series) & { media_type: Show['type'] }; + +interface TMDBPersonDetails { + id: number; + name: string; + biography: string; + birthday: string | null; + deathday: string | null; + place_of_birth: string | null; + known_for_department: string; + profile_path: string | null; + combined_credits?: { cast: TMDBPersonCredit[]; crew: TMDBPersonCredit[] }; +} + +interface Profile { + id: number; + name: string; + photo: string; + biography: string; + department: string; + birthday?: string; + deathday?: string; + birthplace?: string; + shows: Show[]; +} + interface AggregatedActor extends Person { roles: Role[]; total_episode_count: number;
+ {text} +
{text}