import { useState } from 'react';
import {
CheckCircle2,
XCircle,
RefreshCw,
ChevronDown,
ChevronRight,
Terminal,
LogIn,
Download,
Cpu,
ArrowUpCircle,
User,
Building2,
} from 'lucide-react';
import type {
SidecarConnectionDiagnostics,
SidecarConnectionStatus,
SidecarCopilotCliVersionStatus,
} from '@shared/contracts/sidecar';
interface CopilotStatusCardProps {
connection?: SidecarConnectionDiagnostics;
modelCount: number;
isRefreshing: boolean;
onRefresh: () => void;
}
interface StatusConfig {
icon: React.ReactNode;
label: string;
accentClasses: string;
dotClasses: string;
actionIcon?: React.ReactNode;
actionLabel?: string;
}
function getStatusConfig(status: SidecarConnectionStatus): StatusConfig {
switch (status) {
case 'ready':
return {
icon: ,
label: 'Connected to GitHub Copilot',
accentClasses: 'text-emerald-400',
dotClasses: 'bg-emerald-400',
};
case 'copilot-cli-missing':
return {
icon: ,
label: 'Copilot CLI not found',
accentClasses: 'text-amber-400',
dotClasses: 'bg-amber-400',
actionIcon: ,
actionLabel: 'Install the copilot CLI and ensure it is on your PATH',
};
case 'copilot-auth-required':
return {
icon: ,
label: 'Sign-in required',
accentClasses: 'text-blue-400',
dotClasses: 'bg-blue-400',
actionIcon: ,
actionLabel: 'Run copilot auth login in your terminal, then refresh',
};
case 'copilot-error':
return {
icon: ,
label: 'Connection error',
accentClasses: 'text-red-400',
dotClasses: 'bg-red-400',
};
}
}
function formatCheckedAt(iso: string): string {
try {
const date = new Date(iso);
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffMin = Math.floor(diffMs / 60_000);
if (diffMin < 1) return 'just now';
if (diffMin < 60) return `${diffMin}m ago`;
const diffHr = Math.floor(diffMin / 60);
if (diffHr < 24) return `${diffHr}h ago`;
return date.toLocaleDateString(undefined, { month: 'short', day: 'numeric' });
} catch {
return '';
}
}
function shortenPath(fullPath: string): string {
const parts = fullPath.replace(/\\/g, '/').split('/');
if (parts.length <= 3) return fullPath;
return `…/${parts.slice(-2).join('/')}`;
}
function VersionBadge({ status, installedVersion }: { status: SidecarCopilotCliVersionStatus; installedVersion?: string }) {
const versionLabel = installedVersion ? `v${installedVersion}` : undefined;
switch (status) {
case 'latest':
return (
{versionLabel ?? 'Up to date'}
);
case 'outdated':
return (
Update available
);
case 'unknown':
return (
{versionLabel ?? 'Version unknown'}
);
}
}
function AccountSection({ connection }: { connection: SidecarConnectionDiagnostics }) {
const { account } = connection;
if (!account) return null;
const hasLogin = !!account.login;
const hasOrgs = account.organizations && account.organizations.length > 0;
if (!hasLogin && !account.statusMessage) return null;
const MAX_VISIBLE_ORGS = 5;
const visibleOrgs = hasOrgs ? account.organizations!.slice(0, MAX_VISIBLE_ORGS) : [];
const remainingOrgs = hasOrgs ? account.organizations!.length - MAX_VISIBLE_ORGS : 0;
return (
{/* Identity row */}
{hasLogin ? (
{account.login}
{account.host && (
· {account.host}
)}
) : (
{account.statusMessage}
)}
{/* Organizations */}
{hasOrgs && (
{visibleOrgs.map((org) => (
{org}
))}
{remainingOrgs > 0 && (
+{remainingOrgs} more
)}
)}
);
}
export function CopilotStatusCard({
connection,
modelCount,
isRefreshing,
onRefresh,
}: CopilotStatusCardProps) {
const [showDetails, setShowDetails] = useState(false);
if (!connection) {
return (
Checking connection…
);
}
const config = getStatusConfig(connection.status);
const isHealthy = connection.status === 'ready';
const hasDetail = connection.copilotCliPath;
const checkedLabel = formatCheckedAt(connection.checkedAt);
const hasVersionInfo = !!connection.copilotCliVersion;
const hasAccountInfo = !!connection.account;
return (
{/* Status indicator */}
{config.label}
{isHealthy && (
· {modelCount} model{modelCount === 1 ? '' : 's'} available
)}
{hasVersionInfo && (
)}
{checkedLabel && (
{checkedLabel}
)}
{/* Account info (when healthy) */}
{isHealthy && hasAccountInfo && (
)}
{/* Action hint for non-ready states */}
{!isHealthy && config.actionLabel && (
{config.actionIcon}
{config.actionLabel}
)}
{/* Expandable details (healthy state) */}
{isHealthy && hasDetail && (
{showDetails && (
{connection.copilotCliPath && (
CLI path
{connection.copilotCliPath}
)}
{hasVersionInfo && connection.copilotCliVersion!.status === 'outdated' && connection.copilotCliVersion!.latestVersion && (
Latest version
{connection.copilotCliVersion!.latestVersion}
)}
Last checked
{new Date(connection.checkedAt).toLocaleString()}
)}
)}
{/* Error detail for non-ready states */}
{!isHealthy && hasDetail && (
CLI path
{shortenPath(connection.copilotCliPath!)}
{connection.detail && (
Error detail
{connection.detail}
)}
)}
);
}