import * as React from "react"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Key, Clock, Shield, Loader2, CheckCircle, AlertCircle } from "lucide-react"; import { useAuth } from "@/contexts/useAuth"; 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 } = useAuth(); 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(); if (success) { const expiryDate = currentUser.delegationExpiry ? new Date(currentUser.delegationExpiry).toLocaleString() : '24 hours from now'; setDelegationResult({ success: true, message: "Key delegation successful! You can now interact with the forum without additional wallet approvals.", expiry: expiryDate }); } else { setDelegationResult({ success: false, message: "Key delegation failed. You can still use the forum but will need to approve each action." }); } } catch (error) { setDelegationResult({ success: false, message: "Delegation failed. Please try again." }); } finally { setIsLoading(false); } }; const handleComplete = () => { onComplete(); }; const formatTimeRemaining = () => { const remaining = delegationTimeRemaining(); if (remaining <= 0) return "Expired"; const hours = Math.floor(remaining / (1000 * 60 * 60)); const minutes = Math.floor((remaining % (1000 * 60 * 60)) / (1000 * 60)); if (hours > 0) { return `${hours}h ${minutes}m remaining`; } else { return `${minutes}m remaining`; } }; // Show delegation result if (delegationResult) { return (
{delegationResult.message}
{delegationResult.expiry && (Expires: {delegationResult.expiry}
You already have an active key delegation.
Time remaining: {formatTimeRemaining()}
{currentUser?.delegationExpiry && (Expires: {new Date(currentUser.delegationExpiry).toLocaleString()}
)}Create a browser-based signing key for better user experience
{currentUser.browserPubKey.slice(0, 20)}...{currentUser.browserPubKey.slice(-20)}
Key delegation is optional but recommended for better UX
You can still use the forum without delegation