Skip to content

Commit f020456

Browse files
Copilot0xrinegade
andcommitted
Fix sell button implementation with improved connection error handling and retry mechanism
Co-authored-by: 0xrinegade <[email protected]>
1 parent cb28949 commit f020456

File tree

1 file changed

+118
-15
lines changed

1 file changed

+118
-15
lines changed

src/components/OfferCreation.js

Lines changed: 118 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useContext } from 'react';
1+
import React, { useState, useContext, useEffect } from 'react';
22
import { SystemProgram, Keypair, LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js';
33
// Import BN from @coral-xyz/anchor as a fallback for @project-serum/anchor
44
import { BN } from '@coral-xyz/anchor';
@@ -39,6 +39,23 @@ const OfferCreation = ({ onStartGuidedWorkflow }) => {
3939
const [txHash, setTxHash] = useState('');
4040
const [txStatus, setTxStatus] = useState(null);
4141
const [showConfirmation, setShowConfirmation] = useState(false);
42+
const [connectionRetrying, setConnectionRetrying] = useState(false);
43+
const [connectionAttempts, setConnectionAttempts] = useState(0);
44+
45+
// Track connection status for better UX
46+
useEffect(() => {
47+
// Reset retry state when connection changes
48+
if (connection && program) {
49+
setConnectionRetrying(false);
50+
setConnectionAttempts(0);
51+
setError('');
52+
}
53+
}, [connection, program]);
54+
55+
// Determine connection status for better UX
56+
const isWalletConnected = wallet.connected && wallet.publicKey;
57+
const isSmartContractReady = isWalletConnected && program && connection;
58+
const hasConnectionIssues = isWalletConnected && (!connection || !program);
4259

4360
// Get real price data
4461
const { prices, loading: pricesLoading, error: pricesError, lastUpdated } = useRealPriceData();
@@ -61,13 +78,13 @@ const OfferCreation = ({ onStartGuidedWorkflow }) => {
6178
const handleCreateOffer = async (e) => {
6279
e.preventDefault();
6380

64-
if (!wallet.publicKey || !wallet.connected) {
81+
if (!isWalletConnected) {
6582
setError('Please connect your wallet first');
6683
return;
6784
}
6885

69-
if (!program) {
70-
setError('Program not initialized. Please ensure your wallet is connected and try again.');
86+
if (!isSmartContractReady) {
87+
setError('Smart contract connection is not ready. Please check your network connection and try again.');
7188
return;
7289
}
7390

@@ -85,6 +102,22 @@ const OfferCreation = ({ onStartGuidedWorkflow }) => {
85102
// Show confirmation dialog
86103
setShowConfirmation(true);
87104
};
105+
106+
const handleRetryConnection = async () => {
107+
if (connectionRetrying) return;
108+
109+
setConnectionRetrying(true);
110+
setConnectionAttempts(prev => prev + 1);
111+
setError('');
112+
113+
// Add a small delay to show the retry state
114+
setTimeout(() => {
115+
setConnectionRetrying(false);
116+
if (!program) {
117+
setError('Unable to establish smart contract connection. This may be due to network restrictions or RPC endpoint issues. Please try again or contact support.');
118+
}
119+
}, 2000);
120+
};
88121

89122
// Actual offer creation after confirmation
90123
async function processCreateOffer() {
@@ -398,25 +431,47 @@ const OfferCreation = ({ onStartGuidedWorkflow }) => {
398431

399432
{/* Submit button */}
400433
<div className="ascii-form-actions">
401-
{!wallet.connected ? (
434+
{!isWalletConnected ? (
402435
<ConnectWalletPrompt
403436
action="create sell offers"
404437
className="create-offer-button connect-wallet-button"
405438
/>
406-
) : !program ? (
407-
<button
408-
type="button"
409-
disabled={true}
410-
className="create-offer-button disabled"
411-
title="Initializing smart contract connection..."
412-
>
413-
Connecting to Smart Contract...
414-
</button>
439+
) : hasConnectionIssues ? (
440+
<div className="connection-issue-container">
441+
<button
442+
type="button"
443+
disabled={true}
444+
className="create-offer-button disabled connection-issue"
445+
title="Smart contract connection failed"
446+
>
447+
Smart Contract Connection Failed
448+
</button>
449+
<div className="connection-issue-details">
450+
<p>Unable to connect to the Solana blockchain. This may be due to:</p>
451+
<ul>
452+
<li>Network connectivity issues</li>
453+
<li>RPC endpoint problems</li>
454+
<li>Browser security restrictions</li>
455+
</ul>
456+
<ButtonLoader
457+
type="button"
458+
onClick={handleRetryConnection}
459+
isLoading={connectionRetrying}
460+
disabled={connectionRetrying}
461+
loadingText="Retrying..."
462+
variant="secondary"
463+
size="small"
464+
className="retry-connection-button"
465+
>
466+
Retry Connection {connectionAttempts > 0 && `(${connectionAttempts})`}
467+
</ButtonLoader>
468+
</div>
469+
</div>
415470
) : (
416471
<ButtonLoader
417472
type="submit"
418473
isLoading={isCreating}
419-
disabled={!wallet.connected || !wallet.publicKey || isActionDisabled || !solValidation.isValid || !fiatValidation.isValid || !program}
474+
disabled={!isSmartContractReady || isActionDisabled || !solValidation.isValid || !fiatValidation.isValid}
420475
loadingText="Creating Offer..."
421476
variant="primary"
422477
size="medium"
@@ -536,6 +591,54 @@ const OfferCreation = ({ onStartGuidedWorkflow }) => {
536591
.wallet-connection-prompt {
537592
margin: 1rem 0;
538593
}
594+
595+
.connection-issue-container {
596+
display: flex;
597+
flex-direction: column;
598+
gap: 1rem;
599+
align-items: center;
600+
}
601+
602+
.connection-issue {
603+
background-color: var(--ascii-neutral-600) !important;
604+
color: var(--ascii-red-400) !important;
605+
cursor: not-allowed !important;
606+
}
607+
608+
.connection-issue-details {
609+
background: var(--color-background-alt);
610+
border: 1px solid var(--ascii-yellow-500);
611+
border-radius: 4px;
612+
padding: 16px;
613+
max-width: 400px;
614+
text-align: left;
615+
font-size: 0.9rem;
616+
}
617+
618+
.connection-issue-details p {
619+
margin: 0 0 12px 0;
620+
color: var(--ascii-yellow-600);
621+
font-weight: 500;
622+
}
623+
624+
.connection-issue-details ul {
625+
margin: 0 0 16px 0;
626+
padding-left: 20px;
627+
color: var(--color-foreground-muted);
628+
}
629+
630+
.connection-issue-details li {
631+
margin-bottom: 4px;
632+
}
633+
634+
.retry-connection-button {
635+
width: 100%;
636+
background-color: var(--ascii-blue-600) !important;
637+
}
638+
639+
.retry-connection-button:hover {
640+
background-color: var(--ascii-blue-500) !important;
641+
}
539642
`}</style>
540643
</div>
541644
);

0 commit comments

Comments
 (0)