1+ < < << << < HEAD
12import { Spinner , Text } from '@0xsequence/design-system'
23import { useEffect , useRef } from 'react'
4+ = === ===
5+ import { useProjectAccessKey } from '@0xsequence/connect'
6+ import { Button , Spinner , Text } from '@0xsequence/design-system'
7+ import { useEffect , useRef , useState } from 'react'
8+ > >>> >>> 51730908 ( feat ( onramp ) : add optional Transak alternate flow with default and windowed modes )
39
410import { HEADER_HEIGHT } from '../constants/index.js'
511import type { AddFundsSettings } from '../contexts/AddFundsModal.js'
612import { useAddFundsModal } from '../hooks/index.js'
13+ < << << << HEAD
714import { useTransakWidgetUrl } from '../hooks/useTransakWidgetUrl.js'
15+ = === ===
16+ import { getTransakLink , getTransakLinkFromSequenceApi } from '../utils/transak.js'
17+ > >>> >>> 51730908 ( feat ( onramp ) : add optional Transak alternate flow with default and windowed modes )
818
919const EventTypeOrderCreated = 'TRANSAK_ORDER_CREATED'
1020const EventTypeOrderSuccessful = 'TRANSAK_ORDER_SUCCESSFUL'
@@ -17,6 +27,7 @@ export const AddFundsContent = () => {
1727
1828export const AddFundsContentTransak = ( ) => {
1929 const { addFundsSettings = { } as AddFundsSettings } = useAddFundsModal ( )
30+ << < < << < HEAD
2031
2132 const {
2233 data : transakLinkData ,
@@ -32,7 +43,15 @@ export const AddFundsContentTransak = () => {
3243 defaultCryptoCurrency : addFundsSettings ?. defaultCryptoCurrency || 'USDC' ,
3344 cryptoCurrencyList : addFundsSettings ?. cryptoCurrencyList
3445 } )
46+ === = ===
47+ const { transakApiUrl, transakApiKey, sequenceTransakApiUrl } = useEnvironmentContext ( )
48+ >>> > >>> 51730908 ( feat ( onramp ) : add optional Transak alternate flow with default and windowed modes )
3549 const iframeRef = useRef < HTMLIFrameElement | null > ( null )
50+ const projectAccessKey = useProjectAccessKey ( )
51+ const [ isLoading , setIsLoading ] = useState ( false )
52+ const [ creationLinkFailed , setCreationLinkFailed ] = useState ( false )
53+ const { transakOnRampKind = 'default' } = addFundsSettings
54+ const isTransakOnRampKindWindowed = transakOnRampKind === 'windowed'
3655
3756 useEffect ( ( ) => {
3857 const handleMessage = ( message : MessageEvent < any > ) => {
@@ -79,6 +98,95 @@ export const AddFundsContentTransak = () => {
7998 )
8099 }
81100
101+ async function handleTransakLink ( {
102+ addFundsSettings,
103+ sequenceTransakApiUrl,
104+ projectAccessKey,
105+ setCreationLinkFailed,
106+ setIsLoading
107+ } : {
108+ addFundsSettings : AddFundsSettings
109+ sequenceTransakApiUrl : string
110+ projectAccessKey : string
111+ setCreationLinkFailed : ( value : boolean ) = > void
112+ setIsLoading : ( value : boolean ) = > void
113+ } ) {
114+ try {
115+ setCreationLinkFailed ( false )
116+ setIsLoading ( true )
117+ const link = await getTransakLinkFromSequenceApi ( addFundsSettings , sequenceTransakApiUrl , projectAccessKey )
118+
119+ if ( link ) {
120+ window . open ( link , '_blank' )
121+ } else {
122+ setCreationLinkFailed ( true )
123+ }
124+ setIsLoading ( false )
125+ } catch ( error ) {
126+ console . error ( `The creation of the Transak link has failed. Error: ` , error )
127+ setCreationLinkFailed ( true )
128+ setIsLoading ( false )
129+ }
130+ }
131+
132+ useEffect ( ( ) => {
133+ if ( ! isTransakOnRampKindWindowed ) {
134+ return
135+ }
136+
137+ handleTransakLink ( { addFundsSettings, sequenceTransakApiUrl, projectAccessKey, setIsLoading, setCreationLinkFailed } )
138+ } , [ ] )
139+
140+ if ( isLoading ) {
141+ return (
142+ < div
143+ className = "flex items-center justify-center w-full px-4 pb-4 h-full"
144+ style = { {
145+ height : '600px' ,
146+ paddingTop : HEADER_HEIGHT
147+ } }
148+ >
149+ < Spinner size = "lg" />
150+ </ div >
151+ )
152+ }
153+
154+ if ( isTransakOnRampKindWindowed ) {
155+ return (
156+ < div
157+ className = "flex items-center justify-center w-full px-4 pb-4 h-full"
158+ style = { {
159+ height : '600px' ,
160+ paddingTop : HEADER_HEIGHT
161+ } }
162+ >
163+ { creationLinkFailed ? (
164+ < div className = "flex flex-col gap-2 items-center" >
165+ < Text color = "text100" > The creation of the Transak link failed.</ Text >
166+ < Button
167+ className = "w-fit"
168+ onClick = { ( ) => {
169+ handleTransakLink ( {
170+ addFundsSettings,
171+ sequenceTransakApiUrl,
172+ projectAccessKey,
173+ setIsLoading,
174+ setCreationLinkFailed
175+ } )
176+ } }
177+ >
178+ Try Again
179+ </ Button >
180+ </ div >
181+ ) : (
182+ < div className = "flex gap-2 items-center text-center" >
183+ < Text color = "text100" > Once you've added funds, you can close this window and try buying with crypto again.</ Text >
184+ </ div >
185+ ) }
186+ </ div >
187+ )
188+ }
189+
82190 return (
83191 < div
84192 className = "flex items-center w-full px-4 pb-4 h-full"
0 commit comments