Browse Source

improve perfs, use flex-wrap on dashboard

master
Gaëtan Renaudeau 7 years ago
parent
commit
d423f620ae
  1. 29
      src/components/DashboardPage/AccountCard.js
  2. 55
      src/components/DashboardPage/index.js
  3. 2
      src/components/base/Box/index.js
  4. 10
      src/components/base/FormattedVal/__tests__/__snapshots__/FormattedVal.test.js.snap

29
src/components/DashboardPage/AccountCard.js

@ -1,6 +1,6 @@
// @flow // @flow
import React from 'react' import React, { PureComponent } from 'react'
import styled from 'styled-components' import styled from 'styled-components'
import type { Account, Currency } from '@ledgerhq/live-common/lib/types' import type { Account, Currency } from '@ledgerhq/live-common/lib/types'
@ -20,22 +20,23 @@ const Wrapper = styled(Card).attrs({
cursor: ${p => (p.onClick ? 'pointer' : 'default')}; cursor: ${p => (p.onClick ? 'pointer' : 'default')};
` `
const AccountCard = ({ class AccountCard extends PureComponent<{
counterValue,
account,
onClick,
daysCount,
...props
}: {
counterValue: Currency, counterValue: Currency,
account: Account, account: Account,
onClick?: () => void, onClick?: Account => void,
daysCount: number, daysCount: number,
}) => ( }> {
<Wrapper onClick={onClick} {...props}> render() {
const { counterValue, account, onClick, daysCount, ...props } = this.props
return (
<Wrapper onClick={onClick ? () => onClick(account) : null} {...props}>
<Box flow={4}> <Box flow={4}>
<Box horizontal ff="Open Sans|SemiBold" flow={3} alignItems="center"> <Box horizontal ff="Open Sans|SemiBold" flow={3} alignItems="center">
<Box alignItems="center" justifyContent="center" style={{ color: account.currency.color }}> <Box
alignItems="center"
justifyContent="center"
style={{ color: account.currency.color }}
>
<CryptoCurrencyIcon currency={account.currency} size={20} /> <CryptoCurrencyIcon currency={account.currency} size={20} />
</Box> </Box>
<Box> <Box>
@ -94,6 +95,8 @@ const AccountCard = ({
)} )}
</CalculateBalance> </CalculateBalance>
</Wrapper> </Wrapper>
) )
}
}
export default AccountCard export default AccountCard

55
src/components/DashboardPage/index.js

