From e3400658da8166a8cf15d0320ea4322acd05c025 Mon Sep 17 00:00:00 2001
From: jojobyte <184880+jojobyte@users.noreply.github.com>
Date: Fri, 5 Apr 2024 23:00:23 -0600
Subject: [PATCH] refactor(data): :recycle: move show error to seperate file
---
src/helpers/utils.js | 8 ++
src/main.js | 278 ++++++++----------------------------
src/rigs/phrase-import.js | 1 +
src/rigs/send-confirm.js | 3 +-
src/rigs/send-or-request.js | 1 +
src/rigs/show-error.js | 79 ++++++++++
src/rigs/wallet-decrypt.js | 1 +
7 files changed, 153 insertions(+), 218 deletions(-)
create mode 100644 src/rigs/show-error.js
diff --git a/src/helpers/utils.js b/src/helpers/utils.js
index 57ca83b..79c70df 100644
--- a/src/helpers/utils.js
+++ b/src/helpers/utils.js
@@ -488,6 +488,14 @@ export async function restate(
return state
}
+export function filterPairedContacts(contact) {
+ return !!contact.alias || !!contact.info?.name?.trim()
+}
+
+export function filterUnpairedContacts(contact) {
+ return !filterPairedContacts(contact)
+}
+
export function sortContactsByAlias(a, b) {
const aliasA = a.alias || a.info?.preferred_username?.toUpperCase() || 'zzz';
const aliasB = b.alias || b.info?.preferred_username?.toUpperCase() || 'zzz';
diff --git a/src/main.js b/src/main.js
index ed0125f..3a5347e 100644
--- a/src/main.js
+++ b/src/main.js
@@ -67,6 +67,7 @@ import sendConfirmRig from './rigs/send-confirm.js'
import requestQrRig from './rigs/request-qr.js'
import pairQrRig from './rigs/pair-qr.js'
import txInfoRig from './rigs/tx-info.js'
+import showErrorDialog from './rigs/show-error.js'
// app/data state
let accounts
@@ -157,29 +158,27 @@ let contactsList = await setupContactsList(
events: {
handleClick: state => async event => {
event.preventDefault()
- // console.log(
- // 'handle contacts click',
- // event.target,
- // state,
- // )
let contactArticle = event.target?.closest('a, article')
if (
- // event.target?.nodeName === 'ARTICLE'
contactArticle !== null
) {
let contactID = contactArticle.dataset.id
+
if (!contactID) {
return;
}
+
let contactData = await appTools.storedData?.decryptItem?.(
store.contacts,
contactID,
)
+
if (!contactData.incoming) {
return;
}
+
let contactAccountID = Object.values(contactData.incoming || {})?.[0]?.accountIndex
console.log('contact click data', contactData)
@@ -218,8 +217,43 @@ let contactsList = await setupContactsList(
}
if (
- event.target?.id === 'add_contact' ||
- event.target?.parentNode?.id === 'add_contact'
+ getTarget(event, 'paired_contacts')
+ ) {
+ event.preventDefault()
+ event.stopPropagation()
+
+ // console.log(
+ // 'paired_contacts',
+ // event
+ // )
+
+ await contactsList.render({
+ userInfo,
+ contacts: appState.contacts,
+ showUnpaired: false,
+ })
+ }
+
+ if (
+ getTarget(event, 'unpaired_contacts')
+ ) {
+ event.preventDefault()
+ event.stopPropagation()
+
+ // console.log(
+ // 'unpaired_contacts',
+ // event
+ // )
+
+ await contactsList.render({
+ userInfo,
+ contacts: appState.contacts,
+ showUnpaired: true,
+ })
+ }
+
+ if (
+ getTarget(event, 'add_contact')
) {
await getUserInfo()
@@ -262,7 +296,6 @@ let contactsList = await setupContactsList(
let created = (new Date()).toISOString()
let usage = [0,0]
- // usage[shareAccount.usageIndex] = shareAccount.addressIndex
newAccount = await store.accounts.setItem(
shareAccount.xkeyId,
@@ -270,9 +303,6 @@ let contactsList = await setupContactsList(
createdAt: created,
updatedAt: (new Date()).toISOString(),
accountIndex,
- // addressIndex: shareAccount.addressIndex,
- // changeIndex: shareAccount.addressIndex,
- // usageIndex: shareAccount.usageIndex,
usage,
walletId: shareAccount.id,
xkeyId: shareAccount.xkeyId,
@@ -292,9 +322,7 @@ let contactsList = await setupContactsList(
accountIndex,
finalAddressIndex,
addresses,
- // shareAccount?.xkeyId,
shareAccount,
- // wallet,
)
newContact = await appTools.storedData.encryptItem(
@@ -314,9 +342,10 @@ let contactsList = await setupContactsList(
appState.contacts.push(newContact)
- await contactsList.render(
- appState.contacts.filter(c => !!c.alias || !!c.info?.name?.trim()).sort(sortContactsByAlias)
- )
+ await contactsList.render({
+ userInfo,
+ contacts: appState.contacts,
+ })
console.log(
'share qr new contact',
@@ -350,10 +379,7 @@ async function getUserInfo() {
appState.encryptionPassword,
ks,
)
- // console.log(
- // 'getUserInfo selectedAlias',
- // appState.selectedAlias,
- // )
+
await appTools.storedData?.decryptItem(
store.aliases,
appState.selectedAlias,
@@ -382,6 +408,7 @@ async function getUserInfo() {
title: 'Unable to decrypt seed phrase',
msg: err,
showActBtn: false,
+ confirmAction: appDialogs.confirmAction,
})
})
}
@@ -393,11 +420,6 @@ async function handlePasswordToggle(event) {
name: fieldName, form,
} = event?.target
- // console.log('handlePasswordToggle', {
- // fieldName,
- // form,
- // })
-
if (
fieldName === 'show_pass'
) {
@@ -444,82 +466,6 @@ async function showNotification({
console.log('notification', {type, title, msg, sticky})
}
-async function showErrorDialog(options) {
- let opts = {
- type: 'warn',
- title: '⚠️ Error',
- msg: '',
- showCancelBtn: true,
- showActBtn: true,
- cancelCallback: () => {},
- // timeout: null,
- ...options,
- }
-
- opts.callback = opts.callback || (() => {
- let firstLineFromError = ''
- let { msg } = opts
-
- if (typeof msg !== 'string' && msg.toString) {
- msg = msg.toString()
- }
- if (typeof msg === 'string') {
- firstLineFromError = msg.match(/[^\r\n]+/g)?.[0]
- }
-
- // console.log('firstLineFromError', firstLineFromError)
-
- window.open(
- `https://github.com/dashhive/wallet-ui/issues?q=${firstLineFromError}`,
- '_blank',
- )
- })
-
- if (opts.type === 'dang') {
- console.error('showErrorDialog', opts)
- } else {
- console.log('showErrorDialog', opts)
- }
-
- let outputMsg = opts.msg?.response || opts.msg?.stack || opts.msg
-
- 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)}
-
-
-
- ${outputMsg}
-
-
- ${state.footer(state)}
- `,
- cancelCallback: opts.cancelCallback,
- callback: opts.callback,
- })
-
- return appDialogs.confirmAction?.showModal()
-}
-
async function main() {
appState.encryptionPassword = window.atob(
sessionStorage.encryptionPassword || ''
@@ -671,14 +617,11 @@ async function main() {
ks,
)
} catch(err) {
- // console.error(
- // '[fail] unable to decrypt seed phrase',
- // err
- // )
await showErrorDialog({
title: 'Unable to decrypt seed phrase',
msg: err,
showActBtn: false,
+ confirmAction: appDialogs.confirmAction,
})
sessionStorage.removeItem('encryptionPassword')
}
@@ -741,53 +684,28 @@ async function main() {
let receiveWallet
let selectedWallet = wallets?.[appState.selectedWallet]
- console.log('selectedWallet', wallet, selectedWallet)
-
- // if (wallet?.xpub) {
- // wallet.addressIndex = (
- // selectedWallet?.addressIndex ?? -1
- // ) + 1
- // receiveWallet = await deriveWalletData(
- // appState.phrase,
- // wallet.accountIndex,
- // wallet.addressIndex,
- // )
- // }
+ // console.log('selectedWallet', wallet, selectedWallet)
if (wallet?.xkeyId) {
let aw = await getAccountWallet(
wallet,
appState.phrase,
)
- wallet = aw.wallet
- // tmpAcct.usage = tmpAcct?.usage || [0,0]
- // tmpAcct.usage[
- // wallet.usageIndex
- // ] = wallet.addressIndex
-
- // state.wallet =
- // tmpAcct = await store.accounts.setItem(
- // wallet.xkeyId,
- // {
- // ...tmpAcct,
- // updatedAt: (new Date()).toISOString(),
- // address: wallet.address,
- // }
- // )
+ wallet = aw.wallet
batchGenAcctAddrs(
receiveWallet,
aw.account,
)
- console.log(
- `${fde.intent} TO SELECTED WALLET`,
- {
- wallet,
- account: aw.account,
- }
- )
+ // console.log(
+ // `${fde.intent} TO SELECTED WALLET`,
+ // {
+ // wallet,
+ // account: aw.account,
+ // }
+ // )
await appDialogs.requestQr.render(
{
@@ -873,7 +791,7 @@ async function main() {
appState.contacts = res
contactsList.render({
- contacts: res?.filter(c => !!c.alias || !!c.info?.name?.trim()).sort(sortContactsByAlias),
+ contacts: res,
userInfo,
})
@@ -882,14 +800,12 @@ async function main() {
},
res => async v => {
res.push(await appTools.storedData?.decryptData?.(v) || v)
- // appTools.storedData.decryptData(v)
- // .then(ev => res.push(ev))
}
)
await contactsList.render({
userInfo,
- contacts: appState.contacts?.filter(c => !!c.alias || !!c.info?.name?.trim()).sort(sortContactsByAlias)
+ contacts: appState.contacts,
})
sendRequestBtn.render()
@@ -898,8 +814,6 @@ async function main() {
// @ts-ignore
id,
// @ts-ignore
- nextElementSibling,
- // @ts-ignore
parentElement,
} = event?.target
@@ -912,10 +826,6 @@ async function main() {
console.log('click alias', [aliasTarg])
aliasTarg?.nextElementSibling.classList.toggle('hidden')
- // @ts-ignore
- // targ?.closest?.('menu.user')?.classList?.toggle('hidden')
-
- // event.target.next
}
if (id === 'nav-edit-profile') {
event.preventDefault()
@@ -963,8 +873,6 @@ async function main() {
actionTxt: 'Lock it!',
actionAlt: 'Lock the wallet',
action: 'lock',
- // target: '',
- // targetFallback: 'this wallet',
actionType: 'warn',
alert: state => html``,
callback: () => {
@@ -986,8 +894,6 @@ async function main() {
actionTxt: 'Disconnect',
actionAlt: 'Clear all wallet data stored in browser',
action: 'disconnect',
- // target: '',
- // targetFallback: 'this wallet',
actionType: 'dang',
submitIcon: state => `🧹`, // `💣`,
alert: state => html`
@@ -1013,8 +919,6 @@ async function main() {
appDialogs.confirmAction.showModal()
}
- // @ts-ignore
- // event.target?.closest?.('menu menu:not(.hidden)')?.classList?.add('hidden')
if ((
!id?.startsWith('nav-')
) && (
@@ -1042,26 +946,8 @@ async function main() {
})
})
- // updateAllFunds(wallet, walletFunds)
- // .then(funds => {
- // console.log('updateAllFunds then funds', funds)
- // })
- // .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 = {}
@@ -1099,12 +985,6 @@ async function main() {
}
let now = Date.now();
- // if (mempoolTx?.timestamp) {
- // // don't wait longer than 3s for a txlock
- // if (now - mempoolTx.timestamp > maxTxLockWait) {
- // return mempoolTx;
- // }
- // }
// console.log('dash socket vout', data)
@@ -1131,7 +1011,6 @@ async function main() {
// Updates Insight info for Change Address
if (
- // checkAddr &&
appState?.sentTransactions?.[data.txid]
) {
// console.log('data.vout.filter', vout, data)
@@ -1140,24 +1019,15 @@ async function main() {
store.addresses.getItem(addr)
.then(async storedAddr => {
if (storedAddr?.insight?.updatedAt) {
- // let tmpBalance = storedAddr.insight.balance
storedAddr.insight.balance = (duffs / DUFFS)
storedAddr.insight.balanceSat = duffs
storedAddr.insight.updatedAt = 0
store.addresses.setItem(addr, storedAddr)
-
- // walletFunds.balance = (
- // walletFunds.balance - (tmpBalance - storedAddr.insight.balance)
- // )
}
})
return false
}
- // if (amount && duffs !== amount) {
- // return false;
- // }
-
let newTx = {
address: addr,
timestamp: now,
@@ -1169,26 +1039,12 @@ async function main() {
walletFunds.balance = walletFunds?.balance + newTx.dash
- // dashBalance?.restate({
- // wallet,
- // walletFunds: {
- // balance: walletFunds?.balance || 0
- // }
- // })
-
- // if ("txlock" !== evname) {
- // if (!mempoolTx) {
- // mempoolTx = newTx;
- // }
- // return false;
- // }
-
- // result = newTx;
console.log(
'found address in store',
addr,
newTx,
)
+
updates[addr] = newTx
store.addresses.getItem(addr)
.then(async storedAddr => {
@@ -1199,17 +1055,6 @@ async function main() {
store.addresses.setItem(addr, storedAddr)
}
- // let selectedWallet = wallets?.[appState.selectedWallet]
- // wallet.addressIndex = (
- // selectedWallet?.addressIndex || 0
- // ) + 1
- // let walletTemp = await deriveWalletData(
- // appState.phrase,
- // storedAddr.accountIndex,
- // storedAddr.addressIndex,
- // storedAddr.usageIndex,
- // )
-
let tmpWalletAcct = await store.accounts.getItem(
storedAddr.xkeyId,
) || {}
@@ -1267,7 +1112,6 @@ async function main() {
Object.keys(txUpdates).forEach(
txid => {
- // let txs = appState?.sentTransactions
if (txs?.[txid]) {
delete txs[txid]
}
diff --git a/src/rigs/phrase-import.js b/src/rigs/phrase-import.js
index 6e34725..dc569f9 100644
--- a/src/rigs/phrase-import.js
+++ b/src/rigs/phrase-import.js
@@ -33,6 +33,7 @@ export let phraseImportRig = (async function (globals) {
cancelCallback: () => {
clearFile(state)
},
+ confirmAction: appDialogs.confirmAction,
})
}
diff --git a/src/rigs/send-confirm.js b/src/rigs/send-confirm.js
index 6170979..ac774e0 100644
--- a/src/rigs/send-confirm.js
+++ b/src/rigs/send-confirm.js
@@ -160,6 +160,7 @@ export let sendConfirmRig = (async function (globals) {
title: 'Failed to send transaction',
msg: err,
showActBtn: false,
+ confirmAction: appDialogs.confirmAction,
})
}
}
@@ -193,7 +194,7 @@ export let sendConfirmRig = (async function (globals) {
appState.contacts = res
return contactsList.restate({
- contacts: res?.sort(sortContactsByAlias),
+ contacts: res,
userInfo,
})
}
diff --git a/src/rigs/send-or-request.js b/src/rigs/send-or-request.js
index 59ba965..a2d94ac 100644
--- a/src/rigs/send-or-request.js
+++ b/src/rigs/send-or-request.js
@@ -619,6 +619,7 @@ export let sendOrReceiveRig = (async function (globals) {
title: 'Failed to create transaction',
msg: err,
// showActBtn: false,
+ confirmAction: appDialogs.confirmAction,
})
}
diff --git a/src/rigs/show-error.js b/src/rigs/show-error.js
new file mode 100644
index 0000000..0e590d5
--- /dev/null
+++ b/src/rigs/show-error.js
@@ -0,0 +1,79 @@
+import { lit as html } from '../helpers/lit.js'
+
+export async function showErrorDialog(options) {
+ let opts = {
+ type: 'warn',
+ title: '⚠️ Error',
+ msg: '',
+ showCancelBtn: true,
+ showActBtn: true,
+ cancelCallback: () => {},
+ // timeout: null,
+ ...options,
+ }
+
+ opts.callback = opts.callback || (() => {
+ let firstLineFromError = ''
+ let { msg } = opts
+
+ if (typeof msg !== 'string' && msg.toString) {
+ msg = msg.toString()
+ }
+ if (typeof msg === 'string') {
+ firstLineFromError = msg.match(/[^\r\n]+/g)?.[0]
+ }
+
+ // console.log('firstLineFromError', firstLineFromError)
+
+ window.open(
+ `https://github.com/dashhive/wallet-ui/issues?q=${firstLineFromError}`,
+ '_blank',
+ )
+ })
+
+ if (opts.type === 'dang') {
+ console.error('showErrorDialog', opts)
+ } else {
+ console.log('showErrorDialog', opts)
+ }
+
+ let outputMsg = opts.msg?.response || opts.msg?.stack || opts.msg
+
+ await opts.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)}
+
+
+
+ ${outputMsg}
+
+
+ ${state.footer(state)}
+ `,
+ cancelCallback: opts.cancelCallback,
+ callback: opts.callback,
+ })
+
+ return opts.confirmAction?.showModal()
+}
+
+export default showErrorDialog
diff --git a/src/rigs/wallet-decrypt.js b/src/rigs/wallet-decrypt.js
index b61ac18..f43cd27 100644
--- a/src/rigs/wallet-decrypt.js
+++ b/src/rigs/wallet-decrypt.js
@@ -196,6 +196,7 @@ export let walletDecryptRig = (async function (globals) {
// title: 'Unable to decrypt seed phrase',
// msg: err,
// showActBtn: false,
+ // confirmAction: appDialogs.confirmAction,
// })
event.target.pass.setCustomValidity(
'Unable to decrypt seed phrase. Did you type the correct encryption password?'