@@ -50,7 +50,6 @@ export function App() {
5050
5151 const lastPendingIdRef = useRef < string | null > ( null ) ;
5252 const prevSelectedUuidRef = useRef < string | null > ( null ) ;
53- const pollFnRef = useRef < ( ) => void > ( ( ) => { } ) ;
5453
5554 const walletClient = useMemo ( ( ) => {
5655 if ( ! selected ) return undefined ;
@@ -88,34 +87,6 @@ export function App() {
8887 } catch { }
8988 } , [ account , chainId ] ) ;
9089
91- const pollTick = useCallback ( async ( ) => {
92- if ( ! confirmed ) {
93- await ensureServerConnected ( ) ;
94- }
95-
96- try {
97- const resp = await api < ApiOk < PendingAny > | ApiErr > ( "/api/transaction/request" ) ;
98-
99- if ( ! isOk ( resp ) ) {
100- if ( pending ) {
101- setPending ( null ) ;
102- lastPendingIdRef . current = null ;
103- }
104- } else {
105- const tx = ( resp as ApiOk < PendingAny > ) . data ;
106-
107- if ( ! lastPendingIdRef . current || lastPendingIdRef . current !== tx . id ) {
108- setPending ( tx ) ;
109- lastPendingIdRef . current = tx . id ;
110- setLastTxHash ( null ) ;
111- setLastTxReceipt ( null ) ;
112- } else if ( ! pending ) {
113- setPending ( tx ) ;
114- }
115- }
116- } catch { }
117- } , [ ensureServerConnected , pending ] ) ;
118-
11990 const connect = async ( ) => {
12091 if ( ! selected || confirmed ) return ;
12192
@@ -134,7 +105,15 @@ export function App() {
134105 } ;
135106
136107 const confirm = async ( ) => {
137- await ensureServerConnected ( ) ;
108+ if ( ! account || chainId == null ) {
109+ return ;
110+ }
111+
112+ try {
113+ await api ( "/api/connection" , "POST" , [ account , chainId ] ) ;
114+ } catch {
115+ return ;
116+ }
138117
139118 setConfirmed ( true ) ;
140119 } ;
@@ -153,7 +132,8 @@ export function App() {
153132 setLastTxReceipt ( receipt ) ;
154133
155134 await api ( "/api/transaction/response" , "POST" , { id : pending . id , hash, error : null } ) ;
156- await pollTick ( ) ;
135+
136+ setPending ( null ) ;
157137 } catch ( e : unknown ) {
158138 const msg =
159139 typeof e === "object" &&
@@ -173,7 +153,7 @@ export function App() {
173153 } ) ;
174154 } catch { }
175155
176- await pollTick ( ) ;
156+ setPending ( null ) ;
177157 }
178158 } ;
179159
@@ -255,25 +235,21 @@ export function App() {
255235 } , [ selected , confirmed ] ) ;
256236
257237 useEffect ( ( ) => {
258- pollFnRef . current = ( ) => {
259- void pollTick ( ) ;
260- } ;
261- } , [ pollTick ] ) ;
238+ if ( ! confirmed || pending ) return ;
262239
263- // Polling loop to check for new pending transactions.
264- useEffect ( ( ) => {
265- if ( ! confirmed ) return ;
266-
267- pollFnRef . current ( ) ;
268-
269- const id = window . setInterval ( ( ) => {
270- pollFnRef . current ( ) ;
240+ const id = window . setInterval ( async ( ) => {
241+ try {
242+ const resp = await api < ApiOk < PendingAny > | ApiErr > ( "/api/transaction/request" ) ;
243+ if ( isOk ( resp ) ) {
244+ setPending ( resp . data ) ;
245+ }
246+ } catch { }
271247 } , 1000 ) ;
272248
273249 return ( ) => {
274250 window . clearInterval ( id ) ;
275251 } ;
276- } , [ confirmed ] ) ;
252+ } , [ confirmed , pending ] ) ;
277253
278254 return (
279255 < div className = "wrapper" >
@@ -310,7 +286,12 @@ export function App() {
310286 ) }
311287
312288 { selected && account && ! confirmed && (
313- < button type = "button" className = "wallet-confirm" onClick = { confirm } >
289+ < button
290+ type = "button"
291+ className = "wallet-confirm"
292+ onClick = { confirm }
293+ disabled = { ! account || chainId == null }
294+ >
314295 Confirm Connection
315296 </ button >
316297 ) }
0 commit comments