Skip to content

Commit 2eb2754

Browse files
committed
Add page param to channel songs page
1 parent 8a9465d commit 2eb2754

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

src/pages/ChannelSongs.tsx

+38-19
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import {
88
HStack,
99
} from "@chakra-ui/react";
1010
import axios from "axios";
11-
import { Suspense, useMemo, useState } from "react";
11+
import { Suspense, useMemo, useState, useCallback } from "react";
1212
import { useTranslation } from "react-i18next";
1313
import { FiArrowLeft, FiArrowRight } from "react-icons/fi";
1414
import { useQuery } from "react-query";
15-
import { useNavigate, useParams } from "react-router-dom";
15+
import { useNavigate, useParams, useSearchParams } from "react-router-dom";
1616
import { QueryStatus } from "../components/common/QueryStatus";
1717
import { SongTable } from "../components/data/SongTable";
1818
import { DEFAULT_FETCH_CONFIG } from "../modules/services/defaults";
@@ -21,18 +21,21 @@ import { useSongQueuer } from "../utils/SongQueuerHook";
2121
const PERPAGE = 10;
2222
export default function ChannelSongs() {
2323
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!;
2627

2728
const { data: channel, ...channelStatus } = useQuery(
2829
["channel", channelId],
2930
async (q) => {
3031
return (await axios.get("/api/v2/channels/" + q.queryKey[1])).data;
3132
},
32-
{ ...DEFAULT_FETCH_CONFIG, cacheTime: 600000 /* 10 mins */ }
33+
{ ...DEFAULT_FETCH_CONFIG, cacheTime: 600000 /* 10 mins */ },
3334
);
3435

35-
const [offset, setOffset] = useState(0);
36+
const [offset, setOffset] = useState(
37+
Math.max((Number(searchParams.get("page")) - 1) * PERPAGE, 0),
38+
);
3639
const { data, ...songStatus } = useSongAPI({
3740
channel_id: channelId,
3841
paginated: true,
@@ -42,8 +45,18 @@ export default function ChannelSongs() {
4245

4346
const { items: latest, total } = useMemo(
4447
() => (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],
4658
);
59+
4760
const queueSongs = useSongQueuer();
4861
const navigate = useNavigate();
4962

@@ -96,11 +109,14 @@ export default function ChannelSongs() {
96109
<ButtonGroup colorScheme="brand" mt="3" spacing="5">
97110
<Button
98111
onClick={() =>
99-
setOffset((e) =>
112+
onPageChange(
100113
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+
),
104120
)
105121
}
106122
disabled={offset === 0}
@@ -113,14 +129,14 @@ export default function ChannelSongs() {
113129
fontSize="xl"
114130
onSubmit={(e) => {
115131
const n = Number.parseInt(e);
116-
setOffset(
132+
onPageChange(
117133
Math.max(
118134
Math.min(
119135
Math.floor(total / PERPAGE) * PERPAGE,
120-
(n - 1) * PERPAGE
136+
(n - 1) * PERPAGE,
121137
),
122-
0
123-
)
138+
0,
139+
),
124140
);
125141
}}
126142
>
@@ -134,11 +150,14 @@ export default function ChannelSongs() {
134150
</Editable>
135151
<Button
136152
onClick={() =>
137-
setOffset((e) =>
153+
onPageChange(
138154
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+
),
142161
)
143162
}
144163
disabled={offset + PERPAGE > total}

0 commit comments

Comments
 (0)