-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve insurance stability
- Loading branch information
Showing
8 changed files
with
208 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,24 @@ | ||
import VerifiedUserIcon from '@mui/icons-material/VerifiedUser'; | ||
import type { BoxProps } from '@mui/material'; | ||
import { Box, Collapse, Typography } from '@mui/material'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { InsuraceLogo } from '../../icons'; | ||
import { RouteExecutionStatus } from '../../stores'; | ||
import { Card, CardLabel, CardLabelTypography } from '../Card'; | ||
import { Switch } from '../Switch'; | ||
import { InsuranceCard } from './InsuranceCard'; | ||
import { InsuranceCollapsed } from './InsuranceCollapsed'; | ||
import type { InsuranceProps } from './types'; | ||
|
||
export const Insurance: React.FC< | ||
{ | ||
available?: boolean; | ||
insurableRouteId?: string; | ||
feeAmountUsd?: string; | ||
status?: RouteExecutionStatus; | ||
onChange?: ( | ||
event: React.ChangeEvent<HTMLInputElement>, | ||
checked: boolean, | ||
) => void; | ||
} & Omit<BoxProps, 'onChange'> | ||
> = ({ | ||
export const Insurance: React.FC<InsuranceProps> = ({ | ||
status, | ||
feeAmountUsd, | ||
insurableRouteId, | ||
available, | ||
feeAmountUsd, | ||
onChange, | ||
...props | ||
}) => { | ||
const { t } = useTranslation(); | ||
|
||
return ( | ||
<Collapse | ||
timeout={225} | ||
in={available} | ||
unmountOnExit | ||
mountOnEnter | ||
appear={status === RouteExecutionStatus.Idle} | ||
> | ||
<Card selectionColor="secondary" indented {...props}> | ||
<Box | ||
display="flex" | ||
alignItems="center" | ||
justifyContent="space-between" | ||
mb={2} | ||
> | ||
<CardLabel type={'insurance'}> | ||
<VerifiedUserIcon fontSize="inherit" /> | ||
<CardLabelTypography type="icon"> | ||
{status === RouteExecutionStatus.Idle | ||
? t(`swap.tags.insurance`) | ||
: t(`swap.tags.insured`)} | ||
</CardLabelTypography> | ||
</CardLabel> | ||
<InsuraceLogo /> | ||
</Box> | ||
<Box | ||
display="flex" | ||
alignItems="center" | ||
justifyContent="space-between" | ||
mb={2} | ||
> | ||
<Typography fontSize={24} fontWeight={700} lineHeight={1}> | ||
{t('format.currency', { | ||
value: feeAmountUsd, | ||
})} | ||
</Typography> | ||
{status === RouteExecutionStatus.Idle ? ( | ||
<Switch onChange={onChange} /> | ||
) : null} | ||
</Box> | ||
<Box display="flex" alignItems="center" justifyContent="space-between"> | ||
<Typography fontSize={12}> | ||
Get 100% coverage for lost tokens. | ||
</Typography> | ||
</Box> | ||
</Card> | ||
</Collapse> | ||
return status === RouteExecutionStatus.Idle ? ( | ||
<InsuranceCollapsed | ||
insurableRouteId={insurableRouteId} | ||
feeAmountUsd={feeAmountUsd} | ||
status={status} | ||
onChange={onChange} | ||
{...props} | ||
/> | ||
) : ( | ||
<InsuranceCard feeAmountUsd={feeAmountUsd} status={status} {...props} /> | ||
); | ||
}; |
58 changes: 58 additions & 0 deletions
58
packages/widget/src/components/Insurance/InsuranceCard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import VerifiedUserIcon from '@mui/icons-material/VerifiedUser'; | ||
import { Box, Typography } from '@mui/material'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { InsuraceLogo } from '../../icons'; | ||
import { RouteExecutionStatus } from '../../stores'; | ||
import { Card, CardLabel, CardLabelTypography } from '../Card'; | ||
import { Switch } from '../Switch'; | ||
import type { InsuranceCardProps } from './types'; | ||
|
||
export const InsuranceCard: React.FC<InsuranceCardProps> = ({ | ||
status, | ||
feeAmountUsd, | ||
onChange, | ||
...props | ||
}) => { | ||
const { t } = useTranslation(); | ||
|
||
return ( | ||
<Card selectionColor="secondary" indented {...props}> | ||
<Box | ||
display="flex" | ||
alignItems="center" | ||
justifyContent="space-between" | ||
mb={2} | ||
> | ||
<CardLabel type={'insurance'}> | ||
<VerifiedUserIcon fontSize="inherit" /> | ||
<CardLabelTypography type="icon"> | ||
{status === RouteExecutionStatus.Idle | ||
? t(`swap.tags.insurance`) | ||
: t(`swap.tags.insured`)} | ||
</CardLabelTypography> | ||
</CardLabel> | ||
<InsuraceLogo /> | ||
</Box> | ||
<Box | ||
display="flex" | ||
alignItems="center" | ||
justifyContent="space-between" | ||
mb={2} | ||
> | ||
<Typography fontSize={24} fontWeight={700} lineHeight={1}> | ||
{t('format.currency', { | ||
value: feeAmountUsd, | ||
})} | ||
</Typography> | ||
{status === RouteExecutionStatus.Idle ? ( | ||
<Switch onChange={onChange} /> | ||
) : null} | ||
</Box> | ||
<Box display="flex" alignItems="center" justifyContent="space-between"> | ||
<Typography fontSize={12}> | ||
Get 100% coverage for lost tokens. | ||
</Typography> | ||
</Box> | ||
</Card> | ||
); | ||
}; |
59 changes: 59 additions & 0 deletions
59
packages/widget/src/components/Insurance/InsuranceCollapsed.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Collapse } from '@mui/material'; | ||
import type { PropsWithChildren } from 'react'; | ||
import { useSwapRoutes } from '../../hooks'; | ||
import { | ||
RouteExecutionStatus, | ||
useRouteExecutionStore, | ||
useSetExecutableRoute, | ||
} from '../../stores'; | ||
import { InsuranceCard } from './InsuranceCard'; | ||
import type { InsuranceProps } from './types'; | ||
|
||
export const InsuranceCollapsed: React.FC< | ||
PropsWithChildren<InsuranceProps> | ||
> = ({ | ||
status, | ||
insurableRouteId, | ||
feeAmountUsd, | ||
onChange, | ||
children, | ||
...props | ||
}) => { | ||
const setExecutableRoute = useSetExecutableRoute(); | ||
const routeExecution = useRouteExecutionStore( | ||
(state) => state.routes[insurableRouteId], | ||
); | ||
const { routes } = useSwapRoutes(routeExecution?.route); | ||
|
||
const toggleInsurance = ( | ||
_: React.ChangeEvent<HTMLInputElement>, | ||
checked: boolean, | ||
) => { | ||
const insuredRoute = routes?.[0]; | ||
if (insuredRoute) { | ||
if (checked) { | ||
setExecutableRoute(insuredRoute, insurableRouteId); | ||
} | ||
onChange?.(checked ? insuredRoute.id : insurableRouteId); | ||
} | ||
}; | ||
|
||
const insuredRoute = routes?.[0]; | ||
|
||
return ( | ||
<Collapse | ||
timeout={225} | ||
in={insuredRoute?.insurance?.state === 'INSURED'} | ||
unmountOnExit | ||
mountOnEnter | ||
appear={status === RouteExecutionStatus.Idle} | ||
> | ||
<InsuranceCard | ||
feeAmountUsd={feeAmountUsd} | ||
status={status} | ||
onChange={toggleInsurance} | ||
{...props} | ||
/> | ||
</Collapse> | ||
); | ||
}; |
37 changes: 0 additions & 37 deletions
37
packages/widget/src/components/Insurance/InsuranceSwitch.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { BoxProps } from '@mui/material'; | ||
import type { RouteExecutionStatus } from '../../stores'; | ||
|
||
export interface InsuranceProps extends Omit<BoxProps, 'onChange'> { | ||
insurableRouteId: string; | ||
feeAmountUsd?: string; | ||
status?: RouteExecutionStatus; | ||
onChange?: (routeId: string) => void; | ||
} | ||
|
||
export interface InsuranceCardProps { | ||
feeAmountUsd?: string; | ||
status?: RouteExecutionStatus; | ||
onChange?: (_: React.ChangeEvent<HTMLInputElement>, checked: boolean) => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.