Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨Update CopyTxHash component #530

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions src/components/CopyTxHash/CopyTxHash.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,62 @@ const getExplorerUrl = ({ chain, txHash }: { chain: WalletChain; txHash: string
}
};

enum ExplorerType {
STELLAR_EXPERT = 'STELLAR_EXPERT',
STELLAR_CHAIN = 'STELLAR_CHAIN',
}

interface ExplorerLinks {
stellarExpert: string;
stellarChain: string;
}

const CopyTxHash = ({ txHash }: { txHash: string }) => {
const { notify } = useNotification();

const sorobanContext = useSorobanReact();

const [explorerLink, setExplorerLink] = useState<string | undefined>(undefined);
const activeChain = sorobanContext?.activeChain;

useEffect(() => {
if (!sorobanContext) return;
const [explorersLinks, setExplorersLinks] = useState<ExplorerLinks | undefined>(undefined);

const activeChain = sorobanContext.activeChain;

if (!activeChain) return;
useEffect(() => {
if (!sorobanContext || !activeChain) return;

if (activeChain.name === testnet.name || activeChain.name === mainnet.name) {
setExplorerLink(getExplorerUrl({ chain: activeChain, txHash }));
if (activeChain.name === testnet.name) {
setExplorersLinks({
stellarExpert: `https://stellar.expert/explorer/testnet/tx/${txHash}`,
stellarChain: `https://testnet.stellarchain.io/transactions/${txHash}`,
});
}
}, [sorobanContext, txHash]);

const handleClickViewOnExplorer = () => {
if (!explorerLink) return;
if (activeChain.name === mainnet.name) {
setExplorersLinks({
stellarExpert: `https://stellar.expert/explorer/public/tx/${txHash}`,
stellarChain: `https://stellarchain.io/transactions/${txHash}`,
});
}
}, [sorobanContext, txHash]);

window.open(explorerLink, '_blank');
};
return (
<Box display="flex" alignItems="center" flexDirection="column">
<Box display="flex" alignItems="center" flexDirection="column" sx={{ mt: 1 }}>
{(explorersLinks?.stellarChain && explorersLinks.stellarExpert) && (
<>
<LabelSmall style={{ cursor: 'pointer' }}>
<a href={explorersLinks?.stellarExpert}
target='_blank'>
View on Stellar.Expert
</a>
</LabelSmall>
<LabelSmall style={{ cursor: 'pointer' }}>
<a href={explorersLinks?.stellarChain}
target='_blank'>
View on StellarChain.io
</a>
</LabelSmall>
</>
)}
<CopyToClipboard
text={txHash}
onCopy={() =>
Expand All @@ -61,11 +91,6 @@ const CopyTxHash = ({ txHash }: { txHash: string }) => {
<Clipboard color="white" size="16px" />
</Row>
</CopyToClipboard>
{explorerLink && (
<LabelSmall style={{ cursor: 'pointer' }} onClick={handleClickViewOnExplorer}>
View on explorer
</LabelSmall>
)}
</Box>
);
};
Expand Down