import React from 'react'; import { Button } from './button'; import { useAuth } from '@/contexts/useAuth'; import { CheckCircle, AlertCircle, Trash2 } from 'lucide-react'; import { DelegationDuration } from '@/lib/services/CryptoService'; interface DelegationStepProps { onComplete: () => void; onBack: () => void; isLoading: boolean; setIsLoading: (loading: boolean) => void; } export function DelegationStep({ onComplete, onBack, isLoading, setIsLoading, }: DelegationStepProps) { const { currentUser, delegateKey, isDelegationValid, delegationTimeRemaining, isAuthenticating, clearDelegation, } = useAuth(); const [selectedDuration, setSelectedDuration] = React.useState('7days'); const [delegationResult, setDelegationResult] = React.useState<{ success: boolean; message: string; expiry?: string; } | null>(null); const handleDelegate = async () => { if (!currentUser) return; setIsLoading(true); setDelegationResult(null); try { const success = await delegateKey(selectedDuration); if (success) { const expiryDate = currentUser.delegationExpiry ? new Date(currentUser.delegationExpiry).toLocaleString() : `${selectedDuration === '7days' ? '1 week' : '30 days'} from now`; setDelegationResult({ success: true, message: 'Key delegation successful!', expiry: expiryDate, }); } else { setDelegationResult({ success: false, message: 'Key delegation failed.', }); } } catch (error) { setDelegationResult({ success: false, message: `Delegation failed. Please try again: ${error}`, }); } finally { setIsLoading(false); } }; const handleComplete = () => { onComplete(); }; // Show delegation result if (delegationResult) { return (
{delegationResult.success ? ( ) : ( )} {delegationResult.success ? 'Delegation Complete' : 'Delegation Result'}

{delegationResult.message}

{delegationResult.expiry && (

Expires: {delegationResult.expiry}

)}
{/* Action Button */}
); } // Show minimal delegation status return (

Key Delegation

Delegate signing authority to your browser for convenient forum interactions

{/* Status */}
{isDelegationValid() ? ( ) : ( )} {isDelegationValid() ? 'Delegated' : 'Required'} {isDelegationValid() && ( {Math.floor(delegationTimeRemaining() / (1000 * 60 * 60))}h{' '} {Math.floor( (delegationTimeRemaining() % (1000 * 60 * 60)) / (1000 * 60) )} m remaining )}
{/* Duration Selection */} {!isDelegationValid() && (
)} {/* Delegated Browser Public Key */} {isDelegationValid() && currentUser?.browserPubKey && (
{currentUser.browserPubKey}
)} {/* Wallet Address */} {currentUser && (
{currentUser.address}
)} {/* Delete Button for Active Delegations */} {isDelegationValid() && (
)}
{/* Action Buttons */}
{isDelegationValid() ? ( ) : ( )}
); }