Add button to disconnect wallet #4

Merged
nabarun merged 1 commits from ng-disconnect-wallet into main 2025-01-27 11:43:40 +00:00
3 changed files with 45 additions and 4 deletions

View File

@ -5,7 +5,7 @@ import WalletHeader from '../components/WalletHeader'
import AIServiceCard from '../components/AIServiceCard'
import { generateWithFlux, FluxGenerationResult, FLUX_MODELS } from '../services/fluxService'
import { processMTMPayment } from '../services/paymentService'
import { connectWallet, WalletState } from '../services/walletService'
import { connectWallet, disconnectWallet, WalletState } from '../services/walletService'
import { WalletType } from '../services/types'
const Page: React.FC = (): React.ReactElement => {
@ -29,6 +29,17 @@ const Page: React.FC = (): React.ReactElement => {
}
}
const handleDisconnect = async (): Promise<void> => {
if (walletState.type) {
await disconnectWallet(walletState.type)
setWalletState({
connected: false,
publicKey: null,
type: null
})
}
}
const handleFluxGeneration = (modelId: string, cost: number) => {
return async (prompt: string): Promise<FluxGenerationResult> => {
const type = walletState.type;
@ -69,6 +80,7 @@ const Page: React.FC = (): React.ReactElement => {
<WalletHeader
walletState={walletState}
onConnect={handleConnect}
onDisconnect={handleDisconnect}
/>
</div>

View File

@ -6,9 +6,10 @@ import { WalletState, SUPPORTED_WALLETS } from '../services/walletService'
interface WalletHeaderProps {
walletState: WalletState
onConnect: (walletType: string) => Promise<void>
onDisconnect: () => Promise<void>
}
const WalletHeader: React.FC<WalletHeaderProps> = ({ walletState, onConnect }) => {
const WalletHeader: React.FC<WalletHeaderProps> = ({ walletState, onConnect, onDisconnect }) => {
return (
<div className="w-full bg-slate-900/50 backdrop-blur-lg rounded-xl shadow-lg border border-orange-800/50 mb-8 p-4">
{!walletState.connected ? (
@ -31,6 +32,12 @@ const WalletHeader: React.FC<WalletHeaderProps> = ({ walletState, onConnect }) =
<span className="px-3 py-1 bg-amber-500/20 rounded-full text-amber-200 text-sm">
{walletState.publicKey?.slice(0, 22)}...
</span>
<button
onClick={onDisconnect}
className="bg-red-500 hover:bg-red-600 text-white font-semibold py-2 px-4 rounded-lg transition-all duration-200 shadow-lg hover:shadow-red-500/25 ml-4"
>
Disconnect
</button>
</div>
)}
</div>

View File

@ -10,6 +10,7 @@ export interface WalletConfig {
type: WalletType
name: string
connect: () => Promise<{ publicKey: string } | null>
disconnect: () => Promise<void>
}
const connectSolflare = async (): Promise<{ publicKey: string } | null> => {
@ -18,6 +19,12 @@ const connectSolflare = async (): Promise<{ publicKey: string } | null> => {
return window.solflare.publicKey ? { publicKey: window.solflare.publicKey.toString() } : null
}
const disconnectSolflare = async (): Promise<void> => {
if (window.solflare) {
await window.solflare.disconnect()
}
}
const connectPhantom = async (): Promise<{ publicKey: string } | null> => {
if (!window.phantom?.solana) return null
try {
@ -28,16 +35,24 @@ const connectPhantom = async (): Promise<{ publicKey: string } | null> => {
}
}
const disconnectPhantom = async (): Promise<void> => {
if (window.phantom?.solana) {
await window.phantom.solana.disconnect()
}
}
export const SUPPORTED_WALLETS: WalletConfig[] = [
{
type: 'solflare',
name: 'Solflare',
connect: connectSolflare
connect: connectSolflare,
disconnect: disconnectSolflare
},
{
type: 'phantom',
name: 'Phantom',
connect: connectPhantom
connect: connectPhantom,
disconnect: disconnectPhantom
}
]
@ -62,3 +77,10 @@ export async function connectWallet(type: WalletType): Promise<WalletState> {
}
}
}
export async function disconnectWallet(type: WalletType): Promise<void> {
const wallet = SUPPORTED_WALLETS.find(w => w.type === type)
if (wallet) {
await wallet.disconnect()
}
}