diff --git a/apps/scoutgame/app/(general)/notifications/loading.tsx b/apps/scoutgame/app/(general)/notifications/loading.tsx deleted file mode 100644 index 91800d1023..0000000000 --- a/apps/scoutgame/app/(general)/notifications/loading.tsx +++ /dev/null @@ -1,5 +0,0 @@ -import { LoadingPage } from '@connect-shared/components/layout/LoadingPage'; - -export default function Loading() { - return ; -} diff --git a/apps/scoutgame/components/common/NFTPurchaseDialog/components/NFTPurchaseForm.tsx b/apps/scoutgame/components/common/NFTPurchaseDialog/components/NFTPurchaseForm.tsx index 23b2fcaf5a..f253caf1ce 100644 --- a/apps/scoutgame/components/common/NFTPurchaseDialog/components/NFTPurchaseForm.tsx +++ b/apps/scoutgame/components/common/NFTPurchaseDialog/components/NFTPurchaseForm.tsx @@ -37,8 +37,8 @@ import { useAccount, useSendTransaction, useSwitchChain } from 'wagmi'; import { IconButton } from 'components/common/Button/IconButton'; import { PointsIcon } from 'components/common/Icons'; -import { usePurchase } from 'components/layout/PurchaseProvider'; import { useUser } from 'components/layout/UserProvider'; +import { checkDecentTransactionAction } from 'lib/builderNFTs/checkDecentTransactionAction'; import { purchaseWithPointsAction } from 'lib/builderNFTs/purchaseWithPointsAction'; import { saveDecentTransactionAction } from 'lib/builderNFTs/saveDecentTransactionAction'; import type { MinimalUserInfo } from 'lib/users/interfaces'; @@ -79,7 +79,6 @@ export function NFTPurchaseFormContent({ builder }: NFTPurchaseProps) { const initialQuantities = [1, 11, 111]; const pricePerNft = builder.price ? convertCostToPoints(builder.price).toLocaleString() : ''; const { address, chainId } = useAccount(); - const { checkDecentTransaction, isExecutingTransaction } = usePurchase(); const { switchChainAsync } = useSwitchChain(); @@ -126,6 +125,20 @@ export function NFTPurchaseFormContent({ builder }: NFTPurchaseProps) { } }); + const { + isExecuting: isExecutingTransaction, + hasSucceeded: transactionHasSucceeded, + executeAsync: checkDecentTransaction + } = useAction(checkDecentTransactionAction, { + onError({ error, input }) { + log.error('Error checking Decent transaction', { error, input }); + setSubmitError(error.serverError?.message || 'Something went wrong'); + }, + onExecute() { + setSubmitError(null); + } + }); + const { isExecuting: isSavingDecentTransaction, hasSucceeded: savedDecentTransaction, @@ -312,7 +325,7 @@ export function NFTPurchaseFormContent({ builder }: NFTPurchaseProps) { typeof allowance === 'bigint' && allowance < (typeof amountToPay === 'bigint' ? amountToPay : BigInt(0)); - if (hasPurchasedWithPoints || savedDecentTransaction) { + if (hasPurchasedWithPoints || (savedDecentTransaction && transactionHasSucceeded)) { return ; } diff --git a/apps/scoutgame/components/layout/AppProviders.tsx b/apps/scoutgame/components/layout/AppProviders.tsx index 5639499a95..be2dfa7b41 100644 --- a/apps/scoutgame/components/layout/AppProviders.tsx +++ b/apps/scoutgame/components/layout/AppProviders.tsx @@ -9,8 +9,6 @@ import { WagmiProvider } from 'components/common/WalletLogin/WagmiProvider'; import type { SessionUser } from 'lib/session/getUserFromSession'; import theme from 'theme/theme'; -import { PurchaseProvider } from './PurchaseProvider'; -import { SnackbarProvider } from './SnackbarContext'; import { SWRProvider } from './SwrProvider'; import { UserProvider } from './UserProvider'; @@ -21,11 +19,7 @@ export function AppProviders({ children, user }: { children: ReactNode; user: Se - - - {children} - - + {children} diff --git a/apps/scoutgame/components/layout/PurchaseProvider.tsx b/apps/scoutgame/components/layout/PurchaseProvider.tsx deleted file mode 100644 index 65d9610139..0000000000 --- a/apps/scoutgame/components/layout/PurchaseProvider.tsx +++ /dev/null @@ -1,74 +0,0 @@ -'use client'; - -import { log } from '@charmverse/core/log'; -import { useAction } from 'next-safe-action/hooks'; -import type { ReactNode } from 'react'; -import { createContext, useContext, useEffect, useMemo } from 'react'; -import { useLocalStorage } from 'usehooks-ts'; - -import { checkDecentTransactionAction } from 'lib/builderNFTs/checkDecentTransactionAction'; - -import { useSnackbar } from './SnackbarContext'; - -type PurchaseContext = { - isExecutingTransaction: boolean; - transactionHasSucceeded: boolean; - checkDecentTransaction: (input: { pendingTransactionId: string }) => Promise; - error: string; -}; - -export const PurchaseContext = createContext>(null); - -export function PurchaseProvider({ children }: { children: ReactNode }) { - const [pendingTransactionId, setPendingTransactionId] = useLocalStorage('pendingTransactionId', ''); - const { showMessage } = useSnackbar(); - - const { - isExecuting: isExecutingTransaction, - hasSucceeded: transactionHasSucceeded, - result: transactionResult, - executeAsync: checkDecentTransaction - } = useAction(checkDecentTransactionAction, { - onExecute({ input }) { - setPendingTransactionId(input.pendingTransactionId); - }, - onSuccess({ input }) { - showMessage(`Transaction ${input.pendingTransactionId} was successful`, 'success'); - setPendingTransactionId(''); - }, - onError({ error, input }) { - log.error('Error checking Decent transaction', { error, input }); - showMessage(error.serverError?.message || 'Something went wrong', 'error'); - setPendingTransactionId(''); - } - }); - - useEffect(() => { - // If user refreshes the page we still want to show him the result of the transaction - if (pendingTransactionId && !isExecutingTransaction) { - checkDecentTransaction({ pendingTransactionId }); - } - }, [pendingTransactionId, isExecutingTransaction]); - - const value = useMemo( - () => ({ - isExecutingTransaction, - transactionHasSucceeded, - checkDecentTransaction, - error: transactionResult.serverError?.message || 'Something went wrong' - }), - [isExecutingTransaction, transactionHasSucceeded, checkDecentTransaction, transactionResult.serverError?.message] - ); - - return {children}; -} - -export function usePurchase() { - const context = useContext(PurchaseContext); - - if (!context) { - throw new Error('usePurchase must be used within a PurchaseProvider'); - } - - return context; -} diff --git a/apps/scoutgame/components/layout/SnackbarContext.tsx b/apps/scoutgame/components/layout/SnackbarContext.tsx deleted file mode 100644 index 9f0736f075..0000000000 --- a/apps/scoutgame/components/layout/SnackbarContext.tsx +++ /dev/null @@ -1,63 +0,0 @@ -'use client'; - -import type { AlertColor, SnackbarCloseReason } from '@mui/material'; -import { Alert, Snackbar } from '@mui/material'; -import type { ReactNode } from 'react'; -import { createContext, useCallback, useContext, useMemo, useState } from 'react'; - -type SnackbarContext = { - showMessage: (message: string, severity?: AlertColor) => void; - snackbarOpen: boolean; -}; - -export const SnackbarContext = createContext>(null); - -export function SnackbarProvider({ children }: { children: ReactNode }) { - const [snackbarOpen, setSnackbarOpen] = useState(false); - const [snackbarMessage, setSnackbarMessage] = useState(''); - const [snackbarSeverity, setSnackbarSeverity] = useState('info'); - - const showMessage = useCallback((message: string, severity: AlertColor = 'info') => { - setSnackbarMessage(message); - setSnackbarSeverity(severity); - setSnackbarOpen(true); - }, []); - - const handleClose = useCallback((_event?: React.SyntheticEvent | Event, reason?: SnackbarCloseReason) => { - if (reason === 'clickaway') { - return; - } - - setSnackbarOpen(false); - setSnackbarMessage(''); - setSnackbarSeverity('info'); - }, []); - - const value = useMemo(() => ({ showMessage, handleClose, snackbarOpen }), [showMessage, handleClose, snackbarOpen]); - - return ( - - {children} - - - {snackbarMessage} - - - - ); -} - -export function useSnackbar() { - const context = useContext(SnackbarContext); - - if (!context) { - throw new Error('useSnackbar must be used within a SnackbarProvider'); - } - - return context; -} diff --git a/packages/scoutgame/src/builderNfts/handlePendingTransaction.ts b/packages/scoutgame/src/builderNfts/handlePendingTransaction.ts index 9701b6f929..cbffc02d29 100644 --- a/packages/scoutgame/src/builderNfts/handlePendingTransaction.ts +++ b/packages/scoutgame/src/builderNfts/handlePendingTransaction.ts @@ -23,22 +23,6 @@ export async function handlePendingTransaction({ throw new InvalidInputError(`Pending transaction id must be a valid uuid`); } - const initialTransaction = await prisma.pendingNftTransaction.findFirst({ - where: { - id: pendingTransactionId - } - }); - - if (initialTransaction && (initialTransaction.status === 'completed' || initialTransaction.status === 'failed')) { - log.info( - 'handlePendingTransaction has propably run twice since the transaction is already completed. Abording the process.', - { - pendingTransactionId - } - ); - return; - } - // Atomically set the status to 'processing' only if it's currently 'pending' const updatedTx = await prisma.pendingNftTransaction.updateMany({ where: {