import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import { Switch, Route, matchPath } from 'react-router-dom'; import classnames from 'classnames'; import TabBar from '../../components/app/tab-bar'; import IconCaretLeft from '../../components/ui/icon/icon-caret-left'; import { ALERTS_ROUTE, ADVANCED_ROUTE, SECURITY_ROUTE, GENERAL_ROUTE, ABOUT_US_ROUTE, SETTINGS_ROUTE, NETWORKS_ROUTE, ///: BEGIN:ONLY_INCLUDE_IN(flask) SNAPS_VIEW_ROUTE, SNAPS_LIST_ROUTE, ///: END:ONLY_INCLUDE_IN CONTACT_LIST_ROUTE, CONTACT_ADD_ROUTE, CONTACT_EDIT_ROUTE, CONTACT_VIEW_ROUTE, EXPERIMENTAL_ROUTE, ADD_NETWORK_ROUTE, ADD_POPULAR_CUSTOM_NETWORK, } from '../../helpers/constants/routes'; import { getSettingsRoutes } from '../../helpers/utils/settings-search'; import AddNetwork from '../../components/app/add-network/add-network'; import SettingsTab from './settings-tab'; import AlertsTab from './alerts-tab'; import NetworksTab from './networks-tab'; import AdvancedTab from './advanced-tab'; import InfoTab from './info-tab'; import SecurityTab from './security-tab'; import ContactListTab from './contact-list-tab'; import ExperimentalTab from './experimental-tab'; ///: BEGIN:ONLY_INCLUDE_IN(flask) import SnapListTab from './flask/snaps-list-tab'; import ViewSnap from './flask/view-snap'; ///: END:ONLY_INCLUDE_IN import SettingsSearch from './settings-search'; import SettingsSearchList from './settings-search-list'; class SettingsPage extends PureComponent { static propTypes = { addNewNetwork: PropTypes.bool, addressName: PropTypes.string, backRoute: PropTypes.string, breadCrumbTextKey: PropTypes.string, conversionDate: PropTypes.number, currentPath: PropTypes.string, history: PropTypes.object, initialBreadCrumbKey: PropTypes.string, initialBreadCrumbRoute: PropTypes.string, isAddressEntryPage: PropTypes.bool, isPopup: PropTypes.bool, isSnapViewPage: PropTypes.bool, mostRecentOverviewPage: PropTypes.string.isRequired, pathnameI18nKey: PropTypes.string, }; static contextTypes = { t: PropTypes.func, }; state = { isSearchList: false, lastFetchedConversionDate: null, searchResults: [], searchText: '', }; componentDidMount() { this.handleConversionDate(); } componentDidUpdate() { this.handleConversionDate(); } handleConversionDate() { const { conversionDate } = this.props; if (conversionDate !== null) { this.setState({ lastFetchedConversionDate: conversionDate }); } } handleClickSetting(setting) { const { history } = this.props; history.push(setting.route); this.setState({ isSearchList: '', searchResults: '', }); } render() { const { history, backRoute, currentPath, mostRecentOverviewPage, addNewNetwork, isSnapViewPage, } = this.props; const { searchResults, isSearchList, searchText } = this.state; return (
{currentPath !== SETTINGS_ROUTE && ( history.push(backRoute)} /> )} {this.renderTitle()}
{ if (addNewNetwork) { history.push(NETWORKS_ROUTE); } else { history.push(mostRecentOverviewPage); } }} />
{ this.setState({ isSearchList: searchQuery !== '', searchResults: results, searchText: searchQuery, }); }} settingsRoutesList={getSettingsRoutes()} /> {isSearchList && searchText.length >= 3 && ( this.handleClickSetting(setting)} /> )}
{this.renderTabs()}
{isSnapViewPage ? null : this.renderSubHeader()} {this.renderContent()}
); } renderTitle() { const { t } = this.context; const { isPopup, pathnameI18nKey, addressName, isSnapViewPage } = this.props; let titleText; if (isSnapViewPage) { titleText = t('snaps'); } else if (isPopup && addressName) { titleText = t('details'); } else if (pathnameI18nKey && isPopup) { titleText = t(pathnameI18nKey); } else { titleText = t('settings'); } return (
{titleText}
); } renderSubHeader() { const { t } = this.context; const { currentPath, isPopup, isAddressEntryPage, pathnameI18nKey, addressName, initialBreadCrumbRoute, breadCrumbTextKey, history, initialBreadCrumbKey, } = this.props; let subheaderText; if (isPopup && isAddressEntryPage) { subheaderText = t('settings'); } else if (isAddressEntryPage) { subheaderText = t('contacts'); } else if (initialBreadCrumbKey) { subheaderText = t(initialBreadCrumbKey); } else { subheaderText = t(pathnameI18nKey || 'general'); } return ( !currentPath.startsWith(NETWORKS_ROUTE) && (
initialBreadCrumbRoute && history.push(initialBreadCrumbRoute) } > {subheaderText}
{breadCrumbTextKey && (
{' > '} {t(breadCrumbTextKey)}
)} {isAddressEntryPage && (
{' > '} {addressName}
)}
) ); } renderTabs() { const { history, currentPath } = this.props; const { t } = this.context; return ( , key: GENERAL_ROUTE, }, { content: t('advanced'), icon: , key: ADVANCED_ROUTE, }, { content: t('contacts'), icon: , key: CONTACT_LIST_ROUTE, }, ///: BEGIN:ONLY_INCLUDE_IN(flask) { content: t('snaps'), icon: ( ), key: SNAPS_LIST_ROUTE, }, ///: END:ONLY_INCLUDE_IN { content: t('securityAndPrivacy'), icon: , key: SECURITY_ROUTE, }, { content: t('alerts'), icon: , key: ALERTS_ROUTE, }, { content: t('networks'), icon: , key: NETWORKS_ROUTE, }, { content: t('experimental'), icon: , key: EXPERIMENTAL_ROUTE, }, { content: t('about'), icon: , key: ABOUT_US_ROUTE, }, ]} isActive={(key) => { if (key === GENERAL_ROUTE && currentPath === SETTINGS_ROUTE) { return true; } return matchPath(currentPath, { exact: true, path: key }); }} onSelect={(key) => history.push(key)} /> ); } renderContent() { return ( ( )} /> } /> } /> } /> { ///: BEGIN:ONLY_INCLUDE_IN(flask) ///: END:ONLY_INCLUDE_IN } { ///: BEGIN:ONLY_INCLUDE_IN(flask) ///: END:ONLY_INCLUDE_IN } ( )} /> ); } } export default SettingsPage;