1616 * along with this program. If not, see <https://www.gnu.org/licenses/>.
1717 */
1818
19- import React , { useCallback , useContext , useEffect } from "react" ;
19+ import React , { useCallback , useEffect , useMemo } from "react" ;
2020import type { ModalState } from "./common" ;
21- import { SaveCancelModal } from "./common" ;
21+ import { SaveCancelModal , TorrentsNames } from "./common" ;
2222import { useForm } from "@mantine/form" ;
2323import { useMutateTorrent , useTorrentDetails } from "queries" ;
2424import { notifications } from "@mantine/notifications" ;
25- import { Button , Checkbox , Grid , LoadingOverlay , NumberInput , Text , Textarea } from "@mantine/core" ;
26- import { ConfigContext } from "config" ;
27- import type { TrackerStats } from "rpc/torrent" ;
28- import { useServerRpcVersion , useServerTorrentData } from "rpc/torrent" ;
25+ import { Checkbox , Grid , LoadingOverlay , NumberInput } from "@mantine/core" ;
26+ import { useServerSelectedTorrents , useServerTorrentData } from "rpc/torrent" ;
2927
3028interface FormValues {
3129 downloadLimited ?: boolean ,
@@ -37,16 +35,20 @@ interface FormValues {
3735 seedRatioLimit : number ,
3836 seedIdleMode : number ,
3937 seedIdleLimit : number ,
40- trackerList : string ,
4138 honorsSessionLimits : boolean ,
4239 sequentialDownload : boolean ,
4340}
4441
4542export function EditTorrent ( props : ModalState ) {
46- const config = useContext ( ConfigContext ) ;
4743 const serverData = useServerTorrentData ( ) ;
48- const torrentId = serverData . current ;
49- const rpcVersion = useServerRpcVersion ( ) ;
44+ const selected = useServerSelectedTorrents ( ) ;
45+
46+ const torrentId = useMemo ( ( ) => {
47+ if ( serverData . current === undefined || ! selected . has ( serverData . current ) ) {
48+ return [ ...selected ] [ 0 ] ;
49+ }
50+ return serverData . current ;
51+ } , [ selected , serverData ] ) ;
5052
5153 const { data : torrent , isLoading } = useTorrentDetails (
5254 torrentId ?? - 1 , torrentId !== undefined && props . opened , false , true ) ;
@@ -66,40 +68,22 @@ export function EditTorrent(props: ModalState) {
6668 seedRatioLimit : torrent . seedRatioLimit ,
6769 seedIdleMode : torrent . seedIdleMode ,
6870 seedIdleLimit : torrent . seedIdleLimit ,
69- trackerList : rpcVersion >= 17
70- ? torrent . trackerList
71- : torrent . trackerStats . map ( ( s : TrackerStats ) => s . announce ) . join ( "\n" ) ,
7271 honorsSessionLimits : torrent . honorsSessionLimits ,
7372 sequentialDownload : torrent . sequentialDownload ,
7473 } ) ;
75- } , [ rpcVersion , setValues , torrent ] ) ;
74+ } , [ setValues , torrent ] ) ;
7675
7776 const mutation = useMutateTorrent ( ) ;
7877
7978 const onSave = useCallback ( ( ) => {
8079 if ( torrentId === undefined || torrent === undefined ) return ;
81- let toAdd ;
82- let toRemove ;
83- if ( rpcVersion < 17 ) {
84- const trackers = form . values . trackerList . split ( "\n" ) . filter ( ( s ) => s !== "" ) ;
85- const currentTrackers = Object . fromEntries (
86- torrent . trackerStats . map ( ( s : TrackerStats ) => [ s . announce , s . id ] ) ) ;
8780
88- toAdd = trackers . filter ( ( t ) => ! Object . hasOwn ( currentTrackers , t ) ) ;
89- toRemove = ( torrent . trackerStats as TrackerStats [ ] )
90- . filter ( ( s : TrackerStats ) => ! trackers . includes ( s . announce ) )
91- . map ( ( s : TrackerStats ) => s . id as number ) ;
92- if ( toAdd . length === 0 ) toAdd = undefined ;
93- if ( toRemove . length === 0 ) toRemove = undefined ;
94- }
9581 mutation . mutate (
9682 {
97- torrentIds : [ torrentId ] ,
83+ torrentIds : [ ... selected ] ,
9884 fields : {
9985 ...form . values ,
10086 "peer-limit" : form . values . peerLimit ,
101- trackerAdd : toAdd ,
102- trackerRemove : toRemove ,
10387 } ,
10488 } ,
10589 {
@@ -113,14 +97,7 @@ export function EditTorrent(props: ModalState) {
11397 } ,
11498 ) ;
11599 props . close ( ) ;
116- } , [ form . values , mutation , torrent , props , rpcVersion , torrentId ] ) ;
117-
118- const addDefaultTrackers = useCallback ( ( ) => {
119- let list = form . values . trackerList ;
120- if ( ! list . endsWith ( "\n" ) ) list += "\n" ;
121- list += config . values . interface . defaultTrackers . join ( "\n" ) ;
122- form . setFieldValue ( "trackerList" , list ) ;
123- } , [ config , form ] ) ;
100+ } , [ torrentId , torrent , mutation , selected , form . values , props ] ) ;
124101
125102 return < > { props . opened &&
126103 < SaveCancelModal
@@ -135,7 +112,7 @@ export function EditTorrent(props: ModalState) {
135112 < LoadingOverlay visible = { isLoading } />
136113 < Grid align = "center" >
137114 < Grid . Col >
138- Torrent: { torrent ?. name }
115+ < TorrentsNames />
139116 </ Grid . Col >
140117 < Grid . Col span = { 8 } >
141118 < Checkbox my = "sm"
@@ -216,16 +193,6 @@ export function EditTorrent(props: ModalState) {
216193 < Grid . Col span = { 2 } >
217194 minutes
218195 </ Grid . Col >
219- < Grid . Col span = { 8 } >
220- < Text > Tracker list, one per line, empty line between tiers</ Text >
221- </ Grid . Col >
222- < Grid . Col span = { 4 } >
223- < Button onClick = { addDefaultTrackers } > Add default list</ Button >
224- </ Grid . Col >
225- < Grid . Col >
226- < Textarea minRows = { 6 }
227- { ...form . getInputProps ( "trackerList" ) } />
228- </ Grid . Col >
229196 </ Grid >
230197 </ SaveCancelModal > }
231198 </ > ;
0 commit comments