import React, { useState, useEffect } from 'react'; import { Button } from './button'; import { useAuth, useAuthActions } from '@/hooks'; import { useAuth as useAuthContext } from '@/contexts/useAuth'; import { CheckCircle, AlertCircle, Trash2 } from 'lucide-react'; import { DelegationDuration, DelegationFullStatus } from '@opchan/core'; interface DelegationStepProps { onComplete: () => void; onBack?: () => void; isLoading: boolean; setIsLoading: (loading: boolean) => void; } export function DelegationStep({ onComplete, onBack, isLoading, setIsLoading, }: DelegationStepProps) { const { currentUser, isAuthenticating } = useAuth(); const { getDelegationStatus } = useAuthContext(); const [delegationInfo, setDelegationInfo] = useState(null); const { delegateKey, clearDelegation } = useAuthActions(); // Load delegation status useEffect(() => { getDelegationStatus().then(setDelegationInfo).catch(console.error); }, [getDelegationStatus]); 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 ? 'Success!' : 'Failed'}

{delegationResult.message}

{delegationResult.success && delegationResult.expiry && (

Expires: {delegationResult.expiry}

)}
{onBack && ( )}
); } return (

Step 3: Key Delegation

Delegate signing authority to your browser for convenient forum interactions

{/* Status */}
{delegationInfo?.isValid ? ( ) : ( )} {delegationInfo?.isValid ? 'Delegated' : 'Required'} {delegationInfo?.isValid && delegationInfo?.timeRemaining && ( {delegationInfo.timeRemaining} remaining )}
{/* Duration Selection */} {!delegationInfo?.isValid && (
)} {/* Delegated Browser Public Key */} {delegationInfo?.isValid && currentUser?.browserPubKey && (
{currentUser.browserPubKey}
)} {/* User Address */} {currentUser && (
{currentUser.address}
)} {/* Delete Button for Active Delegations */} {delegationInfo?.isValid && (
)}
{/* Action Buttons */}
{delegationInfo?.isValid ? ( ) : ( )} {onBack && ( )}
); }