"use client"; import { useState, useEffect } from "react"; import { useTranslations } from "next-intl"; import Modal from "./Modal"; import Button from "./Button"; import Input from "./Input"; type CursorAuthModalProps = { isOpen: boolean; onSuccess?: () => void; onClose: () => void; reauthConnection?: unknown; }; /** * Cursor Auth Modal * Auto-detect and import token from Cursor IDE's local SQLite database */ export default function CursorAuthModal({ isOpen, onSuccess, onClose, reauthConnection: _, }: CursorAuthModalProps) { const t = useTranslations("cursorAuthModal"); const [accessToken, setAccessToken] = useState(""); const [machineId, setMachineId] = useState(""); const [error, setError] = useState(null); const [importing, setImporting] = useState(false); const [autoDetecting, setAutoDetecting] = useState(false); const [autoDetected, setAutoDetected] = useState(false); // Auto-detect tokens when modal opens useEffect(() => { if (!isOpen) return; const autoDetect = async () => { setAutoDetecting(true); setError(null); setAutoDetected(false); try { const res = await fetch("/api/oauth/cursor/auto-import"); const data = await res.json(); if (data.found) { setAccessToken(data.accessToken); setMachineId(data.machineId || ""); setAutoDetected(true); } else { setError(data.error || t("errorAutoDetect")); } } catch (err) { setError(t("errorAutoDetectFailed")); } finally { setAutoDetecting(false); } }; autoDetect(); }, [isOpen]); const handleImportToken = async () => { if (!accessToken.trim()) { setError(t("errorEnterToken")); return; } setImporting(true); setError(null); try { const body: Record = { accessToken: accessToken.trim() }; if (machineId.trim()) body.machineId = machineId.trim(); const res = await fetch("/api/oauth/cursor/import", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); const data = await res.json(); if (!res.ok) { throw new Error(data.error || t("errorImportFailed")); } // Success - close modal and trigger refresh onSuccess?.(); onClose(); } catch (err) { setError(err.message); } finally { setImporting(false); } }; return (
{/* Auto-detecting state */} {autoDetecting && (
progress_activity

{t("autoDetecting")}

{t("readingFromCursor")}

)} {/* Form (shown after auto-detect completes) */} {!autoDetecting && ( <> {/* Success message if auto-detected */} {autoDetected && (
check_circle

{t("tokensAutoDetected")}

)} {/* Info message if not auto-detected */} {!autoDetected && !error && (
info

{t("cursorNotDetected")}

)} {/* Access Token Input */}