From 93a06def9e69a14e69326ccefb3c32366ca4dab8 Mon Sep 17 00:00:00 2001 From: jojobyte <184880+jojobyte@users.noreply.github.com> Date: Fri, 15 Mar 2024 19:19:51 -0600 Subject: [PATCH] feat(ui): :goal_net: add `showErrorDialog` to display errors --- src/main.js | 145 ++++++++++++++++++++++++++++++++---- src/rigs/confirm-action.js | 54 +++++++++----- src/rigs/send-or-request.js | 44 +++++++---- src/rigs/wallet-decrypt.js | 8 +- 4 files changed, 204 insertions(+), 47 deletions(-) diff --git a/src/main.js b/src/main.js index b3f0a79..a24131b 100644 --- a/src/main.js +++ b/src/main.js @@ -377,6 +377,13 @@ async function getUserInfo() { ([k,v]) => userInfo[k] = v ) }) + .catch(err => { + showErrorDialog({ + title: 'Unable to decrypt seed phrase', + msg: err, + showActBtn: false, + }) + }) } } @@ -428,6 +435,77 @@ function getTarget(event, selector) { return target } +async function showNotification({ + type = '', + title = '', + msg = '', + sticky = false, +}) { + console.log('notification', {type, title, msg, sticky}) +} + +async function showErrorDialog(options) { + let opts = { + type: 'warn', + title: '⚠️ Error', + msg: '', + showCancelBtn: true, + showActBtn: true, + // timeout: null, + ...options, + } + + if (opts.type === 'dang') { + console.error('showErrorDialog', opts.msg) + } else { + console.log('showErrorDialog', opts) + } + + await appDialogs.confirmAction?.render({ + name: opts.title, + actionTxt: 'Report Issue', + actionAlt: 'Report the error at GitHub', + action: 'lock', + cancelTxt: 'Close', + cancelAlt: `Close`, + // target: '', + // targetFallback: 'this wallet', + actionType: opts.type, + // action: 'disconnect', + // target: '', + // targetFallback: 'this wallet', + // actionType: 'dang', + showCancelBtn: opts.showCancelBtn, + showActBtn: opts.showActBtn, + submitIcon: state => `⚠️`, + alert: state => html``, + content: state => html` + ${state.header(state)} + +
+ + Looks like we encountered an error. + +
${opts.msg}
+
+ + ${state.footer(state)} + `, + callback: () => { + let firstLineFromError = opts.msg.match(/[^\r\n]+/g)?.[0] + + // console.log('firstLineFromError', firstLineFromError) + + window.open( + `https://github.com/dashhive/wallet-ui/issues?q=${firstLineFromError}`, + '_blank', + ) + }, + }) + + return appDialogs.confirmAction?.showModal() +} + async function main() { appState.encryptionPassword = window.atob( sessionStorage.encryptionPassword || '' @@ -463,6 +541,11 @@ async function main() { } ) + appDialogs.confirmAction = await confirmActionRig({ + mainApp, setupDialog, + appDialogs, appState, appTools, + }) + appDialogs.walletEncrypt = await walletEncryptRig({ setupDialog, appDialogs, appState, mainApp, wallet, wallets, bodyNav, dashBalance, @@ -471,6 +554,7 @@ async function main() { appDialogs.walletDecrypt = await walletDecryptRig({ setupDialog, appDialogs, appState, mainApp, importFromJson, wallets, decryptKeystore, getUserInfo, store, deriveWalletData, + showErrorDialog, }) appDialogs.walletBackup = await walletBackupRig({ @@ -503,11 +587,6 @@ async function main() { mainApp, wallet, userInfo, contactsList, }) - appDialogs.confirmAction = await confirmActionRig({ - mainApp, setupDialog, - appDialogs, appState, appTools, - }) - appDialogs.confirmDelete = await confirmDeleteRig({ mainApp, setupDialog, appDialogs, appState, appTools, store, userInfo, contactsList, @@ -532,7 +611,7 @@ async function main() { mainApp, appDialogs, appState, appTools, store, wallet, account: appState.account, walletFunds, setupDialog, deriveWalletData, createTx, - getAddrsWithFunds, batchGenAcctAddrs, getUnusedChangeAddress, getAccountWallet, + getAddrsWithFunds, batchGenAcctAddrs, getUnusedChangeAddress, getAccountWallet, showErrorDialog, }) appDialogs.txInfo = await txInfoRig({ @@ -577,10 +656,15 @@ async function main() { ks, ) } catch(err) { - console.error( - '[fail] unable to decrypt seed phrase', - err - ) + // console.error( + // '[fail] unable to decrypt seed phrase', + // err + // ) + await showErrorDialog({ + title: 'Unable to decrypt seed phrase', + msg: err, + showActBtn: false, + }) sessionStorage.removeItem('encryptionPassword') } } @@ -743,7 +827,14 @@ async function main() { .then(funds => { console.log('updateAllFunds then funds', funds) }) - .catch(err => console.error('catch updateAllFunds', err, wallet)) + .catch(err => { + // console.error('catch updateAllFunds', err, wallet) + showNotification({ + type: 'error', + title: 'Update funds', + msg: err, + }) + }) }) }) @@ -940,10 +1031,22 @@ async function main() { // .then(funds => { // console.log('updateAllFunds then funds', funds) // }) - // .catch(err => console.error('catch updateAllFunds', err, wallet)) + // .catch(err => { + // // console.error('catch updateAllFunds', err, wallet) + // showNotification({ + // type: 'error', + // title: 'Update funds', + // msg: err, + // }) + // }) let storedAddrs = (await store.addresses.keys()) || [] + // showErrorDialog({ + // title: 'Test error', + // msg: err, + // }) + initDashSocket({ onMessage: async function (evname, data) { let updates = {} @@ -968,7 +1071,14 @@ async function main() { .then(funds => { console.log('updateAllFunds then funds', funds) }) - .catch(err => console.error('catch updateAllFunds', err, wallet)), + .catch(err => { + // console.error('catch updateAllFunds', err, wallet) + showNotification({ + type: 'error', + title: 'Update funds', + msg: err, + }) + }), 1000 ) } @@ -1127,7 +1237,14 @@ async function main() { .then(funds => { console.log('updateAllFunds then funds', funds) }) - .catch(err => console.error('catch updateAllFunds', err, wallet)), + .catch(err => { + // console.error('catch updateAllFunds', err, wallet) + showNotification({ + type: 'error', + title: 'Update funds', + msg: err, + }) + }), 1000 ) } diff --git a/src/rigs/confirm-action.js b/src/rigs/confirm-action.js index a7996ad..798390a 100644 --- a/src/rigs/confirm-action.js +++ b/src/rigs/confirm-action.js @@ -30,36 +30,52 @@ export let confirmActionRig = (async function (globals) { warn: 'outline brd-warn warn dark-hover bg-warn-hover', dang: 'outline brd-dang dang light-hover bg-dang-hover', }, + showCancelBtn: true, + showActBtn: true, // submitIcon: state => html` // // // // `, + cancelBtn: state => { + if (!state.showCancelBtn) { + return `` + } + + return html`` + }, + actionBtn: state => { + if (!state.showActBtn) { + return `` + } + + return html`` + }, submitIcon: state => `🔒`, footer: state => html` `, diff --git a/src/rigs/send-or-request.js b/src/rigs/send-or-request.js index ec876b5..fdd9e43 100644 --- a/src/rigs/send-or-request.js +++ b/src/rigs/send-or-request.js @@ -17,7 +17,7 @@ export let sendOrReceiveRig = (async function (globals) { let { mainApp, setupDialog, appDialogs, appState, appTools, store, createTx, deriveWalletData, getAddrsWithFunds, batchGenAcctAddrs, - wallet, wallets, accounts, walletFunds, getUnusedChangeAddress, getAccountWallet, + wallet, wallets, accounts, walletFunds, getUnusedChangeAddress, getAccountWallet, showErrorDialog, } = globals let sendOrReceive = await setupDialog( @@ -516,7 +516,15 @@ export let sendOrReceiveRig = (async function (globals) { address = to } - if (amount > 0 && walletFunds.balance < amount) { + let leftoverBalance = walletFunds.balance - amount + // let fullTransfer = leftoverBalance <= 0.0010_0200 + let fullTransfer = leftoverBalance <= 0.0001_0200 + + if ( + amount > 0 && + walletFunds.balance < amount && + !fullTransfer + ) { console.log( `INSUFFICIENT FUNDS IN WALLET`, [ @@ -591,18 +599,28 @@ export let sendOrReceiveRig = (async function (globals) { fundingAddrs = Object.values( fundingAddrs || {} ) - let leftoverBalance = walletFunds.balance - amount - // let fullTransfer = leftoverBalance <= 0.0010_0200 - let fullTransfer = leftoverBalance <= 0.0001_0200 - let { tx, changeAddr, fee } = await createTx( - state.wallet, - fundingAddrs, - [changeAddress], - address, - amount, - fullTransfer, - ) + let createdTx = {} + + try { + createdTx = await createTx( + state.wallet, + fundingAddrs, + [changeAddress], + address, + amount, + fullTransfer, + ) + } catch(err) { + await showErrorDialog({ + type: 'dang', + title: 'Failed to create transaction', + msg: err, + // showActBtn: false, + }) + } + + let { tx, changeAddr, fee } = createdTx let fullAmount = 0 diff --git a/src/rigs/wallet-decrypt.js b/src/rigs/wallet-decrypt.js index ee7b58f..b61ac18 100644 --- a/src/rigs/wallet-decrypt.js +++ b/src/rigs/wallet-decrypt.js @@ -13,6 +13,7 @@ export let walletDecryptRig = (async function (globals) { setupDialog, appDialogs, appState, mainApp, wallets, decryptKeystore, getUserInfo, store, deriveWalletData, importFromJson, + // showErrorDialog, } = globals; let walletDecrypt = await setupDialog( @@ -190,7 +191,12 @@ export let walletDecryptRig = (async function (globals) { state.elements.dialog.returnValue = String(fde.intent) } catch(err) { - console.error('[fail] unable to decrypt seed phrase', err) + console.warn('[fail] unable to decrypt seed phrase', err) + // await showErrorDialog({ + // title: 'Unable to decrypt seed phrase', + // msg: err, + // showActBtn: false, + // }) event.target.pass.setCustomValidity( 'Unable to decrypt seed phrase. Did you type the correct encryption password?' )