import React from 'react';
import { Just, Nothing } from 'folktale/maybe';
import { Grid, H5, H1, HelpText, LinkButton, Flex } from 'indigo-react';
import { useHistory } from 'store/history';
import { useWallet } from 'store/wallet';
import { usePointCache } from 'store/pointCache';
import { usePointCursor } from 'store/pointCursor';
import * as need from 'lib/need';
import { ROUTE_NAMES } from 'lib/routeNames';
import { isZeroAddress, abbreviateAddress } from 'lib/wallet';
import useIsEclipticOwner from 'lib/useIsEclipticOwner';
import { useSyncKnownPoints } from 'lib/useSyncPoints';
import View from 'components/View';
import FooterButton from 'components/FooterButton';
import Blinky from 'components/Blinky';
import Passport from 'components/Passport';
import useRejectedIncomingPointTransfers from 'lib/useRejectedIncomingPointTransfers';
import pluralize from 'lib/pluralize';
const getFromMaybe = (obj, key, defaultValue) =>
obj.matchWith({
Nothing: () => defaultValue,
Just: p =>
p.value.matchWith({
Ok: r => r.value[key],
Error: e => defaultValue,
}),
});
const hasTransferProxy = (details, point) =>
!isZeroAddress(details.transferProxy);
const PointList = function({ points, className, actions, ...rest }) {
const { setPointCursor } = usePointCursor();
const history = useHistory();
return (
{points.map((point, i) => (
{
setPointCursor(Just(point));
history.push(history.names.POINT);
}}
{...rest}
/>
{actions && (
{actions(point, i)}
)}
))}
);
};
function ActionButtons({ actions = [] }) {
return (
{actions.map(action => (
{action.text}
))}
);
}
export default function Points() {
const { wallet } = useWallet();
const history = useHistory();
const { setPointCursor } = usePointCursor();
const { controlledPoints, getDetails } = usePointCache();
const isEclipticOwner = useIsEclipticOwner();
const [
rejectedPoints,
addRejectedPoint,
] = useRejectedIncomingPointTransfers();
const address = need.addressFromWallet(wallet);
const loading = Nothing.hasInstance(controlledPoints);
const ownedPoints = getFromMaybe(controlledPoints, 'ownedPoints', []);
const incomingPoints = getFromMaybe(
controlledPoints,
'incomingPoints',
[]
).filter(point => !rejectedPoints.includes(point));
const managingPoints = getFromMaybe(controlledPoints, 'managingPoints', []);
const votingPoints = getFromMaybe(controlledPoints, 'votingPoints', []);
const spawningPoints = getFromMaybe(controlledPoints, 'spawningPoints', []);
const outgoingPoints = ownedPoints.filter(point =>
getDetails(point).matchWith({
Nothing: () => false,
Just: p => hasTransferProxy(p.value),
})
);
const multipassPoints = [
...ownedPoints,
...managingPoints,
...votingPoints,
...spawningPoints,
];
// sync display details for known points
useSyncKnownPoints([
...ownedPoints,
...incomingPoints,
...managingPoints,
...votingPoints,
...spawningPoints,
]);
return (
{abbreviateAddress(address)}
{loading && (
Loading...
)}
{incomingPoints.length > 0 && (
{pluralize(
incomingPoints.length,
'Incoming Transfer',
'Incoming Transfers'
)}
(
{
setPointCursor(Just(point));
history.push(history.names.ACCEPT_TRANSFER);
},
},
{
text: 'Reject',
onClick: () => {
addRejectedPoint(point);
},
},
]}
/>
)}
inverted
/>
)}
{outgoingPoints.length > 0 && (
{pluralize(
outgoingPoints.length,
'Outgoing Transfer',
'Outgoing Transfers'
)}
(
{
setPointCursor(Just(point));
// TODO: deep linking to fix this duplicate route
history.push(history.names.CANCEL_TRANSFER);
},
},
]}
/>
)}
inverted
/>
)}
{multipassPoints.length > 0 && (
{pluralize(multipassPoints.length, 'Multipass', 'Multipasses')}
)}
{isEclipticOwner && (
history.push(ROUTE_NAMES.CREATE_GALAXY)}>
Create a galaxy
)}
);
}