import React, { useState } from 'react'; import { motion } from 'framer-motion'; import { toast } from 'react-toastify'; import { useStore } from '@nanostores/react'; import { classNames } from '~/utils/classNames'; import { Collapsible, CollapsibleTrigger, CollapsibleContent } from '~/components/ui/Collapsible'; import { Button } from '~/components/ui/Button'; import { githubConnectionAtom, githubConnectionStore, isGitHubConnected, isGitHubConnecting, isGitHubLoadingStats, } from '~/lib/stores/githubConnection'; import { AuthDialog } from './AuthDialog'; import { StatsDisplay } from './StatsDisplay'; import { RepositoryList } from './RepositoryList'; interface GitHubConnectionProps { onCloneRepository?: (repoUrl: string) => void; } export default function GitHubConnection({ onCloneRepository }: GitHubConnectionProps = {}) { const connection = useStore(githubConnectionAtom); const isConnected = useStore(isGitHubConnected); const isConnecting = useStore(isGitHubConnecting); const isLoadingStats = useStore(isGitHubLoadingStats); const [isAuthDialogOpen, setIsAuthDialogOpen] = useState(false); const [isStatsExpanded, setIsStatsExpanded] = useState(false); const [isReposExpanded, setIsReposExpanded] = useState(false); const handleConnect = () => { setIsAuthDialogOpen(true); }; const handleDisconnect = () => { githubConnectionStore.disconnect(); setIsStatsExpanded(false); setIsReposExpanded(false); toast.success('Disconnected from GitHub'); }; const handleRefreshStats = async () => { try { await githubConnectionStore.fetchStats(); toast.success('GitHub stats refreshed'); } catch (error) { toast.error(`Failed to refresh stats: ${error instanceof Error ? error.message : 'Unknown error'}`); } }; const handleTokenTypeChange = (tokenType: 'classic' | 'fine-grained') => { githubConnectionStore.updateTokenType(tokenType); }; const handleCloneRepository = (repoUrl: string) => { if (onCloneRepository) { onCloneRepository(repoUrl); } else { window.open(repoUrl, '_blank'); } }; return (
{/* Header */}

GitHub

{isConnected ? `Connected as ${connection.user?.login}` : 'Connect your GitHub account to manage repositories'}

{isConnected ? ( <> ) : ( )}
{/* Connection Status */}
{isConnected ? 'Connected' : 'Not Connected'} {connection.rateLimit && ( Rate limit: {connection.rateLimit.remaining}/{connection.rateLimit.limit} )}
{/* Token Type Selection */} {isConnected && (
{(['classic', 'fine-grained'] as const).map((type) => ( ))}
)}
{/* User Profile */} {isConnected && connection.user && (
{connection.user.login}

{connection.user.name || connection.user.login}

@{connection.user.login}

{connection.user.bio && (

{connection.user.bio}

)}
{connection.user.public_repos?.toLocaleString() || 0}
repositories
)} {/* Stats Section */} {isConnected && connection.stats && (
GitHub Stats
)} {/* Repositories Section */} {isConnected && connection.stats?.repos && connection.stats.repos.length > 0 && (
Repositories ({connection.stats.repos.length})
)} {/* Auth Dialog */} setIsAuthDialogOpen(false)} />
); }