|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { useTranslation } from "react-i18next"; |
| 3 | +import { Label } from "@/components/ui/label"; |
| 4 | +import { Input } from "@/components/ui/input"; |
| 5 | +import { Button } from "@/components/ui/button"; |
| 6 | +import { Badge } from "@/components/ui/badge"; |
| 7 | +import { CheckCircle, Circle } from "lucide-react"; |
| 8 | + |
| 9 | +const { invoke } = window.__TAURI__.core; |
| 10 | + |
| 11 | +interface CliStatus { |
| 12 | + installed: boolean; |
| 13 | + dismissed: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +interface InstallResult { |
| 17 | + success: boolean; |
| 18 | + path: string; |
| 19 | + error: string; |
| 20 | +} |
| 21 | + |
| 22 | +export function CliInstallSettings() { |
| 23 | + const { t } = useTranslation(); |
| 24 | + const [status, setStatus] = useState<CliStatus | null>(null); |
| 25 | + const [suggestedPaths, setSuggestedPaths] = useState<string[]>([]); |
| 26 | + const [selectedPath, setSelectedPath] = useState(""); |
| 27 | + const [installing, setInstalling] = useState(false); |
| 28 | + const [result, setResult] = useState<InstallResult | null>(null); |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + invoke<CliStatus>("check_cli_status") |
| 32 | + .then(setStatus) |
| 33 | + .catch(console.error); |
| 34 | + |
| 35 | + invoke<string[]>("get_cli_suggested_paths") |
| 36 | + .then((paths) => { |
| 37 | + setSuggestedPaths(paths); |
| 38 | + if (paths.length > 0) setSelectedPath(paths[0]); |
| 39 | + }) |
| 40 | + .catch(console.error); |
| 41 | + }, []); |
| 42 | + |
| 43 | + async function handleInstall() { |
| 44 | + if (!selectedPath.trim()) return; |
| 45 | + setInstalling(true); |
| 46 | + setResult(null); |
| 47 | + try { |
| 48 | + const r = await invoke<InstallResult>("install_cli_to_path", { path: selectedPath.trim() }); |
| 49 | + setResult(r); |
| 50 | + if (r.success) { |
| 51 | + setStatus((prev) => prev ? { ...prev, installed: true } : { installed: true, dismissed: false }); |
| 52 | + } |
| 53 | + } catch (e) { |
| 54 | + setResult({ success: false, path: "", error: String(e) }); |
| 55 | + } finally { |
| 56 | + setInstalling(false); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return ( |
| 61 | + <div className="space-y-4"> |
| 62 | + {/* Status */} |
| 63 | + <div className="flex items-center gap-2"> |
| 64 | + {status?.installed ? ( |
| 65 | + <CheckCircle className="h-4 w-4 text-green-500 shrink-0" /> |
| 66 | + ) : ( |
| 67 | + <Circle className="h-4 w-4 text-muted-foreground shrink-0" /> |
| 68 | + )} |
| 69 | + <span className="text-sm text-muted-foreground"> |
| 70 | + {status?.installed |
| 71 | + ? t("settings.cliStatusInstalled") |
| 72 | + : t("settings.cliStatusNotInstalled")} |
| 73 | + </span> |
| 74 | + </div> |
| 75 | + |
| 76 | + {/* Suggested paths */} |
| 77 | + {suggestedPaths.length > 0 && ( |
| 78 | + <div className="space-y-2"> |
| 79 | + <Label className="text-sm font-medium">{t("settings.cliSuggestedPaths")}</Label> |
| 80 | + <div className="flex flex-wrap gap-2"> |
| 81 | + {suggestedPaths.map((p) => ( |
| 82 | + <Badge |
| 83 | + key={p} |
| 84 | + variant={selectedPath === p ? "default" : "outline"} |
| 85 | + className="cursor-pointer font-mono text-xs" |
| 86 | + onClick={() => { setSelectedPath(p); setResult(null); }} |
| 87 | + > |
| 88 | + {p} |
| 89 | + </Badge> |
| 90 | + ))} |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + )} |
| 94 | + |
| 95 | + {/* Custom path input */} |
| 96 | + <div className="space-y-2"> |
| 97 | + <Label className="text-sm font-medium">{t("settings.cliCustomPath")}</Label> |
| 98 | + <Input |
| 99 | + className="font-mono text-sm" |
| 100 | + placeholder={t("settings.cliCustomPathPlaceholder")} |
| 101 | + value={selectedPath} |
| 102 | + onChange={(e) => { setSelectedPath(e.target.value); setResult(null); }} |
| 103 | + /> |
| 104 | + </div> |
| 105 | + |
| 106 | + {/* Install button */} |
| 107 | + <Button |
| 108 | + onClick={handleInstall} |
| 109 | + disabled={installing || !selectedPath.trim()} |
| 110 | + size="sm" |
| 111 | + > |
| 112 | + {installing ? t("settings.cliInstalling") : t("settings.cliInstall")} |
| 113 | + </Button> |
| 114 | + |
| 115 | + {/* Feedback */} |
| 116 | + {result && ( |
| 117 | + <p className={`text-xs ${result.success ? "text-green-600 dark:text-green-400" : "text-destructive"}`}> |
| 118 | + {result.success |
| 119 | + ? t("settings.cliSuccess", { path: result.path }) |
| 120 | + : t("settings.cliError", { error: result.error })} |
| 121 | + </p> |
| 122 | + )} |
| 123 | + |
| 124 | + {/* Hint */} |
| 125 | + <p className="text-xs text-muted-foreground">{t("settings.cliHint")}</p> |
| 126 | + </div> |
| 127 | + ); |
| 128 | +} |
0 commit comments