Skip to content

Commit 58e8781

Browse files
authored
refactor: paging apis (#2067)
1 parent d642f9b commit 58e8781

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+252
-239
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
"rehype-remark": "^10.0.1",
116116
"remark-parse": "^11.0.0",
117117
"remark-stringify": "^11.0.0",
118-
"threadiverse": "^0.4.1",
118+
"threadiverse": "^0.5.0",
119119
"ua-parser-js": "^2.0.3",
120120
"unified": "^11.0.5",
121121
"unist-util-visit": "^5.0.0",

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/features/comment/commentSlice.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export const modNukeCommentChain =
214214

215215
if (!client) throw new Error("Not authorized");
216216

217-
const { comments } = await client.getComments({
217+
const { data: comments } = await client.getComments({
218218
parent_id: commentId,
219219
max_depth: 100,
220220
});
@@ -265,7 +265,7 @@ export const getCommentContent = createAsyncThunk(
265265

266266
const log = await client.getModlog({ comment_id: commentId });
267267

268-
return log.modlog.filter((l) => "mod_remove_comment" in l)[0]?.comment
268+
return log.data.filter((l) => "mod_remove_comment" in l)[0]?.comment
269269
.content;
270270
},
271271
{

src/features/comment/inTree/CommentExpander.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export default function CommentExpander({
7272
setLoading(false);
7373
}
7474

75-
if (response.comments.length === 0) {
75+
if (response.data.length === 0) {
7676
presentToast({
7777
message: `Uh-oh. Looks like Lemmy returned 0 comments, but there's actually ${missing}`,
7878
color: "danger",
@@ -87,7 +87,7 @@ export default function CommentExpander({
8787
});
8888

8989
// Only append comment that are direct descendants of the current comment, and unhydrated
90-
const newComments = response.comments.filter((c) => {
90+
const newComments = response.data.filter((c) => {
9191
// Say we have a tree like this. Parenthesis are missing from frontend:
9292
// 1
9393
// 1.1

src/features/comment/inTree/Comments.tsx

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import React, {
1010
useRef,
1111
useState,
1212
} from "react";
13-
import { CommentView } from "threadiverse";
13+
import { CommentView, PageCursor } from "threadiverse";
1414
import { VListHandle } from "virtua";
1515

1616
import FeedLoadMoreFailed from "#/features/feed/endItems/FeedLoadMoreFailed";
@@ -70,7 +70,7 @@ export default function Comments({
7070
virtualEnabled,
7171
}: CommentsProps) {
7272
const dispatch = useAppDispatch();
73-
const [page, setPage] = useState(0);
73+
const [cursor, setCursor] = useState<PageCursor | undefined>();
7474
const [loading, _setLoading] = useState(true);
7575
const loadingRef = useRef(false);
7676
const finishedPagingRef = useRef(false);
@@ -220,7 +220,7 @@ export default function Comments({
220220
if (!ready) return;
221221

222222
if (refresh) {
223-
if (page === 0 && loadingRef.current) return; // Still loading first page
223+
if (!cursor && loadingRef.current) return; // Still loading first page
224224
finishedPagingRef.current = false;
225225
} else {
226226
if (loadingRef.current) return;
@@ -229,7 +229,7 @@ export default function Comments({
229229

230230
let response;
231231

232-
const currentPage = refresh ? 1 : page + 1;
232+
const currentPage = refresh ? undefined : cursor;
233233

234234
const reqPostId = postId;
235235
const reqCommentId = parentCommentId;
@@ -239,14 +239,11 @@ export default function Comments({
239239
response = await client.getComments({
240240
post_id: reqPostId,
241241
parent_id: parentCommentId,
242-
limit: 60,
243242
...sortParams,
244243
type_: "All",
245-
244+
limit: 60,
246245
max_depth: maxDepth,
247-
248-
saved_only: false,
249-
page: currentPage,
246+
page_cursor: currentPage,
250247
});
251248
} catch (error) {
252249
if (reqPostId === postId && reqCommentId === parentCommentId)
@@ -258,23 +255,23 @@ export default function Comments({
258255
setLoadFailed(true);
259256
if (refresh) {
260257
setComments([]);
261-
setPage(0);
258+
setCursor(undefined);
262259
}
263260

264261
throw error;
265262
} finally {
266263
setLoading(false);
267264
}
268265

269-
dispatch(receivedComments(response.comments));
266+
dispatch(receivedComments(response.data));
270267

271268
if (reqPostId !== postId || reqCommentId !== parentCommentId) return;
272269

273270
const existingComments = refresh ? [] : comments;
274271

275272
// Remove comments that are already received
276273
let newComments = differenceBy(
277-
response.comments,
274+
response.data,
278275
existingComments,
279276
(c) => c.comment.id,
280277
);
@@ -301,7 +298,7 @@ export default function Comments({
301298
);
302299

303300
setComments(potentialComments);
304-
setPage(currentPage);
301+
setCursor(response.next_page);
305302
setLoadFailed(false);
306303

307304
if (refresh) scrolledRef.current = false;
@@ -311,7 +308,7 @@ export default function Comments({
311308
comments,
312309
dispatch,
313310
maxDepth,
314-
page,
311+
cursor,
315312
parentCommentId,
316313
postId,
317314
presentToast,

src/features/community/communitySlice.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ export const getTrendingCommunities =
156156
getState(),
157157
)?.listCommunities({
158158
type_: "All",
159-
sort: "Hot",
159+
sort: "Hot" as never, // TODO Piefed doesn't support Hot for communities sort, ignore for now and hopefully supported soon
160160
limit: 6,
161161
});
162162

163-
dispatch(recievedTrendingCommunities(trendingCommunities.communities));
163+
dispatch(recievedTrendingCommunities(trendingCommunities.data));
164164
};

src/features/community/list/GuestCommunitiesList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default function GuestCommunitiesList({ actor }: CommunitiesListProps) {
5858
let communities;
5959

6060
try {
61-
({ communities } = await client.listCommunities({
61+
({ data: communities } = await client.listCommunities({
6262
...commonPostFeedParams,
6363
type_: SHOW_LOCAL_ONLY.includes(actor) ? "Local" : "All",
6464
...sortParams,

src/features/community/titleSearch/TitleSearchResults.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Community, CommunityView } from "threadiverse";
1414
import useShowModeratorFeed from "#/features/community/list/useShowModeratorFeed";
1515
import { getHandle } from "#/helpers/lemmy";
1616
import { useBuildGeneralBrowseLink } from "#/helpers/routes";
17+
import { getTopAllSearchSort } from "#/helpers/threadiverse";
1718
import useClient from "#/helpers/useClient";
1819
import { useOptimizedIonRouter } from "#/helpers/useOptimizedIonRouter";
1920
import { useAppSelector } from "#/store";
@@ -173,10 +174,10 @@ export default function TitleSearchResults() {
173174
limit: 20,
174175
type_: "Communities",
175176
listing_type: "All",
176-
sort: "TopAll",
177+
...getTopAllSearchSort(await client.getMode()),
177178
});
178179

179-
setSearchPayload(result.communities);
180+
setSearchPayload(result.data as CommunityView[]);
180181
});
181182

182183
useEffect(() => {

src/features/community/useGetRandomCommunity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default function useGetRandomCommunity() {
4444
response = await client.listCommunities({
4545
type_: "All",
4646
limit: RANDOM_CHUNK,
47-
page: Math.floor(
47+
page_cursor: Math.floor(
4848
(Math.random() * totalCommunitiesCount) / RANDOM_CHUNK,
4949
),
5050
});
@@ -54,7 +54,7 @@ export default function useGetRandomCommunity() {
5454
throw error;
5555
}
5656

57-
const randomCommunitiesByPosts = sortBy(response.communities, [
57+
const randomCommunitiesByPosts = sortBy(response.data, [
5858
(c) => -c.counts.posts,
5959
]);
6060

0 commit comments

Comments
 (0)