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 { Quality } from '@/data/parsers/yts/models';
import { useM3UUrl } from '@/hooks/useM3UStreamUrl'; import { useM3UUrl } from '@/hooks/useM3UStreamUrl';
import { useStreamUrl } from '@/hooks/useStreamUrl'; 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 showPlayerModal from './components/Player';
import useMagnet from './useMagnet'; import useMagnet from './useMagnet';
@@ -43,13 +45,19 @@ const Actions: FC<Props> = ({ torrent, title, provider, show }) => {
const fetchMagnet = useMagnet(torrent); const fetchMagnet = useMagnet(torrent);
const { pin } = usePinnedTorrents();
const magnet = fetchMagnet.data || torrent.magnet; const magnet = fetchMagnet.data || torrent.magnet;
const hash = getMagnetHash(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 supportedForCast = prefix && !prefix.includes('{host}');
const handleM3ULink = () => { const handleM3ULink = () => {
pinPlayed();
setIsStreamPending(true); setIsStreamPending(true);
const params = new URLSearchParams({ const params = new URLSearchParams({
@@ -89,6 +97,7 @@ const Actions: FC<Props> = ({ torrent, title, provider, show }) => {
{ invisible: !magnet || fetchMagnet.isPending } { invisible: !magnet || fetchMagnet.isPending }
)} )}
onClick={() => { onClick={() => {
pinPlayed();
showPlayerModal({ backdrop: show.backdrop, title, hash, magnet }); showPlayerModal({ backdrop: show.backdrop, title, hash, magnet });
}} }}
title='Play' title='Play'
@@ -127,7 +136,14 @@ const Actions: FC<Props> = ({ torrent, title, provider, show }) => {
</Button> </Button>
) : ( ) : (
supportedForCast && ( 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} /> <Cast size={20} />
</Button> </Button>
) )
@@ -1,10 +1,11 @@
import { FC, Fragment, useState } from 'react'; import { FC, Fragment, MouseEvent, useState } from 'react';
import { ArrowDownIcon } from 'lucide-react'; import { ArrowDownIcon, Pin } from 'lucide-react';
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
import { Sort } from '@/data/parsers'; 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 showHostManagerModal from '../HostManager';
import TableHeadSortable from '../TableHeadSortable'; 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 [episodesSort, setEpisodesSort] = useState<EpisodesSort | null>(null);
const rows = episodesSort const { isPinned, toggle } = usePinnedTorrents();
const sorted = episodesSort
? [...torrents].sort((a, b) => { ? [...torrents].sort((a, b) => {
const [seasonA, episodeA] = seasonEpisode(a.episodes); const [seasonA, episodeA] = seasonEpisode(a.episodes);
const [seasonB, episodeB] = seasonEpisode(b.episodes); const [seasonB, episodeB] = seasonEpisode(b.episodes);
@@ -44,6 +47,36 @@ const TorrentsTable: FC<Props> = ({ title, torrents, show, sort, sortable, provi
}) })
: torrents; : 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 // server sort (size/seeds) refetches in a new order, so drop the client-side S:E sort
const handleChangeSort = (next: Sort) => { const handleChangeSort = (next: Sort) => {
setEpisodesSort(null); setEpisodesSort(null);
@@ -95,16 +128,22 @@ const TorrentsTable: FC<Props> = ({ title, torrents, show, sort, sortable, provi
return ( return (
<Fragment key={torrent.magnet + torrent.id + torrent.download}> <Fragment key={torrent.magnet + torrent.id + torrent.download}>
<TableRow className='table-row border-b-0 hover:bg-transparent md:hidden'> <TableRow className='table-row border-b-0 hover:bg-transparent md:hidden'>
<TableCell className='break-all p-2' colSpan={colSpan}> <TableCell className='p-2' colSpan={colSpan}>
<span className='flex w-full flex-wrap items-center gap-3'> <span className='flex w-full flex-wrap items-center gap-1'>
{torrent.title} {isSeries && torrent.episodes && `[${torrent.episodes}]`}{' '} {renderPin(torrent, true)}
{torrent.quality && `[${torrent.quality}]`} <span className='break-all'>
{torrent.title} {isSeries && torrent.episodes && `[${torrent.episodes}]`}{' '}
{torrent.quality && `[${torrent.quality}]`}
</span>
</span> </span>
</TableCell> </TableCell>
</TableRow> </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}> <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> </TableCell>
{isSeries && ( {isSeries && (
<TableCell className='hidden p-2 md:table-cell'> <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;
+5
View File
@@ -32,6 +32,11 @@ export const getMagnetHash = (link: string) => {
return hash?.toUpperCase(); return hash?.toUpperCase();
}; };
// Stable identity for a torrent that survives the lazy empty -> fetched magnet transition:
// Toloka has a permanent download URL, the rest always carry a magnet infohash.
export const torrentKey = (torrent: Torrent) =>
torrent.download || getMagnetHash(torrent.magnet) || torrent.hash || torrent.id;
export const formatBytes = (bytes?: number) => { export const formatBytes = (bytes?: number) => {
if (bytes == null || !isFinite(bytes)) return ''; if (bytes == null || !isFinite(bytes)) return '';