Skip to content

Commit

Permalink
refactor status and toast hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
alissacrane-cb committed Aug 14, 2024
1 parent 12adb20 commit 2819215
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/transaction/components/TransactionStatusAction.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { cn, text } from '../../styles/theme';
import { useGetTransactionAction } from '../hooks/useGetTransactionAction';
import { useGetTransactionStatusAction } from '../hooks/useGetTransactionStatusAction';
import type { TransactionStatusActionReact } from '../types';

export function TransactionStatusAction({
className,
}: TransactionStatusActionReact) {
const { actionElement } = useGetTransactionAction({ context: 'status' });
const { actionElement } = useGetTransactionStatusAction();

return (
<div className={cn(text.label2, 'min-w-[70px]', className)}>
Expand Down
4 changes: 2 additions & 2 deletions src/transaction/components/TransactionToastAction.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { cn, text } from '../../styles/theme';
import { useGetTransactionAction } from '../hooks/useGetTransactionAction';
import { useGetTransactionToastAction } from '../hooks/useGetTransactionToastAction';
import type { TransactionToastActionReact } from '../types';

export function TransactionToastAction({
className,
}: TransactionToastActionReact) {
const { actionElement } = useGetTransactionAction({ context: 'toast' });
const { actionElement } = useGetTransactionToastAction();

return (
<div className={cn(text.label1, 'text-nowrap', className)}>
Expand Down
70 changes: 70 additions & 0 deletions src/transaction/hooks/useGetTransactionStatusAction.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { useMemo } from 'react';
import type { ReactNode } from 'react';
import { useChainId } from 'wagmi';
import { useShowCallsStatus } from 'wagmi/experimental';
import { getChainExplorer } from '../../network/getChainExplorer';
import { cn, color, text } from '../../styles/theme';
import { useTransactionContext } from '../components/TransactionProvider';

export function useGetTransactionStatusAction() {
const {
chainId,
errorMessage,
onSubmit,
receipt,
transactionHash,
transactionId,
} = useTransactionContext();
const accountChainId = chainId ?? useChainId();

const { showCallsStatus } = useShowCallsStatus();

return useMemo(() => {
const chainExplorer = getChainExplorer(accountChainId);

let actionElement: ReactNode = null;

// EOA will have txn hash
if (transactionHash) {
actionElement = (
<a
href={`${chainExplorer}/tx/${transactionHash}`}
target="_blank"
rel="noreferrer"
>
<span className={cn(text.label1, color.primary)}>
View transaction
</span>
</a>
);
}

// SW will have txn id
if (transactionId) {
actionElement = (
<button
onClick={() => showCallsStatus({ id: transactionId })}
type="button"
>
<span className={cn(text.label1, color.primary)}>
View transaction
</span>
</button>
);
}

if (receipt) {
actionElement = null;
}

return { actionElement };
}, [
accountChainId,
errorMessage,
onSubmit,
receipt,
showCallsStatus,
transactionHash,
transactionId,
]);
}
74 changes: 74 additions & 0 deletions src/transaction/hooks/useGetTransactionToastAction.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { useMemo } from 'react';
import type { ReactNode } from 'react';
import { useChainId } from 'wagmi';
import { useShowCallsStatus } from 'wagmi/experimental';
import { getChainExplorer } from '../../network/getChainExplorer';
import { cn, color, text } from '../../styles/theme';
import { useTransactionContext } from '../components/TransactionProvider';

export function useGetTransactionToastAction() {
const {
chainId,
errorMessage,
onSubmit,
receipt,
transactionHash,
transactionId,
} = useTransactionContext();
const accountChainId = chainId ?? useChainId();

const { showCallsStatus } = useShowCallsStatus();

return useMemo(() => {
const chainExplorer = getChainExplorer(accountChainId);

let actionElement: ReactNode = null;

// EOA will have txn hash
if (transactionHash) {
actionElement = (
<a
href={`${chainExplorer}/tx/${transactionHash}`}
target="_blank"
rel="noreferrer"
>
<span className={cn(text.label1, color.primary)}>
View transaction
</span>
</a>
);
}

// SW will have txn id
if (transactionId) {
actionElement = (
<button
onClick={() => showCallsStatus({ id: transactionId })}
type="button"
>
<span className={cn(text.label1, color.primary)}>
View transaction
</span>
</button>
);
}

if (errorMessage) {
actionElement = (
<button type="button" onClick={onSubmit}>
<span className={cn(text.label1, color.primary)}>Try again</span>
</button>
);
}

return { actionElement };
}, [
accountChainId,
errorMessage,
onSubmit,
receipt,
showCallsStatus,
transactionHash,
transactionId,
]);
}

0 comments on commit 2819215

Please sign in to comment.