-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix-connect-wallet-component
- Loading branch information
Showing
10 changed files
with
67 additions
and
64 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
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
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