'use client' import { useState, useEffect, useCallback, useRef } from 'react' import { githubService } from '@/services/githubService' import { repoService } from '@/services/repoService' import { parseRepositoryInput } from '@/utils/repositoryRouting' interface LinkedRepo { owner: string name: string fullName: string description?: string stars?: number language?: string } interface RepoLinkerProps { onSelect: (repo: LinkedRepo) => void onClose: () => void } interface SearchResult { id?: string owner: string name: string fullName?: string full_name?: string description?: string stars?: number stargazers_count?: number language?: string } export default function RepoLinker({ onSelect, onClose }: RepoLinkerProps) { const [searchQuery, setSearchQuery] = useState('') const [searchResults, setSearchResults] = useState([]) const [myProjects, setMyProjects] = useState([]) const [loading, setLoading] = useState(false) const [loadingProjects, setLoadingProjects] = useState(true) const [activeTab, setActiveTab] = useState<'my' | 'search'>('my') const searchTimerRef = useRef(null) const inputRef = useRef(null) useEffect(() => { loadMyProjects() if (inputRef.current) inputRef.current.focus() }, []) const loadMyProjects = async () => { try { const data = await githubService.getProjects() setMyProjects(data.projects || []) } catch { setMyProjects([]) } finally { setLoadingProjects(false) } } const searchRepos = useCallback(async (query: string) => { if (!query.trim()) { setSearchResults([]) return } setLoading(true) try { const response = await repoService.search({ q: query, limit: 10 }) setSearchResults(response.data?.repositories || response.data || []) } catch { setSearchResults([]) } finally { setLoading(false) } }, []) const handleSearchChange = (val: string) => { setSearchQuery(val) if (val.trim()) setActiveTab('search') // Debounced search if (searchTimerRef.current) clearTimeout(searchTimerRef.current) searchTimerRef.current = setTimeout(() => { searchRepos(val) }, 400) } const handleSelect = (repo: SearchResult) => { onSelect({ owner: repo.owner, name: repo.name, fullName: repo.fullName || repo.full_name || `${repo.owner}/${repo.name}`, description: repo.description, stars: repo.stars ?? repo.stargazers_count, language: repo.language, }) } const handleGitHubUrlPaste = (val: string) => { const repositoryRef = parseRepositoryInput(val) if (repositoryRef) { onSelect({ owner: repositoryRef.owner, name: repositoryRef.name, fullName: repositoryRef.fullName, }) } } const displayRepos = activeTab === 'my' ? myProjects : searchResults const isLoading = activeTab === 'my' ? loadingProjects : loading return (
{/* Header */}

Link Repository

{/* Search Input */}
{ handleSearchChange(e.target.value) handleGitHubUrlPaste(e.target.value) }} placeholder="Search repos or paste a GitHub URL..." className="w-full pl-10 pr-4 py-2.5 text-sm bg-background border border-border rounded-lg focus:outline-none focus:ring-2 focus:ring-primary text-foreground placeholder:text-foreground-secondary" />
{/* Tabs */}
{/* Results */}
{isLoading ? (
) : displayRepos.length === 0 ? (

{activeTab === 'my' ? 'No repositories found. Sync your GitHub projects first.' : searchQuery.trim() ? 'No repositories match your search.' : 'Type to search for repositories.'}

) : (
    {displayRepos.map((repo, index) => (
  • ))}
)}
{/* Footer */}
) }