Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Surface Error Messages for End Users #64

Merged
merged 8 commits into from
Mar 21, 2024
Binary file added public/favicon.ico
Binary file not shown.
Binary file modified public/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 11 additions & 32 deletions src/components/balance.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { lit as html } from '../helpers/lit.js'
import { fixedDash, envoy, } from '../helpers/utils.js'
import { envoy, formatDash, } from '../helpers/utils.js'
import { updateAllFunds, } from '../helpers/wallet.js'

const initialState = {
Expand All @@ -14,6 +14,7 @@ const initialState = {
addr: null,
maxlen: 10,
fract: 8,
sigsplit: 3,
render(
renderState = {},
position = 'afterbegin',
Expand All @@ -23,41 +24,19 @@ const initialState = {
<figcaption>${state.name} ${state.id}</figcaption>
`,
content: state => {
let funds = 0
let balance = `${funds}`

if (state.walletFunds.balance) {
funds += state.walletFunds.balance
balance = fixedDash(funds, state.fract)
// TODO FIX: does not support large balances

// console.log('balance fixedDash', balance, balance.length)

let [fundsInt,fundsFract] = balance.split('.')
state.maxlen -= fundsInt.length

let fundsFraction = fundsFract?.substring(
0, Math.min(Math.max(0, state.maxlen), 3)
)

let fundsRemainder = fundsFract?.substring(
fundsFraction.length,
Math.max(0, state.maxlen)
)

balance = html`${
fundsInt
}<sub><span>.${
fundsFraction
}</span>${
fundsRemainder
}</sub>`
}
let balance = formatDash(
state.walletFunds.balance,
{
fract: state.fract,
maxlen: state.maxlen,
sigsplit: state.sigsplit,
}
)

return html`
${state.header(state)}

<div title="${funds}">
<div title="${state.walletFunds.balance}">
<svg width="32" height="33" viewBox="0 0 32 33">
<use xlink:href="#icon-dash-mark"></use>
</svg>
Expand Down
2 changes: 1 addition & 1 deletion src/components/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ const initialState = {
// )
state.elements.dialog.close('cancel')
}
}
},
},
}

Expand Down
141 changes: 135 additions & 6 deletions src/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,50 @@ export function toDuff(dash) {
return Math.round(parseFloat(dash) * DUFFS);
}

export function formatDash(
unformattedBalance,
options = {},
) {
let opts = {
maxlen: 10,
fract: 8,
sigsplit: 3,
...options,
}
let funds = 0
let balance = `${funds}`

if (unformattedBalance) {
funds += unformattedBalance
balance = fixedDash(funds, opts.fract)
// TODO FIX: does not support large balances

// console.log('balance fixedDash', balance, balance.length)

let [fundsInt,fundsFract] = balance.split('.')
opts.maxlen -= fundsInt.length

let fundsFraction = fundsFract?.substring(
0, Math.min(Math.max(0, opts.maxlen), opts.sigsplit)
)

let fundsRemainder = fundsFract?.substring(
fundsFraction.length,
Math.max(0, opts.maxlen)
)

balance = `${
fundsInt
}<sub><span>.${
fundsFraction
}</span>${
fundsRemainder
}</sub>`
}

return balance
}

export function formDataEntries(event) {
let fd = new FormData(
event.target,
Expand Down Expand Up @@ -984,20 +1028,90 @@ export async function getAvatar(c) {
return `${avStr}">${initials}</div>`
}

export function readFile(file, callback) {
export function fileIsSubType(file, type) {
const fileType = file?.type?.split('/')?.[1]

if (!fileType) {
return false
}

return fileType === type
}

// fileInTypes({type:'application/json'}, ['image/png'])
export function fileInMIMETypes(file, types = []) {
const fileType = file?.type

if (!fileType) {
return false
}

return types.includes(fileType)
}

export function fileTypeInTypes(file, types = []) {
const fileType = file?.type?.split('/')?.[0]

if (!fileType) {
return false
}

return types.includes(fileType)
}

export function fileTypeInSubtype(file, subtypes = []) {
const fileSubType = file?.type?.split('/')?.[1]

if (!fileSubType) {
return false
}

return subtypes.includes(fileSubType)
}

export function readFile(file, options) {
let opts = {
expectedFileType: 'json',
denyFileTypes: ['audio','video','image','font','model'],
denyFileSubTypes: ['msword','xml'],
callback: () => {},
errorCallback: () => {},
...options,
}
let reader = new FileReader();
let result

reader.addEventListener('load', () => {
if (
fileTypeInTypes(
file,
opts.denyFileTypes,
) || fileTypeInSubtype(
file,
opts.denyFileSubTypes,
)
) {
return opts.errorCallback?.({
err: `Wrong file type: ${file.type}. Expected: ${opts.expectedFileType}.`,
file,
})
}

try {
// @ts-ignore
result = JSON.parse(reader?.result || '{}');

console.log('parse loaded json', result);
callback?.(result)
// console.log('parse loaded json', result);

opts.callback?.(result, file)

// state[key] = result
} catch(err) {
opts.errorCallback?.({
err,
file,
})

throw new Error(`failed to parse JSON data from ${file.name}`)
}
});
Expand Down Expand Up @@ -1054,11 +1168,26 @@ export function getPartialHDPath(wallet) {
].join('/')
}

export function getAddressIndexFromUsage(wallet, account, usage) {
let usageIndex = usage ?? wallet.usageIndex
export function getAddressIndexFromUsage(wallet, account, usageIdx) {
let usageIndex = usageIdx ?? wallet?.usageIndex ?? 0
let addressIndex = account.usage?.[usageIndex] ?? account.addressIndex ?? 0
let usage = account.usage ?? [
account.addressIndex ?? 0,
0
]

// console.log(
// 'getAddressIndexFromUsage',
// usageIndex,
// addressIndex,
// account,
// usage,
// )

return {
...account,
usage,
usageIndex,
addressIndex: account.usage[usageIndex],
addressIndex,
}
}
1 change: 1 addition & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ main header figure figcaption + div svg {
main header figure figcaption + div sub {
align-self: self-end;
font-size: 2.75rem;
font-size: 74%;
}
main footer {
text-align: center;
Expand Down
Loading
Loading