Browse Source

Merge pull request #81 from loeck/master

Add cpu usage in DevToolbar and add CopyPaste color
master
Meriadec Pillet 7 years ago
committed by GitHub
parent
commit
6a894a7b5e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      flow-defs/process.js
  2. 10
      package.json
  3. 127
      src/components/DevToolbar.js
  4. 17
      src/components/base/Chart/index.js
  5. 49
      src/internals/index.js
  6. 38
      yarn.lock

10
flow-defs/process.js

@ -1,11 +1,3 @@
/* eslint-disable */
declare var process: {
send(args: any): void,
on(event: string, args: any): void,
nextTick(callback: Function): void,
setMaxListeners(any): void,
removeListener(string, Function): void,
title: string,
env: Object,
}
declare var process: Object

10
package.json

@ -43,7 +43,7 @@
"@fortawesome/fontawesome-free-regular": "^5.0.6",
"@fortawesome/fontawesome-free-solid": "^5.0.6",
"@fortawesome/react-fontawesome": "^0.0.17",
"@ledgerhq/common": "2.1.3",
"@ledgerhq/common": "2.3.0",
"@ledgerhq/hw-app-btc": "^2.2.0",
"@ledgerhq/hw-app-eth": "^2.2.0",
"@ledgerhq/hw-transport": "^2.2.0",
@ -55,7 +55,7 @@
"bs58check": "^2.1.1",
"color": "^3.0.0",
"cross-env": "^5.1.3",
"downshift": "^1.27.1",
"downshift": "^1.28.0",
"electron-store": "^1.3.0",
"electron-updater": "^2.20.1",
"fuse.js": "^3.2.0",
@ -84,7 +84,7 @@
"redux-actions": "^2.2.1",
"redux-thunk": "^2.2.0",
"shortid": "^2.2.8",
"smooth-scrollbar": "^8.2.5",
"smooth-scrollbar": "^8.2.6",
"source-map-support": "^0.5.3",
"styled-components": "^3.1.6",
"styled-system": "^1.1.4"
@ -108,7 +108,7 @@
"chance": "^1.0.13",
"concurrently": "^3.5.1",
"dotenv": "^5.0.0",
"electron": "1.7.12",
"electron": "1.8.2",
"electron-builder": "^19.56.0",
"electron-devtools-installer": "^2.2.3",
"electron-webpack": "1.12.1",
@ -120,7 +120,7 @@
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.6.1",
"flow-bin": "^0.64.0",
"flow-bin": "^0.65.0",
"flow-typed": "^2.3.0",
"husky": "^0.14.3",
"lint-staged": "^6.1.0",

127
src/components/DevToolbar.js

