refactor: migrate TrumpSignal to TypeScript monorepo and Pages Router
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
'use client'
|
||||
|
||||
/**
|
||||
* MobileWalletSheet — bottom-drawer shown on mobile when no injected wallet
|
||||
* is detected. Offers deep links to common mobile wallets so the user can
|
||||
* open MetaMask (or Trust / Coinbase / OKX) and land directly on this dApp.
|
||||
*
|
||||
* Rendered as a fixed overlay + slide-up panel. Backdrop tap dismisses it.
|
||||
* The CSS animation class `mobile-sheet-enter` is defined in globals.css.
|
||||
*
|
||||
* Usage:
|
||||
* <MobileWalletSheet open={open} onClose={() => setOpen(false)} />
|
||||
*/
|
||||
|
||||
import { useEffect, useState } from 'react'
|
||||
import { getWalletLinks } from '@/lib/mobileWallet'
|
||||
import type { WalletLink } from '@/lib/mobileWallet'
|
||||
|
||||
interface Props {
|
||||
open: boolean
|
||||
onClose: () => void
|
||||
}
|
||||
|
||||
export default function MobileWalletSheet({ open, onClose }: Props) {
|
||||
const [links, setLinks] = useState<WalletLink[]>([])
|
||||
|
||||
// Build deep links client-side only (needs window.location.href).
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setLinks(getWalletLinks(window.location.href))
|
||||
}
|
||||
}, [open])
|
||||
|
||||
// Lock body scroll while sheet is open.
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
document.body.style.overflow = 'hidden'
|
||||
return () => {
|
||||
document.body.style.overflow = ''
|
||||
}
|
||||
}
|
||||
}, [open])
|
||||
|
||||
// Escape key closes.
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
function onKey(e: KeyboardEvent) {
|
||||
if (e.key === 'Escape') onClose()
|
||||
}
|
||||
window.addEventListener('keydown', onKey)
|
||||
return () => window.removeEventListener('keydown', onKey)
|
||||
}, [open, onClose])
|
||||
|
||||
if (!open) return null
|
||||
|
||||
return (
|
||||
/* Backdrop */
|
||||
<div
|
||||
style={{
|
||||
position: 'fixed',
|
||||
inset: 0,
|
||||
zIndex: 9000,
|
||||
background: 'rgba(0,0,0,0.55)',
|
||||
display: 'flex',
|
||||
alignItems: 'flex-end',
|
||||
backdropFilter: 'blur(4px)',
|
||||
WebkitBackdropFilter: 'blur(4px)',
|
||||
}}
|
||||
onClick={onClose}
|
||||
aria-modal="true"
|
||||
role="dialog"
|
||||
aria-label="Connect a wallet"
|
||||
>
|
||||
{/* Sheet */}
|
||||
<div
|
||||
className="mobile-sheet-enter"
|
||||
style={{
|
||||
width: '100%',
|
||||
background: 'var(--bg-elev)',
|
||||
borderRadius: '20px 20px 0 0',
|
||||
padding: '20px 20px 32px',
|
||||
boxShadow: '0 -8px 40px rgba(0,0,0,0.18)',
|
||||
maxHeight: '80vh',
|
||||
overflowY: 'auto',
|
||||
}}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
{/* Handle bar */}
|
||||
<div
|
||||
style={{
|
||||
width: 40,
|
||||
height: 4,
|
||||
background: 'var(--line-2)',
|
||||
borderRadius: 99,
|
||||
margin: '0 auto 20px',
|
||||
}}
|
||||
/>
|
||||
|
||||
{/* Header */}
|
||||
<div style={{ marginBottom: 20 }}>
|
||||
<div style={{ fontSize: 18, fontWeight: 700, letterSpacing: '-0.01em', marginBottom: 4 }}>
|
||||
Connect a wallet
|
||||
</div>
|
||||
<div style={{ fontSize: 13, color: 'var(--ink-3)', lineHeight: 1.5 }}>
|
||||
Open the dApp inside your wallet's built-in browser to connect.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Wallet list */}
|
||||
<div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
|
||||
{links.map((link) => (
|
||||
<a
|
||||
key={link.name}
|
||||
href={link.href}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
style={{
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
gap: 14,
|
||||
padding: '14px 16px',
|
||||
borderRadius: 'var(--r-md)',
|
||||
border: '1px solid var(--line)',
|
||||
background: 'var(--surface)',
|
||||
textDecoration: 'none',
|
||||
color: 'inherit',
|
||||
transition: 'background 120ms, border-color 120ms',
|
||||
}}
|
||||
onTouchStart={(e) => {
|
||||
;(e.currentTarget as HTMLAnchorElement).style.background = 'var(--bg-sunk)'
|
||||
}}
|
||||
onTouchEnd={(e) => {
|
||||
;(e.currentTarget as HTMLAnchorElement).style.background = 'var(--surface)'
|
||||
}}
|
||||
>
|
||||
{/* Icon */}
|
||||
<div
|
||||
style={{
|
||||
width: 44,
|
||||
height: 44,
|
||||
borderRadius: 12,
|
||||
background: link.color,
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
fontSize: link.abbr.length > 2 ? 12 : 20,
|
||||
fontWeight: 700,
|
||||
color: '#fff',
|
||||
flexShrink: 0,
|
||||
letterSpacing: '-0.01em',
|
||||
}}
|
||||
>
|
||||
{link.abbr}
|
||||
</div>
|
||||
|
||||
{/* Text */}
|
||||
<div style={{ flex: 1, minWidth: 0 }}>
|
||||
<div style={{ fontSize: 15, fontWeight: 600, marginBottom: 2 }}>{link.name}</div>
|
||||
<div style={{ fontSize: 12, color: 'var(--ink-3)' }}>{link.hint}</div>
|
||||
</div>
|
||||
|
||||
{/* Arrow */}
|
||||
<svg
|
||||
width="16"
|
||||
height="16"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
style={{ color: 'var(--ink-3)', flexShrink: 0 }}
|
||||
>
|
||||
<path
|
||||
d="M9 18l6-6-6-6"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
/>
|
||||
</svg>
|
||||
</a>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* WalletConnect note */}
|
||||
<div
|
||||
style={{
|
||||
marginTop: 18,
|
||||
padding: '12px 14px',
|
||||
borderRadius: 'var(--r-sm)',
|
||||
background: 'var(--bg-sunk)',
|
||||
fontSize: 12,
|
||||
color: 'var(--ink-3)',
|
||||
lineHeight: 1.55,
|
||||
}}
|
||||
>
|
||||
<strong style={{ color: 'var(--ink-2)' }}>Already in your wallet's browser?</strong> Tap
|
||||
Cancel to dismiss, then use the built-in browser navigation to reload the page — MetaMask
|
||||
should auto-detect the dApp.
|
||||
</div>
|
||||
|
||||
{/* Close */}
|
||||
<button
|
||||
onClick={onClose}
|
||||
style={{
|
||||
marginTop: 14,
|
||||
width: '100%',
|
||||
padding: '14px',
|
||||
borderRadius: 'var(--r-md)',
|
||||
background: 'var(--bg-sunk)',
|
||||
border: '1px solid var(--line)',
|
||||
fontSize: 14,
|
||||
fontWeight: 500,
|
||||
color: 'var(--ink-2)',
|
||||
}}
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
'use client'
|
||||
|
||||
/**
|
||||
* SignConfirmSheet — elegant pre-signing confirmation UI.
|
||||
*
|
||||
* Usage (imperative, no context needed):
|
||||
*
|
||||
* import { confirmSign } from '@/components/wallet/SignConfirmSheet'
|
||||
*
|
||||
* const ok = await confirmSign({
|
||||
* label: 'Enable Auto-Trade',
|
||||
* description: 'Bot will open live trades on Hyperliquid when signals fire.',
|
||||
* danger: true,
|
||||
* })
|
||||
* if (!ok) return // user cancelled
|
||||
* const env = await signRequest(...) // only NOW triggers MetaMask
|
||||
*/
|
||||
|
||||
import { createRoot } from 'react-dom/client'
|
||||
|
||||
export interface SignConfirmOptions {
|
||||
/** Short action title, e.g. "Enable Auto-Trade" */
|
||||
label: string
|
||||
/** One-sentence explanation shown to the user */
|
||||
description: string
|
||||
/** If true, renders a red/warning accent — for irreversible / live-money actions */
|
||||
danger?: boolean
|
||||
}
|
||||
|
||||
// Track the currently-mounted sheet so a second confirmSign() call can resolve
|
||||
// the first one with `false` instead of leaving its promise dangling forever.
|
||||
let _activeCancel: (() => void) | null = null
|
||||
|
||||
/** Renders a confirmation sheet and resolves true (confirm) or false (cancel). */
|
||||
export function confirmSign(opts: SignConfirmOptions): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
// If another sheet is already mounted, cancel it first so its awaiting
|
||||
// caller resolves with `false` rather than hanging indefinitely.
|
||||
if (_activeCancel) {
|
||||
try {
|
||||
_activeCancel()
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
const existing = document.getElementById('sign-confirm-sheet-root')
|
||||
if (existing?.parentNode) {
|
||||
existing.parentNode.removeChild(existing)
|
||||
}
|
||||
const container = document.createElement('div')
|
||||
container.id = 'sign-confirm-sheet-root'
|
||||
document.body.appendChild(container)
|
||||
const root = createRoot(container)
|
||||
let settled = false
|
||||
|
||||
function cleanup(result: boolean) {
|
||||
if (settled) return
|
||||
settled = true
|
||||
// Clear the active-cancel slot only if it still points to *this* sheet —
|
||||
// a later confirmSign() may have already replaced it.
|
||||
if (_activeCancel === cancelSelf) _activeCancel = null
|
||||
root.unmount()
|
||||
if (container.parentNode) {
|
||||
container.parentNode.removeChild(container)
|
||||
}
|
||||
resolve(result)
|
||||
}
|
||||
|
||||
// Stable reference used both as the active-cancel handle and inside cleanup.
|
||||
const cancelSelf = () => cleanup(false)
|
||||
_activeCancel = cancelSelf
|
||||
|
||||
root.render(<Sheet {...opts} onConfirm={() => cleanup(true)} onCancel={cancelSelf} />)
|
||||
})
|
||||
}
|
||||
|
||||
/* ─── Internal sheet component ─────────────────────────────────────────────── */
|
||||
|
||||
function Sheet({
|
||||
label,
|
||||
description,
|
||||
danger,
|
||||
onConfirm,
|
||||
onCancel,
|
||||
}: SignConfirmOptions & { onConfirm: () => void; onCancel: () => void }) {
|
||||
const accent = danger ? '#ef4444' : '#f5a524'
|
||||
const accentSoft = danger ? 'rgba(239,68,68,0.1)' : 'rgba(245,165,36,0.1)'
|
||||
|
||||
// Dismiss on backdrop click
|
||||
function handleBackdrop(e: React.MouseEvent<HTMLDivElement>) {
|
||||
if (e.target === e.currentTarget) onCancel()
|
||||
}
|
||||
|
||||
// Dismiss on Escape
|
||||
function handleKey(e: React.KeyboardEvent) {
|
||||
if (e.key === 'Escape') onCancel()
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
onClick={handleBackdrop}
|
||||
onKeyDown={handleKey}
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-label={label}
|
||||
style={{
|
||||
position: 'fixed',
|
||||
inset: 0,
|
||||
background: 'rgba(0,0,0,0.70)',
|
||||
backdropFilter: 'blur(3px)',
|
||||
WebkitBackdropFilter: 'blur(3px)',
|
||||
zIndex: 99999,
|
||||
display: 'flex',
|
||||
alignItems: 'flex-end',
|
||||
justifyContent: 'center',
|
||||
padding: '0 0 env(safe-area-inset-bottom, 0)',
|
||||
}}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
width: '100%',
|
||||
maxWidth: 480,
|
||||
background: '#111',
|
||||
borderRadius: '16px 16px 0 0',
|
||||
border: '1px solid #2a2a2a',
|
||||
borderBottom: 'none',
|
||||
padding: '28px 24px 32px',
|
||||
boxShadow: '0 -12px 48px rgba(0,0,0,0.5)',
|
||||
animation: 'signSheetIn 0.22s cubic-bezier(0.32,0.72,0,1)',
|
||||
}}
|
||||
>
|
||||
{/* 拖拽条 */}
|
||||
<div
|
||||
style={{
|
||||
width: 36,
|
||||
height: 4,
|
||||
borderRadius: 2,
|
||||
background: '#333',
|
||||
margin: '0 auto 24px',
|
||||
}}
|
||||
/>
|
||||
|
||||
{/* 图标 + 标题 */}
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: 12, marginBottom: 12 }}>
|
||||
<div
|
||||
style={{
|
||||
width: 40,
|
||||
height: 40,
|
||||
borderRadius: 10,
|
||||
background: accentSoft,
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
fontSize: 20,
|
||||
flexShrink: 0,
|
||||
}}
|
||||
>
|
||||
🔏
|
||||
</div>
|
||||
<div>
|
||||
<div
|
||||
style={{
|
||||
fontSize: 11,
|
||||
color: '#666',
|
||||
letterSpacing: '0.07em',
|
||||
textTransform: 'uppercase',
|
||||
marginBottom: 2,
|
||||
}}
|
||||
>
|
||||
Wallet signature required
|
||||
</div>
|
||||
<div style={{ fontSize: 17, fontWeight: 700, color: '#fff', lineHeight: 1.2 }}>
|
||||
{label}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 说明文字 */}
|
||||
<div
|
||||
style={{
|
||||
fontSize: 14,
|
||||
color: '#999',
|
||||
lineHeight: 1.6,
|
||||
margin: '16px 0 24px',
|
||||
paddingLeft: 52, // 与标题对齐
|
||||
}}
|
||||
>
|
||||
{description}
|
||||
</div>
|
||||
|
||||
{/* 说明条 */}
|
||||
<div
|
||||
style={{
|
||||
padding: '10px 12px',
|
||||
borderRadius: 8,
|
||||
background: 'rgba(255,255,255,0.04)',
|
||||
border: '1px solid #2a2a2a',
|
||||
fontSize: 12,
|
||||
color: '#666',
|
||||
lineHeight: 1.5,
|
||||
marginBottom: 20,
|
||||
}}
|
||||
>
|
||||
Signing is used only to verify your identity. It will NOT authorize any on-chain transfer
|
||||
and will NOT cost any gas. The MetaMask popup will appear after you click Confirm.
|
||||
</div>
|
||||
|
||||
{/* 按钮组 */}
|
||||
<div style={{ display: 'flex', gap: 10 }}>
|
||||
<button
|
||||
onClick={onCancel}
|
||||
style={{
|
||||
flex: 1,
|
||||
padding: '12px 0',
|
||||
borderRadius: 10,
|
||||
border: '1px solid #2a2a2a',
|
||||
background: 'transparent',
|
||||
color: '#888',
|
||||
fontSize: 14,
|
||||
fontWeight: 600,
|
||||
cursor: 'pointer',
|
||||
}}
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
onClick={onConfirm}
|
||||
autoFocus
|
||||
style={{
|
||||
flex: 2,
|
||||
padding: '12px 0',
|
||||
borderRadius: 10,
|
||||
border: 'none',
|
||||
background: accent,
|
||||
color: danger ? '#fff' : '#000',
|
||||
fontSize: 14,
|
||||
fontWeight: 700,
|
||||
cursor: 'pointer',
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
gap: 6,
|
||||
}}
|
||||
>
|
||||
<span>Open MetaMask</span>
|
||||
<span style={{ opacity: 0.7, fontSize: 16 }}>→</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>{`
|
||||
@keyframes signSheetIn {
|
||||
from { transform: translateY(100%); opacity: 0; }
|
||||
to { transform: translateY(0); opacity: 1; }
|
||||
}
|
||||
`}</style>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user