analytics.showViewed({...}) reads better than track('show_viewed', {...}) and
drops the track_ alias Captions needed, where vidstack already exports track.
The methods are derived from the event catalogue with a mapped type plus a
Proxy rather than written out, so the catalogue stays the only place an event
is declared and the two cannot drift. Names stay snake_case on the wire.
Emitters are cached so a method is referentially stable and safe to pass to a
hook or use as an effect dependency.
Events with no payload are typed void and take no arguments.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
113 lines
3.6 KiB
TypeScript
113 lines
3.6 KiB
TypeScript
'use client';
|
|
|
|
import { FC, ReactNode } from 'react';
|
|
import { create, InstanceProps } from 'react-modal-promise';
|
|
|
|
import Poster from '@/components/Poster';
|
|
import Credits from '@/components/Preview/components/Credits';
|
|
import useDetails from '@/components/Preview/useDetails';
|
|
import { Modal } from '@/components/ui/dialog';
|
|
import analytics from '@/lib/analytics';
|
|
import useTrackOnce from '@/lib/analytics/useTrackOnce';
|
|
import { cn } from '@/lib/utils';
|
|
|
|
import Actions from './components/Actions';
|
|
import Airing from './components/Airing';
|
|
import YearHint from './components/YearHint';
|
|
|
|
interface Props {
|
|
show?: Show & Partial<Details>;
|
|
showType?: Show['type'];
|
|
showId?: Show['id'];
|
|
className?: string;
|
|
posterClassName?: string;
|
|
onClose?: () => void;
|
|
poster?: ReactNode;
|
|
externalLink?: boolean;
|
|
heading?: boolean;
|
|
}
|
|
|
|
const Preview: FC<Props> = ({
|
|
show: baseShow,
|
|
showType,
|
|
showId,
|
|
className,
|
|
posterClassName,
|
|
onClose,
|
|
poster,
|
|
externalLink = true,
|
|
heading = false
|
|
}) => {
|
|
const { data: detailedShow } = useDetails({ showId: showId || baseShow?.id, showType: showType || baseShow?.type });
|
|
|
|
const show = (detailedShow || baseShow) as (Show & Partial<Details>) | undefined;
|
|
|
|
const isModal = Boolean(onClose);
|
|
|
|
useTrackOnce(analytics.showViewed, show?.id, () => ({
|
|
showId: show!.id,
|
|
showType: show!.type,
|
|
title: show!.title,
|
|
surface: isModal ? ('modal' as const) : ('page' as const)
|
|
}));
|
|
|
|
if (!show) return null;
|
|
|
|
const Title = heading ? 'h1' : 'span';
|
|
|
|
return (
|
|
<div
|
|
className={cn(
|
|
'flex flex-col md:flex-row gap-2 rounded-xl overflow-hidden',
|
|
{ 'gap-0 md:p-0 p-2': isModal },
|
|
className
|
|
)}
|
|
>
|
|
{poster || <Poster className={cn('mx-auto md:rounded-none md:rounded-l-xl', posterClassName)} show={show} />}
|
|
<div className={cn('flex grow flex-col bg-background', { 'p-0 pt-2 md:p-2': isModal })}>
|
|
<Title className='order-3 mb-4 text-3xl md:order-1 md:line-clamp-2'>{show.title}</Title>
|
|
|
|
<div className='order-3 mb-4 flex w-full flex-wrap items-center gap-5 whitespace-nowrap text-sm md:order-2'>
|
|
{Boolean(show.availability) ? <YearHint show={show} /> : <span>{new Date(show.release).getFullYear()}</span>}
|
|
{Boolean(show.airing) && <Airing airing={show.airing!} className='animate-fade-aside-slide-in opacity-0' />}
|
|
{Boolean(show.runtime) && (
|
|
<span className='animate-fade-aside-slide-in opacity-0'>{show.runtime} minutes</span>
|
|
)}
|
|
{Boolean(show.genres) && (
|
|
<span className='animate-fade-aside-slide-in opacity-0'>
|
|
{show.genres
|
|
?.slice(0, 3)
|
|
.map(({ name }) => name)
|
|
.join(' | ')}
|
|
</span>
|
|
)}
|
|
</div>
|
|
|
|
<Actions externalLink={externalLink} className='order-1 mb-4 md:order-2' show={show} />
|
|
|
|
<p key={show.id} className='order-4 md:order-4 md:line-clamp-3' title={show.overview}>
|
|
{show.overview}
|
|
</p>
|
|
<div className='order-5 mt-5 grid grid-cols-1 rounded-lg md:order-5 md:mt-auto'>
|
|
<Credits showType={show.type} showId={show.id} onClick={onClose} />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export const showPreviewModal = create(({ onResolve, onClose, show }: Props & InstanceProps<void>) => (
|
|
<Modal className='block bg-background p-0 md:bg-black' onClose={onResolve}>
|
|
<Preview
|
|
show={show}
|
|
onClose={() => {
|
|
onClose?.();
|
|
onResolve();
|
|
window.scrollTo({ top: 0, behavior: 'smooth' });
|
|
}}
|
|
/>
|
|
</Modal>
|
|
));
|
|
|
|
export default Preview;
|