@ -1,6 +1,11 @@
// @flow
import React, { PureComponent } from 'react'
import { ipcRenderer } from 'electron'
import { translate } from 'react-i18next'
import { AreaChart, Area } from 'recharts'
import takeRight from 'lodash/takeRight'
import last from 'lodash/last'
import reduce from 'lodash/fp/reduce'
import flow from 'lodash/fp/flow'
import filter from 'lodash/fp/filter'
@ -9,6 +14,8 @@ import styled from 'styled-components'
import color from 'color'
import Box from 'components/base/Box'
import GrowScroll from 'components/base/GrowScroll'
import CopyToClipboard from 'components/base/CopyToClipboard'
import theme from 'styles/theme'
@ -61,12 +68,15 @@ const Container = styled(Box).attrs({
const Handle = styled(Box).attrs({
bg: 'night',
justify: 'center',
px: 1,
})`
cursor: pointer;
padding: 3px 8px 3px 8px;
position: absolute;
bottom: 100%;
right: 5px;
font-size: 9px;
height: 20px;
border-top-left-radius: 3px;
border-top-right-radius: 3px;
`
@ -76,7 +86,6 @@ const Colors = styled(Box).attrs({
align: 'flex-start',
})`
flex-wrap: wrap;
width: 416px;
`
const Cl = styled(Box).attrs({
@ -85,38 +94,136 @@ const Cl = styled(Box).attrs({
p: 2,
})`
border: 2px solid white;
cursor: pointer;
margin: 2px;
width: 100px;
user-select: text;
cursor: text;
width: 80px;
`
const Color = ({ color }: { color: ColorType }) => (
<Cl bg={color.val} color={color.isDark ? 'white' : 'night'}>
const Color = ({ onClick, color }: { onClick: Function, color: ColorType }) => (
<Cl bg={color.val} color={color.isDark ? 'white' : 'night'} onClick={onClick}>
{color.name}
</Cl>
)
const Lang = styled(Box).attrs({
bg: p => (p.current ? 'white' : 'night'),
color: p => (p.current ? 'night' : 'white'),
borderColor: 'white',
borderWidth: 1,
p: 1,
})`
border-radius: 3px;
cursor: pointer;
`
const LANGUAGES = {
fr: 'français',
en: 'english',
}
type State = {
isOpened: boolean,
cpuUsage: Object,
}
class DevToolbar extends PureComponent<any, State> {
state = {
isOpened: false,
cpuUsage: {},
}
componentDidMount() {
ipcRenderer.on('msg', this.handleMessage)
}
componentWillUnmount() {
ipcRenderer.removeListener('msg', this.handleMessage)
}
handleMessage = (e: any, { type, data }: Object) => {
if (type === 'usage.cpu') {
this.setState(prev => ({
cpuUsage: {
...prev.cpuUsage,
[data.name]: takeRight(
[
...(prev.cpuUsage[data.name] || []),
{
value: parseFloat(data.value),
},
],
10,
),
},
}))
}
}
handleToggle = () => this.setState({ isOpened: !this.state.isOpened })
handleChangeLanguage = lang => () => this.props.i18n.changeLanguage(lang)
render() {
const { isOpened } = this.state
const { i18n } = this.props
const { isOpened, cpuUsage } = this.state
return (
<Container isOpened={isOpened}>
<Handle onClick={this.handleToggle}>{'DEV'}</Handle>
<Colors>{colors.map(color => <Color key={color.name} color={color} />)}</Colors>
<Box horizontal grow flow={10}>
<Box flow={10}>
{Object.keys(LANGUAGES).map(lang => (
<Lang
key={lang}
current={i18n.language === lang}
onClick={this.handleChangeLanguage(lang)}
>
{LANGUAGES[lang]}
</Lang>
))}
</Box>
<Box style={{ width: 168 }}>
<GrowScroll>
<Colors>
{colors.map(color => (
<CopyToClipboard
key={color.name}
data={color.name}
render={copy => <Color color={color} onClick={copy} />}
/>
))}
</Colors>
</GrowScroll>
</Box>
<Box flow={10}>
{Object.keys(cpuUsage).map(k => (
<Box key={k} grow>
<Box horizontal align="center">
<Box grow>{k}</Box>
<Box fontSize="8px">{last(cpuUsage[k]).value}%</Box>
</Box>
<Box>
<AreaChart
width={100}
height={40}
data={cpuUsage[k]}
margin={{ top: 10, right: 0, left: 0, bottom: 0 }}
>
<Area
type="monotone"
stroke="#8884d8"
fill="#8884d8"
dataKey="value"
isAnimationActive={false}
/>
</AreaChart>
</Box>
</Box>
))}
</Box>
</Box>
</Container>
)
}
}
export default DevToolbar
export default translate()(DevToolbar)

17
src/components/base/Chart/index.js

@ -40,15 +40,14 @@ class Container extends PureComponent<Props, State> {
)
if (this._node) {
this._ro = new ResizeObserver(entries =>
entries.forEach(entry => {
if (this._node === entry.target) {
this.setState({
width: entry.contentRect.width,
})
}
}),
)
this._ro = new ResizeObserver(entries => {
const entry = entries.find(entry => this._node === entry.target)
if (entry) {
this.setState({
width: entry.contentRect.width,
})
}
})
this._ro.observe(this._node)
}

49
src/internals/index.js

@ -2,7 +2,9 @@
import objectPath from 'object-path'
process.title = `ledger-wallet-desktop-${process.env.FORK_TYPE}`
const { FORK_TYPE } = process.env
process.title = `ledger-wallet-desktop-${FORK_TYPE}`
function sendEvent(type: string, data: any, options: Object = { kill: true }) {
process.send({ type, data, options })
@ -15,7 +17,7 @@ if (__PROD__ && __SENTRY_URL__) {
}
// $FlowFixMe
const func = require(`./${process.env.FORK_TYPE}`) // eslint-disable-line import/no-dynamic-require
const func = require(`./${FORK_TYPE}`) // eslint-disable-line import/no-dynamic-require
const handlers = Object.keys(func).reduce((result, key) => {
result[key] = func[key](sendEvent)
@ -32,3 +34,46 @@ const onMessage = payload => {
}
process.on('message', onMessage)
if (__DEV__) {
const TIMEOUT_CPU_USAGE = 5e3
let startTime = process.hrtime()
let startUsage = process.cpuUsage()
const cpuUsage = () => {
const now = Date.now()
while (Date.now() - now < 500);
const newStartTime = process.hrtime()
const newStartUsage = process.cpuUsage()
const elapTime = process.hrtime(startTime)
const elapUsage = process.cpuUsage(startUsage)
startTime = newStartTime
startUsage = newStartUsage
const elapTimeMS = elapTime[0] * 1e3 + elapTime[1] / 1e6
const elapUserMS = elapUsage.user / 1e3
const elapSystMS = elapUsage.system / 1e3
const cpuPercent = (100 * (elapUserMS + elapSystMS) / elapTimeMS).toFixed(1)
sendEvent(
'usage.cpu',
{
name: FORK_TYPE,
value: cpuPercent,
},
{
kill: false,
},
)
setTimeout(cpuUsage, TIMEOUT_CPU_USAGE)
}
cpuUsage()
}

38
yarn.lock

@ -131,9 +131,9 @@
dependencies:
humps "^2.0.1"
"@ledgerhq/common@2.1.3":
version "2.1.3"
resolved "https://registry.yarnpkg.com/@ledgerhq/common/-/common-2.1.3.tgz#02ee37050a5a7c22f4b88c6f26d78cf103b5c284"
"@ledgerhq/common@2.3.0":
version "2.3.0"
resolved "https://registry.yarnpkg.com/@ledgerhq/common/-/common-2.3.0.tgz#a6bffa04f05f939f40e623ab8f3c39906ef36dbd"
dependencies:
fbjs "^0.8.16"
invariant "^2.2.2"
@ -373,9 +373,9 @@
version "3.0.1"
resolved "https://registry.yarnpkg.com/@types/inline-style-prefixer/-/inline-style-prefixer-3.0.1.tgz#8541e636b029124b747952e9a28848286d2b5bf6"
"@types/node@^7.0.18":
version "7.0.52"
resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.52.tgz#8990d3350375542b0c21a83cd0331e6a8fc86716"
"@types/node@^8.0.24":
version "8.5.10"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.5.10.tgz#49bd3637125dea5f55d7d1e8f51efd6cb835e1f7"
"@types/react@^16.0.18":
version "16.0.34"
@ -3297,9 +3297,9 @@ dotenv@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-5.0.0.tgz#0206eb5b336639bf377618a2a304ff00c6a1fddb"
downshift@^1.27.1:
version "1.27.1"
resolved "https://registry.yarnpkg.com/downshift/-/downshift-1.27.1.tgz#c91830ed476771373da7353b69726b436a3e3df3"
downshift@^1.28.0:
version "1.28.0"
resolved "https://registry.yarnpkg.com/downshift/-/downshift-1.28.0.tgz#4d2d153ee6a45fcdba0a7121a7858aff42dd0bf3"
duplexer3@^0.1.4:
version "0.1.4"
@ -3518,11 +3518,11 @@ electron-webpack@1.12.1:
webpack-merge "^4.1.1"
yargs "^11.0.0"
electron@1.7.12:
version "1.7.12"
resolved "https://registry.yarnpkg.com/electron/-/electron-1.7.12.tgz#dcc61a2c1b0c3df25f68b3425379a01abd01190e"
electron@1.8.2:
version "1.8.2"
resolved "https://registry.yarnpkg.com/electron/-/electron-1.8.2.tgz#a817cd733c2972b3c7cc4f777caf6e424b88014d"
dependencies:
"@types/node" "^7.0.18"
"@types/node" "^8.0.24"
electron-download "^3.0.1"
extract-zip "^1.0.3"
@ -4233,9 +4233,9 @@ flatten@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.2.tgz#dae46a9d78fbe25292258cc1e780a41d95c03782"
flow-bin@^0.64.0:
version "0.64.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.64.0.tgz#ddd3fb3b183ab1ab35a5d5dec9caf5ebbcded167"
flow-bin@^0.65.0:
version "0.65.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.65.0.tgz#64ffeca27211c786e2d68508c65686ba1b8a2169"
flow-typed@^2.3.0:
version "2.3.0"
@ -8413,9 +8413,9 @@ slice-ansi@1.0.0:
dependencies:
readable-stream "~1.0.31"
smooth-scrollbar@^8.2.5:
version "8.2.5"
resolved "https://registry.yarnpkg.com/smooth-scrollbar/-/smooth-scrollbar-8.2.5.tgz#676a2595b1aad97fe0835d2425e403b0d9c70eb3"
smooth-scrollbar@^8.2.6:
version "8.2.6"
resolved "https://registry.yarnpkg.com/smooth-scrollbar/-/smooth-scrollbar-8.2.6.tgz#5f4683d8381ea8dc4aaea7678b0805e4f5711297"
dependencies:
core-js "^2.5.1"
lodash-es "^4.17.4"

Loading…
Cancel
Save