import React from 'react';
import { shell } from 'electron';
import { useDispatch, useSelector } from 'react-redux';
import ip2country from 'ip2country';
import countryLookup from 'country-code-lookup';
import { Snode } from '../../data/data';
import { onionPathModal } from '../../state/ducks/modalDialog';
import {
getFirstOnionPath,
getFirstOnionPathLength,
getIsOnline,
getOnionPathsCount,
} from '../../state/selectors/onions';
import { Flex } from '../basic/Flex';
// tslint:disable-next-line: no-submodule-imports
import useHover from 'react-use/lib/useHover';
import { SessionSpinner } from '../basic/SessionSpinner';
import { SessionIcon, SessionIconButton } from '../icon';
import { SessionWrapperModal } from '../SessionWrapperModal';
import styled from 'styled-components';
export type StatusLightType = {
glowStartDelay: number;
glowDuration: number;
color?: string;
};
const StyledCountry = styled.div`
margin: var(--margins-sm);
min-width: 150px;
`;
const StyledOnionNodeList = styled.div`
display: flex;
flex-direction: column;
margin: var(--margins-sm);
align-items: center;
min-width: 10vw;
position: relative;
`;
const StyledOnionDescription = styled.p`
min-width: 400px;
width: 0;
`;
const StyledVerticalLine = styled.div`
background: rgba(#7a7a7a, 0.6);
position: absolute;
height: calc(100% - 2 * 15px);
margin: 15px calc(100% / 2 - 1px);
width: 1px;
`;
const StyledLightsContainer = styled.div`
position: relative;
`;
const StyledGrowingIcon = styled.div`
flex-grow: 1;
display: flex;
align-items: center;
`;
const OnionCountryDisplay = ({ labelText, snodeIp }: { snodeIp?: string; labelText: string }) => {
const element = (hovered: boolean) => (
{hovered && snodeIp ? snodeIp : labelText}
);
const [hoverable] = useHover(element);
return hoverable;
};
const OnionPathModalInner = () => {
const onionPath = useSelector(getFirstOnionPath);
const isOnline = useSelector(getIsOnline);
// including the device and destination in calculation
const glowDuration = onionPath.length + 2;
if (!isOnline || !onionPath || onionPath.length === 0) {
return ;
}
const nodes = [
{
label: window.i18n('device'),
},
...onionPath,
{
label: window.i18n('destination'),
},
];
return (
<>
{window.i18n('onionPathIndicatorDescription')}
{nodes.map((_snode: Snode | any, index: number) => {
return (
);
})}
{nodes.map((snode: Snode | any) => {
let labelText = snode.label
? snode.label
: `${countryLookup.byIso(ip2country(snode.ip))?.country}`;
if (!labelText) {
labelText = window.i18n('unknownCountry');
}
return labelText ? (
) : null;
})}
>
);
};
export type OnionNodeStatusLightType = {
glowStartDelay: number;
glowDuration: number;
};
/**
* Component containing a coloured status light.
*/
export const OnionNodeStatusLight = (props: OnionNodeStatusLightType): JSX.Element => {
const { glowStartDelay, glowDuration } = props;
return (
);
};
/**
* An icon with a pulsating glow emission.
*/
export const ModalStatusLight = (props: StatusLightType) => {
const { glowStartDelay, glowDuration, color } = props;
return (
);
};
/**
* A status light specifically for the action panel. Color is based on aggregate node states instead of individual onion node state
*/
export const ActionPanelOnionStatusLight = (props: {
isSelected: boolean;
handleClick: () => void;
dataTestId?: string;
id: string;
}) => {
const { isSelected, handleClick, dataTestId, id } = props;
const onionPathsCount = useSelector(getOnionPathsCount);
const firstPathLength = useSelector(getFirstOnionPathLength);
const isOnline = useSelector(getIsOnline);
// Set icon color based on result
const red = 'var(--color-destructive)';
const green = 'var(--color-accent)';
const orange = 'var(--color-warning)';
// start with red
let iconColor = red;
//if we are not online or the first path is not valid, we keep red as color
if (isOnline && firstPathLength > 1) {
iconColor = onionPathsCount >= 2 ? green : onionPathsCount >= 1 ? orange : red;
}
return (
);
};
export const OnionPathModal = () => {
const onConfirm = () => {
void shell.openExternal('https://getsession.org/faq/#onion-routing');
};
const dispatch = useDispatch();
return (
// tslint:disable-next-line: use-simple-attributes
dispatch(onionPathModal(null))}
showExitIcon={true}
>
);
};