Skip to content

Commit 1d3341a

Browse files
committed
types: make GitHub cursor paging type-safe (SearchType + typed cursorsRef)
1 parent b8a87eb commit 1d3341a

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

src/hooks/useGitHubData.ts

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

33
export const useGitHubData = (getOctokit: () => any) => {
4+
type SearchType = 'issue' | 'pr';
45
const [issues, setIssues] = useState([]);
56
const [prs, setPrs] = useState([]);
67
const [loading, setLoading] = useState(false);
@@ -9,12 +10,12 @@ export const useGitHubData = (getOctokit: () => any) => {
910
const [totalPrs, setTotalPrs] = useState(0);
1011
const [rateLimited, setRateLimited] = useState(false);
1112

12-
const cursorsRef = useRef<{ issue: Record<number, string | null>, pr: Record<number, string | null> }>({
13+
const cursorsRef = useRef<Record<SearchType, Record<number, string | null>>>({
1314
issue: {},
1415
pr: {},
1516
});
1617

17-
const fetchPaginated = async (octokit: any, username: string, type: string, page = 1, per_page = 10) => {
18+
const fetchPaginated = async (octokit: any, username: string, type: SearchType, page = 1, per_page = 10) => {
1819
const query = `
1920
query ($queryString: String!, $first: Int!, $after: String) {
2021
search(query: $queryString, type: ISSUE, first: $first, after: $after) {
@@ -53,7 +54,7 @@ export const useGitHubData = (getOctokit: () => any) => {
5354
const response = await octokit.graphql(query, {
5455
queryString,
5556
first: Math.min(100, per_page),
56-
after: page > 1 ? (cursorsRef.current?.[type]?.[page - 1] ?? null) : null,
57+
after: page > 1 ? (cursorsRef.current[type]?.[page - 1] ?? null) : null,
5758
});
5859

5960
const items = response.search.edges.map((edge: any) => {

0 commit comments

Comments
 (0)