|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { useState, useEffect } from "react" |
| 4 | +import { motion, AnimatePresence } from "framer-motion" |
| 5 | +import { Settings, Info, CheckCircle } from "lucide-react" |
| 6 | +import { Button } from "@/components/ui/button" |
| 7 | +import { Input } from "@/components/ui/input" |
| 8 | +import { Label } from "@/components/ui/label" |
| 9 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" |
| 10 | +import { Alert, AlertDescription } from "@/components/ui/alert" |
| 11 | + |
| 12 | +interface RPCInputProps { |
| 13 | + onRPCSet: (rpcUrl: string) => void |
| 14 | + isConnected: boolean |
| 15 | +} |
| 16 | + |
| 17 | +export function RPCInput({ onRPCSet, isConnected }: RPCInputProps) { |
| 18 | + const [rpcUrl, setRpcUrl] = useState("") |
| 19 | + const [isExpanded, setIsExpanded] = useState(false) |
| 20 | + const [hasCustomRPC, setHasCustomRPC] = useState(false) |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + // Check if user has already set a custom RPC |
| 24 | + const savedRPC = localStorage.getItem("custom_rpc_url") |
| 25 | + if (savedRPC) { |
| 26 | + setRpcUrl(savedRPC) |
| 27 | + setHasCustomRPC(true) |
| 28 | + onRPCSet(savedRPC) |
| 29 | + } |
| 30 | + }, [onRPCSet]) |
| 31 | + |
| 32 | + const handleSetRPC = () => { |
| 33 | + if (rpcUrl.trim()) { |
| 34 | + localStorage.setItem("custom_rpc_url", rpcUrl.trim()) |
| 35 | + setHasCustomRPC(true) |
| 36 | + onRPCSet(rpcUrl.trim()) |
| 37 | + setIsExpanded(false) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + const handleUsePublicRPC = () => { |
| 42 | + localStorage.removeItem("custom_rpc_url") |
| 43 | + setHasCustomRPC(false) |
| 44 | + const publicRPC = "https://base-sepolia.drpc.org" |
| 45 | + setRpcUrl(publicRPC) |
| 46 | + onRPCSet(publicRPC) |
| 47 | + setIsExpanded(false) |
| 48 | + } |
| 49 | + |
| 50 | + if (!isConnected) return null |
| 51 | + |
| 52 | + return ( |
| 53 | + <AnimatePresence> |
| 54 | + <motion.div |
| 55 | + initial={{ opacity: 0, y: -20 }} |
| 56 | + animate={{ opacity: 1, y: 0 }} |
| 57 | + exit={{ opacity: 0, y: -20 }} |
| 58 | + transition={{ duration: 0.3 }} |
| 59 | + > |
| 60 | + <Card className="max-w-md mx-auto mb-6"> |
| 61 | + <CardHeader className="pb-3"> |
| 62 | + <CardTitle className="flex items-center gap-2 text-lg"> |
| 63 | + <Settings className="w-5 h-5" /> |
| 64 | + RPC Configuration |
| 65 | + </CardTitle> |
| 66 | + <CardDescription> |
| 67 | + {hasCustomRPC ? "Using private RPC for optimal performance" : "Configure your RPC endpoint"} |
| 68 | + </CardDescription> |
| 69 | + </CardHeader> |
| 70 | + <CardContent className="space-y-4"> |
| 71 | + {hasCustomRPC ? ( |
| 72 | + <div className="space-y-3"> |
| 73 | + <div className="p-3 bg-green-50 dark:bg-green-900/20 border border-green-200 dark:border-green-800 rounded-lg"> |
| 74 | + <div className="flex items-center gap-2 text-green-800 dark:text-green-200"> |
| 75 | + <CheckCircle className="w-4 h-4" /> |
| 76 | + <span className="font-medium">Private RPC Active</span> |
| 77 | + </div> |
| 78 | + <p className="text-sm text-green-700 dark:text-green-300 mt-1"> |
| 79 | + Using: {rpcUrl.slice(0, 30)}... |
| 80 | + </p> |
| 81 | + </div> |
| 82 | + <div className="flex gap-2"> |
| 83 | + <Button |
| 84 | + onClick={() => setIsExpanded(true)} |
| 85 | + variant="outline" |
| 86 | + size="sm" |
| 87 | + className="flex-1" |
| 88 | + > |
| 89 | + Change RPC |
| 90 | + </Button> |
| 91 | + <Button |
| 92 | + onClick={handleUsePublicRPC} |
| 93 | + variant="outline" |
| 94 | + size="sm" |
| 95 | + className="flex-1" |
| 96 | + > |
| 97 | + Use Public RPC |
| 98 | + </Button> |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + ) : ( |
| 102 | + <div className="space-y-3"> |
| 103 | + <Alert className="border-blue-200 dark:border-blue-800 bg-blue-50 dark:bg-blue-900/20"> |
| 104 | + <Info className="h-4 w-4 text-blue-600 dark:text-blue-400 flex-shrink-0 mt-0.5" /> |
| 105 | + <AlertDescription className="text-blue-800 dark:text-blue-200 text-left leading-normal"> |
| 106 | + <strong>Recommended:</strong> Use a private RPC for best performance when reading logs and listening to events. |
| 107 | + </AlertDescription> |
| 108 | + </Alert> |
| 109 | + |
| 110 | + <div className="space-y-2"> |
| 111 | + <Label htmlFor="rpc-url">RPC URL</Label> |
| 112 | + <Input |
| 113 | + id="rpc-url" |
| 114 | + value={rpcUrl} |
| 115 | + onChange={(e) => setRpcUrl(e.target.value)} |
| 116 | + placeholder="https://your-rpc-endpoint.com" |
| 117 | + className="font-mono text-sm" |
| 118 | + /> |
| 119 | + </div> |
| 120 | + |
| 121 | + <div className="flex gap-2"> |
| 122 | + <Button |
| 123 | + onClick={handleSetRPC} |
| 124 | + disabled={!rpcUrl.trim()} |
| 125 | + className="flex-1" |
| 126 | + > |
| 127 | + Set RPC |
| 128 | + </Button> |
| 129 | + <Button |
| 130 | + onClick={handleUsePublicRPC} |
| 131 | + variant="outline" |
| 132 | + className="flex-1" |
| 133 | + > |
| 134 | + Use Public RPC |
| 135 | + </Button> |
| 136 | + </div> |
| 137 | + |
| 138 | + <div className="text-xs text-gray-500 dark:text-gray-400 text-center"> |
| 139 | + Public RPC: base-sepolia.drpc.org |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + )} |
| 143 | + </CardContent> |
| 144 | + </Card> |
| 145 | + </motion.div> |
| 146 | + </AnimatePresence> |
| 147 | + ) |
| 148 | +} |
0 commit comments