diff --git a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx index 80dcaf1..f9a0250 100644 --- a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx +++ b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx @@ -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 = ({ 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 = ({ 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 = ({ torrent, title, provider, show }) => { ) : ( supportedForCast && ( - ) diff --git a/components/Preview/components/Torrents/components/TorrentsTable/index.tsx b/components/Preview/components/Torrents/components/TorrentsTable/index.tsx index 4b376bc..517a686 100644 --- a/components/Preview/components/Torrents/components/TorrentsTable/index.tsx +++ b/components/Preview/components/Torrents/components/TorrentsTable/index.tsx @@ -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 = ({ title, torrents, show, sort, sortable, provi const [episodesSort, setEpisodesSort] = useState(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 = ({ 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 ( + + ); + }; + // 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 = ({ title, torrents, show, sort, sortable, provi return ( - - - {torrent.title} {isSeries && torrent.episodes && `[${torrent.episodes}]`}{' '} - {torrent.quality && `[${torrent.quality}]`} + + + {renderPin(torrent, true)} + + {torrent.title} {isSeries && torrent.episodes && `[${torrent.episodes}]`}{' '} + {torrent.quality && `[${torrent.quality}]`} + - + - {torrent.title} + + {renderPin(torrent)} + {torrent.title} + {isSeries && ( diff --git a/components/Preview/components/Torrents/usePinnedTorrents.ts b/components/Preview/components/Torrents/usePinnedTorrents.ts new file mode 100644 index 0000000..3617f66 --- /dev/null +++ b/components/Preview/components/Torrents/usePinnedTorrents.ts @@ -0,0 +1,26 @@ +import { useAtom } from 'jotai'; +import { atomWithStorage } from 'jotai/utils'; + +const pinnedAtom = atomWithStorage('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; diff --git a/lib/utils.ts b/lib/utils.ts index a75cc71..f2d5f97 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -32,6 +32,11 @@ export const getMagnetHash = (link: string) => { 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) => { if (bytes == null || !isFinite(bytes)) return '';