feat(torrents): pin played torrents to the top

Pin torrents (via the pin toggle or automatically on play/stream) so
they stick to the top of the list regardless of the active sort, and
persist the pins to localStorage. The pin sits before the title,
revealing on row hover on desktop and always shown on touch or when
pinned.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
nerandClaude Fable 5 committed 2026-08-06 22:38:57 +03:00
1 parent c144181881
commit 06f142510b
4 files changed
+98 -12

No files matched your search

@@ -8,7 +8,9 @@ import ButtonsGroup from '@/components/ui/buttons-group';
import { Quality } from '@/data/parsers/yts/models';
import { useM3UUrl } from '@/hooks/useM3UStreamUrl';
import { useStreamUrl } from '@/hooks/useStreamUrl';
import { cn, detectSafari, getMagnetHash, isStandaloneApp } from '@/lib/utils';
import { cn, detectSafari, getMagnetHash, isStandaloneApp, torrentKey } from '@/lib/utils';
import usePinnedTorrents from '../../../../usePinnedTorrents';
import showPlayerModal from './components/Player';
import useMagnet from './useMagnet';
@@ -43,13 +45,19 @@ const Actions: FC<Props> = ({ torrent, title, provider, show }) => {
const fetchMagnet = useMagnet(torrent);
const { pin } = usePinnedTorrents();
const magnet = fetchMagnet.data || torrent.magnet;
const hash = getMagnetHash(magnet);
// starting to watch a torrent pins it to the top for next time
const pinPlayed = () => pin(torrentKey(torrent));
const supportedForCast = prefix && !prefix.includes('{host}');
const handleM3ULink = () => {
pinPlayed();
setIsStreamPending(true);
const params = new URLSearchParams({
@@ -89,6 +97,7 @@ const Actions: FC<Props> = ({ torrent, title, provider, show }) => {
{ invisible: !magnet || fetchMagnet.isPending }
)}
onClick={() => {
pinPlayed();
showPlayerModal({ backdrop: show.backdrop, title, hash, magnet });
}}
title='Play'
@@ -127,7 +136,14 @@ const Actions: FC<Props> = ({ torrent, title, provider, show }) => {
</Button>
) : (
supportedForCast && (
<Button className='grow-0 px-3' variant='outline' onClick={() => cast(magnet)}>
<Button
className='grow-0 px-3'
variant='outline'
onClick={() => {
pinPlayed();
cast(magnet);
}}
>
<Cast size={20} />
</Button>
)
@@ -1,10 +1,11 @@
import { FC, Fragment, useState } from 'react';
import { ArrowDownIcon } from 'lucide-react';
import { FC, Fragment, MouseEvent, useState } from 'react';
import { ArrowDownIcon, Pin } from 'lucide-react';
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
import { Sort } from '@/data/parsers';
import { cn } from '@/lib/utils';
import { cn, torrentKey } from '@/lib/utils';
import usePinnedTorrents from '../../usePinnedTorrents';
import showHostManagerModal from '../HostManager';
import TableHeadSortable from '../TableHeadSortable';
@@ -34,7 +35,9 @@ const TorrentsTable: FC<Props> = ({ title, torrents, show, sort, sortable, provi
const [episodesSort, setEpisodesSort] = useState<EpisodesSort | null>(null);
const rows = episodesSort
const { isPinned, toggle } = usePinnedTorrents();
const sorted = episodesSort
? [...torrents].sort((a, b) => {
const [seasonA, episodeA] = seasonEpisode(a.episodes);
const [seasonB, episodeB] = seasonEpisode(b.episodes);
@@ -44,6 +47,36 @@ const TorrentsTable: FC<Props> = ({ title, torrents, show, sort, sortable, provi
})
: torrents;
// pinned (previously played) torrents stick to the top, keeping their order within each group
const rows = [
...sorted.filter((torrent) => isPinned(torrentKey(torrent))),
...sorted.filter((torrent) => !isPinned(torrentKey(torrent)))
];
const renderPin = (torrent: Torrent, mobile?: boolean) => {
const pinned = isPinned(torrentKey(torrent));
const onClick = (event: MouseEvent) => {
event.stopPropagation();
toggle(torrentKey(torrent));
};
return (
<button
type='button'
onClick={onClick}
aria-label={pinned ? 'Unpin torrent' : 'Pin torrent to top'}
className={cn(
'flex shrink-0 items-center overflow-hidden text-muted-foreground transition-all duration-200 hover:text-foreground',
pinned || mobile
? 'w-5 opacity-100'
: 'w-5 opacity-100 [@media(hover:hover)]:w-0 [@media(hover:hover)]:opacity-0 [@media(hover:hover)]:group-hover:w-5 [@media(hover:hover)]:group-hover:opacity-100'
)}
>
<Pin className={cn('size-4', pinned && 'fill-current')} />
</button>
);
};
// server sort (size/seeds) refetches in a new order, so drop the client-side S:E sort
const handleChangeSort = (next: Sort) => {
setEpisodesSort(null);
@@ -95,16 +128,22 @@ const TorrentsTable: FC<Props> = ({ title, torrents, show, sort, sortable, provi
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}>
<span className='flex w-full flex-wrap items-center gap-3'>
{torrent.title} {isSeries && torrent.episodes && `[${torrent.episodes}]`}{' '}
{torrent.quality && `[${torrent.quality}]`}
<TableCell className='p-2' colSpan={colSpan}>
<span className='flex w-full flex-wrap items-center gap-1'>
{renderPin(torrent, true)}
<span className='break-all'>
{torrent.title} {isSeries && torrent.episodes && `[${torrent.episodes}]`}{' '}
{torrent.quality && `[${torrent.quality}]`}
</span>
</span>
</TableCell>
</TableRow>
<TableRow className='hover:bg-transparent'>
<TableRow className='group hover:bg-transparent'>
<TableCell className='hidden break-all p-2 md:table-cell' title={torrent.originalTitle}>
{torrent.title}
<span className='flex items-center'>
{renderPin(torrent)}
<span className='break-all'>{torrent.title}</span>
</span>
</TableCell>
{isSeries && (
<TableCell className='hidden p-2 md:table-cell'>
@@ -0,0 +1,26 @@
import { useAtom } from 'jotai';
import { atomWithStorage } from 'jotai/utils';
const pinnedAtom = atomWithStorage<string[]>('pinned-torrents', [], undefined, {
unstable_getOnInit: true
});
const usePinnedTorrents = () => {
const [pinned, setPinned] = useAtom(pinnedAtom);
const isPinned = (key?: string) => Boolean(key) && pinned.includes(key!);
const pin = (key?: string) =>
void setPinned((current) => (!key || current.includes(key) ? current : [...current, key]));
const toggle = (key?: string) =>
void setPinned((current) => {
if (!key) return current;
return current.includes(key) ? current.filter((item) => item !== key) : [...current, key];
});
return { pinned, isPinned, pin, toggle };
};
export default usePinnedTorrents;