From 461ff517214dcd4c2a4d6dfc2f4cf25735ebe33e Mon Sep 17 00:00:00 2001 From: ner Date: Thu, 6 Aug 2026 22:51:05 +0300 Subject: [PATCH] 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 --- .../components/Actions/index.tsx | 18 +++++++++--- .../components/TorrentsTable/index.tsx | 4 +-- .../components/Torrents/usePinnedTorrents.ts | 29 +++++++++++++++---- 3 files changed, 39 insertions(+), 12 deletions(-) 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 f9a0250..0839a8e 100644 --- a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx +++ b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx @@ -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 = ({ 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}'); diff --git a/components/Preview/components/Torrents/components/TorrentsTable/index.tsx b/components/Preview/components/Torrents/components/TorrentsTable/index.tsx index 517a686..5a89f2d 100644 --- a/components/Preview/components/Torrents/components/TorrentsTable/index.tsx +++ b/components/Preview/components/Torrents/components/TorrentsTable/index.tsx @@ -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 = ({ 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' )} > - + ); }; diff --git a/components/Preview/components/Torrents/usePinnedTorrents.ts b/components/Preview/components/Torrents/usePinnedTorrents.ts index 3617f66..67f0c96 100644 --- a/components/Preview/components/Torrents/usePinnedTorrents.ts +++ b/components/Preview/components/Torrents/usePinnedTorrents.ts @@ -5,22 +5,39 @@ const pinnedAtom = atomWithStorage('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>('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;