From c897d61ea8c4ea423c54877f4f3ebd543ca0aac4 Mon Sep 17 00:00:00 2001 From: Umair Sarfraz Date: Sat, 20 Mar 2021 17:00:24 +0100 Subject: [PATCH 1/3] feat: group confirmations state notifications for internal transfers --- packages/shared/lib/wallet.ts | 65 +++++++++++++++++++++++++++++---- packages/shared/locales/en.json | 1 + 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/packages/shared/lib/wallet.ts b/packages/shared/lib/wallet.ts index 40b786fbfd1..267e0336af9 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 = { @@ -91,6 +92,7 @@ export const wallet = writable({ }), accounts: writable([]), accountsLoaded: writable(false), + confirmedInternalMessageIds: writable<{ [key: string]: number }>({}) }) export const resetWallet = () => { @@ -357,12 +359,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); @@ -376,8 +379,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) { @@ -398,11 +403,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 = 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]) + ); + + 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 2c2e0f29699..eee3ab3a65e 100644 --- a/packages/shared/locales/en.json +++ b/packages/shared/locales/en.json @@ -573,6 +573,7 @@ "notifications": { "valueTx": "Receiving {{value}} to {{account}}", "confirmed": "Outgoing {{value}} from {{account}} has confirmed", + "confirmedInternal": "Transaction with {{value}} from {{senderAccount}} to {{receiverAccount}} has confirmed", "failed": "Outgoing {{value}} from {{account}} has failed", "downloadingUpdate": "Downloading update", "updateReady": "Update ready", From 0b2834c95c5a6a287592d8b97c63e066ad0e4778 Mon Sep 17 00:00:00 2001 From: Umair Sarfraz Date: Sun, 21 Mar 2021 13:54:31 +0100 Subject: [PATCH 2/3] hotfix: correctly pass sender account alias --- packages/shared/lib/wallet.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/shared/lib/wallet.ts b/packages/shared/lib/wallet.ts index e290c843e3f..f3dbcb97eff 100644 --- a/packages/shared/lib/wallet.ts +++ b/packages/shared/lib/wallet.ts @@ -406,7 +406,7 @@ export const initialiseListeners = () => { // Notify user const messageKey = confirmed ? 'confirmed' : 'failed' - const _notify = (senderAccountAlias = null) => { + const _notify = (senderAccountAlias: string | null = null) => { let notificationMessage if (senderAccountAlias) { @@ -438,7 +438,7 @@ export const initialiseListeners = () => { // 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]) + get(accounts).find((account) => account.index === messageIds[message.id]).alias ); confirmedInternalMessageIds.update((ids) => { From df25336d619897c9c29a5921c70e7ce97f80f015 Mon Sep 17 00:00:00 2001 From: Charlie Varley Date: Mon, 22 Mar 2021 04:53:46 +0000 Subject: [PATCH 3/3] fix: transaction string --- packages/shared/locales/en.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shared/locales/en.json b/packages/shared/locales/en.json index 21b42e6dcd5..4e0a9e97b0d 100644 --- a/packages/shared/locales/en.json +++ b/packages/shared/locales/en.json @@ -577,7 +577,7 @@ "notifications": { "valueTx": "Receiving {{value}} to {{account}}", "confirmed": "Outgoing {{value}} from {{account}} has confirmed", - "confirmedInternal": "Transaction with {{value}} from {{senderAccount}} to {{receiverAccount}} has confirmed", + "confirmedInternal": "{{value}} from {{senderAccount}} to {{receiverAccount}} has confirmed", "failed": "Outgoing {{value}} from {{account}} has failed", "downloadingUpdate": "Downloading update", "updateReady": "Update ready",