@ -5,7 +5,6 @@ import { compose } from 'redux'
import { translate } from 'react-i18next' import { translate } from 'react-i18next'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { push } from 'react-router-redux' import { push } from 'react-router-redux'
import chunk from 'lodash/chunk'
import { createStructuredSelector } from 'reselect' import { createStructuredSelector } from 'reselect'
import type { Account, Currency } from '@ledgerhq/live-common/lib/types' import type { Account, Currency } from '@ledgerhq/live-common/lib/types'
@ -51,36 +50,17 @@ type Props = {
} }
type State = { type State = {
accountsChunk: Array<Array<?Account>>,
selectedTime: string, selectedTime: string,
daysCount: number, daysCount: number,
} }
const ACCOUNTS_BY_LINE = 3
const getAccountsChunk = accounts => {
// create shallow copy of accounts, to be mutated
const listAccounts = [...accounts]
while (listAccounts.length % ACCOUNTS_BY_LINE !== 0) listAccounts.push(null)
return chunk(listAccounts, ACCOUNTS_BY_LINE)
}
class DashboardPage extends PureComponent<Props, State> { class DashboardPage extends PureComponent<Props, State> {
state = { state = {
accountsChunk: getAccountsChunk(this.props.accounts),
selectedTime: 'week', selectedTime: 'week',
daysCount: 7, daysCount: 7,
} }
componentWillReceiveProps(nextProps) { onAccountClick = account => this.props.push(`/account/${account.id}`)
if (nextProps.accounts !== this.props.accounts) {
this.setState({
accountsChunk: getAccountsChunk(nextProps.accounts),
})
}
}
handleGreeting = () => { handleGreeting = () => {
const localTimeHour = new Date().getHours() const localTimeHour = new Date().getHours()
@ -104,8 +84,8 @@ class DashboardPage extends PureComponent<Props, State> {
_cacheBalance = null _cacheBalance = null
render() { render() {
const { push, accounts, t, counterValue } = this.props const { accounts, t, counterValue } = this.props
const { accountsChunk, selectedTime, daysCount } = this.state const { selectedTime, daysCount } = this.state
const timeFrame = this.handleGreeting() const timeFrame = this.handleGreeting()
const totalAccounts = accounts.length const totalAccounts = accounts.length
@ -161,31 +141,24 @@ class DashboardPage extends PureComponent<Props, State> {
<AccountsOrder /> <AccountsOrder />
</Box> </Box>
</Box> </Box>
<Box flow={5}>
{accountsChunk.map((accountsByLine, i) => (
<Box <Box
key={i} // eslint-disable-line react/no-array-index-key
horizontal horizontal
flow={5} flexWrap="wrap"
justifyContent="flex-start"
alignItems="center"
style={{ margin: '0 -16px' }}
> >
{accountsByLine.map( {accounts.concat(Array(3 - accounts.length % 3).fill(null)).map((account, i) => (
(account: any, j) => <Box key={account ? account.id : `placeholder_${i}`} flex="33%" p={16}>
account === null ? ( {account ? (
<Box
key={j} // eslint-disable-line react/no-array-index-key
p={4}
flex={1}
/>
) : (
<AccountCard <AccountCard
key={account.id}
counterValue={counterValue} counterValue={counterValue}
account={account} account={account}
daysCount={daysCount} daysCount={daysCount}
key={account.id} onClick={this.onAccountClick}
onClick={() => push(`/account/${account.id}`)}
/> />
), ) : null}
)}
</Box> </Box>
))} ))}
</Box> </Box>
@ -193,7 +166,7 @@ class DashboardPage extends PureComponent<Props, State> {
{displayOperations && ( {displayOperations && (
<OperationsList <OperationsList
canShowMore canShowMore
onAccountClick={account => push(`/account/${account.id}`)} onAccountClick={this.onAccountClick}
accounts={accounts} accounts={accounts}
title={t('dashboard:recentActivity')} title={t('dashboard:recentActivity')}
withAccount withAccount

2
src/components/base/Box/index.js

@ -8,6 +8,7 @@ import {
boxShadow, boxShadow,
color, color,
flex, flex,
flexWrap,
fontSize, fontSize,
justifyContent, justifyContent,
space, space,
@ -34,6 +35,7 @@ const Box = styled.div`
${boxShadow}; ${boxShadow};
${color}; ${color};
${flex}; ${flex};
${flexWrap};
${fontFamily}; ${fontFamily};
${fontSize}; ${fontSize};
${justifyContent}; ${justifyContent};

10
src/components/base/FormattedVal/__tests__/__snapshots__/FormattedVal.test.js.snap

@ -2,7 +2,7 @@
exports[`components FormattedVal renders a formatted val 1`] = ` exports[`components FormattedVal renders a formatted val 1`] = `
<div <div
className="k45ou1-0 iqaJGf e345n3-0 kZMOmW" className="k45ou1-0 iqaJGf e345n3-0 ghfAOi"
color="#66be54" color="#66be54"
> >
4 4
@ -11,7 +11,7 @@ exports[`components FormattedVal renders a formatted val 1`] = `
exports[`components FormattedVal renders a percent 1`] = ` exports[`components FormattedVal renders a percent 1`] = `
<div <div
className="k45ou1-0 iqaJGf e345n3-0 kZMOmW" className="k45ou1-0 iqaJGf e345n3-0 ghfAOi"
color="#66be54" color="#66be54"
> >
30 % 30 %
@ -20,7 +20,7 @@ exports[`components FormattedVal renders a percent 1`] = `
exports[`components FormattedVal shows code 1`] = ` exports[`components FormattedVal shows code 1`] = `
<div <div
className="k45ou1-0 iqaJGf e345n3-0 kZMOmW" className="k45ou1-0 iqaJGf e345n3-0 ghfAOi"
color="#66be54" color="#66be54"
> >
BTC 4 BTC 4
@ -29,7 +29,7 @@ exports[`components FormattedVal shows code 1`] = `
exports[`components FormattedVal shows sign 1`] = ` exports[`components FormattedVal shows sign 1`] = `
<div <div
className="k45ou1-0 iqaJGf e345n3-0 kZMOmW" className="k45ou1-0 iqaJGf e345n3-0 ghfAOi"
color="#66be54" color="#66be54"
> >
+ 4 + 4
@ -38,7 +38,7 @@ exports[`components FormattedVal shows sign 1`] = `
exports[`components FormattedVal shows sign 2`] = ` exports[`components FormattedVal shows sign 2`] = `
<div <div
className="k45ou1-0 iqaJGf e345n3-0 fMRVKr" className="k45ou1-0 iqaJGf e345n3-0 dBXPqF"
color="#ea2e49" color="#ea2e49"
> >
- 4 - 4

Loading…
Cancel
Save