Skip to content

Commit a6e1760

Browse files
committed
Refactor loading state handling
1 parent 0530898 commit a6e1760

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/lib/statsProvider.tsx

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
/** @jsxImportSource react */
21
import React, {
32
createContext,
43
useCallback,
54
useContext,
65
useEffect,
76
useMemo,
87
useState,
9-
ReactNode,
8+
type ReactNode,
109
} from "react";
1110
import { githubService, type GitHubOrgStats } from "../services/githubService";
1211
import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
@@ -166,12 +165,12 @@ export function CommunityStatsProvider({
166165
} = useDocusaurusContext();
167166
const token = customFields?.gitToken || "";
168167

169-
const [loading, setLoading] = useState(true);
168+
const [loading, setLoading] = useState(false); // Start with false to avoid hourglass
170169
const [error, setError] = useState<string | null>(null);
171-
const [githubStarCount, setGithubStarCount] = useState(0);
172-
const [githubContributorsCount, setGithubContributorsCount] = useState(0);
173-
const [githubForksCount, setGithubForksCount] = useState(0);
174-
const [githubReposCount, setGithubReposCount] = useState(0);
170+
const [githubStarCount, setGithubStarCount] = useState(984); // Placeholder value - updated to match production
171+
const [githubContributorsCount, setGithubContributorsCount] = useState(467); // Placeholder value - updated to match production
172+
const [githubForksCount, setGithubForksCount] = useState(1107); // Placeholder value - updated to match production
173+
const [githubReposCount, setGithubReposCount] = useState(10); // Placeholder value - updated to match production
175174
const [githubDiscussionsCount, setGithubDiscussionsCount] = useState(0);
176175
const [lastUpdated, setLastUpdated] = useState<Date | null>(null);
177176

@@ -412,17 +411,28 @@ export function CommunityStatsProvider({
412411

413412
const fetchAllStats = useCallback(
414413
async (signal: AbortSignal) => {
415-
setLoading(true);
416-
setError(null);
417-
418-
// Check cache first
414+
// Check cache first and load it immediately without showing loading state
419415
const now = Date.now();
420-
if (cache.data && now - cache.timestamp < CACHE_DURATION) {
416+
const isCacheValid = cache.data && now - cache.timestamp < CACHE_DURATION;
417+
418+
if (isCacheValid) {
419+
// Use cached data immediately
421420
setAllContributors(cache.data.contributors);
422421
setLoading(false);
423422
return;
424423
}
425424

425+
// If cache is expired or empty, show cached data anyway but fetch fresh data
426+
// This provides immediate content while updating in the background
427+
if (cache.data) {
428+
setAllContributors(cache.data.contributors);
429+
setLoading(false); // Don't show loading state for background refresh
430+
} else {
431+
setLoading(true); // Only show loading on first load
432+
}
433+
434+
setError(null);
435+
426436
if (!token) {
427437
setError(
428438
"GitHub token not found. Please set customFields.gitToken in docusaurus.config.js.",

0 commit comments

Comments
 (0)