Skip to content

Commit 14bfce2

Browse files
committed
fix(github-data): improve GraphQL query with correct fields, pagination and cursor handling
1 parent 74eee8f commit 14bfce2

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/hooks/useGitHubData.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useCallback } from 'react';
1+
import { useState, useCallback, useRef } from 'react';
22

33
export const useGitHubData = (getOctokit: () => any) => {
44
const [issues, setIssues] = useState([]);
@@ -9,6 +9,11 @@ export const useGitHubData = (getOctokit: () => any) => {
99
const [totalPrs, setTotalPrs] = useState(0);
1010
const [rateLimited, setRateLimited] = useState(false);
1111

12+
const cursorsRef = useRef<{ issue: Record<number, string | null>, pr: Record<number, string | null> }>({
13+
issue: {},
14+
pr: {},
15+
});
16+
1217
const fetchPaginated = async (octokit: any, username: string, type: string, page = 1, per_page = 10) => {
1318
const query = `
1419
query ($queryString: String!, $first: Int!, $after: String) {
@@ -44,13 +49,18 @@ export const useGitHubData = (getOctokit: () => any) => {
4449
}
4550
`;
4651

47-
const queryString = `author:${username} is:${type}`;
52+
const queryString = `author:${username} is:${type} sort:updated-desc`;
4853
const response = await octokit.graphql(query, {
4954
queryString,
50-
first: per_page,
51-
after: page > 1 ? btoa(`cursor:${(page - 1) * per_page}`) : null,
55+
first: Math.min(100, per_page),
56+
after: page > 1 ? (cursorsRef.current?.[type]?.[page - 1] ?? null) : null,
5257
});
5358

59+
if (!cursorsRef.current[type]) {
60+
cursorsRef.current[type] = {};
61+
}
62+
cursorsRef.current[type][page] = response.search.pageInfo.endCursor;
63+
5464
return {
5565
items: response.search.edges.map((edge: any) => edge.node),
5666
total: response.search.issueCount,

0 commit comments

Comments
 (0)