|
| 1 | +import { useState } from "react"; |
| 2 | +import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from "@/components/ui/dialog"; |
| 3 | +import { Button } from "@/components/ui/button"; |
| 4 | +import { Input } from "@/components/ui/input"; |
| 5 | +import { Bot, Cpu, Cloud, Check } from "lucide-react"; |
| 6 | +import { cn } from "@/lib/utils"; |
| 7 | +import { useI18n } from "@/lib/i18n"; |
| 8 | + |
| 9 | +type OnboardingProvider = "ollama" | "llamacpp" | "openai" | "anthropic" | "gemini"; |
| 10 | + |
| 11 | +const CLOUD_KEY_INFO: Record<Exclude<OnboardingProvider, "ollama" | "llamacpp">, { secretKey: string; placeholder: string }> = { |
| 12 | + openai: { secretKey: "openai_api_key", placeholder: "sk-..." }, |
| 13 | + anthropic: { secretKey: "anthropic_api_key", placeholder: "sk-ant-..." }, |
| 14 | + gemini: { secretKey: "gemini_api_key", placeholder: "AIza..." }, |
| 15 | +}; |
| 16 | + |
| 17 | +export function OnboardingWizard({ open, onDone }: { open: boolean; onDone: () => void }) { |
| 18 | + const { t } = useI18n(); |
| 19 | + const [selected, setSelected] = useState<OnboardingProvider | null>(null); |
| 20 | + const [keyInput, setKeyInput] = useState(""); |
| 21 | + const [saved, setSaved] = useState(false); |
| 22 | + |
| 23 | + function selectProvider(provider: OnboardingProvider) { |
| 24 | + setSelected(provider); |
| 25 | + setKeyInput(""); |
| 26 | + setSaved(false); |
| 27 | + } |
| 28 | + |
| 29 | + async function finish() { |
| 30 | + await window.api.settings.save({ onboardingComplete: true }); |
| 31 | + onDone(); |
| 32 | + } |
| 33 | + |
| 34 | + async function saveKeyAndFinish() { |
| 35 | + if (selected && selected !== "ollama" && selected !== "llamacpp" && keyInput.trim()) { |
| 36 | + await window.api.secrets.set(CLOUD_KEY_INFO[selected].secretKey, keyInput.trim()); |
| 37 | + setSaved(true); |
| 38 | + } |
| 39 | + await finish(); |
| 40 | + } |
| 41 | + |
| 42 | + const options: { id: OnboardingProvider; label: string; description: string; icon: React.ReactNode }[] = [ |
| 43 | + { id: "ollama", label: t.onboardingOllama, description: t.onboardingOllamaDesc, icon: <Bot className="size-5" /> }, |
| 44 | + { id: "llamacpp", label: t.onboardingLlamaCpp, description: t.onboardingLlamaCppDesc, icon: <Cpu className="size-5" /> }, |
| 45 | + { id: "openai", label: "ChatGPT (OpenAI)", description: t.onboardingCloudDesc, icon: <Cloud className="size-5" /> }, |
| 46 | + { id: "anthropic", label: "Claude (Anthropic)", description: t.onboardingCloudDesc, icon: <Cloud className="size-5" /> }, |
| 47 | + { id: "gemini", label: "Gemini (Google)", description: t.onboardingCloudDesc, icon: <Cloud className="size-5" /> }, |
| 48 | + ]; |
| 49 | + |
| 50 | + const isCloud = selected && selected !== "ollama" && selected !== "llamacpp"; |
| 51 | + |
| 52 | + return ( |
| 53 | + <Dialog open={open} onOpenChange={(o) => !o && finish()}> |
| 54 | + <DialogContent className="max-w-md" showCloseButton={false}> |
| 55 | + <DialogHeader> |
| 56 | + <DialogTitle>{t.onboardingTitle}</DialogTitle> |
| 57 | + <DialogDescription>{t.onboardingSubtitle}</DialogDescription> |
| 58 | + </DialogHeader> |
| 59 | + <div className="flex flex-col gap-2"> |
| 60 | + {options.map((o) => ( |
| 61 | + <button |
| 62 | + key={o.id} |
| 63 | + onClick={() => selectProvider(o.id)} |
| 64 | + className={cn( |
| 65 | + "flex items-center gap-3 rounded-lg border p-3 text-left transition-colors", |
| 66 | + selected === o.id ? "border-primary bg-primary/5" : "border-border hover:bg-muted/50" |
| 67 | + )} |
| 68 | + > |
| 69 | + <span className="text-muted-foreground">{o.icon}</span> |
| 70 | + <span className="min-w-0 flex-1"> |
| 71 | + <span className="block text-sm font-medium">{o.label}</span> |
| 72 | + <span className="block text-xs text-muted-foreground">{o.description}</span> |
| 73 | + </span> |
| 74 | + {selected === o.id && <Check className="size-4 shrink-0 text-primary" />} |
| 75 | + </button> |
| 76 | + ))} |
| 77 | + </div> |
| 78 | + |
| 79 | + {isCloud && ( |
| 80 | + <div className="flex flex-col gap-2 rounded-lg border border-border bg-muted/30 p-3"> |
| 81 | + <label className="text-xs text-muted-foreground">{t.onboardingKeyLabel}</label> |
| 82 | + <Input |
| 83 | + type="password" |
| 84 | + value={keyInput} |
| 85 | + onChange={(e) => setKeyInput(e.target.value)} |
| 86 | + placeholder={CLOUD_KEY_INFO[selected].placeholder} |
| 87 | + autoFocus |
| 88 | + /> |
| 89 | + <p className="text-xs text-muted-foreground">{t.onboardingKeyHint}</p> |
| 90 | + </div> |
| 91 | + )} |
| 92 | + |
| 93 | + <div className="flex items-center justify-between"> |
| 94 | + <button onClick={finish} className="text-xs text-muted-foreground hover:text-foreground"> |
| 95 | + {t.onboardingSkip} |
| 96 | + </button> |
| 97 | + {selected && ( |
| 98 | + <Button |
| 99 | + size="sm" |
| 100 | + onClick={isCloud ? saveKeyAndFinish : finish} |
| 101 | + disabled={isCloud ? !keyInput.trim() || saved : false} |
| 102 | + > |
| 103 | + {t.onboardingContinue} |
| 104 | + </Button> |
| 105 | + )} |
| 106 | + </div> |
| 107 | + </DialogContent> |
| 108 | + </Dialog> |
| 109 | + ); |
| 110 | +} |
0 commit comments