-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(upgrade-account-modal): able to withdraw account
- Loading branch information
Showing
32 changed files
with
1,063 additions
and
92 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
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
47 changes: 47 additions & 0 deletions
47
packages/connect-kit/src/hooks/use-withdraw-email-account.ts
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,47 @@ | ||
import { useAccountState } from "@crossbell/connect-kit"; | ||
import { useMutation, UseMutationOptions } from "@tanstack/react-query"; | ||
import { useContract } from "@crossbell/contract"; | ||
import { indexer } from "@crossbell/indexer"; | ||
import { isAddressEqual } from "@crossbell/util-ethers"; | ||
|
||
import { getWithdrawProof } from "../apis"; | ||
import { asyncRetry } from "../utils"; | ||
|
||
export function useWithdrawEmailAccount(options?: UseMutationOptions) { | ||
const account = useAccountState(); | ||
const contract = useContract(); | ||
|
||
const mutation = useMutation(async () => { | ||
const { wallet, email } = account; | ||
|
||
if (!wallet || !email) return; | ||
|
||
const { nonce, expires, proof } = await getWithdrawProof({ | ||
token: email.token, | ||
}); | ||
|
||
await contract.withdrawCharacterFromNewbieVilla( | ||
wallet.address, | ||
email.characterId, | ||
nonce, | ||
expires, | ||
proof | ||
); | ||
|
||
const character = await asyncRetry(async (RETRY) => { | ||
const character = await indexer.getCharacter(email.characterId); | ||
|
||
return isAddressEqual(character?.owner, wallet.address) | ||
? character! | ||
: RETRY; | ||
}); | ||
|
||
await account.refreshWallet(); | ||
|
||
account.switchCharacter(character); | ||
|
||
await account.disconnectEmail(); | ||
}, options); | ||
|
||
return { ...mutation, account }; | ||
} |
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
46 changes: 46 additions & 0 deletions
46
...es/connect-kit/src/modals/upgrade-account-modal/scenes/confirm-upgrade/confirm.module.css
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,46 @@ | ||
@media (min-width: 768px) { | ||
.container { | ||
width: 360px; | ||
} | ||
} | ||
|
||
.main { | ||
padding: 0 24px 24px; | ||
} | ||
|
||
.tips { | ||
font-size: 14px; | ||
color: #999; | ||
text-align: center; | ||
margin: 10px 0 20px; | ||
} | ||
|
||
.walletIcon { | ||
width: 24px; | ||
height: 24px; | ||
} | ||
|
||
.address { | ||
padding: 10px 16px; | ||
font-size: 14px; | ||
background: rgba(28, 27, 31, 0.04); | ||
color: rgba(28, 27, 31, 0.38); | ||
border-radius: 12px; | ||
} | ||
|
||
.actions { | ||
display: flex; | ||
justify-content: space-between; | ||
gap: 12px; | ||
margin-top: 48px; | ||
} | ||
|
||
.actions button:first-of-type { | ||
background: #f6f7f9; | ||
color: #000; | ||
} | ||
|
||
.actions button { | ||
flex: 1; | ||
height: 48px; | ||
} |
48 changes: 48 additions & 0 deletions
48
packages/connect-kit/src/modals/upgrade-account-modal/scenes/confirm-upgrade/confirm.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,48 @@ | ||
import React from "react"; | ||
|
||
import { useAccountState } from "../../../../hooks"; | ||
import { Field, WalletIcon, ActionBtn } from "../../../../components"; | ||
|
||
import { useUpgradeAccountModal } from "../../stores"; | ||
import { Header } from "../../components/header"; | ||
import styles from "./confirm.module.css"; | ||
|
||
export type ConfirmProps = { | ||
onConfirm: () => void; | ||
}; | ||
|
||
export function Confirm({ onConfirm }: ConfirmProps) { | ||
const address = useAccountState((s) => s.wallet?.address); | ||
const { hide } = useUpgradeAccountModal(); | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<Header title="Upgrade Account" /> | ||
|
||
<div className={styles.main}> | ||
<p className={styles.tips}> | ||
Please note that once you upgrade, you'll no longer be able to access | ||
your email account. | ||
<br /> | ||
Are you sure you want to continue? | ||
</p> | ||
|
||
<Field | ||
title="Confirm Wallet Address" | ||
icon={<WalletIcon className={styles.walletIcon} />} | ||
> | ||
<p className={styles.address}>{address}</p> | ||
</Field> | ||
|
||
<div className={styles.actions}> | ||
<ActionBtn onClick={hide} size="md"> | ||
Remain | ||
</ActionBtn> | ||
<ActionBtn color="green" onClick={onConfirm} size="md"> | ||
Upgrade Now | ||
</ActionBtn> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
80 changes: 80 additions & 0 deletions
80
packages/connect-kit/src/modals/upgrade-account-modal/scenes/confirm-upgrade/index.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,80 @@ | ||
import React from "react"; | ||
import { utils } from "ethers"; | ||
import { LoadingOverlay, useUrlComposer } from "@crossbell/ui"; | ||
import { useRefCallback } from "@crossbell/util-hooks"; | ||
|
||
import { WalletClaimCSB } from "../../../../scenes"; | ||
import { | ||
useAccountState, | ||
useWalletAccountBalance, | ||
useWithdrawEmailAccount, | ||
} from "../../../../hooks"; | ||
import { Header } from "../../components/header"; | ||
import { useScenesStore, useUpgradeAccountModal } from "../../stores"; | ||
import { SceneKind } from "../../types"; | ||
import { Confirm } from "./confirm"; | ||
|
||
export type ConfirmUpgradeProps = { | ||
scene: "claim-csb" | "confirm"; | ||
}; | ||
|
||
export function ConfirmUpgrade({ scene }: ConfirmUpgradeProps) { | ||
const [goTo, updateLast] = useScenesStore((s) => [s.goTo, s.updateLast]); | ||
const { hide: hideModal } = useUpgradeAccountModal(); | ||
const urlComposer = useUrlComposer(); | ||
const { balance } = useWalletAccountBalance(); | ||
|
||
const { | ||
account, | ||
mutate: withdraw, | ||
isLoading, | ||
} = useWithdrawEmailAccount({ | ||
onSuccess() { | ||
const character = useAccountState.getState().computed.account?.character; | ||
|
||
goTo({ | ||
kind: SceneKind.congrats, | ||
title: "Congrats!", | ||
desc: "Now you can return into the feed and enjoy Crossbell.", | ||
tips: "Welcome to new Crossbell", | ||
timeout: "15s", | ||
btnText: character ? "Check Character" : "Close", | ||
onClose: hideModal, | ||
onClickBtn: () => { | ||
if (character) { | ||
window.open(urlComposer.characterUrl(character), "_blank"); | ||
} | ||
hideModal(); | ||
}, | ||
}); | ||
}, | ||
}); | ||
|
||
const handleConfirm = useRefCallback(() => { | ||
if (!account.wallet || !account.email || isLoading) return; | ||
|
||
const hasEnoughCSB = !!balance?.value.gte(utils.parseEther("0.001")); | ||
|
||
if (hasEnoughCSB) { | ||
withdraw(); | ||
} else { | ||
updateLast({ kind: SceneKind.confirmUpgrade, scene: "claim-csb" }); | ||
} | ||
}); | ||
|
||
return ( | ||
<> | ||
{scene === "confirm" && <Confirm onConfirm={handleConfirm} />} | ||
|
||
{scene === "claim-csb" && ( | ||
<WalletClaimCSB | ||
Header={Header} | ||
onSuccess={withdraw} | ||
claimBtnText="Finish" | ||
/> | ||
)} | ||
|
||
<LoadingOverlay visible={isLoading}>Upgrading...</LoadingOverlay> | ||
</> | ||
); | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/connect-kit/src/modals/upgrade-account-modal/scenes/connect-wallet/index.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,22 @@ | ||
import React from "react"; | ||
import { useRefCallback } from "@crossbell/util-hooks"; | ||
|
||
import { | ||
ConnectWallet as Main, | ||
ConnectWalletProps as Props, | ||
} from "../../../../scenes"; | ||
import { Header } from "../../components/header"; | ||
import { useScenesStore } from "../../stores"; | ||
import { SceneKind } from "../../types"; | ||
|
||
export type ConnectWalletProps = Omit<Props, "Header" | "onConnect">; | ||
|
||
export function ConnectWallet({ wallet }: ConnectWalletProps) { | ||
const updateLast = useScenesStore((s) => s.updateLast); | ||
|
||
const handleConnect = useRefCallback(() => { | ||
updateLast({ kind: SceneKind.confirmUpgrade, scene: "confirm" }); | ||
}); | ||
|
||
return <Main onConnect={handleConnect} wallet={wallet} Header={Header} />; | ||
} |
Oops, something went wrong.