Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,46 @@ export const useTransactionSocketListener = (key?: QueryKey) => {
});
}, [queryClient]);

// When the worker sends minimal tx data (no full transaction object),
// the updateTransactions handler skips the cache replacement.
// This handler invalidates the relevant queries so React Query refetches
// the fully formatted transaction from the API.
const handleWorkerUpdate = useCallback(
(event: ITransactionReactQueryUpdate) => {
if (!event?.transaction) return;

// Detect minimal payload from worker (missing `name` field)
if (!event.transaction.name) {
const workspaceId = userInfos.workspace?.id ?? '';
const { predicateId, id } = event.transaction;

queryClient.invalidateQueries({
queryKey: HomeQueryKey.HOME_WORKSPACE(workspaceId),
});
queryClient.invalidateQueries({
queryKey:
vaultInfinityQueryKey.VAULT_TRANSACTION_LIST_PAGINATION_QUERY_KEY(
predicateId,
),
});
queryClient.invalidateQueries({
queryKey:
WorkspacesQueryKey.TRANSACTION_LIST_PAGINATION_QUERY_KEY(
workspaceId,
),
});
queryClient.invalidateQueries({
queryKey: getTransactionHistoryQueryKey(id, predicateId),
});
}
},
[queryClient, userInfos],
);

useSocketEvent<ITransactionReactQueryUpdate>(SocketEvents.TRANSACTION, [
updateTransactions,
updateHistory,
handleSignaturePending,
handleWorkerUpdate,
]);
};
40 changes: 38 additions & 2 deletions src/modules/transactions/hooks/send/useSendTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { TransactionStatus } from 'bakosafe';
import { useCallback } from 'react';

import { queryClient } from '@/config';
import { useAuth } from '@/modules';
import { SocketEvents, useAuth } from '@/modules';
import { useBakoSafeTransactionSend, WitnessStatus } from '@/modules/core';
import { ITransaction } from '@/modules/core/hooks/bakosafe/utils/types';
import { useSocketEvent } from '@/modules/core/hooks/socket/useSocketEvent';
import { useNotificationsStore } from '@/modules/notifications/store';
import { TransactionService } from '@/modules/transactions/services';
import { ITransactionReactQueryUpdate } from '@/modules/transactions/services/types';

import { useTransactionToast } from '../../providers/toast';
import { useTransactionState } from '../../states';
Expand All @@ -17,11 +20,44 @@ export type IUseSendTransaction = {

const useSendTransaction = ({ onTransactionSuccess }: IUseSendTransaction) => {
const { setHasNewNotification } = useNotificationsStore();
const { setIsCurrentTxPending } = useTransactionState();
const { isCurrentTxPending, setIsCurrentTxPending } = useTransactionState();
const toast = useTransactionToast();

const { userInfos } = useAuth();

// Listen for socket events to resolve the loading toast when the
// worker finishes processing the transaction asynchronously.
const handleSocketResult = useCallback(
async (event: ITransactionReactQueryUpdate) => {
if (!isCurrentTxPending.isPending) return;
if (!event?.transaction) return;
if (event.transaction.id !== isCurrentTxPending.transactionId) return;

const { id, status, predicateId } = event.transaction;

if (status === TransactionStatus.SUCCESS) {
const fullTx = await TransactionService.getById(id);
toast.success(fullTx);
setIsCurrentTxPending({ isPending: false, transactionId: '' });
queryClient.invalidateQueries({
queryKey: [TRANSACTION_HISTORY_QUERY_KEY, id, predicateId],
});
setHasNewNotification(true);
}

if (status === TransactionStatus.FAILED) {
toast.error(id, 'Transaction failed');
setIsCurrentTxPending({ isPending: false, transactionId: '' });
setHasNewNotification(true);
}
},
[isCurrentTxPending, setIsCurrentTxPending, toast, setHasNewNotification],
);

useSocketEvent<ITransactionReactQueryUpdate>(SocketEvents.TRANSACTION, [
handleSocketResult,
]);

const { mutate: sendTransaction } = useBakoSafeTransactionSend({
onSuccess: (transaction: ITransaction) => {
onTransactionSuccess();
Expand Down
8 changes: 8 additions & 0 deletions src/modules/transactions/services/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ export class TransactionService {

const { type, transaction } = event;

// Worker sends minimal tx data (id, status, hash, predicateId).
// Skip cache replacement — invalidateQueries handles the refetch.
if (!transaction?.name) return oldData;

if (type !== '[CREATED]') {
return {
...oldData,
Expand All @@ -357,6 +361,10 @@ export class TransactionService {
if (!oldData) return oldData;

const { type, transaction } = event;

// Worker sends minimal tx data — skip cache replacement
if (!transaction?.name) return oldData;

const { pageParams, pages } = oldData;

if (type !== '[CREATED]') {
Expand Down
Loading