-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FRE-1228] refactor swap execution page to separate logic and fix pen…
…ding screen (#475) Co-authored-by: Nur Fikri <[email protected]>
- Loading branch information
1 parent
1c63da1
commit b9f17e6
Showing
8 changed files
with
266 additions
and
201 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@skip-go/widget': patch | ||
--- | ||
|
||
fix timeout page freeze |
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
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
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
139 changes: 139 additions & 0 deletions
139
packages/widget/src/pages/SwapExecutionPage/SwapExecutionButton.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,139 @@ | ||
// SwapExecutionButton.tsx | ||
import { MainButton } from "@/components/MainButton"; | ||
import { ICONS } from "@/icons"; | ||
import { SwapExecutionState } from "./SwapExecutionPage"; | ||
import { useTheme } from "styled-components"; | ||
import pluralize from "pluralize"; | ||
import { convertSecondsToMinutesOrHours } from "@/utils/number"; | ||
import { useSetAtom } from "jotai"; | ||
import { clearAssetInputAmountsAtom } from "@/state/swapPage"; | ||
import { currentPageAtom, Routes } from "@/state/router"; | ||
import { errorAtom, ErrorType } from "@/state/errorPage"; | ||
import NiceModal from "@ebay/nice-modal-react"; | ||
import { Modals } from "@/modals/registerModals"; | ||
import { RouteResponse } from "@skip-go/client"; | ||
import { ClientOperation } from "@/utils/clientType"; | ||
|
||
interface SwapExecutionButtonProps { | ||
swapExecutionState: SwapExecutionState | undefined; | ||
route: RouteResponse | undefined; | ||
signaturesRemaining: number; | ||
lastOperation: ClientOperation; | ||
connectRequiredChains: (openModal?: boolean) => Promise<void>; | ||
submitExecuteRouteMutation: () => void; | ||
} | ||
|
||
export const SwapExecutionButton: React.FC<SwapExecutionButtonProps> = ({ | ||
swapExecutionState, | ||
route, | ||
signaturesRemaining, | ||
lastOperation, | ||
connectRequiredChains, | ||
submitExecuteRouteMutation, | ||
}) => { | ||
const theme = useTheme(); | ||
const setError = useSetAtom(errorAtom); | ||
const setCurrentPage = useSetAtom(currentPageAtom); | ||
const clearAssetInputAmounts = useSetAtom(clearAssetInputAmountsAtom); | ||
|
||
switch (swapExecutionState) { | ||
case SwapExecutionState.recoveryAddressUnset: | ||
return ( | ||
<MainButton | ||
label="Set recovery address" | ||
icon={ICONS.rightArrow} | ||
onClick={() => { | ||
connectRequiredChains(true); | ||
}} | ||
/> | ||
); | ||
case SwapExecutionState.destinationAddressUnset: | ||
return ( | ||
<MainButton | ||
label="Set destination address" | ||
icon={ICONS.rightArrow} | ||
onClick={() => { | ||
const destinationChainID = route?.destAssetChainID; | ||
if (!destinationChainID) return; | ||
NiceModal.show(Modals.SetAddressModal, { | ||
signRequired: lastOperation.signRequired, | ||
chainId: destinationChainID, | ||
}); | ||
}} | ||
/> | ||
); | ||
case SwapExecutionState.ready: { | ||
const onClickConfirmSwap = () => { | ||
if (route?.txsRequired && route.txsRequired > 1) { | ||
setError({ | ||
errorType: ErrorType.AdditionalSigningRequired, | ||
onClickContinue: () => submitExecuteRouteMutation(), | ||
signaturesRequired: route.txsRequired, | ||
}); | ||
return; | ||
} | ||
submitExecuteRouteMutation(); | ||
}; | ||
return ( | ||
<MainButton | ||
label="Confirm swap" | ||
icon={ICONS.rightArrow} | ||
onClick={onClickConfirmSwap} | ||
/> | ||
); | ||
} | ||
case SwapExecutionState.validatingGasBalance: | ||
return ( | ||
<MainButton | ||
label="Validating gas and balance" | ||
icon={ICONS.rightArrow} | ||
loading | ||
/> | ||
); | ||
case SwapExecutionState.waitingForSigning: | ||
return ( | ||
<MainButton label="Confirm swap" icon={ICONS.rightArrow} loading /> | ||
); | ||
case SwapExecutionState.approving: | ||
return ( | ||
<MainButton label="Approving allowance" icon={ICONS.rightArrow} loading /> | ||
); | ||
case SwapExecutionState.pending: | ||
return ( | ||
<MainButton | ||
label="Swap in progress" | ||
loading | ||
loadingTimeString={convertSecondsToMinutesOrHours( | ||
route?.estimatedRouteDurationSeconds | ||
)} | ||
/> | ||
); | ||
case SwapExecutionState.signaturesRemaining: | ||
return ( | ||
<MainButton | ||
label={`${signaturesRemaining} ${pluralize( | ||
"signature", | ||
signaturesRemaining | ||
)} ${signaturesRemaining > 1 ? "are" : "is"} still required`} | ||
loading | ||
loadingTimeString={convertSecondsToMinutesOrHours( | ||
route?.estimatedRouteDurationSeconds | ||
)} | ||
/> | ||
); | ||
case SwapExecutionState.confirmed: | ||
return ( | ||
<MainButton | ||
label="Swap again" | ||
icon={ICONS.checkmark} | ||
backgroundColor={theme.success.text} | ||
onClick={() => { | ||
clearAssetInputAmounts(); | ||
setCurrentPage(Routes.SwapPage); | ||
}} | ||
/> | ||
); | ||
default: | ||
return null; | ||
} | ||
}; |
Oops, something went wrong.