Skip to content

Commit

Permalink
feat: use-explorer-link got separated into bitcoin and stacks functio…
Browse files Browse the repository at this point in the history
…n variant
  • Loading branch information
Polybius93 committed Nov 30, 2023
1 parent 4b158d2 commit 89bd3c5
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 77 deletions.
25 changes: 25 additions & 0 deletions src/app/common/hooks/use-bitcoin-explorer-link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useCallback } from 'react';

import { makeBitcoinTxExplorerLink } from '@app/common/utils';
import { useCurrentNetworkState } from '@app/store/networks/networks.hooks';

import { openInNewTab } from '../utils/open-in-new-tab';

interface HandleOpenBitcoinTxLinkArgs {
txid: string;
}

export function useBitcoinExplorerLink() {
const { chain } = useCurrentNetworkState();
const { bitcoin } = chain;
const handleOpenBitcoinTxLink = useCallback(
({ txid }: HandleOpenBitcoinTxLinkArgs) => {
openInNewTab(makeBitcoinTxExplorerLink({ txid, bitcoin }));
},
[bitcoin]
);

return {
handleOpenBitcoinTxLink,
};
}
28 changes: 0 additions & 28 deletions src/app/common/hooks/use-explorer-link.ts

This file was deleted.

25 changes: 25 additions & 0 deletions src/app/common/hooks/use-stacks-explorer-link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useCallback } from 'react';

import { makeStacksTxExplorerLink } from '@app/common/utils';
import { useCurrentNetworkState } from '@app/store/networks/networks.hooks';

import { openInNewTab } from '../utils/open-in-new-tab';

export interface HandleOpenStacksTxLinkArgs {
suffix?: string;
txid: string;
}
export function useStacksExplorerLink() {
const { mode } = useCurrentNetworkState();

const handleOpenStacksTxLink = useCallback(
({ suffix, txid }: HandleOpenStacksTxLinkArgs) => {
openInNewTab(makeStacksTxExplorerLink({ mode, suffix, txid }));
},
[mode]
);

return {
handleOpenStacksTxLink,
};
}
38 changes: 23 additions & 15 deletions src/app/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import { toUnicode } from 'punycode';

import { BitcoinChainConfig, BitcoinNetworkModes, KEBAB_REGEX } from '@shared/constants';
import type { Blockchains } from '@shared/models/blockchain.model';

export function createNullArrayOfLength(length: number) {
return new Array(length).fill(null);
Expand Down Expand Up @@ -39,28 +38,37 @@ export function extractPhraseFromString(value: string) {
}
}

