import React, { useState, useEffect } from 'react'; import { motion } from 'framer-motion'; import { toast } from 'react-toastify'; import { classNames } from '~/utils/classNames'; import { Collapsible, CollapsibleTrigger, CollapsibleContent } from '~/components/ui/Collapsible'; import { Button } from '~/components/ui/Button'; import { useGitLabConnection } from '~/lib/stores/gitlabConnection'; import { RepositoryList } from './RepositoryList'; import { StatsDisplay } from './StatsDisplay'; import type { GitLabProjectInfo } from '~/types/GitLab'; interface GitLabConnectionProps { onCloneRepository?: (repoUrl: string) => void; } export default function GitLabConnection({ onCloneRepository }: GitLabConnectionProps = {}) { const { connection: connectionAtom, isConnected, user: userAtom, stats, gitlabUrl: gitlabUrlAtom, connect, disconnect, fetchStats, loadSavedConnection, setGitLabUrl, setToken, autoConnect, } = useGitLabConnection(); const [isLoading, setIsLoading] = useState(true); const [isConnecting, setIsConnecting] = useState(false); const [isFetchingStats, setIsFetchingStats] = useState(false); const [isStatsExpanded, setIsStatsExpanded] = useState(false); useEffect(() => { const initializeConnection = async () => { setIsLoading(true); const saved = loadSavedConnection(); if (saved?.user && saved?.token) { // If we have stats, no need to fetch them again if (!saved.stats || !saved.stats.projects || saved.stats.projects.length === 0) { await fetchStats(); } } else if (import.meta.env?.VITE_GITLAB_ACCESS_TOKEN) { // Auto-connect using environment variable if no saved connection const result = await autoConnect(); if (result.success) { toast.success('Connected to GitLab automatically'); } } setIsLoading(false); }; initializeConnection(); }, [autoConnect, fetchStats, loadSavedConnection]); const handleConnect = async (event: React.FormEvent) => { event.preventDefault(); setIsConnecting(true); try { const result = await connect(connectionAtom.get().token, gitlabUrlAtom.get()); if (result.success) { toast.success('Connected to GitLab successfully'); await fetchStats(); } else { toast.error(`Failed to connect to GitLab: ${result.error}`); } } catch (error) { console.error('Failed to connect to GitLab:', error); toast.error(`Failed to connect to GitLab: ${error instanceof Error ? error.message : 'Unknown error'}`); } finally { setIsConnecting(false); } }; const handleDisconnect = () => { disconnect(); toast.success('Disconnected from GitLab'); }; const handleCloneRepository = (repoUrl: string) => { if (onCloneRepository) { onCloneRepository(repoUrl); } else { window.open(repoUrl, '_blank'); } }; if (isLoading || isConnecting || isFetchingStats) { return (
Tip: You can also set the{' '}
VITE_GITLAB_ACCESS_TOKEN{' '}
environment variable to connect automatically.
For self-hosted GitLab instances, also set{' '}
VITE_GITLAB_URL=https://your-gitlab-instance.com
{userAtom.get()?.username}