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 (
{isConnected ? `Connected as ${connection.user?.login}` : 'Connect your GitHub account to manage repositories'}
@{connection.user.login}
{connection.user.bio && ({connection.user.bio}
)}