|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState } from 'react'; |
| 4 | +import { useWallet } from '@/context/WalletContext'; |
| 5 | +import { KeyRound, X, ChevronDown, ChevronUp } from 'lucide-react'; |
| 6 | + |
| 7 | +/** |
| 8 | + * DEV-ONLY floating panel that lets you inject an arbitrary Stellar public key |
| 9 | + * directly into WalletContext — no wallet extension required. |
| 10 | + * |
| 11 | + * Both this component AND `WalletContext.setMockPublicKey` independently |
| 12 | + * check `process.env.NODE_ENV`, ensuring the feature is a guaranteed no-op |
| 13 | + * in production even if only one guard were to be removed. |
| 14 | + */ |
| 15 | +export default function DevWalletSwitcher() { |
| 16 | + // Independent production guard (layer 1 of 2). |
| 17 | + // Returning null *before* any hooks would violate the Rules of Hooks, so we |
| 18 | + // place the early-return after the hook declarations. |
| 19 | + const { setMockPublicKey } = useWallet(); |
| 20 | + const [collapsed, setCollapsed] = useState(true); |
| 21 | + const [inputValue, setInputValue] = useState(''); |
| 22 | + const [dismissed, setDismissed] = useState(false); |
| 23 | + |
| 24 | + // Layer 1 guard: component renders nothing in production. |
| 25 | + if (process.env.NODE_ENV === 'production') { |
| 26 | + return null; |
| 27 | + } |
| 28 | + |
| 29 | + if (dismissed) return null; |
| 30 | + |
| 31 | + return ( |
| 32 | + <div |
| 33 | + className="fixed bottom-4 right-4 z-[9999] w-72 rounded-xl border border-amber-400/40 bg-slate-900/95 shadow-2xl backdrop-blur-sm" |
| 34 | + role="complementary" |
| 35 | + aria-label="Dev wallet switcher" |
| 36 | + > |
| 37 | + {/* ── Header bar ─────────────────────────────────────────────────────── */} |
| 38 | + <div className="flex items-center justify-between px-3 py-2 border-b border-slate-700/60"> |
| 39 | + <button |
| 40 | + type="button" |
| 41 | + onClick={() => setCollapsed((c) => !c)} |
| 42 | + className="flex items-center gap-1.5 text-amber-400 text-xs font-semibold tracking-wide uppercase focus:outline-none focus:ring-2 focus:ring-amber-400/60 rounded" |
| 43 | + aria-expanded={!collapsed} |
| 44 | + aria-controls="dev-wallet-switcher-body" |
| 45 | + > |
| 46 | + <KeyRound className="w-3.5 h-3.5" aria-hidden="true" /> |
| 47 | + Dev Wallet |
| 48 | + {collapsed |
| 49 | + ? <ChevronDown className="w-3.5 h-3.5 ml-0.5" aria-hidden="true" /> |
| 50 | + : <ChevronUp className="w-3.5 h-3.5 ml-0.5" aria-hidden="true" />} |
| 51 | + </button> |
| 52 | + |
| 53 | + <button |
| 54 | + type="button" |
| 55 | + onClick={() => setDismissed(true)} |
| 56 | + aria-label="Dismiss dev wallet panel" |
| 57 | + className="text-slate-500 hover:text-slate-300 transition-colors focus:outline-none focus:ring-2 focus:ring-amber-400/60 rounded" |
| 58 | + > |
| 59 | + <X className="w-3.5 h-3.5" aria-hidden="true" /> |
| 60 | + </button> |
| 61 | + </div> |
| 62 | + |
| 63 | + {/* ── Collapsible body ────────────────────────────────────────────────── */} |
| 64 | + {!collapsed && ( |
| 65 | + <div |
| 66 | + id="dev-wallet-switcher-body" |
| 67 | + className="px-3 py-3 flex flex-col gap-2" |
| 68 | + > |
| 69 | + <label |
| 70 | + htmlFor="dev-wallet-input" |
| 71 | + className="text-xs text-slate-400 font-medium" |
| 72 | + > |
| 73 | + Stellar public key |
| 74 | + </label> |
| 75 | + <input |
| 76 | + id="dev-wallet-input" |
| 77 | + type="text" |
| 78 | + value={inputValue} |
| 79 | + onChange={(e) => setInputValue(e.target.value)} |
| 80 | + placeholder="G..." |
| 81 | + spellCheck={false} |
| 82 | + className="w-full rounded-lg border border-slate-700 bg-slate-800 px-3 py-2 text-xs font-mono text-slate-100 placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-amber-400/60 transition-colors" |
| 83 | + /> |
| 84 | + <button |
| 85 | + type="button" |
| 86 | + onClick={() => { |
| 87 | + if (inputValue.trim()) { |
| 88 | + setMockPublicKey(inputValue.trim()); |
| 89 | + } |
| 90 | + }} |
| 91 | + disabled={!inputValue.trim()} |
| 92 | + className="flex items-center justify-center gap-2 rounded-lg bg-amber-500 hover:bg-amber-400 disabled:opacity-40 disabled:cursor-not-allowed text-slate-900 text-xs font-semibold px-3 py-2 transition-colors focus:outline-none focus:ring-2 focus:ring-amber-400/60 focus:ring-offset-2 focus:ring-offset-slate-900" |
| 93 | + > |
| 94 | + <KeyRound className="w-3.5 h-3.5" aria-hidden="true" /> |
| 95 | + Apply key |
| 96 | + </button> |
| 97 | + <p className="text-[10px] text-slate-500 leading-snug"> |
| 98 | + ⚠ Dev only — this panel and the underlying method are both no-ops in production. |
| 99 | + </p> |
| 100 | + </div> |
| 101 | + )} |
| 102 | + </div> |
| 103 | + ); |
| 104 | +} |
0 commit comments