import { useEffect, useState } from 'react'; import { baseUrl } from '../lib/utils'; interface GitHubPRCountProps { initialCount: number | null; } export default function GitHubPRCount({ initialCount }: GitHubPRCountProps) { const [prCount, setPrCount] = useState(null); const [isLoading, setIsLoading] = useState(true); const [dataSource, setDataSource] = useState<'static' | 'live'>('static'); useEffect(() => { // Step 1: Initialize from Astro endpoint const initializeFromEndpoint = async () => { try { console.log('[Build Data] 🔄 Initializing PR count from static JSON file...'); const response = await fetch(baseUrl('build-data-pr.json')); if (response.ok) { const data = await response.json(); if (data.count !== null && data.count !== undefined) { setPrCount(data.count); setDataSource('static'); setIsLoading(false); console.log(`[Build Data] ✓ Initialized from static JSON: ${data.count} open PRs`); return true; } } } catch (error) { console.error('[Build Data] ✗ Failed to initialize from static JSON:', error); } return false; }; // Step 2: Try to update with direct GitHub API call const updateFromGitHub = async () => { setIsLoading(true); console.log('[GitHub API] 🔄 Attempting to update PR count from GitHub API...'); try { const response = await fetch( 'https://api.github.com/search/issues?q=repo:ipython/ipython+type:pr+state:open' ); if (response.ok) { const data = await response.json(); const count = data.total_count || 0; setPrCount(count); setDataSource('live'); console.log(`[GitHub API] ✓ Updated from GitHub: ${count} open PRs`); } else { console.error(`[GitHub API] ✗ Could not reach GitHub API: ${response.status} ${response.statusText}`); // Keep dataSource as 'static' since update failed setDataSource('static'); } } catch (error) { console.error('[GitHub API] ✗ Could not reach GitHub API:', error); // Keep dataSource as 'static' since update failed setDataSource('static'); } finally { setIsLoading(false); } }; // Initialize from endpoint, then try to update from GitHub initializeFromEndpoint().then(() => { // Try to update after a short delay setTimeout(updateFromGitHub, 100); }); }, []); if (prCount === null) { return null; } return (

Open Pull Requests {dataSource === 'static' && ( (static) )} {dataSource === 'live' && ( (live) )} {isLoading && ( (updating...) )}

{prCount}

); }