2021-02-04 19:15:23 +01:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
import { findKey } from 'lodash';
|
2021-01-27 17:51:59 +01:00
|
|
|
import {
|
|
|
|
STATUS_CONNECTED,
|
|
|
|
STATUS_CONNECTED_TO_ANOTHER_ACCOUNT,
|
|
|
|
STATUS_NOT_CONNECTED,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../helpers/constants/connected-sites';
|
|
|
|
import ColorIndicator from '../../ui/color-indicator';
|
|
|
|
import { COLORS } from '../../../helpers/constants/design-system';
|
|
|
|
import { useI18nContext } from '../../../hooks/useI18nContext';
|
2021-01-27 17:51:59 +01:00
|
|
|
import {
|
|
|
|
getAddressConnectedDomainMap,
|
|
|
|
getOriginOfCurrentTab,
|
|
|
|
getSelectedAddress,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../selectors';
|
2021-01-27 17:51:59 +01:00
|
|
|
|
|
|
|
export default function ConnectedStatusIndicator({ onClick }) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const t = useI18nContext();
|
2021-01-27 17:51:59 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const selectedAddress = useSelector(getSelectedAddress);
|
|
|
|
const addressConnectedDomainMap = useSelector(getAddressConnectedDomainMap);
|
|
|
|
const originOfCurrentTab = useSelector(getOriginOfCurrentTab);
|
2021-01-27 17:51:59 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const selectedAddressDomainMap = addressConnectedDomainMap[selectedAddress];
|
2021-01-27 17:51:59 +01:00
|
|
|
const currentTabIsConnectedToSelectedAddress = Boolean(
|
|
|
|
selectedAddressDomainMap && selectedAddressDomainMap[originOfCurrentTab],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
let status;
|
2021-01-27 17:51:59 +01:00
|
|
|
if (currentTabIsConnectedToSelectedAddress) {
|
2021-02-04 19:15:23 +01:00
|
|
|
status = STATUS_CONNECTED;
|
2021-01-27 17:51:59 +01:00
|
|
|
} else if (findKey(addressConnectedDomainMap, originOfCurrentTab)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
status = STATUS_CONNECTED_TO_ANOTHER_ACCOUNT;
|
2021-01-27 17:51:59 +01:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
status = STATUS_NOT_CONNECTED;
|
2021-01-27 17:51:59 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let indicatorType = ColorIndicator.TYPES.OUTLINE;
|
|
|
|
let indicatorColor = COLORS.UI4;
|
2021-01-27 17:51:59 +01:00
|
|
|
|
|
|
|
if (status === STATUS_CONNECTED) {
|
2021-02-04 19:15:23 +01:00
|
|
|
indicatorColor = COLORS.SUCCESS1;
|
|
|
|
indicatorType = ColorIndicator.TYPES.PARTIAL;
|
2021-01-27 17:51:59 +01:00
|
|
|
} else if (status === STATUS_CONNECTED_TO_ANOTHER_ACCOUNT) {
|
2021-02-04 19:15:23 +01:00
|
|
|
indicatorColor = COLORS.ALERT1;
|
2021-01-27 17:51:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const text =
|
2021-02-04 19:15:23 +01:00
|
|
|
status === STATUS_CONNECTED
|
|
|
|
? t('statusConnected')
|
|
|
|
: t('statusNotConnected');
|
2021-01-27 17:51:59 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<button className="connected-status-indicator" onClick={onClick}>
|
|
|
|
<ColorIndicator color={indicatorColor} type={indicatorType} />
|
|
|
|
<div className="connected-status-indicator__text">{text}</div>
|
|
|
|
</button>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-01-27 17:51:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ConnectedStatusIndicator.defaultProps = {
|
|
|
|
onClick: undefined,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2021-01-27 17:51:59 +01:00
|
|
|
|
|
|
|
ConnectedStatusIndicator.propTypes = {
|
|
|
|
onClick: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|