Skip to content

Commit

Permalink
Merge branch 'main' into fix-connect-wallet-component
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuba authored Nov 26, 2024
2 parents f984c62 + 18b5373 commit 54d5a34
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export default function BuildTransactionModal({
return (
<>
<ModalCloseButton onClick={onClose} />
<ModalHeader>Building transaction data...</ModalHeader>
<ModalHeader>Preparing withdrawal transaction...</ModalHeader>
<ModalBody>
<Spinner size="xl" variant="filled" />
<TextMd>We are building your withdrawal data.</TextMd>
<TextMd>This may take a few minutes.</TextMd>
<Button size="lg" width="100%" variant="outline" onClick={onClose}>
Cancel
</Button>
Expand Down
17 changes: 12 additions & 5 deletions dapp/src/components/TransactionModal/WalletInteractionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
ProgressProps,
} from "@chakra-ui/react"
import { AcreSignIcon } from "#/assets/icons"
import { useActionFlowType, useConnector } from "#/hooks"
import { ACTION_FLOW_TYPES } from "#/types"
import { useActionFlowType, useConnector, useIsEmbed } from "#/hooks"
import { ACTION_FLOW_TYPES, DappMode } from "#/types"
import { Alert, AlertIcon } from "../shared/Alert"
import { TextMd } from "../shared/Typography"

Expand All @@ -22,11 +22,16 @@ const ICON_STYLES = {

type WalletInteractionStep = "opening-wallet" | "awaiting-transaction"

const CONTENT_BY_DAPP_MODE: Record<DappMode, string> = {
standalone: "wallet",
"ledger-live": "Ledger Device",
}

const DATA: Record<
WalletInteractionStep,
{
header: string
description: (action: string) => string
description: (action: string, mode: DappMode) => string
progressProps?: ProgressProps
}
> = {
Expand All @@ -37,7 +42,8 @@ const DATA: Record<
},
"awaiting-transaction": {
header: "Awaiting signature confirmation",
description: () => "Waiting for your wallet to confirm the transaction.",
description: (_, mode: DappMode) =>
`Communicating with your ${CONTENT_BY_DAPP_MODE[mode]}...`,
progressProps: { transform: "scaleX(-1)" },
},
}
Expand All @@ -50,6 +56,7 @@ export default function WalletInteractionModal({
const actionType = useActionFlowType()
const connector = useConnector()
const { header, description, progressProps } = DATA[step]
const { embeddedApp } = useIsEmbed()

return (
<>
Expand Down Expand Up @@ -78,14 +85,14 @@ export default function WalletInteractionModal({
<TextMd>
{description(
actionType === ACTION_FLOW_TYPES.STAKE ? "deposit" : "withdraw",
embeddedApp ?? "standalone",
)}
</TextMd>
{step === "awaiting-transaction" && (
<Alert variant="elevated">
<AlertIcon />
<AlertDescription>
<TextMd>This may take up to a minute.</TextMd>
<TextMd>Don&apos;t close this window.</TextMd>
</AlertDescription>
</Alert>
)}
Expand Down
1 change: 0 additions & 1 deletion dapp/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export { default as useMobileMode } from "./useMobileMode"
export { default as useBitcoinRecoveryAddress } from "./useBitcoinRecoveryAddress"
export { default as useIsFetchedWalletData } from "./useIsFetchedWalletData"
export { default as useLocalStorage } from "./useLocalStorage"
export { default as useDetectReferral } from "./useDetectReferral"
export { default as useReferral } from "./useReferral"
export { default as useMats } from "./useMats"
export { default as useIsEmbed } from "./useIsEmbed"
Expand Down
10 changes: 0 additions & 10 deletions dapp/src/hooks/useDetectReferral.ts

This file was deleted.

2 changes: 0 additions & 2 deletions dapp/src/hooks/useInitApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useAccountsChangedOKX } from "./orangeKit/useAccountsChangedOKX"
import { useInitDataFromSdk, useInitializeAcreSdk } from "./sdk"
import { useSentry } from "./sentry"
import useDetectEmbed from "./useDetectEmbed"
import useDetectReferral from "./useDetectReferral"
import { useDisconnectWallet } from "./useDisconnectWallet"
import { useFetchBTCPriceUSD } from "./useFetchBTCPriceUSD"

Expand All @@ -13,7 +12,6 @@ export function useInitApp() {
// useDetectThemeMode()
useSentry()
useDetectEmbed()
useDetectReferral()
useInitializeAcreSdk()
useInitDataFromSdk()
useFetchBTCPriceUSD()
Expand Down
7 changes: 6 additions & 1 deletion dapp/src/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { useLocalStorage as useRehooksLocalStorage } from "@rehooks/local-storage"
import {
useLocalStorage as useRehooksLocalStorage,
writeStorage,
} from "@rehooks/local-storage"

export const parseLocalStorageValue = (value: string | null | undefined) => {
if (
Expand All @@ -12,6 +15,8 @@ export const parseLocalStorageValue = (value: string | null | undefined) => {
return value
}

export { writeStorage }

export const getLocalStorageItem = (key: string): string | undefined => {
const value = localStorage.getItem(key)
return parseLocalStorageValue(value)
Expand Down
48 changes: 9 additions & 39 deletions dapp/src/hooks/useReferral.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,33 @@
import { env } from "#/constants"
import { referralProgram } from "#/utils"

import { useCallback, useMemo } from "react"
import { MODAL_TYPES } from "#/types"
import useIsEmbed from "./useIsEmbed"
import useLocalStorage from "./useLocalStorage"
import { useModal } from "./useModal"
import useLocalStorage, { writeStorage } from "./useLocalStorage"

type UseReferralReturn = {
referral: number | null
detectReferral: () => void
resetReferral: () => void
}

const LOCAL_STORAGE_KEY = "acre.referral"

export const writeReferral = (value: string) => {
writeStorage(LOCAL_STORAGE_KEY, value)
}

export default function useReferral(): UseReferralReturn {
const [referral, setReferral] = useLocalStorage<number>(
"acre.referral",
LOCAL_STORAGE_KEY,
env.REFERRAL,
)
const { openModal } = useModal()
const { isEmbed, embeddedApp } = useIsEmbed()

const detectReferral = useCallback(() => {
const param = referralProgram.getReferralFromURL()

if (isEmbed && embeddedApp) {
setReferral(referralProgram.getReferralByEmbeddedApp(embeddedApp))
return
}

if (param === null) {
setReferral(env.REFERRAL)
return
}

const convertedReferral = Number(param)

if (referralProgram.isValidReferral(convertedReferral)) {
setReferral(convertedReferral)
} else {
console.error("Incorrect referral")
setReferral(null)
openModal(MODAL_TYPES.UNEXPECTED_ERROR, {
withCloseButton: false,
closeOnEsc: false,
})
}
}, [isEmbed, embeddedApp, openModal, setReferral])

const resetReferral = useCallback(() => {
setReferral(env.REFERRAL)
}, [setReferral])

return useMemo(
() => ({
detectReferral,
resetReferral,
referral,
}),
[detectReferral, resetReferral, referral],
[resetReferral, referral],
)
}
2 changes: 1 addition & 1 deletion dapp/src/pages/DashboardPage/PositionDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default function PositionDetails() {
<VStack alignItems="start" spacing={0}>
{/* TODO: Component should be moved to `CardHeader` */}
<TextMd>
Your deposit
Your balance
{/* TODO: Uncomment when position will be implemented */}
{/* {positionPercentage && (
<Tag
Expand Down
20 changes: 20 additions & 0 deletions dapp/src/router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import AccessPage from "#/pages/AccessPage"
import WelcomePage from "#/pages/WelcomePage"
import welcomePageLoader from "#/pages/WelcomePage/loader"
import accessPageLoader from "#/pages/AccessPage/loader"
import { writeReferral } from "#/hooks/useReferral"
import { env } from "#/constants"
import { routerPath } from "./path"

const mainLayoutLoader: LoaderFunction = ({ request }) => {
Expand All @@ -32,6 +34,24 @@ export const router = createBrowserRouter([
{
path: "/",
element: <Layout />,
loader: ({ request }) => {
// TODO: display the error page/modal when the referral is invalid.
const referralCodeFromUrl = referralProgram.getReferralFromURL()

const referralCode = referralProgram.isValidReferral(referralCodeFromUrl)
? referralCodeFromUrl!
: env.REFERRAL

writeReferral(referralCode.toString())

const embedApp = referralProgram.getEmbeddedApp(request.url)
if (referralProgram.isEmbedApp(embedApp)) {
writeReferral(
referralProgram.getReferralByEmbeddedApp(embedApp).toString(),
)
}
return null
},
children: [
{
index: true,
Expand Down
20 changes: 17 additions & 3 deletions dapp/src/utils/referralProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,23 @@ const EMBEDDED_APP_TO_REFERRAL: Record<EmbedApp, number> = {
"ledger-live": 2083,
}

const isValidReferral = (value: number) => {
const isInteger = Number.isInteger(value)
return isInteger && value >= 0 && value <= MAX_UINT16
const isValidReferral = (value: unknown) => {
let valueAsNumber: number | undefined

if (typeof value === "string") {
// Only digits w/o leading zeros.
const isNumber = /^(?:[1-9][0-9]*|0)$/.test(value)
valueAsNumber = isNumber ? Number(value) : undefined
} else if (typeof value === "number") {
valueAsNumber = value
}

return (
!!valueAsNumber &&
Number.isInteger(valueAsNumber) &&
valueAsNumber >= 0 &&
valueAsNumber <= MAX_UINT16
)
}

const getReferralFromURL = () =>
Expand Down

0 comments on commit 54d5a34

Please sign in to comment.