import React, { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Badge } from "@/components/ui/badge"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { api, type ClaudeInstallation } from "@/lib/api"; import { cn } from "@/lib/utils"; import { CheckCircle, HardDrive, Settings } from "lucide-react"; import { useTranslation } from "@/hooks/useTranslation"; interface ClaudeVersionSelectorProps { /** * Currently selected installation path */ selectedPath?: string | null; /** * Callback when an installation is selected */ onSelect: (installation: ClaudeInstallation) => void; /** * Optional className for styling */ className?: string; /** * Whether to show the save button */ showSaveButton?: boolean; /** * Callback when save is clicked */ onSave?: () => void; /** * Whether save is in progress */ isSaving?: boolean; } /** * ClaudeVersionSelector component for selecting Claude Code installations * Supports system installations and user preferences * * @example * setSelectedInstallation(installation)} * /> */ export const ClaudeVersionSelector: React.FC = ({ selectedPath, onSelect, className, showSaveButton = false, onSave, isSaving = false, }) => { const { t } = useTranslation(); const [installations, setInstallations] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [selectedInstallation, setSelectedInstallation] = useState(null); useEffect(() => { loadInstallations(); }, []); useEffect(() => { // Update selected installation when selectedPath changes if (selectedPath && installations.length > 0) { const found = installations.find(i => i.path === selectedPath); if (found) { setSelectedInstallation(found); } } }, [selectedPath, installations]); const loadInstallations = async () => { try { setLoading(true); setError(null); const foundInstallations = await api.listClaudeInstallations(); setInstallations(foundInstallations); // If we have a selected path, find and select it if (selectedPath) { const found = foundInstallations.find(i => i.path === selectedPath); if (found) { setSelectedInstallation(found); } } else if (foundInstallations.length > 0) { // Auto-select the first (best) installation setSelectedInstallation(foundInstallations[0]); onSelect(foundInstallations[0]); } } catch (err) { console.error("Failed to load Claude installations:", err); setError(err instanceof Error ? err.message : "Failed to load Claude installations"); } finally { setLoading(false); } }; const handleInstallationChange = (installationPath: string) => { const installation = installations.find(i => i.path === installationPath); if (installation) { setSelectedInstallation(installation); onSelect(installation); } }; const getInstallationIcon = (installation: ClaudeInstallation) => { switch (installation.installation_type) { case "System": return ; case "Custom": return ; default: return ; } }; const getInstallationTypeColor = (installation: ClaudeInstallation) => { switch (installation.installation_type) { case "System": return "bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-300"; case "Custom": return "bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-300"; default: return "bg-gray-100 text-gray-800 dark:bg-gray-900 dark:text-gray-300"; } }; if (loading) { return ( {t('settings.generalOptions.claudeCodeInstallation')} {t('settings.generalOptions.loadingAvailableInstallations')}
); } if (error) { return ( {t('settings.generalOptions.claudeCodeInstallation')} {t('settings.generalOptions.errorLoadingInstallations')}
{error}
); } const systemInstallations = installations.filter(i => i.installation_type === "System"); const customInstallations = installations.filter(i => i.installation_type === "Custom"); return ( {t('settings.generalOptions.claudeCodeInstallation')} {t('settings.generalOptions.choosePreferredInstallation')} {/* Available Installations */}
{/* Installation Details */} {selectedInstallation && (
{t('settings.selectedInstallation')} {selectedInstallation.installation_type}
{t('settings.path')}: {selectedInstallation.path}
{t('settings.source')}: {selectedInstallation.source}
{selectedInstallation.version && (
{t('settings.version')}: {selectedInstallation.version}
)}
)} {/* Save Button */} {showSaveButton && ( )}
); };