-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the referral mechanism (#886)
Closes: #876 Detect the referral code in the main router loader instead of the hooks. Using a router loader is simpler and we avoid race between two hooks `useDetectEmbed` and `useDetectReferral`. We noticed a bug with hooks implementation - `acre.referral` set to `0` for Ledger Live after clearing local storage. The `acre.referral` key should always be set to `2083` value for users using Acre in Ledger Live.
- Loading branch information
Showing
7 changed files
with
52 additions
and
56 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 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
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
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], | ||
) | ||
} |
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