import React from 'react';
import { Grid, Flex, H5, Text } from 'indigo-react';
import { Just, Nothing } from 'folktale/maybe';
import { formatDotsWithTime } from 'lib/dateFormat';
import { segmentNetworkKey, CURVE_ZERO_ADDR } from 'lib/keys';
import { usePointCache } from 'store/pointCache';
const chainKeyProp = name => d =>
d[name] === CURVE_ZERO_ADDR ? Nothing() : Just(d[name]);
const renderNetworkKey = key => {
const segments = segmentNetworkKey(key);
if (!segments) {
return 'Unset';
}
return segments.join('\n');
};
export default function NetworkingKeys({ point }) {
const { getDetails, getRekeyDate } = usePointCache();
const details = getDetails(point);
const hasKeys = details.matchWith({
Nothing: () => false,
Just: ({ value: details }) => parseInt(details.keyRevisionNumber, 10) > 0,
// we actually don't mind the default NaN behavior of parseInt here,
// since NaN > 0 === false and that's a reasonable result
});
const renderNetworkKeySection = (title, key) => (
<>
{title}
{key.matchWith({
Nothing: () => (
Unset
),
Just: ({ value: key }) => (
<>
0x
{renderNetworkKey(key)}
>
),
})}
>
);
const renderDetail = (title, value) => (
{title}
{value.matchWith({
Nothing: () => (
Unset
),
Just: ({ value }) => {value},
})}
);
return (
<>
{renderNetworkKeySection(
'Authentication',
details.chain(chainKeyProp('authenticationKey'))
)}
{renderNetworkKeySection(
'Encryption',
details.chain(chainKeyProp('encryptionKey'))
)}
{renderDetail(
'Revision',
details.map(d => d.keyRevisionNumber)
)}
{renderDetail(
'Continuity Era',
details.map(d => d.continuityNumber)
)}
{renderDetail(
'Crypto Suite Ver.',
details.map(d => d.cryptoSuiteVersion)
)}
{/* TODO: retrieve last rekeydate, if possible? */}
{hasKeys && (
{renderDetail(
'Last Set',
getRekeyDate(point).map(date =>
date.matchWith({
Ok: r => formatDotsWithTime(r.value),
Error: () => 'No date available',
})
)
)}
)}
>
);
}