Persist home tab state (#8612)

feature/default_network_editable
Whymarrh Whitby 5 years ago committed by GitHub
parent 3cedf708e9
commit 835386bf35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      app/scripts/controllers/app-state.js
  2. 1
      app/scripts/metamask-controller.js
  3. 29
      ui/app/components/ui/tabs/tabs.component.js
  4. 10
      ui/app/components/ui/tabs/tabs.stories.js
  5. 6
      ui/app/pages/home/home.component.js
  6. 4
      ui/app/pages/home/home.container.js
  7. 6
      ui/app/store/actions.js

@ -23,6 +23,7 @@ export default class AppStateController extends EventEmitter {
this.store = new ObservableStore(Object.assign({ this.store = new ObservableStore(Object.assign({
timeoutMinutes: 0, timeoutMinutes: 0,
connectedStatusPopoverHasBeenShown: true, connectedStatusPopoverHasBeenShown: true,
defaultHomeActiveTabName: null,
}, initState)) }, initState))
this.timer = null this.timer = null
@ -90,6 +91,16 @@ export default class AppStateController extends EventEmitter {
} }
} }
/**
* Sets the default home tab
* @param {string} [defaultHomeActiveTabName] - the tab name
*/
setDefaultHomeActiveTabName (defaultHomeActiveTabName) {
this.store.updateState({
defaultHomeActiveTabName,
})
}
/** /**
* Record that the user has seen the connected status info popover * Record that the user has seen the connected status info popover
*/ */

@ -505,6 +505,7 @@ export default class MetamaskController extends EventEmitter {
// AppStateController // AppStateController
setLastActiveTime: nodeify(this.appStateController.setLastActiveTime, this.appStateController), setLastActiveTime: nodeify(this.appStateController.setLastActiveTime, this.appStateController),
setDefaultHomeActiveTabName: nodeify(this.appStateController.setDefaultHomeActiveTabName, this.appStateController),
setConnectedStatusPopoverHasBeenShown: nodeify(this.appStateController.setConnectedStatusPopoverHasBeenShown, this.appStateController), setConnectedStatusPopoverHasBeenShown: nodeify(this.appStateController.setConnectedStatusPopoverHasBeenShown, this.appStateController),
// EnsController // EnsController

@ -3,24 +3,31 @@ import PropTypes from 'prop-types'
export default class Tabs extends Component { export default class Tabs extends Component {
static defaultProps = { static defaultProps = {
defaultActiveTabIndex: 0, defaultActiveTabName: null,
onTabClick: null,
} }
static propTypes = { static propTypes = {
defaultActiveTabIndex: PropTypes.number, defaultActiveTabName: PropTypes.string,
onTabClick: PropTypes.func,
children: PropTypes.node.isRequired, children: PropTypes.node.isRequired,
} }
state = { state = {
activeTabIndex: this.props.defaultActiveTabIndex, activeTabIndex: Math.max(this._findChildByName(this.props.defaultActiveTabName), 0),
} }
handleTabClick (tabIndex) { handleTabClick (tabIndex, tabName) {
const { onTabClick } = this.props
const { activeTabIndex } = this.state const { activeTabIndex } = this.state
if (tabIndex !== activeTabIndex) { if (tabIndex !== activeTabIndex) {
this.setState({ this.setState({
activeTabIndex: tabIndex, activeTabIndex: tabIndex,
}, () => {
if (onTabClick) {
onTabClick(tabName)
}
}) })
} }
} }
@ -29,11 +36,11 @@ export default class Tabs extends Component {
const numberOfTabs = React.Children.count(this.props.children) const numberOfTabs = React.Children.count(this.props.children)
return React.Children.map(this.props.children, (child, index) => { return React.Children.map(this.props.children, (child, index) => {
const tabName = child?.props.name
return child && React.cloneElement(child, { return child && React.cloneElement(child, {
onClick: (index) => this.handleTabClick(index), onClick: (index) => this.handleTabClick(index, tabName),
tabIndex: index, tabIndex: index,
isActive: numberOfTabs > 1 && index === this.state.activeTabIndex, isActive: numberOfTabs > 1 && index === this.state.activeTabIndex,
key: index,
}) })
}) })
} }
@ -66,4 +73,14 @@ export default class Tabs extends Component {
</div> </div>
) )
} }
/**
* Returns the index of the child with the given key
* @param {string} key - the child key to search for
* @returns {number}
* @private
*/
_findChildByName (name) {
return React.Children.toArray(this.props.children).findIndex((c) => c?.props.name === name)
}
} }

