Files
movie-advisor/components/Preview/index.tsx
T
nerandClaude Opus 5 d67a0d4df6 feat(analytics): self-hosted openpanel with a typed event catalogue
Replaces Vercel Analytics with OpenPanel, driven entirely by env vars so the
whole layer is inert until NEXT_PUBLIC_OPENPANEL_CLIENT_ID is set.

Zero initial load cost. next/script emits a rel=preload even on lazyOnload,
which pulls the sdk during the first paint, so the component skips it: it
inlines the 416 byte official stub (window.op queues calls immediately) and
appends the real script itself on window load plus requestIdleCallback. The
built html carries no preload and no script src for the analytics host.

Events live in one typed catalogue (lib/analytics/events.ts) and track() is
keyed to it, so a wrong name or payload is a type error rather than a silent
bad row. Emitters sit in the hooks that already own the behaviour instead of
being sprinkled through components:

- useShowState covers every favourite and watchlist toggle
- useInfiniteList covers pagination for all six lists
- useTorrents covers all three providers
- Preview, Person and useSearch use a shared useTrackOnce hook

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-14 12:08:16 +03:00

112 lines
3.5 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 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('show_viewed', show?.id, () => ({
showId: show!.id,
showType: show!.type,
title: show!.title,
surface: isModal ? 'modal' : 'page'
}));
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;