@@ -8,11 +8,11 @@ import {
8
8
HStack ,
9
9
} from "@chakra-ui/react" ;
10
10
import axios from "axios" ;
11
- import { Suspense , useMemo , useState } from "react" ;
11
+ import { Suspense , useMemo , useState , useCallback } from "react" ;
12
12
import { useTranslation } from "react-i18next" ;
13
13
import { FiArrowLeft , FiArrowRight } from "react-icons/fi" ;
14
14
import { useQuery } from "react-query" ;
15
- import { useNavigate , useParams } from "react-router-dom" ;
15
+ import { useNavigate , useParams , useSearchParams } from "react-router-dom" ;
16
16
import { QueryStatus } from "../components/common/QueryStatus" ;
17
17
import { SongTable } from "../components/data/SongTable" ;
18
18
import { DEFAULT_FETCH_CONFIG } from "../modules/services/defaults" ;
@@ -21,18 +21,21 @@ import { useSongQueuer } from "../utils/SongQueuerHook";
21
21
const PERPAGE = 10 ;
22
22
export default function ChannelSongs ( ) {
23
23
const { t } = useTranslation ( ) ;
24
- let params = useParams ( ) ;
25
- let channelId = params . id ! ;
24
+ const params = useParams ( ) ;
25
+ const [ searchParams , setSearchParams ] = useSearchParams ( ) ;
26
+ const channelId = params . id ! ;
26
27
27
28
const { data : channel , ...channelStatus } = useQuery (
28
29
[ "channel" , channelId ] ,
29
30
async ( q ) => {
30
31
return ( await axios . get ( "/api/v2/channels/" + q . queryKey [ 1 ] ) ) . data ;
31
32
} ,
32
- { ...DEFAULT_FETCH_CONFIG , cacheTime : 600000 /* 10 mins */ }
33
+ { ...DEFAULT_FETCH_CONFIG , cacheTime : 600000 /* 10 mins */ } ,
33
34
) ;
34
35
35
- const [ offset , setOffset ] = useState ( 0 ) ;
36
+ const [ offset , setOffset ] = useState (
37
+ Math . max ( ( Number ( searchParams . get ( "page" ) ) - 1 ) * PERPAGE , 0 ) ,
38
+ ) ;
36
39
const { data, ...songStatus } = useSongAPI ( {
37
40
channel_id : channelId ,
38
41
paginated : true ,
@@ -42,8 +45,18 @@ export default function ChannelSongs() {
42
45
43
46
const { items : latest , total } = useMemo (
44
47
( ) => ( data as any ) || { items : undefined , total : 0 } ,
45
- [ data ]
48
+ [ data ] ,
49
+ ) ;
50
+
51
+ const onPageChange = useCallback (
52
+ ( nextOffset : number ) => {
53
+ setOffset ( nextOffset ) ;
54
+ searchParams . set ( "page" , ( nextOffset / PERPAGE + 1 ) . toString ( ) ) ;
55
+ setSearchParams ( searchParams , { replace : true } ) ;
56
+ } ,
57
+ [ searchParams , setSearchParams ] ,
46
58
) ;
59
+
47
60
const queueSongs = useSongQueuer ( ) ;
48
61
const navigate = useNavigate ( ) ;
49
62
@@ -96,11 +109,14 @@ export default function ChannelSongs() {
96
109
< ButtonGroup colorScheme = "brand" mt = "3" spacing = "5" >
97
110
< Button
98
111
onClick = { ( ) =>
99
- setOffset ( ( e ) =>
112
+ onPageChange (
100
113
Math . max (
101
- Math . min ( Math . floor ( total / PERPAGE ) * PERPAGE , e - PERPAGE ) ,
102
- 0
103
- )
114
+ Math . min (
115
+ Math . floor ( total / PERPAGE ) * PERPAGE ,
116
+ offset - PERPAGE ,
117
+ ) ,
118
+ 0 ,
119
+ ) ,
104
120
)
105
121
}
106
122
disabled = { offset === 0 }
@@ -113,14 +129,14 @@ export default function ChannelSongs() {
113
129
fontSize = "xl"
114
130
onSubmit = { ( e ) => {
115
131
const n = Number . parseInt ( e ) ;
116
- setOffset (
132
+ onPageChange (
117
133
Math . max (
118
134
Math . min (
119
135
Math . floor ( total / PERPAGE ) * PERPAGE ,
120
- ( n - 1 ) * PERPAGE
136
+ ( n - 1 ) * PERPAGE ,
121
137
) ,
122
- 0
123
- )
138
+ 0 ,
139
+ ) ,
124
140
) ;
125
141
} }
126
142
>
@@ -134,11 +150,14 @@ export default function ChannelSongs() {
134
150
</ Editable >
135
151
< Button
136
152
onClick = { ( ) =>
137
- setOffset ( ( e ) =>
153
+ onPageChange (
138
154
Math . max (
139
- Math . min ( Math . floor ( total / PERPAGE ) * PERPAGE , e + PERPAGE ) ,
140
- 0
141
- )
155
+ Math . min (
156
+ Math . floor ( total / PERPAGE ) * PERPAGE ,
157
+ offset + PERPAGE ,
158
+ ) ,
159
+ 0 ,
160
+ ) ,
142
161
)
143
162
}
144
163
disabled = { offset + PERPAGE > total }
0 commit comments