1- import React from "react" ;
1+ import React , { useEffect } from "react" ;
22import getConfig from "next/config" ;
33import { useRouter } from "next/router" ;
44import moment from "moment" ;
55import slugify from "slugify" ;
6+ import qs from "qs" ;
67import { ListUl } from "@styled-icons/boxicons-regular/ListUl" ;
78import { Upload } from "@styled-icons/boxicons-regular/Upload" ;
89import { Download } from "@styled-icons/boxicons-regular/Download" ;
@@ -18,28 +19,62 @@ import Text from "./Text";
1819import Box from "./Box" ;
1920import Button from "./Button" ;
2021
21- const TorrentList = ( { torrents = [ ] , categories, total } ) => {
22+ const pageSize = 25 ;
23+
24+ const TorrentList = ( {
25+ torrents = [ ] ,
26+ setTorrents,
27+ categories,
28+ total,
29+ fetchPath,
30+ token,
31+ } ) => {
2232 const {
2333 publicRuntimeConfig : { SQ_SITE_WIDE_FREELEECH } ,
2434 } = getConfig ( ) ;
2535
2636 const router = useRouter ( ) ;
2737 const {
28- asPath,
29- query : { page : pageParam } ,
38+ query : { page : pageParam , sort } ,
3039 } = router ;
3140
3241 const page = pageParam ? parseInt ( pageParam ) - 1 : 0 ;
3342
34- const maxPage = Math . floor ( total / 25 ) ;
43+ const maxPage = total > pageSize ? Math . floor ( total / pageSize ) : 0 ;
3544 const canPrevPage = page > 0 ;
3645 const canNextPage = page < maxPage ;
3746
3847 const setPage = ( number ) => {
39- if ( number === 0 ) router . push ( asPath . split ( "?" ) [ 0 ] ) ;
40- else router . push ( `${ asPath . split ( "?" ) [ 0 ] } ?page=${ number + 1 } ` ) ;
48+ const query = qs . parse ( window . location . search . replace ( "?" , "" ) ) ;
49+ if ( number === 0 ) delete query . page ;
50+ else query . page = number + 1 ;
51+ router . push (
52+ Object . keys ( query ) . length
53+ ? `${ window . location . pathname } ?${ qs . stringify ( query ) } `
54+ : window . location . pathname
55+ ) ;
4156 } ;
4257
58+ useEffect ( ( ) => {
59+ const fetchTorrents = async ( ) => {
60+ try {
61+ const searchRes = await fetch (
62+ `${ fetchPath } ?${ qs . stringify ( router . query ) } ` ,
63+ {
64+ headers : {
65+ Authorization : `Bearer ${ token } ` ,
66+ "Content-Type" : "application/json" ,
67+ } ,
68+ }
69+ ) ;
70+
71+ const results = await searchRes . json ( ) ;
72+ setTorrents ( results . torrents ) ;
73+ } catch ( e ) { }
74+ } ;
75+ if ( fetchPath && token ) fetchTorrents ( ) ;
76+ } , [ sort , page ] ) ;
77+
4378 return (
4479 < >
4580 < List
@@ -95,6 +130,7 @@ const TorrentList = ({ torrents = [], categories, total }) => {
95130 ) ,
96131 gridWidth : "100px" ,
97132 rightAlign : true ,
133+ sortable : ! ! token ,
98134 } ,
99135 {
100136 header : "Leechers" ,
@@ -109,6 +145,7 @@ const TorrentList = ({ torrents = [], categories, total }) => {
109145 ) ,
110146 gridWidth : "100px" ,
111147 rightAlign : true ,
148+ sortable : ! ! token ,
112149 } ,
113150 {
114151 header : "Downloads" ,
@@ -121,8 +158,9 @@ const TorrentList = ({ torrents = [], categories, total }) => {
121158 { value || 0 }
122159 </ Text >
123160 ) ,
124- gridWidth : "100px " ,
161+ gridWidth : "115px " ,
125162 rightAlign : true ,
163+ sortable : ! ! token ,
126164 } ,
127165 {
128166 header : "Comments" ,
@@ -135,8 +173,9 @@ const TorrentList = ({ torrents = [], categories, total }) => {
135173 { value || 0 }
136174 </ Text >
137175 ) ,
138- gridWidth : "100px " ,
176+ gridWidth : "110px " ,
139177 rightAlign : true ,
178+ sortable : ! ! token ,
140179 } ,
141180 {
142181 header : "Uploaded" ,
@@ -146,6 +185,7 @@ const TorrentList = ({ torrents = [], categories, total }) => {
146185 ) ,
147186 gridWidth : "140px" ,
148187 rightAlign : true ,
188+ sortable : ! ! token ,
149189 } ,
150190 ] }
151191 />
0 commit comments