interface MakeTxExplorerLinkArgs {
blockchain: Blockchains;
interface MakeBitcoinTxExplorerLinkArgs {
txid: string;
bitcoin: BitcoinChainConfig;
}

interface MakeStacksTxExplorerLinkArgs {
mode: BitcoinNetworkModes;
suffix?: string;
txid: string;
bitcoin: BitcoinChainConfig;
}
export function makeTxExplorerLink({
blockchain,

export function makeStacksTxExplorerLink({
mode,
suffix = '',
txid,
}: MakeStacksTxExplorerLinkArgs) {
return `https://explorer.hiro.so/txid/${txid}?chain=${mode}${suffix}`;
}

export function makeBitcoinTxExplorerLink({
txid,
bitcoin: { bitcoinUrl, bitcoinNetwork },
}: MakeTxExplorerLinkArgs) {
switch (blockchain) {
case 'bitcoin':
if (bitcoinNetwork === 'regtest') {
return `${bitcoinUrl}/tx/${txid}`;
}
return `https://mempool.space/${mode !== 'mainnet' ? mode + '/' : ''}tx/${txid}`;
case 'stacks':
return `https://explorer.hiro.so/txid/${txid}?chain=${mode}${suffix}`;
}: MakeBitcoinTxExplorerLinkArgs) {
switch (bitcoinNetwork) {
case 'mainnet':
case 'testnet':
return `https://mempool.space/${
bitcoinNetwork !== 'mainnet' ? bitcoinNetwork + '/' : ''
}tx/${txid}`;
case 'regtest':
return `${bitcoinUrl}/tx/${txid}`;
default:
return '';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { BitcoinTx } from '@shared/models/transactions/bitcoin-transaction.model
import { RouteUrls } from '@shared/route-urls';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { useExplorerLink } from '@app/common/hooks/use-explorer-link';
import { useBitcoinExplorerLink } from '@app/common/hooks/use-bitcoin-explorer-link';
import {
containsTaprootInput,
getBitcoinTxCaption,
Expand Down Expand Up @@ -50,7 +50,7 @@ export function BitcoinTransactionItem({ transaction, ...rest }: BitcoinTransact
});

const bitcoinAddress = useCurrentAccountNativeSegwitAddressIndexZero();

Check warning on line 52 in src/app/components/bitcoin-transaction-item/bitcoin-transaction-item.tsx

View workflow job for this annotation

GitHub Actions / lint-eslint

'useCurrentAccountNativeSegwitAddressIndexZero' is deprecated. Use signer.address instead

Check warning on line 52 in src/app/components/bitcoin-transaction-item/bitcoin-transaction-item.tsx

View workflow job for this annotation

GitHub Actions / lint-eslint

'useCurrentAccountNativeSegwitAddressIndexZero' is deprecated. Use signer.address instead
const { handleOpenTxLink } = useExplorerLink();
const { handleOpenBitcoinTxLink: handleOpenTxLink } = useBitcoinExplorerLink();
const analytics = useAnalytics();
const caption = useMemo(() => getBitcoinTxCaption(transaction), [transaction]);
const value = useMemo(
Expand All @@ -70,7 +70,7 @@ export function BitcoinTransactionItem({ transaction, ...rest }: BitcoinTransact
openInNewTab(createInscriptionInfoUrl(inscriptionData.id));
return;
}
handleOpenTxLink({ blockchain: 'bitcoin', txid: transaction?.txid || '' });
handleOpenTxLink({ txid: transaction?.txid || '' });
};

const isOriginator = !isBitcoinTxInbound(bitcoinAddress, transaction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { StacksTx, TxTransferDetails } from '@shared/models/transactions/stacks-
import { RouteUrls } from '@shared/route-urls';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { useExplorerLink } from '@app/common/hooks/use-explorer-link';
import { useStacksExplorerLink } from '@app/common/hooks/use-stacks-explorer-link';
import {
getTxCaption,
getTxTitle,
Expand Down Expand Up @@ -36,7 +36,7 @@ export function StacksTransactionItem({
...rest
}: StacksTransactionItemProps) {
const [component, bind, { isHovered }] = usePressable(true);
const { handleOpenTxLink } = useExplorerLink();
const { handleOpenStacksTxLink: handleOpenTxLink } = useStacksExplorerLink();
const currentAccount = useCurrentStacksAccount();
const analytics = useAnalytics();
const [_, setRawTxId] = useRawTxIdState();
Expand All @@ -49,7 +49,6 @@ export function StacksTransactionItem({
const openTxLink = () => {
void analytics.track('view_transaction');
handleOpenTxLink({
blockchain: 'stacks',
txid: transaction?.tx_id || transferDetails?.link || '',
});
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StacksTransaction } from '@stacks/transactions';
import { Box, HStack, Stack } from 'leather-styles/jsx';

import { useExplorerLink } from '@app/common/hooks/use-explorer-link';
import { useStacksExplorerLink } from '@app/common/hooks/use-stacks-explorer-link';
import { getTxSenderAddress } from '@app/common/transactions/stacks/transaction.utils';
import { usePressable } from '@app/components/item-hover';
import { Tooltip } from '@app/components/tooltip';
Expand All @@ -18,7 +18,7 @@ interface SubmittedTransactionItemProps {
}
export function SubmittedTransactionItem({ transaction, txId }: SubmittedTransactionItemProps) {
const [component, bind] = usePressable(true);
const { handleOpenTxLink } = useExplorerLink();
const { handleOpenStacksTxLink: handleOpenTxLink } = useStacksExplorerLink();

if (!transaction) return null;

Expand All @@ -38,7 +38,6 @@ export function SubmittedTransactionItem({ transaction, txId }: SubmittedTransac
alignItems="center"
onClick={() =>
handleOpenTxLink({
blockchain: 'stacks',
suffix: `&submitted=true`,
txid: txId,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box, Flex, HStack, styled } from 'leather-styles/jsx';

import { useBitcoinExplorerLink } from '@app/common/hooks/use-bitcoin-explorer-link';
import { useClipboard } from '@app/common/hooks/use-copy-to-clipboard';
import { useExplorerLink } from '@app/common/hooks/use-explorer-link';
import { Flag } from '@app/components/layout/flag';
import { Tooltip } from '@app/components/tooltip';
import { LeatherButton } from '@app/ui/components/button';
Expand All @@ -24,7 +24,7 @@ export function PsbtInputOutputItemLayout({
txIdHoverLabel,
}: PsbtInputOutputItemLayoutProps) {
const { onCopy, hasCopied } = useClipboard(addressHoverLabel ?? '');
const { handleOpenTxLink } = useExplorerLink();
const { handleOpenBitcoinTxLink: handleOpenTxLink } = useBitcoinExplorerLink();

return (
<Flag align="middle" img={<></>} mt="space.05" spacing="space.04">
Expand Down Expand Up @@ -56,7 +56,6 @@ export function PsbtInputOutputItemLayout({
<LeatherButton
onClick={() =>
handleOpenTxLink({
blockchain: 'bitcoin',
txid: txIdHoverLabel ?? '',
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Suspense } from 'react';

import { Stack } from 'leather-styles/jsx';

import { useExplorerLink } from '@app/common/hooks/use-explorer-link';
import { useStacksExplorerLink } from '@app/common/hooks/use-stacks-explorer-link';
import { formatContractId } from '@app/common/utils';
import { AttachmentRow } from '@app/features/stacks-transaction-request/attachment-row';
import { ContractPreviewLayout } from '@app/features/stacks-transaction-request/contract-preview';
Expand All @@ -13,7 +13,7 @@ import { FunctionArgumentsList } from './function-arguments-list';

function ContractCallDetailsSuspense() {
const transactionRequest = useTransactionRequestState();
const { handleOpenTxLink } = useExplorerLink();
const { handleOpenStacksTxLink: handleOpenTxLink } = useStacksExplorerLink();

if (!transactionRequest || transactionRequest.txType !== 'contract_call') return null;
const { contractAddress, contractName, functionName, attachment } = transactionRequest;
Expand All @@ -34,7 +34,6 @@ function ContractCallDetailsSuspense() {
<ContractPreviewLayout
onClick={() =>
handleOpenTxLink({
blockchain: 'stacks',
txid: formatContractId(contractAddress, contractName),
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Flex, HStack, styled } from 'leather-styles/jsx';

import { createMoneyFromDecimal } from '@shared/models/money.model';

import { useExplorerLink } from '@app/common/hooks/use-explorer-link';
import { useBitcoinExplorerLink } from '@app/common/hooks/use-bitcoin-explorer-link';
import { baseCurrencyAmountInQuote } from '@app/common/money/calculate-money';
import { i18nFormatCurrency } from '@app/common/money/format-money';
import { satToBtc } from '@app/common/money/unit-conversion';
Expand All @@ -25,7 +25,7 @@ export function BitcoinContractListItemLayout({
collateralAmount,
txid,
}: BitcoinContractListItemLayoutProps) {
const { handleOpenTxLink } = useExplorerLink();
const { handleOpenBitcoinTxLink: handleOpenTxLink } = useBitcoinExplorerLink();
const bitcoinMarketData = useCryptoCurrencyMarketData('BTC');

const getFiatValue = useCallback(
Expand All @@ -41,8 +41,6 @@ export function BitcoinContractListItemLayout({
marginBottom="15px"
onClick={() =>
handleOpenTxLink({
blockchain: 'bitcoin',
suffix: `&submitted=true`,
txid,
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/pages/rpc-send-transfer/rpc-send-transfer-summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useLocation } from 'react-router-dom';
import { HStack, Stack } from 'leather-styles/jsx';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { useBitcoinExplorerLink } from '@app/common/hooks/use-bitcoin-explorer-link';
import { useClipboard } from '@app/common/hooks/use-copy-to-clipboard';
import { useExplorerLink } from '@app/common/hooks/use-explorer-link';
import { FormAddressDisplayer } from '@app/components/address-displayer/form-address-displayer';
import {
InfoCard,
Expand All @@ -21,7 +21,7 @@ import { ExternalLinkIcon } from '@app/ui/components/icons/external-link-icon';

export function RpcSendTransferSummary() {
const { state } = useLocation();
const { handleOpenTxLink } = useExplorerLink();
const { handleOpenBitcoinTxLink: handleOpenTxLink } = useBitcoinExplorerLink();
const analytics = useAnalytics();

const {
Expand Down
4 changes: 2 additions & 2 deletions src/app/pages/rpc-sign-psbt/rpc-sign-psbt-summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useLocation } from 'react-router-dom';
import { Flex, HStack, Stack } from 'leather-styles/jsx';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { useBitcoinExplorerLink } from '@app/common/hooks/use-bitcoin-explorer-link';
import { useClipboard } from '@app/common/hooks/use-copy-to-clipboard';
import { useExplorerLink } from '@app/common/hooks/use-explorer-link';
import {
InfoCard,
InfoCardAssetValue,
Expand All @@ -19,7 +19,7 @@ import { ExternalLinkIcon } from '@app/ui/components/icons/external-link-icon';

export function RpcSignPsbtSummary() {
const { state } = useLocation();
const { handleOpenTxLink } = useExplorerLink();
const { handleOpenBitcoinTxLink: handleOpenTxLink } = useBitcoinExplorerLink();
const analytics = useAnalytics();

const { fee, sendingValue, totalSpend, txId, txFiatValue, txFiatValueSymbol, txLink, txValue } =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useLocation } from 'react-router-dom';
import { HStack, styled } from 'leather-styles/jsx';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { useBitcoinExplorerLink } from '@app/common/hooks/use-bitcoin-explorer-link';
import { useClipboard } from '@app/common/hooks/use-copy-to-clipboard';
import { useExplorerLink } from '@app/common/hooks/use-explorer-link';
import { useRouteHeader } from '@app/common/hooks/use-route-header';
import { satToBtc } from '@app/common/money/unit-conversion';
import {
Expand All @@ -25,7 +25,7 @@ export function LockBitcoinSummary() {
const { txId, txMoney, txFiatValue, txFiatValueSymbol, symbol, txLink } = state;

const { onCopy } = useClipboard(txId);
const { handleOpenTxLink } = useExplorerLink();
const { handleOpenBitcoinTxLink: handleOpenTxLink } = useBitcoinExplorerLink();
const analytics = useAnalytics();

function onClickLink() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { SupportedInscription } from '@shared/models/inscription.model';
import { RouteUrls } from '@shared/route-urls';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { useBitcoinExplorerLink } from '@app/common/hooks/use-bitcoin-explorer-link';
import { useClipboard } from '@app/common/hooks/use-copy-to-clipboard';
import { useExplorerLink } from '@app/common/hooks/use-explorer-link';
import { FormAddressDisplayer } from '@app/components/address-displayer/form-address-displayer';
import { BaseDrawer } from '@app/components/drawer/base-drawer';
import {
Expand Down Expand Up @@ -47,7 +47,7 @@ export function SendInscriptionSummary() {
};

const { onCopy } = useClipboard(txid || '');
const { handleOpenTxLink } = useExplorerLink();
const { handleOpenBitcoinTxLink: handleOpenTxLink } = useBitcoinExplorerLink();
const analytics = useAnalytics();

function onClickLink() {
Expand Down
4 changes: 2 additions & 2 deletions src/app/pages/send/sent-summary/brc20-sent-symmary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import get from 'lodash.get';

import { createMoney } from '@shared/models/money.model';

import { HandleOpenTxLinkArgs } from '@app/common/hooks/use-explorer-link';
import { useRouteHeader } from '@app/common/hooks/use-route-header';
import { HandleOpenStacksTxLinkArgs } from '@app/common/hooks/use-stacks-explorer-link';
import { formatMoney } from '@app/common/money/format-money';
import { openInNewTab } from '@app/common/utils/open-in-new-tab';
import {
Expand All @@ -33,7 +33,7 @@ function useBrc20SentSummaryState() {
tick: get(location.state, 'tick') as string,
amount: get(location.state, 'amount') as string,
txId: get(location.state, 'txId') as string,
txLink: get(location.state, 'txLink') as HandleOpenTxLinkArgs,
txLink: get(location.state, 'txLink') as HandleOpenStacksTxLinkArgs,
feeRowValue: get(location.state, 'feeRowValue') as string,
};
}
Expand Down
Loading

0 comments on commit 89bd3c5

Please sign in to comment.