@ -1,7 +1,7 @@
import React from 'react' import React from 'react'
import Tab from './tab/tab.component' import Tab from './tab/tab.component'
import Tabs from './tabs.component' import Tabs from './tabs.component'
import { number, text } from '@storybook/addon-knobs/react' import { text } from '@storybook/addon-knobs/react'
export default { export default {
title: 'Tabs', title: 'Tabs',
@ -20,9 +20,7 @@ function renderTab (id) {
export const twoTabs = () => { export const twoTabs = () => {
return ( return (
<Tabs <Tabs>
defaultActiveTabIndex={number('Default Active Tab Index', 0, { min: 0 })}
>
{ {
['A', 'B'] ['A', 'B']
.map(renderTab) .map(renderTab)
@ -33,9 +31,7 @@ export const twoTabs = () => {
export const manyTabs = () => { export const manyTabs = () => {
return ( return (
<Tabs <Tabs>
defaultActiveTabIndex={number('Default Active Tab Index', 0, { min: 0 })}
>
{ {
['A', 'B', 'C', 'D', 'E'] ['A', 'B', 'C', 'D', 'E']
.map(renderTab) .map(renderTab)

@ -56,6 +56,8 @@ export default class Home extends PureComponent {
decimals: PropTypes.number, decimals: PropTypes.number,
symbol: PropTypes.string, symbol: PropTypes.string,
}), }),
defaultHomeActiveTabName: PropTypes.string.isRequired,
onTabClick: PropTypes.func.isRequired,
} }
UNSAFE_componentWillMount () { UNSAFE_componentWillMount () {
@ -209,6 +211,8 @@ export default class Home extends PureComponent {
render () { render () {
const { const {
defaultHomeActiveTabName,
onTabClick,
forgottenPassword, forgottenPassword,
history, history,
connectedStatusPopoverHasBeenShown, connectedStatusPopoverHasBeenShown,
@ -257,7 +261,7 @@ export default class Home extends PureComponent {
<div className="home__balance-wrapper"> <div className="home__balance-wrapper">
{ homeOverview } { homeOverview }
</div> </div>
<Tabs> <Tabs defaultActiveTabName={defaultHomeActiveTabName} onTabClick={onTabClick}>
<Tab <Tab
activeClassName="home__tab--active" activeClassName="home__tab--active"
className="home__tab" className="home__tab"

@ -16,6 +16,7 @@ import {
getThreeBoxLastUpdated, getThreeBoxLastUpdated,
setShowRestorePromptToFalse, setShowRestorePromptToFalse,
setConnectedStatusPopoverHasBeenShown, setConnectedStatusPopoverHasBeenShown,
setDefaultHomeActiveTabName,
} from '../../store/actions' } from '../../store/actions'
import { setThreeBoxLastUpdated } from '../../ducks/app/app' import { setThreeBoxLastUpdated } from '../../ducks/app/app'
import { getEnvironmentType } from '../../../../app/scripts/lib/util' import { getEnvironmentType } from '../../../../app/scripts/lib/util'
@ -34,6 +35,7 @@ const mapStateToProps = (state) => {
showRestorePrompt, showRestorePrompt,
selectedAddress, selectedAddress,
connectedStatusPopoverHasBeenShown, connectedStatusPopoverHasBeenShown,
defaultHomeActiveTabName,
} = metamask } = metamask
const accountBalance = getCurrentEthBalance(state) const accountBalance = getCurrentEthBalance(state)
const { forgottenPassword, threeBoxLastUpdated } = appState const { forgottenPassword, threeBoxLastUpdated } = appState
@ -63,6 +65,7 @@ const mapStateToProps = (state) => {
firstPermissionsRequestId, firstPermissionsRequestId,
totalUnapprovedCount, totalUnapprovedCount,
connectedStatusPopoverHasBeenShown, connectedStatusPopoverHasBeenShown,
defaultHomeActiveTabName,
} }
} }
@ -82,6 +85,7 @@ const mapDispatchToProps = (dispatch) => ({
restoreFromThreeBox: (address) => dispatch(restoreFromThreeBox(address)), restoreFromThreeBox: (address) => dispatch(restoreFromThreeBox(address)),
setShowRestorePromptToFalse: () => dispatch(setShowRestorePromptToFalse()), setShowRestorePromptToFalse: () => dispatch(setShowRestorePromptToFalse()),
setConnectedStatusPopoverHasBeenShown: () => dispatch(setConnectedStatusPopoverHasBeenShown()), setConnectedStatusPopoverHasBeenShown: () => dispatch(setConnectedStatusPopoverHasBeenShown()),
onTabClick: (name) => dispatch(setDefaultHomeActiveTabName(name)),
}) })
export default compose( export default compose(

@ -1815,6 +1815,12 @@ export function updatePreferences (value) {
} }
} }
export function setDefaultHomeActiveTabName (value) {
return async () => {
await promisifiedBackground.setDefaultHomeActiveTabName(value)
}
}
export function setUseNativeCurrencyAsPrimaryCurrencyPreference (value) { export function setUseNativeCurrencyAsPrimaryCurrencyPreference (value) {
return setPreference('useNativeCurrencyAsPrimaryCurrency', value) return setPreference('useNativeCurrencyAsPrimaryCurrency', value)
} }

Loading…
Cancel
Save