Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add success state to OperatorAddressMappingCard #261

Merged
merged 9 commits into from
Oct 26, 2022
109 changes: 90 additions & 19 deletions src/pages/Staking/OperatorAddressMappingCard.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
import { Icon } from "@chakra-ui/icons"
import {
Alert,
AlertIcon,
Badge,
BodyMd,
BodyXs,
Box,
BoxLabel,
Button,
Card,
HStack,
LabelSm,
LineDivider,
Tooltip,
useColorModeValue,
} from "@threshold-network/components"
import { FC } from "react"
import { ModalType } from "../../enums"
import { useModal } from "../../hooks/useModal"
import shortenAddress from "../../utils/shortenAddress"
import { isAddressZero } from "../../web3/utils"
import { FcCheckmark, FiLink2 } from "react-icons/all"
import { MappedOperatorsForStakingProvider } from "../../threshold-ts/mas"

interface AppLabels {
tbtc: string
randomBeacon: string
}

const OperatorAddressMappingCard: FC<{
mappedOperatorTbtc: string
mappedOperatorRandomBeacon: string
}> = ({ mappedOperatorTbtc, mappedOperatorRandomBeacon }) => {
stakingProvider: string
mappedOperators: MappedOperatorsForStakingProvider
}> = ({ stakingProvider, mappedOperators }) => {
const { tbtc: mappedOperatorTbtc, randomBeacon: mappedOperatorRandomBeacon } =
mappedOperators

const appLabels: AppLabels = {
tbtc: "tBTC",
randomBeacon: "Random Beacon",
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use a function from ea991e2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d818bfa


const { openModal } = useModal()
const isOperatorMappedOnlyInTbtc =
!isAddressZero(mappedOperatorTbtc) &&
Expand All @@ -29,6 +52,10 @@ const OperatorAddressMappingCard: FC<{
const isOneOfTheAppsNotMapped =
isOperatorMappedOnlyInRandomBeacon || isOperatorMappedOnlyInTbtc

const shoudlDisplaySuccessState =
!isAddressZero(mappedOperatorTbtc) &&
!isAddressZero(mappedOperatorRandomBeacon)

const onStartMappingClick = () => {
openModal(ModalType.MapOperatorToStakingProvider, {
mappedOperatorTbtc,
Expand All @@ -44,22 +71,66 @@ const OperatorAddressMappingCard: FC<{
Node operators only
</Badge>
</HStack>
<Alert
status={isOneOfTheAppsNotMapped ? "error" : "warning"}
alignItems={"center"}
mt={5}
p={"8px 10px"}
>
<AlertIcon h={"14px"} as={"div"} alignSelf="auto" />
<BodyXs>
{isOneOfTheAppsNotMapped
? "One application from the tBTC + Random Beacon Rewards Bundle Suite requires the Operator Address mapped."
: "This section is for Staking Providers and self-operating stakers only. Map your Operator Address to your Provider Address for a better support for your hardware wallet in the node setup."}
</BodyXs>
</Alert>
<Button size="lg" w="100%" mt="5" onClick={onStartMappingClick}>
Start Mapping
</Button>
{shoudlDisplaySuccessState ? (
Object.entries(mappedOperators).map(([appName, operator]) => {
return (
<Box key={`mapped_operator_${appName}_${operator}`}>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can get rid of the Box component and use Hstack directly. Also adding the divider prop to the HStack we can get rid of the logic for rendering the line divider, I mean this code snippet:

 {Object.keys(mappedOperators)[
                Object.keys(mappedOperators).length - 1
              ] !== appName && <LineDivider />}

<HStack mt={5}>
<BoxLabel status="secondary" size={"sm"}>
{appLabels[appName as keyof AppLabels]} App
</BoxLabel>
<Badge variant="subtle" size={"md"} bgColor={"green.50"} py={1}>
<HStack>
<Icon as={FcCheckmark} />{" "}
<LabelSm color={"green.500"}>Operator Mapped</LabelSm>
</HStack>
</Badge>
</HStack>
<HStack justify="space-between" mt={5}>
<BoxLabel status="primary" size={"sm"}>
Provider
</BoxLabel>
<BodyMd color={useColorModeValue("brand.500", "brand.550")}>
<Tooltip label={`Staking provider`}>
{shortenAddress(stakingProvider)}
</Tooltip>
</BodyMd>
<Icon color="gray.500" boxSize="16px" as={FiLink2} />
<BoxLabel status="secondary" size={"sm"}>
Operator
</BoxLabel>
<BodyMd color={useColorModeValue("gray.500", "gray.300")}>
<Tooltip label={`Operator`}>
{shortenAddress(operator)}
</Tooltip>
</BodyMd>
</HStack>
{Object.keys(mappedOperators)[
Object.keys(mappedOperators).length - 1
] !== appName && <LineDivider />}
</Box>
)
})
) : (
<>
<Alert
status={isOneOfTheAppsNotMapped ? "error" : "warning"}
alignItems={"center"}
mt={5}
p={"8px 10px"}
>
<AlertIcon h={"14px"} as={"div"} alignSelf="auto" />
<BodyXs>
{isOneOfTheAppsNotMapped
? "One application from the tBTC + Random Beacon Rewards Bundle Suite requires the Operator Address mapped."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that this matches designs, but I think it might be more clear to tell the user which app needs to be mapped. wdyt? worth bringing up?

: "This section is for Staking Providers and self-operating stakers only. Map your Operator Address to your Provider Address for a better support for your hardware wallet in the node setup."}
</BodyXs>
</Alert>
<Button size="lg" w="100%" mt="5" onClick={onStartMappingClick}>
Start Mapping
</Button>
</>
)}
</Card>
)
}
Expand Down
18 changes: 15 additions & 3 deletions src/pages/Staking/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const StakingPage: PageComponent = (props) => {
const hasStakes = stakes.length > 0

const {
address,
isStakingProvider,
operatorMapping: {
isInitialFetchDone: isOperatorMappingInitialFetchDone,
Expand All @@ -70,13 +71,14 @@ const StakingPage: PageComponent = (props) => {
>
Your Stake
</H4>
{isStakingProvider &&
{address &&
isStakingProvider &&
isOperatorMappingInitialFetchDone &&
(isAddressZero(mappedOperators.tbtc) ||
isAddressZero(mappedOperators.randomBeacon)) && (
<OperatorAddressMappingCard
mappedOperatorTbtc={mappedOperators.tbtc}
mappedOperatorRandomBeacon={mappedOperators.randomBeacon}
stakingProvider={address}
mappedOperators={mappedOperators}
/>
)}
{hasStakes ? (
Expand All @@ -86,6 +88,16 @@ const StakingPage: PageComponent = (props) => {
) : (
<NewStakeCard />
)}
{address &&
isStakingProvider &&
isOperatorMappingInitialFetchDone &&
!isAddressZero(mappedOperators.tbtc) &&
!isAddressZero(mappedOperators.randomBeacon) && (
<OperatorAddressMappingCard
stakingProvider={address}
mappedOperators={mappedOperators}
/>
)}
</VStack>

<VStack w={"100%"} spacing={5}>
Expand Down