-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added transaction toasts
- Loading branch information
Showing
9 changed files
with
265 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
import { TransactionToastList } from 'lib/sdkDappCoreUi'; | ||
import { removeTransactionToast } from 'store/actions/toasts/toastsActions'; | ||
import { isServerTransactionPending } from 'store/actions/trackedTransactions/transactionStateByStatus'; | ||
import { ToastsSliceState } from 'store/slices/toast/toastSlice.types'; | ||
import { getStore } from 'store/store'; | ||
import { | ||
ProviderErrorsEnum, | ||
TransactionBatchStatusesEnum, | ||
TransactionServerStatusesEnum | ||
} from 'types'; | ||
import { SignedTransactionType } from 'types/transactions.types'; | ||
import { createModalElement } from 'utils/createModalElement'; | ||
import { | ||
GetToastsOptionsDataPropsType, | ||
ITransactionToast, | ||
TransactionsDefaultTitles, | ||
TransactionToastEventsEnum, | ||
IToastDataState | ||
} from './types'; | ||
|
||
export class ToastManager { | ||
private transactionToastsList: TransactionToastList | undefined; | ||
private unsubscribe: () => void; | ||
store = getStore(); | ||
|
||
constructor() { | ||
const { toasts, trackedTransactions } = this.store.getState(); | ||
this.onToastListChange(toasts); | ||
|
||
let previousToasts = toasts; | ||
let previousTrackedTransactions = trackedTransactions; | ||
this.unsubscribe = this.store.subscribe(() => { | ||
const { toasts, trackedTransactions } = this.store.getState(); | ||
const currentToasts = toasts; | ||
|
||
const currentTrackedTransactions = trackedTransactions; | ||
|
||
if ( | ||
previousToasts !== currentToasts || | ||
previousTrackedTransactions !== currentTrackedTransactions | ||
) { | ||
previousToasts = currentToasts; | ||
previousTrackedTransactions = currentTrackedTransactions; | ||
this.onToastListChange(currentToasts); | ||
} | ||
}); | ||
} | ||
|
||
private async onToastListChange(toastList: ToastsSliceState) { | ||
const { trackedTransactions, account } = this.store.getState(); | ||
const transactionToasts: ITransactionToast[] = []; | ||
|
||
toastList.transactionToasts.forEach(async (toast) => { | ||
const sessionTransactions = trackedTransactions[toast.toastId]; | ||
if (!sessionTransactions) { | ||
return; | ||
} | ||
|
||
const transaction: ITransactionToast = { | ||
toastDataState: this.getToastDataStateByStatus({ | ||
address: account.address, | ||
sender: sessionTransactions.transactions[0].sender, | ||
toastId: toast.toastId, | ||
status: sessionTransactions.status | ||
}), | ||
processedTransactionsStatus: this.getToastProceededStatus( | ||
sessionTransactions.transactions | ||
), | ||
toastId: toast.toastId, | ||
transactions: sessionTransactions.transactions.map((transaction) => ({ | ||
hash: transaction.hash, | ||
status: transaction.status | ||
})) | ||
}; | ||
|
||
transactionToasts.push(transaction); | ||
}); | ||
await this.renderUIToasts(transactionToasts); | ||
} | ||
|
||
private async renderUIToasts(transactionsToasts: ITransactionToast[]) { | ||
if (!this.transactionToastsList) { | ||
this.transactionToastsList = | ||
await createModalElement<TransactionToastList>( | ||
'transaction-toast-list' | ||
); | ||
} | ||
|
||
const eventBus = await this.transactionToastsList.getEventBus(); | ||
|
||
if (!eventBus) { | ||
throw new Error(ProviderErrorsEnum.eventBusError); | ||
} | ||
|
||
eventBus.publish( | ||
TransactionToastEventsEnum.TRANSACTION_TOAST_DATA_UPDATE, | ||
transactionsToasts | ||
); | ||
eventBus.subscribe( | ||
TransactionToastEventsEnum.CLOSE_TOAST, | ||
(toastId: string) => { | ||
removeTransactionToast(toastId); | ||
} | ||
); | ||
return this.transactionToastsList; | ||
} | ||
|
||
private getToastDataStateByStatus = ({ | ||
address, | ||
sender, | ||
status, | ||
toastId | ||
}: GetToastsOptionsDataPropsType) => { | ||
const successToastData: IToastDataState = { | ||
id: toastId, | ||
icon: 'check', | ||
hasCloseButton: true, | ||
title: TransactionsDefaultTitles.success, | ||
iconClassName: 'success' | ||
}; | ||
|
||
const receivedToastData: IToastDataState = { | ||
id: toastId, | ||
icon: 'check', | ||
hasCloseButton: true, | ||
title: TransactionsDefaultTitles.received, | ||
iconClassName: 'success' | ||
}; | ||
|
||
const pendingToastData: IToastDataState = { | ||
id: toastId, | ||
icon: 'hourglass', | ||
hasCloseButton: false, | ||
title: TransactionsDefaultTitles.pending, | ||
iconClassName: 'warning' | ||
}; | ||
|
||
const failToastData: IToastDataState = { | ||
id: toastId, | ||
icon: 'times', | ||
title: TransactionsDefaultTitles.failed, | ||
hasCloseButton: true, | ||
iconClassName: 'danger' | ||
}; | ||
|
||
const invalidToastData: IToastDataState = { | ||
id: toastId, | ||
icon: 'ban', | ||
title: TransactionsDefaultTitles.invalid, | ||
hasCloseButton: true, | ||
iconClassName: 'warning' | ||
}; | ||
|
||
const timedOutToastData = { | ||
id: toastId, | ||
icon: 'times', | ||
title: TransactionsDefaultTitles.timedOut, | ||
hasCloseButton: true, | ||
iconClassName: 'warning' | ||
}; | ||
|
||
switch (status) { | ||
case TransactionBatchStatusesEnum.signed: | ||
case TransactionBatchStatusesEnum.sent: | ||
return pendingToastData; | ||
case TransactionBatchStatusesEnum.success: | ||
return sender !== address ? receivedToastData : successToastData; | ||
case TransactionBatchStatusesEnum.cancelled: | ||
case TransactionBatchStatusesEnum.fail: | ||
return failToastData; | ||
case TransactionBatchStatusesEnum.timedOut: | ||
return timedOutToastData; | ||
case TransactionBatchStatusesEnum.invalid: | ||
return invalidToastData; | ||
default: | ||
return pendingToastData; | ||
} | ||
}; | ||
|
||
private getToastProceededStatus = (transactions: SignedTransactionType[]) => { | ||
const processedTransactions = transactions.filter( | ||
(tx) => | ||
!isServerTransactionPending(tx.status as TransactionServerStatusesEnum) | ||
).length; | ||
|
||
const totalTransactions = transactions.length; | ||
|
||
if (totalTransactions === 1 && processedTransactions === 1) { | ||
return isServerTransactionPending( | ||
transactions[0].status as TransactionServerStatusesEnum | ||
) | ||
? 'Processing transaction' | ||
: 'Transaction processed'; | ||
} | ||
|
||
return `${processedTransactions} / ${totalTransactions} transactions processed`; | ||
}; | ||
|
||
public destroy() { | ||
this.unsubscribe(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './toast.types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { | ||
TransactionBatchStatusesEnum, | ||
TransactionServerStatusesEnum | ||
} from 'types'; | ||
|
||
export enum TransactionsDefaultTitles { | ||
success = 'Transaction successful', | ||
received = 'Transaction received', | ||
failed = 'Transaction failed', | ||
pending = 'Processing transaction', | ||
timedOut = 'Transaction timed out', | ||
invalid = 'Transaction invalid' | ||
} | ||
|
||
export interface GetToastsOptionsDataPropsType { | ||
address: string; | ||
sender: string; | ||
status?: TransactionBatchStatusesEnum | TransactionServerStatusesEnum; | ||
toastId: string; | ||
} | ||
|
||
export interface IToastDataState { | ||
id: string; | ||
icon: string; | ||
hasCloseButton: boolean; | ||
title: string; | ||
iconClassName: string; | ||
} | ||
export interface ITransactionProgressState { | ||
progressClass?: string; | ||
currentRemaining: number; | ||
} | ||
export interface ITransaction { | ||
hash: string; | ||
status: string; | ||
} | ||
export interface ITransactionToast { | ||
toastId: string; | ||
wrapperClass?: string; // TODO: remove ? | ||
processedTransactionsStatus: string; | ||
transactions: ITransaction[]; | ||
toastDataState: IToastDataState; | ||
transactionProgressState?: ITransactionProgressState; | ||
} | ||
|
||
export enum TransactionToastEventsEnum { | ||
'CLOSE_TOAST' = 'CLOSE_TOAST', | ||
'TRANSACTION_TOAST_DATA_UPDATE' = 'TRANSACTION_TOAST_DATA_UPDATE' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters