'use client' import { useState, useEffect, useCallback } from 'react' interface GitHubLabel { name: string color?: string } interface GitHubIssue { number: number title: string body: string | null state: 'open' | 'closed' labels: GitHubLabel[] assignee: { login: string } | null html_url: string created_at: string updated_at: string } interface SyncRecord { id: number repo: string last_synced_at: number issue_count: number sync_direction: string status: string error: string | null created_at: number } interface LinkedTask { id: number title: string status: string priority: string metadata: { github_repo?: string github_issue_number?: number github_issue_url?: string github_synced_at?: string github_state?: string } } export function GitHubSyncPanel() { // Connection status const [tokenStatus, setTokenStatus] = useState<{ connected: boolean; user?: string } | null>(null) // Import form const [repo, setRepo] = useState('') const [labelFilter, setLabelFilter] = useState('') const [stateFilter, setStateFilter] = useState<'open' | 'closed' | 'all'>('open') const [assignAgent, setAssignAgent] = useState('') const [agents, setAgents] = useState<{ name: string }[]>([]) // Preview const [previewIssues, setPreviewIssues] = useState([]) const [previewing, setPreviewing] = useState(false) // Sync const [syncing, setSyncing] = useState(false) const [syncResult, setSyncResult] = useState<{ imported: number; skipped: number; errors: number } | null>(null) // Sync history const [syncHistory, setSyncHistory] = useState([]) // Linked tasks const [linkedTasks, setLinkedTasks] = useState([]) // Feedback const [feedback, setFeedback] = useState<{ ok: boolean; text: string } | null>(null) const [loading, setLoading] = useState(true) const showFeedback = (ok: boolean, text: string) => { setFeedback({ ok, text }) setTimeout(() => setFeedback(null), 4000) } // Check GitHub token status const checkToken = useCallback(async () => { try { const res = await fetch('/api/integrations', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'test', integrationId: 'github' }), }) const data = await res.json() setTokenStatus({ connected: data.ok === true, user: data.detail?.replace('User: ', ''), }) } catch { setTokenStatus({ connected: false }) } }, []) // Fetch sync history const fetchSyncHistory = useCallback(async () => { try { const res = await fetch('/api/github', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'status' }), }) if (res.ok) { const data = await res.json() setSyncHistory(data.syncs || []) } } catch { /* ignore */ } }, []) // Fetch linked tasks const fetchLinkedTasks = useCallback(async () => { try { const res = await fetch('/api/tasks?limit=200') if (res.ok) { const data = await res.json() const linked = (data.tasks || []).filter( (t: LinkedTask) => t.metadata?.github_repo ) setLinkedTasks(linked) } } catch { /* ignore */ } }, []) // Fetch agents for assign dropdown const fetchAgents = useCallback(async () => { try { const res = await fetch('/api/agents') if (res.ok) { const data = await res.json() setAgents((data.agents || []).map((a: any) => ({ name: a.name }))) } } catch { /* ignore */ } }, []) useEffect(() => { Promise.all([checkToken(), fetchSyncHistory(), fetchLinkedTasks(), fetchAgents()]) .finally(() => setLoading(false)) }, [checkToken, fetchSyncHistory, fetchLinkedTasks, fetchAgents]) // Preview issues from GitHub const handlePreview = async () => { if (!repo) { showFeedback(false, 'Enter a repository (owner/repo)') return } setPreviewing(true) setPreviewIssues([]) setSyncResult(null) try { const params = new URLSearchParams({ action: 'issues', repo, state: stateFilter }) if (labelFilter) params.set('labels', labelFilter) const res = await fetch(`/api/github?${params}`) const data = await res.json() if (res.ok) { setPreviewIssues(data.issues || []) if (data.issues?.length === 0) showFeedback(true, 'No issues found matching filters') } else { showFeedback(false, data.error || 'Failed to fetch issues') } } catch { showFeedback(false, 'Network error') } finally { setPreviewing(false) } } // Import issues as tasks const handleImport = async () => { if (!repo) return setSyncing(true) setSyncResult(null) try { const res = await fetch('/api/github', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'sync', repo, labels: labelFilter || undefined, state: stateFilter, assignAgent: assignAgent || undefined, }), }) const data = await res.json() if (res.ok) { setSyncResult({ imported: data.imported, skipped: data.skipped, errors: data.errors }) showFeedback(true, `Imported ${data.imported} issue${data.imported === 1 ? '' : 's'}, skipped ${data.skipped}`) setPreviewIssues([]) fetchSyncHistory() fetchLinkedTasks() } else { showFeedback(false, data.error || 'Sync failed') } } catch { showFeedback(false, 'Network error') } finally { setSyncing(false) } } if (loading) { return (
Loading GitHub sync...
) } return (
{/* Header */}

GitHub Issues Sync

Import GitHub issues as Mission Control tasks

{/* Connection status badge */}
{tokenStatus?.connected ? `GitHub: ${tokenStatus.user || 'connected'}` : 'GitHub: not configured'}
{/* Feedback */} {feedback && (
{feedback.text}
)} {/* Sync result banner */} {syncResult && (
Imported: {syncResult.imported} Skipped: {syncResult.skipped} {syncResult.errors > 0 && Errors: {syncResult.errors}}
)} {/* Import Issues Form */}

Import Issues

{/* Repo input */}
setRepo(e.target.value)} placeholder="owner/repo" className="w-full px-3 py-1.5 text-sm rounded-md border border-border bg-background text-foreground placeholder:text-muted-foreground/50 focus:outline-none focus:ring-1 focus:ring-primary" />
{/* Label filter */}
setLabelFilter(e.target.value)} placeholder="bug,enhancement" className="w-full px-3 py-1.5 text-sm rounded-md border border-border bg-background text-foreground placeholder:text-muted-foreground/50 focus:outline-none focus:ring-1 focus:ring-primary" />
{/* State filter */}
{/* Assign to agent */}
{/* Actions */}
{/* Issue Preview Table */} {previewIssues.length > 0 && (

Preview ({previewIssues.length} issues)

{previewIssues.map(issue => ( ))}
# Title Labels State Created
{issue.number} {issue.title}
{issue.labels.map(l => ( {l.name} ))}
{issue.state} {new Date(issue.created_at).toLocaleDateString()}
)} {/* Sync History */} {syncHistory.length > 0 && (

Sync History

{syncHistory.map(sync => ( ))}
Repo Issues Status Synced At
{sync.repo} {sync.issue_count} {sync.status} {new Date(sync.created_at * 1000).toLocaleString()}
)} {/* Linked Tasks */} {linkedTasks.length > 0 && (

Linked Tasks ({linkedTasks.length})

{linkedTasks.map(task => ( ))}
Task Status Priority GitHub Synced
{task.title} {task.status} {task.priority} {task.metadata.github_issue_url ? ( {task.metadata.github_repo}#{task.metadata.github_issue_number} ) : ( — )} {task.metadata.github_synced_at ? new Date(task.metadata.github_synced_at).toLocaleDateString() : '—'}
)}
) }