feat(torrents): persist pinned Toloka magnets, bookmark icon

Cache the fetched magnet of a pinned torrent in localStorage so a
pinned Toloka row stays playable right after a reload without
re-fetching from Toloka; the magnet is dropped when unpinned. Also
switch the pin control to a bookmark icon.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
nerandClaude Fable 5 committed 2026-08-06 22:51:05 +03:00
1 parent 06f142510b
commit 461ff51721
3 files changed
+39 -12

No files matched your search

@@ -1,4 +1,4 @@
import { FC, useMemo, useState } from 'react';
import { FC, useEffect, useMemo, useState } from 'react';
import { Cast, Download, ListVideo, Loader, Magnet, Play, TrafficCone } from 'lucide-react';
import { providers } from '@/components/Preview/components/Torrents/constants';
@@ -45,14 +45,24 @@ const Actions: FC<Props> = ({ torrent, title, provider, show }) => {
const fetchMagnet = useMagnet(torrent);
const { pin } = usePinnedTorrents();
const { pin, isPinned, savedMagnet, rememberMagnet } = usePinnedTorrents();
const magnet = fetchMagnet.data || torrent.magnet;
const key = torrentKey(torrent);
const pinned = isPinned(key);
// savedMagnet keeps a pinned Toloka torrent playable after a reload without re-fetching
const magnet = fetchMagnet.data || torrent.magnet || savedMagnet(key) || '';
const hash = getMagnetHash(magnet);
// persist the fetched magnet while pinned; only Toloka needs it (others carry it in the result)
useEffect(() => {
if (pinned && magnet && !torrent.magnet) rememberMagnet(key, magnet);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [pinned, magnet, key, torrent.magnet]);
// starting to watch a torrent pins it to the top for next time
const pinPlayed = () => pin(torrentKey(torrent));
const pinPlayed = () => pin(key);
const supportedForCast = prefix && !prefix.includes('{host}');
@@ -1,5 +1,5 @@
import { FC, Fragment, MouseEvent, useState } from 'react';
import { ArrowDownIcon, Pin } from 'lucide-react';
import { ArrowDownIcon, Bookmark } from 'lucide-react';
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
import { Sort } from '@/data/parsers';
@@ -72,7 +72,7 @@ const TorrentsTable: FC<Props> = ({ title, torrents, show, sort, sortable, provi
: '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')} />
<Bookmark className={cn('size-4', pinned && 'fill-current')} />
</button>
);
};
@@ -5,22 +5,39 @@ const pinnedAtom = atomWithStorage<string[]>('pinned-torrents', [], undefined, {
unstable_getOnInit: true
});
// Toloka magnets are fetched lazily, so cache the fetched magnet of a pinned torrent
// to keep it playable right after a reload without hitting Toloka again
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) =>
void setPinned((current) => {
if (!key) return current;
const toggle = (key?: string) => {
if (!key) return;
return current.includes(key) ? current.filter((item) => item !== key) : [...current, key];
});
if (pinned.includes(key)) forgetMagnet(key);
return { pinned, isPinned, pin, toggle };
setPinned((current) => (current.includes(key) ? current.filter((item) => item !== key) : [...current, key]));
};
return { pinned, isPinned, pin, toggle, savedMagnet, rememberMagnet };
};
export default usePinnedTorrents;