"use client"; import { useState, useEffect, useRef, useCallback } from "react"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogFooter, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Loader2, Terminal, CheckCircle2, XCircle, Copy, RotateCcw, } from "lucide-react"; import { toast } from "sonner"; import { useOAuthAuthentication } from "../hooks"; interface SetupTokenModalProps { open: boolean; onClose: () => void; onTokenObtained: (token: string) => void; } export function SetupTokenModal({ open, onClose, onTokenObtained, }: SetupTokenModalProps) { // Use the OAuth authentication hook const { authState, output, token, error, startAuth, reset } = useOAuthAuthentication({ cliType: "claude" }); const [manualToken, setManualToken] = useState(""); const scrollRef = useRef(null); // Auto-scroll to bottom when output changes useEffect(() => { if (scrollRef.current) { scrollRef.current.scrollTop = scrollRef.current.scrollHeight; } }, [output]); // Reset state when modal opens/closes useEffect(() => { if (open) { reset(); setManualToken(""); } }, [open, reset]); const handleUseToken = useCallback(() => { const tokenToUse = token || manualToken; if (tokenToUse.trim()) { onTokenObtained(tokenToUse.trim()); onClose(); } }, [token, manualToken, onTokenObtained, onClose]); const copyCommand = useCallback(() => { navigator.clipboard.writeText("claude setup-token"); toast.success("Command copied to clipboard"); }, []); const handleRetry = useCallback(() => { reset(); setManualToken(""); }, [reset]); return ( Claude Subscription Authentication {authState === "idle" && "Click Start to begin the authentication process."} {authState === "running" && "Complete the sign-in in your browser..."} {authState === "success" && "Authentication successful! Your token has been captured."} {authState === "error" && "Authentication failed. Please try again or enter the token manually."} {authState === "manual" && "Copy the token from your terminal and paste it below."} {/* Terminal Output */}
{output.map((line, index) => (
{line.startsWith("Error") || line.startsWith("⚠") ? ( {line} ) : line.startsWith("✓") ? ( {line} ) : ( line )}
))} {output.length === 0 && (
Waiting to start...
)} {authState === "running" && (
Waiting for authentication...
)}
{/* Manual Token Input (for fallback) */} {(authState === "manual" || authState === "error") && (
Run this command in your terminal: claude setup-token
setManualToken(e.target.value)} className="bg-input border-border text-foreground" data-testid="manual-token-input" />
)} {/* Success State */} {authState === "success" && (

Token captured successfully!

Click "Use Token" to save and continue.

)} {/* Error State */} {error && authState === "error" && (

Error

{error}

)} {authState === "idle" && ( )} {authState === "running" && ( )} {authState === "success" && ( )} {authState === "manual" && ( )} {authState === "error" && ( <> {manualToken.trim() && ( )} )}
); }