-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get referral code from the web app when onboarding invitee #618
Merged
vyorkin
merged 5 commits into
feature/referral-program
from
feature/get-referral-code-from-web-app
Oct 11, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
753d1da
Get referral code from the web app when onboarding invitee
vyorkin c73b649
Fixes
vyorkin 8330f36
Merge branch 'feature/referral-program' into feature/get-referral-cod…
vyorkin 534234d
Remove cacheTime: 0, add staleTime: 10000
vyorkin 980586f
Fixes
vyorkin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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,18 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { walletPort } from 'src/ui/shared/channels'; | ||
import type { RemoteConfig } from './types'; | ||
|
||
export function useRemoteConfigValue<K extends keyof RemoteConfig>(key: K) { | ||
return useQuery({ | ||
queryKey: ['wallet/getRemoteConfigValue', key], | ||
queryFn: async () => { | ||
const value = await walletPort.request('getRemoteConfigValue', { key }); | ||
return value as RemoteConfig[K]; | ||
}, | ||
useErrorBoundary: false, | ||
staleTime: 10000, | ||
suspense: false, | ||
refetchOnMount: false, | ||
refetchOnWindowFocus: false, | ||
}); | ||
} |
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,17 +1,23 @@ | ||
import type { ClientOptions } from '../shared'; | ||
import { ZerionHttpClient } from '../shared'; | ||
|
||
interface Params { | ||
referralCode: string; | ||
} | ||
|
||
export interface ReferrerData { | ||
referralCode: string; | ||
address: string | null; | ||
handle: string | null; | ||
} | ||
|
||
interface Response { | ||
data: null; | ||
data: ReferrerData; | ||
errors?: { title: string; detail: string }[]; | ||
} | ||
|
||
export function checkReferral(params: Params) { | ||
return ZerionHttpClient.post<Response>({ | ||
endpoint: 'wallet/check-referral/v1', | ||
body: JSON.stringify({ referralCode: params.referralCode }), | ||
}); | ||
export function checkReferral(payload: Params, options?: ClientOptions) { | ||
const params = new URLSearchParams({ referralCode: payload.referralCode }); | ||
const endpoint = `wallet/check-referral/v1?${params}`; | ||
return ZerionHttpClient.get<Response>({ endpoint, ...options }); | ||
} |
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 |
---|---|---|
|
@@ -13,7 +13,11 @@ import JigsawIcon from 'jsx:./assets/jigsaw.svg'; | |
import coinImgSrc from 'src/ui/assets/zer_coin.png'; | ||
import sparkImgSrc from 'src/ui/assets/zer_spark.png'; | ||
import starImgSrc from 'src/ui/assets/zer_star.png'; | ||
import { useRemoteConfigValue } from 'src/modules/remote-config/useRemoteConfigValue'; | ||
import * as styles from './styles.module.css'; | ||
import { WebAppMessageHandler } from './WebAppMessageHandler'; | ||
|
||
const DEBUG_INVITEE_FLOW = false; | ||
|
||
export function Success() { | ||
const { isNarrowView } = useWindowSizeStore(); | ||
|
@@ -107,8 +111,20 @@ export function Success() { | |
}, | ||
}); | ||
|
||
const { | ||
data: referralProgramEnabled, | ||
isLoading: isLoadingRemoteConfigValue, | ||
} = useRemoteConfigValue('extension_referral_program'); | ||
|
||
if (isLoadingRemoteConfigValue) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we use isLoading, we can set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, updated ✅ |
||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
{referralProgramEnabled || DEBUG_INVITEE_FLOW ? ( | ||
<WebAppMessageHandler pathname="/get-referral-code" /> | ||
) : null} | ||
<canvas | ||
ref={confettiRef} | ||
style={{ | ||
|
81 changes: 81 additions & 0 deletions
81
src/ui/features/onboarding/Success/WebAppMessageHandler.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,81 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
import { ZerionAPI } from 'src/modules/zerion-api/zerion-api.client'; | ||
import { invariant } from 'src/shared/invariant'; | ||
import { isObj } from 'src/shared/isObj'; | ||
|
||
const ZERION_WEB_APP_URL = new URL('https://app.zerion.io'); | ||
|
||
type WebAppCallbackMethod = 'set-referral-code'; | ||
|
||
interface WebAppMessage { | ||
method: WebAppCallbackMethod; | ||
params?: unknown; | ||
} | ||
|
||
export function isWebAppMessage( | ||
event: MessageEvent, | ||
{ | ||
expectedSource, | ||
}: { | ||
expectedSource: Window | null; | ||
} | ||
): event is MessageEvent<WebAppMessage> { | ||
return ( | ||
event.origin === ZERION_WEB_APP_URL.origin && | ||
event.source === expectedSource && | ||
isObj(event.data) && | ||
'method' in event.data | ||
); | ||
} | ||
|
||
async function setReferralCode(referralCode: string) { | ||
const response = await ZerionAPI.checkReferral({ referralCode }); | ||
// @ts-ignore | ||
const checkedReferrer = response.data; | ||
// await saveReferrer(checkedReferrer); | ||
} | ||
|
||
async function handleMessage({ | ||
event, | ||
expectedSource, | ||
}: { | ||
event: MessageEvent; | ||
expectedSource: Window | null; | ||
}) { | ||
if (!isWebAppMessage(event, { expectedSource })) return; | ||
const { method, params } = event.data; | ||
|
||
if (method === 'set-referral-code') { | ||
invariant( | ||
isObj(params) && typeof params.referralCode === 'string', | ||
'Invalid payload for set-referral-code web app message' | ||
); | ||
|
||
await setReferralCode(params.referralCode); | ||
} | ||
} | ||
|
||
export function WebAppMessageHandler({ pathname }: { pathname: string }) { | ||
const iframeRef = useRef<HTMLIFrameElement | null>(null); | ||
const iframeUrl = new URL(pathname, ZERION_WEB_APP_URL); | ||
|
||
useEffect(() => { | ||
invariant(iframeRef.current, 'Iframe should be mounted'); | ||
const webAppWindow = iframeRef.current.contentWindow; | ||
|
||
const handler = (event: MessageEvent) => | ||
handleMessage({ event, expectedSource: webAppWindow }); | ||
|
||
window.addEventListener('message', handler); | ||
return () => window.removeEventListener('message', handler); | ||
}, []); | ||
|
||
return ( | ||
<iframe | ||
sandbox="allow-same-origin allow-scripts" | ||
ref={iframeRef} | ||
src={iframeUrl.toString()} | ||
hidden={true} | ||
/> | ||
); | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to provide
suspense
here?Hope this will not lead to ui blinking
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ Yep, let's have
suspense: false