Skip to content

Commit fb43872

Browse files
fix(github): handle unavailable discussions stats
1 parent 8f47c28 commit fb43872

2 files changed

Lines changed: 42 additions & 13 deletions

File tree

src/lib/statsProvider.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface ICommunityStatsContext {
2121
githubForksCountText: string;
2222
githubReposCount: number;
2323
githubReposCountText: string;
24-
githubDiscussionsCount: number;
24+
githubDiscussionsCount: number | null;
2525
githubDiscussionsCountText: string;
2626
loading: boolean;
2727
error: string | null;
@@ -165,7 +165,9 @@ export function CommunityStatsProvider({
165165
const [githubContributorsCount, setGithubContributorsCount] = useState(467); // Placeholder value - updated to match production
166166
const [githubForksCount, setGithubForksCount] = useState(1107); // Placeholder value - updated to match production
167167
const [githubReposCount, setGithubReposCount] = useState(10); // Placeholder value - updated to match production
168-
const [githubDiscussionsCount, setGithubDiscussionsCount] = useState(0);
168+
const [githubDiscussionsCount, setGithubDiscussionsCount] = useState<
169+
number | null
170+
>(null);
169171
const [lastUpdated, setLastUpdated] = useState<Date | null>(null);
170172

171173
// Time filter state
@@ -476,7 +478,7 @@ export function CommunityStatsProvider({
476478
setGithubContributorsCount(140);
477479
setGithubForksCount(0);
478480
setGithubReposCount(20);
479-
setGithubDiscussionsCount(0);
481+
setGithubDiscussionsCount(null);
480482
}
481483
} finally {
482484
setLoading(false);
@@ -562,7 +564,11 @@ export const useCommunityStatsContext = (): ICommunityStatsContext => {
562564
return context;
563565
};
564566

565-
export const convertStatToText = (num: number): string => {
567+
export const convertStatToText = (num: number | null): string => {
568+
if (num === null) {
569+
return "N/A";
570+
}
571+
566572
const hasIntlSupport =
567573
typeof Intl === "object" && Intl && typeof Intl.NumberFormat === "function";
568574

src/services/githubService.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface GitHubOrgStats {
1010
totalRepositories: number;
1111
totalContributors: number;
1212
publicRepositories: number;
13-
discussionsCount: number;
13+
discussionsCount: number | null;
1414
lastUpdated: number;
1515
}
1616

@@ -82,6 +82,27 @@ class GitHubService {
8282
return typeof window === "undefined";
8383
}
8484

85+
private getGitHubToken(): string | null {
86+
if (typeof window !== "undefined") {
87+
return null;
88+
}
89+
90+
return process.env.GITHUB_TOKEN?.trim() || null;
91+
}
92+
93+
private getGraphQLHeaders(): Record<string, string> {
94+
const token = this.getGitHubToken();
95+
96+
if (!token) {
97+
throw new Error(this.DISCUSSIONS_UNAVAILABLE_MESSAGE);
98+
}
99+
100+
return {
101+
...this.getHeaders(),
102+
Authorization: `Bearer ${token}`,
103+
};
104+
}
105+
85106
// === ADDED: setter to toggle anonymous contributors inclusion
86107
setIncludeAnonymousContributors(value: boolean) {
87108
this.includeAnonymousContributors = value;
@@ -277,9 +298,9 @@ class GitHubService {
277298
private async getDiscussionsCount(
278299
signal?: AbortSignal,
279300
repoName: string = "Support",
280-
): Promise<number> {
301+
): Promise<number | null> {
281302
if (!this.canUseGitHubGraphQL()) {
282-
return 0;
303+
return null;
283304
}
284305

285306
try {
@@ -295,7 +316,7 @@ class GitHubService {
295316
const resp = await fetch("https://api.github.com/graphql", {
296317
method: "POST",
297318
headers: {
298-
...this.getHeaders(),
319+
...this.getGraphQLHeaders(),
299320
"Content-Type": "application/json",
300321
},
301322
body: JSON.stringify({ query, variables }),
@@ -304,20 +325,20 @@ class GitHubService {
304325

305326
if (!resp.ok) {
306327
console.warn(`GraphQL request for discussions failed: ${resp.status}`);
307-
return 0;
328+
return null;
308329
}
309330

310331
const data = await resp.json();
311332
if (data.errors) {
312333
console.warn("GraphQL errors while fetching discussions:", data.errors);
313-
return 0;
334+
return null;
314335
}
315336

316337
const count = data?.data?.repository?.discussions?.totalCount || 0;
317338
return Number(count);
318339
} catch (error) {
319340
console.warn("Error fetching discussions count via GraphQL:", error);
320-
return 0;
341+
return null;
321342
}
322343
}
323344

@@ -379,7 +400,7 @@ class GitHubService {
379400
totalRepositories: 0,
380401
publicRepositories: 0,
381402
totalContributors: 0,
382-
discussionsCount: 0,
403+
discussionsCount: null,
383404
lastUpdated: Date.now(),
384405
};
385406

@@ -415,6 +436,8 @@ class GitHubService {
415436
throw new Error(this.DISCUSSIONS_UNAVAILABLE_MESSAGE);
416437
}
417438

439+
const graphqlHeaders = this.getGraphQLHeaders();
440+
418441
const query = `
419442
query GetDiscussions($owner: String!, $name: String!, $first: Int!) {
420443
repository(owner: $owner, name: $name) {
@@ -463,7 +486,7 @@ class GitHubService {
463486
const response = await fetch("https://api.github.com/graphql", {
464487
method: "POST",
465488
headers: {
466-
...this.getHeaders(),
489+
...graphqlHeaders,
467490
"Content-Type": "application/json",
468491
},
469492
body: JSON.stringify({ query, variables }),

0 commit comments

Comments
 (0)