import * as React from "react"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { CheckCircle, Circle, Loader2 } from "lucide-react"; import { useAuth } from "@/contexts/useAuth"; import { WalletConnectionStep } from "./wallet-connection-step"; import { VerificationStep } from "./verification-step"; import { DelegationStep } from "./delegation-step"; interface WalletWizardProps { open: boolean; onOpenChange: (open: boolean) => void; onComplete: () => void; } type WizardStep = 1 | 2 | 3; export function WalletWizard({ open, onOpenChange, onComplete, }: WalletWizardProps) { const [currentStep, setCurrentStep] = React.useState(1); const [isLoading, setIsLoading] = React.useState(false); const { currentUser, isAuthenticated, verificationStatus, isDelegationValid } = useAuth(); const hasInitialized = React.useRef(false); // Reset wizard when opened and determine starting step React.useEffect(() => { if (open && !hasInitialized.current) { // Determine the appropriate starting step based on current state if (!isAuthenticated) { setCurrentStep(1); // Start at connection step if not authenticated } else if (isAuthenticated && (verificationStatus === 'unverified' || verificationStatus === 'verifying')) { setCurrentStep(2); // Start at verification step if authenticated but not verified } else if (isAuthenticated && (verificationStatus === 'verified-owner' || verificationStatus === 'verified-basic' || verificationStatus === 'verified-none') && !isDelegationValid()) { setCurrentStep(3); // Start at delegation step if verified but no valid delegation } else { setCurrentStep(3); // Default to step 3 if everything is complete } setIsLoading(false); hasInitialized.current = true; } else if (!open) { hasInitialized.current = false; } }, [open, isAuthenticated, verificationStatus, isDelegationValid]); const handleStepComplete = (step: WizardStep) => { if (step < 3) { setCurrentStep((step + 1) as WizardStep); } else { onComplete(); onOpenChange(false); } }; const handleClose = () => { if (isLoading) return; // Prevent closing during operations onOpenChange(false); }; const getStepStatus = (step: WizardStep) => { if (step === 1) { return isAuthenticated ? 'complete' : 'current'; } else if (step === 2) { if (!isAuthenticated) return 'disabled'; if (verificationStatus === 'unverified' || verificationStatus === 'verifying') return 'current'; if (verificationStatus === 'verified-owner' || verificationStatus === 'verified-basic' || verificationStatus === 'verified-none') return 'complete'; return 'disabled'; } else if (step === 3) { if (!isAuthenticated || (verificationStatus !== 'verified-owner' && verificationStatus !== 'verified-basic' && verificationStatus !== 'verified-none')) return 'disabled'; if (isDelegationValid()) return 'complete'; return 'current'; } return 'disabled'; }; const renderStepIcon = (step: WizardStep) => { const status = getStepStatus(step); if (status === "complete") { return ; } else if (status === "current") { return ; } else { return ; } }; const getStepTitle = (step: WizardStep) => { switch (step) { case 1: return "Connect Wallet"; case 2: return "Verify Ownership"; case 3: return "Delegate Key"; default: return ""; } }; return ( Setup Your Account Complete these steps to access all OpChan features {/* Progress Indicator */}
{[1, 2, 3].map((step) => (
{renderStepIcon(step as WizardStep)} {getStepTitle(step as WizardStep)}
{step < 3 && (
)}
))}
{/* Step Content - Fixed height container */}
{currentStep === 1 && ( handleStepComplete(1)} isLoading={isLoading} setIsLoading={setIsLoading} /> )} {currentStep === 2 && ( handleStepComplete(2)} onBack={() => setCurrentStep(1)} isLoading={isLoading} setIsLoading={setIsLoading} /> )} {currentStep === 3 && ( handleStepComplete(3)} onBack={() => setCurrentStep(2)} isLoading={isLoading} setIsLoading={setIsLoading} /> )}
{/* Footer */}

Step {currentStep} of 3

{currentStep > 1 && ( )}
); }