|
1 | | -import { useState, useEffect } from 'react'; |
2 | | -import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'; |
| 1 | +import { useEffect } from 'react'; |
| 2 | +import { BrowserRouter, Routes, Route, useNavigate } from 'react-router-dom'; |
3 | 3 | import './App.css'; |
4 | | -import { Balance } from './components/wallet/Balance'; |
5 | | -import { Send } from './components/wallet/Send'; |
6 | | -import { History } from './components/wallet/History'; |
7 | | -import { SessionKeys } from './components/wallet/SessionKeys'; |
8 | | -import { WalletLayout } from './components/layout/WalletLayout'; |
9 | | -import { Wallet, Transaction } from './types/wallet'; |
10 | | -import { indexerService } from './services/IndexerService'; |
11 | | -import { useConfig } from './hooks/useConfig'; |
12 | | -import { AppEvent, webSocketService } from './services/WebSocketService'; |
13 | | -import { ConnectWallet } from './components/connect/ConnectWallet'; |
14 | | -import { ConnectWalletExamples } from './components/connect/ConnectWalletExamples'; |
15 | | - |
16 | | -function App() { |
17 | | - const [wallet, setWallet] = useState<Wallet | null>(null); |
18 | | - const [balance, setBalance] = useState<number>(0); |
19 | | - const [transactions, setTransactions] = useState<Transaction[]>([]); |
| 4 | +import { WalletShowcase } from './components/WalletShowcase'; |
| 5 | +import { useWalletBalance } from './hooks/useWalletBalance'; |
| 6 | +import { useWalletTransactions } from './hooks/useWalletTransactions'; |
| 7 | +import { useWebSocketConnection } from './hooks/useWebSocketConnection'; |
| 8 | +import { getPublicRoutes, getProtectedRoutes, ROUTES } from './routes/routes'; |
| 9 | +import { WalletProvider, useWallet } from 'hyle-wallet'; |
| 10 | +import { useConfig } from 'hyle-wallet'; |
| 11 | +import { LoadingErrorState } from './components/common/LoadingErrorState'; |
| 12 | + |
| 13 | +function AppContent() { |
20 | 14 | const { isLoading: isLoadingConfig, error: configError } = useConfig(); |
21 | | - |
22 | | - // Function to fetch balance |
23 | | - const fetchBalance = async () => { |
24 | | - if (wallet) { |
25 | | - const balance = await indexerService.getBalance(wallet.address); |
26 | | - setBalance(balance); |
27 | | - } |
28 | | - }; |
29 | | - |
30 | | - // Function to fetch transaction history |
31 | | - const fetchTransactions = async () => { |
32 | | - if (wallet) { |
33 | | - const transactions = await indexerService.getTransactionHistory(wallet.address); |
34 | | - setTransactions(transactions); |
| 15 | + const { wallet, logout, stage, error } = useWallet(); |
| 16 | + const navigate = useNavigate(); |
| 17 | + |
| 18 | + // Use custom hooks |
| 19 | + const { balance, fetchBalance } = useWalletBalance(wallet?.address); |
| 20 | + const { |
| 21 | + transactions, |
| 22 | + handleTxEvent |
| 23 | + } = useWalletTransactions(wallet?.address); |
| 24 | + |
| 25 | + // Setup WebSocket connection |
| 26 | + useWebSocketConnection(wallet?.address, event => { |
| 27 | + handleTxEvent(event); |
| 28 | + // If transaction was successful, update balance |
| 29 | + if (event.tx.status === 'Success') { |
| 30 | + fetchBalance(); |
35 | 31 | } |
36 | | - }; |
| 32 | + }); |
37 | 33 |
|
38 | | - // Initialize WebSocket connection when wallet is set |
| 34 | + // Redirect back to root on auth settlement error and show message via state |
39 | 35 | useEffect(() => { |
40 | | - if (wallet) { |
41 | | - webSocketService.connect(wallet.address); |
42 | | - |
43 | | - const handleTxEvent = async (event: AppEvent['TxEvent']) => { |
44 | | - console.log('Received transaction event:', event); |
45 | | - if (event.tx.status === 'Success') { |
46 | | - // Update balance |
47 | | - await fetchBalance(); |
48 | | - } |
49 | | - |
50 | | - // Update transactions |
51 | | - const newTransaction: Transaction = event.tx; |
52 | | - |
53 | | - setTransactions(prevTransactions => { |
54 | | - const existingIndex = prevTransactions.findIndex(tx => tx.id === newTransaction.id); |
55 | | - if (existingIndex !== -1) { |
56 | | - console.log('Updating existing transaction'); |
57 | | - // Update existing transaction in-place |
58 | | - const updatedTransactions = [...prevTransactions]; |
59 | | - updatedTransactions[existingIndex] = newTransaction; |
60 | | - return updatedTransactions; |
61 | | - } else { |
62 | | - console.log('Adding new transaction'); |
63 | | - // Add new transaction at the beginning of the list |
64 | | - return [newTransaction, ...prevTransactions]; |
65 | | - } |
66 | | - }); |
67 | | - }; |
68 | | - |
69 | | - const unsubscribeTxEvents = webSocketService.subscribeToTxEvents(handleTxEvent); |
70 | | - |
71 | | - // Initial data fetch |
72 | | - fetchBalance(); |
73 | | - fetchTransactions(); |
74 | | - |
75 | | - return () => { |
76 | | - unsubscribeTxEvents(); |
77 | | - webSocketService.disconnect(); |
78 | | - }; |
| 36 | + if (stage === 'error') { |
| 37 | + navigate(ROUTES.ROOT, { state: { authError: error } }); |
79 | 38 | } |
80 | | - }, [wallet]); |
81 | | - |
82 | | - const handleWalletLoggedIn = (loggedInWallet: Wallet) => { |
83 | | - setWallet(loggedInWallet); |
84 | | - localStorage.setItem('wallet', JSON.stringify(loggedInWallet)); |
85 | | - }; |
| 39 | + }, [stage, error, navigate]); |
86 | 40 |
|
87 | 41 | const handleLogout = () => { |
88 | | - setWallet(null); |
89 | | - localStorage.removeItem('wallet'); |
| 42 | + logout(); |
| 43 | + navigate(ROUTES.ROOT); |
90 | 44 | }; |
91 | 45 |
|
92 | | - // Check if wallet exists in localStorage on component mount |
93 | | - useEffect(() => { |
94 | | - const storedWallet = localStorage.getItem('wallet'); |
95 | | - if (storedWallet) { |
96 | | - setWallet(JSON.parse(storedWallet)); |
97 | | - } |
98 | | - }, []); |
99 | | - |
100 | 46 | if (isLoadingConfig) { |
101 | | - return <div>Loading configuration...</div>; |
| 47 | + return <LoadingErrorState isLoading={true} error={null} loadingMessage="Loading configuration..." />; |
102 | 48 | } |
103 | 49 |
|
104 | 50 | if (configError) { |
105 | | - return <div>Error loading configuration: {configError}</div>; |
| 51 | + return <LoadingErrorState isLoading={false} error={`Error loading configuration: ${configError}`} />; |
106 | 52 | } |
107 | 53 |
|
108 | | - return ( |
109 | | - <BrowserRouter> |
110 | | - {/* Global connect wallet modal (renders its own button) */} |
111 | | - {!wallet && ( |
112 | | - <div className="showcase-container"> |
113 | | - <div className="showcase-header"> |
114 | | - <h1>Wallet Integration</h1> |
115 | | - <p>Connect to your wallet using a fully customizable modal component.</p> |
116 | | - </div> |
117 | | - <ConnectWalletExamples onWalletConnected={handleWalletLoggedIn} /> |
118 | | - </div> |
119 | | - )} |
120 | | - <Routes> |
121 | | - <Route path="/" element={ |
122 | | - wallet ? <Navigate to="/wallet/balance" replace /> : <div /> |
123 | | - } /> |
| 54 | + // If wallet is not connected, show the showcase screen |
| 55 | + if (!wallet) { |
| 56 | + return <WalletShowcase providers={['password', 'google', 'github']} />; |
| 57 | + } |
124 | 58 |
|
125 | | - {wallet && ( |
126 | | - <Route path="/wallet" element={<WalletLayout wallet={wallet} onLogout={handleLogout} />}> |
127 | | - <Route path="balance" element={<Balance wallet={wallet} balance={balance} />} /> |
128 | | - <Route path="send" element={<Send wallet={wallet} />} /> |
129 | | - <Route path="history" element={<History transactions={transactions} />} /> |
130 | | - <Route path="session-keys" element={<SessionKeys wallet={wallet} />} /> |
131 | | - <Route index element={<Navigate to="balance" replace />} /> |
132 | | - </Route> |
133 | | - )} |
| 59 | + // Generate routes based on authentication state |
| 60 | + const publicRoutes = getPublicRoutes(); |
| 61 | + const protectedRoutes = getProtectedRoutes(wallet, balance, transactions, handleLogout); |
| 62 | + const allRoutes = [...publicRoutes, ...protectedRoutes]; |
| 63 | + |
| 64 | + return <Routes>{allRoutes.map(route => |
| 65 | + <Route |
| 66 | + key={route.path} |
| 67 | + path={route.path} |
| 68 | + element={route.element} |
| 69 | + > |
| 70 | + {route.children?.map(childRoute => ( |
| 71 | + <Route |
| 72 | + key={childRoute.path} |
| 73 | + path={childRoute.path} |
| 74 | + element={childRoute.element} |
| 75 | + index={childRoute.index} |
| 76 | + /> |
| 77 | + ))} |
| 78 | + </Route> |
| 79 | + )}</Routes>; |
| 80 | +} |
134 | 81 |
|
135 | | - <Route path="*" element={<Navigate to="/" replace />} /> |
136 | | - </Routes> |
| 82 | +export default function App() { |
| 83 | + return ( |
| 84 | + <BrowserRouter> |
| 85 | + <WalletProvider> |
| 86 | + <AppContent /> |
| 87 | + </WalletProvider> |
137 | 88 | </BrowserRouter> |
138 | 89 | ); |
139 | 90 | } |
140 | | - |
141 | | -export default App; |
0 commit comments