import React, { useEffect, useState } from "react"; import { Check, Eye, EyeOff, RefreshCw } from "lucide-react"; import { useI18n } from "../../../../application/i18n/I18nProvider"; import { decryptField } from "../../../../infrastructure/persistence/secureFieldAdapter"; import { Button } from "../../../ui/button"; import { cn } from "../../../../lib/utils"; import type { AgentPathInfo } from "./types"; export const CursorSdkCard: React.FC<{ pathInfo: AgentPathInfo | null; isResolvingPath: boolean; encryptedApiKey?: string; onSaveApiKey: (apiKey: string) => Promise; onRecheckPath: () => void; }> = ({ pathInfo, isResolvingPath, encryptedApiKey, onSaveApiKey, onRecheckPath, }) => { const { t } = useI18n(); const [apiKeyDraft, setApiKeyDraft] = useState(""); const [showApiKey, setShowApiKey] = useState(false); const [isDecrypting, setIsDecrypting] = useState(false); const [isSaving, setIsSaving] = useState(false); const [saved, setSaved] = useState(false); useEffect(() => { let cancelled = false; setSaved(false); if (!encryptedApiKey) { setApiKeyDraft(""); return; } setIsDecrypting(true); decryptField(encryptedApiKey) .then((value) => { if (!cancelled) setApiKeyDraft(value ?? ""); }) .catch(() => { if (!cancelled) setApiKeyDraft(""); }) .finally(() => { if (!cancelled) setIsDecrypting(false); }); return () => { cancelled = true; }; }, [encryptedApiKey]); const installed = Boolean(pathInfo?.installed); const available = Boolean(pathInfo?.available); const hasStoredApiKey = Boolean(encryptedApiKey); const usesEnvApiKey = pathInfo?.authSource === "CURSOR_API_KEY"; const hasAnyApiKey = hasStoredApiKey || usesEnvApiKey; const canSave = !isSaving && !isDecrypting && (Boolean(apiKeyDraft.trim()) || hasStoredApiKey); const installStatus = isResolvingPath ? t("ai.cursor.detecting") : installed ? t("ai.cursor.installed") : t("ai.cursor.notInstalled"); const keyStatus = hasAnyApiKey ? usesEnvApiKey && !hasStoredApiKey ? t("ai.cursor.apiKeyFromEnv") : t("ai.cursor.apiKeyConfigured") : t("ai.cursor.apiKeyMissing"); const installStatusClassName = isResolvingPath ? "text-muted-foreground" : installed ? "text-emerald-500" : "text-amber-500"; const keyStatusClassName = hasAnyApiKey ? "text-emerald-500" : "text-amber-500"; const handleSave = async () => { setIsSaving(true); setSaved(false); try { await onSaveApiKey(apiKeyDraft.trim()); setSaved(true); } finally { setIsSaving(false); } }; return (
{t("ai.cursor.installStatus")} {installStatus}
{t("ai.cursor.apiKeyStatus")} {keyStatus}
{!available && (

{installed ? t("ai.cursor.notFoundHint") : t("ai.cursor.notInstalledHint")}

)}
{ setSaved(false); setApiKeyDraft(event.target.value); }} placeholder={ isDecrypting ? t("ai.providers.apiKey.decrypting") : usesEnvApiKey && !hasStoredApiKey ? t("ai.cursor.apiKeyPlaceholder.env") : t("ai.cursor.apiKeyPlaceholder") } disabled={isDecrypting} className="w-full h-8 rounded-md border border-input bg-background px-3 pr-9 text-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:opacity-50" />
{usesEnvApiKey && !hasStoredApiKey ? (

{t("ai.cursor.apiKeyEnvHint")}

) : null} {usesEnvApiKey && hasStoredApiKey ? (

{t("ai.cursor.apiKeyOverrideHint")}

) : null}
); };