diff --git a/packages/shared/lib/wallet.ts b/packages/shared/lib/wallet.ts index 77ffb2fc268..a363459e459 100644 --- a/packages/shared/lib/wallet.ts +++ b/packages/shared/lib/wallet.ts @@ -59,6 +59,7 @@ type WalletState = { balanceOverview: Writable accounts: Writable accountsLoaded: Writable + confirmedInternalMessageIds: Writable<{ [key: string]: number }> } type BalanceTimestamp = { @@ -95,6 +96,7 @@ export const wallet = writable({ }), accounts: writable([]), accountsLoaded: writable(false), + confirmedInternalMessageIds: writable<{ [key: string]: number }>({}) }) export const resetWallet = () => { @@ -361,12 +363,13 @@ export const initialiseListeners = () => { onSuccess(response) { const accounts = get(wallet).accounts const account = get(accounts).find((account) => account.id === response.payload.accountId) - const message = response.payload.message - const messageKey = response.payload.confirmed ? 'confirmed' : 'failed' + const message = response.payload.message + const confirmed = response.payload.confirmed; const essence = message.payload.data.essence - if (response.payload.confirmed && !essence.data.internal) { + + if (confirmed && !essence.data.internal) { const { balanceOverview } = get(wallet); const overview = get(balanceOverview); @@ -380,8 +383,10 @@ export const initialiseListeners = () => { ); } + // Update state const accountMessage = account.messages.find((_message) => _message.id === message.id) accountMessage.confirmed = response.payload.confirmed + accounts.update((storedAccounts) => { return storedAccounts.map((storedAccount) => { if (storedAccount.id === account.id) { @@ -402,11 +407,57 @@ export const initialiseListeners = () => { }) }) - const notificationMessage = localize(`notifications.${messageKey}`) - .replace('{{value}}', formatUnit(message.payload.data.essence.data.value)) - .replace('{{account}}', account.alias) + // Notify user + const messageKey = confirmed ? 'confirmed' : 'failed' + + const _notify = (senderAccountAlias: string | null = null) => { + let notificationMessage + + if (senderAccountAlias) { + notificationMessage = localize(`notifications.${messageKey}Internal`) + .replace('{{value}}', formatUnit(message.payload.data.essence.data.value)) + .replace('{{senderAccount}}', senderAccountAlias) + .replace('{{receiverAccount}}', account.alias) + } else { + notificationMessage = localize(`notifications.${messageKey}`) + .replace('{{value}}', formatUnit(message.payload.data.essence.data.value)) + .replace('{{account}}', account.alias) + } + + showSystemNotification({ type: "info", message: notificationMessage }) + } + + const { confirmedInternalMessageIds } = get(wallet) + const messageIds = get(confirmedInternalMessageIds) + + // If this event is emitted because a message failed, then this message will only exist on the sender account + // Therefore, show the notification (no need to group). + if (!confirmed) { + _notify() + } else { + // If this is an external message, notify (no need to group) + if (!essence.data.internal) { + _notify(); + } else { + // If this is an internal message, check if we have already receive confirmation state of this message + if (Object.keys(messageIds).includes(message.id)) { + _notify( + get(accounts).find((account) => account.index === messageIds[message.id]).alias + ); + + confirmedInternalMessageIds.update((ids) => { + delete ids[message.id] + + return ids; + }) + } else { + // Otherwise, add the message id and do not notify yet + messageIds[message.id] = account.index + } + } + } + - showSystemNotification({ type: "info", message: notificationMessage }) }, onError(error) { console.error(error) diff --git a/packages/shared/locales/en.json b/packages/shared/locales/en.json index 5a0fd659e86..fd9323cc22f 100644 --- a/packages/shared/locales/en.json +++ b/packages/shared/locales/en.json @@ -577,6 +577,7 @@ "notifications": { "valueTx": "Receiving {{value}} to {{account}}", "confirmed": "Outgoing {{value}} from {{account}} has confirmed", + "confirmedInternal": "{{value}} from {{senderAccount}} to {{receiverAccount}} has confirmed", "failed": "Outgoing {{value}} from {{account}} has failed", "downloadingUpdate": "Downloading update", "updateReady": "Update ready",