feat(torrents): launched state, year-scoped Toloka search, stacked modal backdrops
- replace pin-to-top with persistent launched-once state (filled play/cast icons) - move download/vlc actions into a more-options dropdown when cast is available - scope Toloka search query by show year (no IMDB filter on the tracker) - strip session sid from Toloka download hrefs so cached magnets stay valid - restore row hover, subtle row borders, taller rows; drop table rounding - stacked modal backdrops: :has(~ [data-overlay]) instead of :not(:last-of-type) - only count search in analytics after the query has settled (1s debounce)
This commit is contained in:
1 parent
66776c582f
commit
c52d798221
12 files changed
+194
-107
No files matched your search
+97
-58
@@ -1,16 +1,23 @@
|
||||
import { FC, useEffect, useMemo, useState } from 'react';
|
||||
import { Bookmark, Cast, Download, ListVideo, Loader, Magnet, Play, TrafficCone } from 'lucide-react';
|
||||
import { Cast, Download, ListVideo, Loader, Magnet, MoreHorizontal, Play, TrafficCone } from 'lucide-react';
|
||||
|
||||
import { providers } from '@/components/Preview/components/Torrents/constants';
|
||||
import { useCastMagnet, usePrefix } from '@/components/Preview/components/Torrents/hooks/useMagnetHosts';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import ButtonsGroup from '@/components/ui/buttons-group';
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger
|
||||
} from '@/components/ui/dropdown-menu';
|
||||
import { Quality } from '@/data/parsers/yts/models';
|
||||
import { useM3UUrl } from '@/hooks/useM3UStreamUrl';
|
||||
import { useStreamUrl } from '@/hooks/useStreamUrl';
|
||||
import analytics from '@/lib/analytics';
|
||||
import { cn, detectSafari, getMagnetHash, isStandaloneApp, seasonFromEpisodes, torrentKey } from '@/lib/utils';
|
||||
|
||||
import useLaunchedTorrents from '../../../../useLaunchedTorrents';
|
||||
import usePinnedTorrents from '../../../../usePinnedTorrents';
|
||||
import useUnplayable from '../../../../useUnplayable';
|
||||
|
||||
@@ -47,11 +54,12 @@ const Actions: FC<Props> = ({ torrent, title, provider, show }) => {
|
||||
|
||||
const fetchMagnet = useMagnet(torrent);
|
||||
|
||||
const { pin, toggle, isPinned, savedMagnet, rememberMagnet } = usePinnedTorrents();
|
||||
const { savedMagnet, rememberMagnet } = usePinnedTorrents();
|
||||
const { isLaunched, launch } = useLaunchedTorrents();
|
||||
const { reasonFor } = useUnplayable();
|
||||
|
||||
const key = torrentKey(torrent);
|
||||
const pinned = isPinned(key);
|
||||
const launched = isLaunched(key);
|
||||
|
||||
const magnet = fetchMagnet.data || torrent.magnet || savedMagnet(key) || '';
|
||||
|
||||
@@ -60,11 +68,11 @@ const Actions: FC<Props> = ({ torrent, title, provider, show }) => {
|
||||
const unplayable = reasonFor(hash);
|
||||
|
||||
useEffect(() => {
|
||||
if (pinned && magnet && !torrent.magnet) rememberMagnet(key, magnet);
|
||||
if (magnet && !torrent.magnet) rememberMagnet(key, magnet);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [pinned, magnet, key, torrent.magnet]);
|
||||
}, [magnet, key, torrent.magnet]);
|
||||
|
||||
const pinPlayed = () => {
|
||||
const trackLaunch = () => {
|
||||
analytics.torrentSelected({
|
||||
showId: show.id,
|
||||
showType: show.type,
|
||||
@@ -77,13 +85,18 @@ const Actions: FC<Props> = ({ torrent, title, provider, show }) => {
|
||||
codec: torrent.codec
|
||||
});
|
||||
|
||||
pin(key);
|
||||
launch(key);
|
||||
};
|
||||
|
||||
const supportedForCast = prefix && !prefix.includes('{host}');
|
||||
const showCast = Boolean(M3UUrl || supportedForCast);
|
||||
const m3uUrl = `${streamUrl}/stream?m3u&link=${encodeURIComponent(magnet)}`;
|
||||
const vlcUrl = `vlc://${m3uUrl}`;
|
||||
|
||||
const launchedCastClass = 'border-blue-600 bg-blue-600 text-white hover:bg-blue-600 hover:text-white';
|
||||
|
||||
const handleM3ULink = () => {
|
||||
pinPlayed();
|
||||
trackLaunch();
|
||||
setIsStreamPending(true);
|
||||
|
||||
const params = new URLSearchParams({
|
||||
@@ -144,75 +157,101 @@ const Actions: FC<Props> = ({ torrent, title, provider, show }) => {
|
||||
{ invisible: !magnet || fetchMagnet.isPending }
|
||||
)}
|
||||
onClick={() => {
|
||||
pinPlayed();
|
||||
trackLaunch();
|
||||
showPlayerModal({ backdrop: show.backdrop, title, hash, magnet, show, episodes: torrent.episodes });
|
||||
}}
|
||||
title={unplayable ? `Can’t stream in browser: ${unplayable}` : 'Play'}
|
||||
>
|
||||
<Play size={15} />
|
||||
<Play size={15} fill={launched ? 'currentColor' : 'none'} />
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
variant='outline'
|
||||
className='grow-0 px-3'
|
||||
onClick={() => toggle(key)}
|
||||
title={pinned ? 'Unpin' : 'Pin to top'}
|
||||
>
|
||||
<Bookmark size={16} className={cn(pinned && 'fill-current')} />
|
||||
</Button>
|
||||
<Button variant='outline' className='relative grow-0 px-3' title='Download m3u playlist'>
|
||||
<a
|
||||
className='absolute left-0 top-0 size-full'
|
||||
target='_blank'
|
||||
rel='noopener noreferrer'
|
||||
href={`${streamUrl}/stream?m3u&link=${encodeURIComponent(magnet)}`}
|
||||
/>
|
||||
<ListVideo size={20} />
|
||||
</Button>
|
||||
<Button className='relative grow-0 px-3' variant='outline' title='Download magnet'>
|
||||
<a className='absolute left-0 top-0 size-full' target='_blank' rel='noopener noreferrer' href={magnet} />
|
||||
<Magnet size={20} />
|
||||
</Button>
|
||||
{M3UUrl ? (
|
||||
<Button
|
||||
className='relative h-[32px] w-[45px] grow-0 px-3'
|
||||
variant='outline'
|
||||
title='Play m3u playlist'
|
||||
onClick={handleM3ULink}
|
||||
disabled={isStreamPending}
|
||||
>
|
||||
{isStreamPending ? (
|
||||
<div className='animate-spin'>
|
||||
<Loader size={18} />
|
||||
</div>
|
||||
) : (
|
||||
<Cast size={20} />
|
||||
)}
|
||||
</Button>
|
||||
) : (
|
||||
supportedForCast && (
|
||||
{showCast &&
|
||||
(M3UUrl ? (
|
||||
<Button
|
||||
className='grow-0 px-3'
|
||||
className={cn(
|
||||
'relative h-[32px] w-[45px] grow-0 px-3',
|
||||
supportedForStream && '-ml-px',
|
||||
launched && launchedCastClass
|
||||
)}
|
||||
variant='outline'
|
||||
title='Play m3u playlist'
|
||||
onClick={handleM3ULink}
|
||||
disabled={isStreamPending}
|
||||
>
|
||||
{isStreamPending ? (
|
||||
<div className='animate-spin'>
|
||||
<Loader size={18} />
|
||||
</div>
|
||||
) : (
|
||||
<Cast size={20} />
|
||||
)}
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
className={cn('grow-0 px-3', supportedForStream && '-ml-px', launched && launchedCastClass)}
|
||||
variant='outline'
|
||||
title='Cast to TV'
|
||||
onClick={() => {
|
||||
pinPlayed();
|
||||
trackLaunch();
|
||||
cast(magnet);
|
||||
}}
|
||||
>
|
||||
<Cast size={20} />
|
||||
</Button>
|
||||
)
|
||||
))}
|
||||
{!showCast && (
|
||||
<Button variant='outline' className='relative grow-0 px-3' title='Download m3u playlist'>
|
||||
<a className='absolute left-0 top-0 size-full' target='_blank' rel='noopener noreferrer' href={m3uUrl} />
|
||||
<ListVideo size={20} />
|
||||
</Button>
|
||||
)}
|
||||
{isStandalone && (
|
||||
{!showCast && (
|
||||
<Button className='relative grow-0 px-3' variant='outline' title='Download magnet'>
|
||||
<a className='absolute left-0 top-0 size-full' target='_blank' rel='noopener noreferrer' href={magnet} />
|
||||
<Magnet size={20} />
|
||||
</Button>
|
||||
)}
|
||||
{!showCast && isStandalone && (
|
||||
<Button className='relative grow-0 px-3' variant='outline' title='Play in VLC' asChild>
|
||||
<a
|
||||
className='absolute left-0 top-0 size-full'
|
||||
rel='noopener noreferrer'
|
||||
href={`vlc://${streamUrl}/stream?m3u&link=${encodeURIComponent(magnet)}`}
|
||||
/>
|
||||
<a className='absolute left-0 top-0 size-full' rel='noopener noreferrer' href={vlcUrl} />
|
||||
<TrafficCone size={19} />
|
||||
</Button>
|
||||
)}
|
||||
{showCast && (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button
|
||||
variant='outline'
|
||||
className='h-8 grow-0 rounded-none rounded-r-md border-l-0 px-3 focus-visible:ring-0 data-[state=open]:bg-accent'
|
||||
aria-label='More options'
|
||||
>
|
||||
<MoreHorizontal size={18} />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align='end'>
|
||||
<DropdownMenuItem asChild>
|
||||
<a target='_blank' rel='noopener noreferrer' href={m3uUrl}>
|
||||
<ListVideo />
|
||||
Download m3u playlist
|
||||
</a>
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem asChild>
|
||||
<a target='_blank' rel='noopener noreferrer' href={magnet}>
|
||||
<Magnet />
|
||||
Download magnet
|
||||
</a>
|
||||
</DropdownMenuItem>
|
||||
{isStandalone && (
|
||||
<DropdownMenuItem asChild>
|
||||
<a rel='noopener noreferrer' href={vlcUrl}>
|
||||
<TrafficCone />
|
||||
Play in VLC
|
||||
</a>
|
||||
</DropdownMenuItem>
|
||||
)}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
)}
|
||||
</ButtonsGroup>
|
||||
{!magnet && (
|
||||
<Button
|
||||
|
||||
@@ -3,9 +3,8 @@ import { ArrowDownIcon } from 'lucide-react';
|
||||
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
|
||||
import { Sort } from '@/data/parsers';
|
||||
import { cn, torrentKey } from '@/lib/utils';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
import usePinnedTorrents from '../../usePinnedTorrents';
|
||||
import showHostManagerModal from '../HostManager';
|
||||
import TableHeadSortable from '../TableHeadSortable';
|
||||
|
||||
@@ -34,8 +33,6 @@ const TorrentsTable: FC<Props> = ({ title, torrents, show, sort, sortable, provi
|
||||
|
||||
const [episodesSort, setEpisodesSort] = useState<EpisodesSort | null>(null);
|
||||
|
||||
const { isPinned } = usePinnedTorrents();
|
||||
|
||||
const sorted = episodesSort
|
||||
? [...torrents].sort((a, b) => {
|
||||
const [seasonA, episodeA] = seasonEpisode(a.episodes);
|
||||
@@ -46,11 +43,6 @@ const TorrentsTable: FC<Props> = ({ title, torrents, show, sort, sortable, provi
|
||||
})
|
||||
: torrents;
|
||||
|
||||
const rows = [
|
||||
...sorted.filter((torrent) => isPinned(torrentKey(torrent))),
|
||||
...sorted.filter((torrent) => !isPinned(torrentKey(torrent)))
|
||||
];
|
||||
|
||||
const handleChangeSort = (next: Sort) => {
|
||||
setEpisodesSort(null);
|
||||
onChangeSort(next);
|
||||
@@ -59,10 +51,10 @@ const TorrentsTable: FC<Props> = ({ title, torrents, show, sort, sortable, provi
|
||||
const toggleEpisodesSort = () => setEpisodesSort((current) => (current === 'asc' ? 'desc' : 'asc'));
|
||||
|
||||
return (
|
||||
<Table className='overflow-hidden rounded-xl'>
|
||||
<Table className='overflow-hidden'>
|
||||
{Boolean(torrents?.length) && (
|
||||
<TableHeader>
|
||||
<TableRow className='hover:bg-transparent'>
|
||||
<TableRow className='border-border/50 hover:bg-transparent'>
|
||||
<TableHead className='hidden px-2 md:table-cell'>Title</TableHead>
|
||||
{isSeries && (
|
||||
<TableHead className='hidden cursor-pointer select-none px-2 md:table-cell' onClick={toggleEpisodesSort}>
|
||||
@@ -97,36 +89,36 @@ const TorrentsTable: FC<Props> = ({ title, torrents, show, sort, sortable, provi
|
||||
)}
|
||||
|
||||
<TableBody>
|
||||
{rows?.map((torrent) => {
|
||||
{sorted.map((torrent) => {
|
||||
return (
|
||||
<Fragment key={torrent.magnet + torrent.id + torrent.download}>
|
||||
<TableRow className='table-row border-b-0 hover:bg-transparent md:hidden'>
|
||||
<TableCell className='break-all p-2' colSpan={colSpan}>
|
||||
<TableRow className='table-row border-b-0 md:hidden'>
|
||||
<TableCell className='break-all px-2 py-3' colSpan={colSpan}>
|
||||
<span className='flex w-full flex-wrap items-center gap-3'>
|
||||
{torrent.title} {isSeries && torrent.episodes && `[${torrent.episodes}]`}{' '}
|
||||
{torrent.quality && `[${torrent.quality}]`}
|
||||
</span>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
<TableRow className='hover:bg-transparent'>
|
||||
<TableCell className='hidden break-all p-2 md:table-cell' title={torrent.originalTitle}>
|
||||
<TableRow className='border-border/50'>
|
||||
<TableCell className='hidden break-all px-2 py-3 md:table-cell' title={torrent.originalTitle}>
|
||||
{torrent.title}
|
||||
</TableCell>
|
||||
{isSeries && (
|
||||
<TableCell className='hidden p-2 md:table-cell'>
|
||||
<TableCell className='hidden px-2 py-3 md:table-cell'>
|
||||
<div className='flex shrink-0 items-center gap-1'>{torrent.episodes}</div>
|
||||
</TableCell>
|
||||
)}
|
||||
<TableCell className='hidden p-2 lg:table-cell'>
|
||||
<TableCell className='hidden px-2 py-3 lg:table-cell'>
|
||||
<div className='flex shrink-0 items-center gap-1'>{torrent.year}</div>
|
||||
</TableCell>
|
||||
<TableCell className='hidden p-2 md:table-cell'>
|
||||
<TableCell className='hidden px-2 py-3 md:table-cell'>
|
||||
<div className='flex shrink-0 items-center gap-1'>{torrent.quality}</div>
|
||||
</TableCell>
|
||||
<TableCell className='truncate p-2'>
|
||||
<TableCell className='truncate px-2 py-3'>
|
||||
<div className='flex shrink-0 items-center gap-1'>{torrent.size}</div>
|
||||
</TableCell>
|
||||
<TableCell className='truncate p-2'>{torrent.seeders}</TableCell>
|
||||
<TableCell className='truncate px-2 py-3'>{torrent.seeders}</TableCell>
|
||||
<TableCell className='p-1 pr-2 text-center'>
|
||||
<Actions show={show} title={title} torrent={torrent} provider={provider} />
|
||||
</TableCell>
|
||||
|
||||
@@ -52,6 +52,10 @@ const showTorrentsModal = create(({ onResolve, ...show }: Props) => {
|
||||
|
||||
const isSeries = show.type === 'tv';
|
||||
|
||||
// Toloka's search is a plain "contains" word-AND match with no IMDB filter, so
|
||||
// scope the query by the show's year to keep, e.g., "Dune" (2021) away from "Dune: Part Two" (2024).
|
||||
const tolokaQuery = query && Number.isFinite(year) ? `${query} ${year}` : query;
|
||||
|
||||
const tracked = { id: show.id, type: show.type, title: show.title };
|
||||
|
||||
const yts = useTorrents({
|
||||
@@ -71,7 +75,7 @@ const showTorrentsModal = create(({ onResolve, ...show }: Props) => {
|
||||
show: tracked
|
||||
});
|
||||
const tlk = useTorrents({
|
||||
query,
|
||||
query: tolokaQuery,
|
||||
queryFn: providers.tlk.queryFn,
|
||||
sort,
|
||||
key: providers.tlk.key,
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { useAtom } from 'jotai';
|
||||
import { atomWithStorage } from 'jotai/utils';
|
||||
|
||||
const launchedAtom = atomWithStorage<string[]>('launched-torrents', [], undefined, {
|
||||
unstable_getOnInit: true
|
||||
});
|
||||
|
||||
const useLaunchedTorrents = () => {
|
||||
const [launched, setLaunched] = useAtom(launchedAtom);
|
||||
|
||||
const isLaunched = (key?: string) => Boolean(key) && launched.includes(key!);
|
||||
|
||||
const launch = (key?: string) =>
|
||||
void setLaunched((current) => (!key || current.includes(key) ? current : [...current, key]));
|
||||
|
||||
return { isLaunched, launch };
|
||||
};
|
||||
|
||||
export default useLaunchedTorrents;
|
||||
@@ -1,41 +1,21 @@
|
||||
import { useAtom } from 'jotai';
|
||||
import { atomWithStorage } from 'jotai/utils';
|
||||
|
||||
const pinnedAtom = atomWithStorage<string[]>('pinned-torrents', [], undefined, {
|
||||
unstable_getOnInit: true
|
||||
});
|
||||
|
||||
const magnetsAtom = atomWithStorage<Record<string, string>>('pinned-magnets', {}, undefined, {
|
||||
unstable_getOnInit: true
|
||||
});
|
||||
|
||||
const usePinnedTorrents = () => {
|
||||
const [pinned, setPinned] = useAtom(pinnedAtom);
|
||||
const [magnets, setMagnets] = useAtom(magnetsAtom);
|
||||
|
||||
const isPinned = (key?: string) => Boolean(key) && pinned.includes(key!);
|
||||
|
||||
const savedMagnet = (key?: string) => (key ? magnets[key] : undefined);
|
||||
|
||||
const forgetMagnet = (key: string) => void setMagnets(({ [key]: _removed, ...rest }) => rest);
|
||||
|
||||
const rememberMagnet = (key?: string, magnet?: string) =>
|
||||
void setMagnets((current) =>
|
||||
!key || !magnet || current[key] === magnet ? current : { ...current, [key]: magnet }
|
||||
);
|
||||
|
||||
const pin = (key?: string) =>
|
||||
void setPinned((current) => (!key || current.includes(key) ? current : [...current, key]));
|
||||
|
||||
const toggle = (key?: string) => {
|
||||
if (!key) return;
|
||||
|
||||
if (pinned.includes(key)) forgetMagnet(key);
|
||||
|
||||
setPinned((current) => (current.includes(key) ? current.filter((item) => item !== key) : [...current, key]));
|
||||
};
|
||||
|
||||
return { pinned, isPinned, pin, toggle, savedMagnet, rememberMagnet };
|
||||
return { savedMagnet, rememberMagnet };
|
||||
};
|
||||
|
||||
export default usePinnedTorrents;
|
||||
Reference in new issue
Block a user