Skip to content

Commit

Permalink
fix: error handling of invalid timestamps (#2796)
Browse files Browse the repository at this point in the history
* fix: error handling of invalid timestamps

* fix: invalid date and time in cvs export

* fix: handle undefined index
  • Loading branch information
nicole-obrien authored Apr 8, 2022
1 parent c66d658 commit 6e239c0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 35 deletions.
30 changes: 18 additions & 12 deletions packages/shared/components/ActivityDetail.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
sendAddressFromTransactionPayload,
} from 'shared/lib/wallet'
import { getContext } from 'svelte'
import { Readable, Writable } from 'svelte/store'
import { Writable } from 'svelte/store'
export let locale: Locale
Expand All @@ -33,12 +33,26 @@
export let onBackClick = (): void => {}
let date = locale('error.invalidDate')
$: {
try {
date = formatDate(new Date(timestamp), {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
})
} catch {
date = locale('error.invalidDate')
}
}
const cachedMigrationTx = !payload
const milestonePayload = payload?.type === 'Milestone' ? payload : undefined
const txPayload = payload?.type === 'Transaction' ? payload : undefined
const accounts = getContext<Writable<WalletAccount[]>>('walletAccounts')
const account = getContext<Readable<WalletAccount>>('selectedAccount')
const explorerLink = getOfficialExplorer($accounts[0].clientOptions.network)
let senderAccount: WalletAccount
Expand Down Expand Up @@ -166,18 +180,10 @@
<Text secondary>{locale('general.status')}</Text>
<Text smaller>{locale(`general.${confirmed ? 'confirmed' : 'pending'}`)}</Text>
</div>
{#if timestamp}
{#if date}
<div class="mb-5">
<Text secondary>{locale('general.date')}</Text>
<Text smaller>
{formatDate(new Date(timestamp), {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
})}
</Text>
<Text smaller>{date}</Text>
</div>
{/if}
{#if id}
Expand Down
22 changes: 15 additions & 7 deletions packages/shared/components/ActivityRow.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@
export let onClick = (): void => {}
let messageValue = ''
let date = locale('error.invalidDate')
$: {
try {
date = formatDate(new Date(timestamp), {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
})
} catch {
date = locale('error.invalidDate')
}
}
$: hasCachedMigrationTx = !payload
$: milestonePayload = payload?.type === 'Milestone' ? payload : undefined
Expand Down Expand Up @@ -210,13 +224,7 @@
{/if}
</Text>
<p class="text-10 leading-120 text-gray-500">
{formatDate(new Date(timestamp), {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
})}
{date}
</p>
</div>
<div class="flex-1 items-end flex flex-col ml-4">
Expand Down
27 changes: 17 additions & 10 deletions packages/shared/lib/transactionHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,23 @@ export const generateTransactionHistoryCsvFromAccount = (
const formattedValueString = incoming
? formatUnitBestMatch(value, true)
: '-' + formatUnitBestMatch(value, true)
const date = formatDate(new Date(timestamp), {
year: 'numeric',
month: 'short',
day: 'numeric',
})
const time = formatDate(new Date(timestamp), {
hour: 'numeric',
minute: 'numeric',
timeZoneName: 'short',
})
let date
let time
try {
date = formatDate(new Date(timestamp), {
year: 'numeric',
month: 'short',
day: 'numeric',
})
time = formatDate(new Date(timestamp), {
hour: 'numeric',
minute: 'numeric',
timeZoneName: 'short',
})
} catch {
date = localize('error.invalidDate')
time = localize('error.invalidTime')
}

const csvLineParts: string[] = []
headerParams.id && csvLineParts.push(String(id))
Expand Down
18 changes: 13 additions & 5 deletions packages/shared/lib/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1400,12 +1400,20 @@ export const getWalletBalanceHistory = (accountsBalanceHistory: AccountsBalanceH
Object.values(accountsBalanceHistory).forEach((accBalanceHistory) => {
Object.entries(accBalanceHistory).forEach(([timeframe, data]) => {
if (!balanceHistory[timeframe].length) {
balanceHistory[timeframe] = data
try {
balanceHistory[timeframe] = data
} catch (err) {
console.error(err)
}
} else {
balanceHistory[timeframe] = balanceHistory[timeframe].map(({ balance, timestamp }, index) => ({
timestamp,
balance: balance + data[index].balance,
}))
try {
balanceHistory[timeframe] = balanceHistory[timeframe].map(({ balance, timestamp }, index) => ({
timestamp,
balance: balance + (data[index]?.balance ?? 0),
}))
} catch (err) {
console.error(err)
}
}
})
})
Expand Down
4 changes: 3 additions & 1 deletion packages/shared/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,9 @@
"cannotFindStakingEvent": "Unable to find staking event.",
"cannotVisitAirdropWebsite": "Unable to open the {airdrop} website.",
"invalidStakingEventId": "Invalid staking event ID."
}
},
"invalidDate": "INVALID DATE",
"invalidTime": "INVALID TIME"
},
"warning": {
"node": {
Expand Down

0 comments on commit 6e239c0

Please sign in to comment.