Skip to content

Commit bbcdc72

Browse files
committed
useTransakLink hooks used for fetching url of windowed mode transak flow
1 parent f4473c4 commit bbcdc72

File tree

5 files changed

+28
-93
lines changed

5 files changed

+28
-93
lines changed

examples/react/src/components/Connected.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,8 @@ export const Connected = () => {
467467
const onClickAddFunds = () => {
468468
triggerAddFunds({
469469
walletAddress: address || '',
470-
provider: onRampProvider ? (onRampProvider as TransactionOnRampProvider) : TransactionOnRampProvider.transak
470+
provider: onRampProvider ? (onRampProvider as TransactionOnRampProvider) : TransactionOnRampProvider.transak,
471+
transakOnRampKind: 'default'
471472
})
472473
}
473474

packages/checkout/src/contexts/AddFundsModal.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface AddFundsSettings {
1818
provider?: TransactionOnRampProvider
1919
transakOnRampKind?: 'default' | 'windowed'
2020
cryptoAmount?: string
21+
windowedOnRampMessage?: string
2122
}
2223

2324
type AddFundsModalContext = {

packages/checkout/src/utils/transak.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { ChainId } from '@0xsequence/network'
22
import { zeroAddress } from 'viem'
33

4+
import type { AddFundsSettings } from '../contexts/AddFundsModal.js'
5+
46
export const TRANSAK_PROXY_ADDRESS = '0x4a598b7ec77b1562ad0df7dc64a162695ce4c78a'
57

68
export const getTransakLink = (
@@ -82,6 +84,7 @@ export const getTransakLinkFromSequenceApi = async (
8284

8385
const result = await response.json()
8486

87+
console.log('result', result)
8588
return result?.url
8689
} catch (error) {
8790
console.error('Error:', error)

packages/checkout/src/views/AddFunds.tsx

Lines changed: 21 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
1-
<<<<<<< HEAD
2-
import { Spinner, Text } from '@0xsequence/design-system'
3-
import { useEffect, useRef } from 'react'
4-
=======
5-
import { useProjectAccessKey } from '@0xsequence/connect'
61
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)
2+
import { useEffect, useRef } from 'react'
93

104
import { HEADER_HEIGHT } from '../constants/index.js'
115
import type { AddFundsSettings } from '../contexts/AddFundsModal.js'
126
import { useAddFundsModal } from '../hooks/index.js'
13-
<<<<<<< HEAD
147
import { 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)
188

199
const EventTypeOrderCreated = 'TRANSAK_ORDER_CREATED'
2010
const EventTypeOrderSuccessful = 'TRANSAK_ORDER_SUCCESSFUL'
@@ -27,12 +17,12 @@ export const AddFundsContent = () => {
2717

2818
export const AddFundsContentTransak = () => {
2919
const { addFundsSettings = {} as AddFundsSettings } = useAddFundsModal()
30-
<<<<<<< HEAD
3120

3221
const {
3322
data: transakLinkData,
3423
isLoading: isLoadingTransakLink,
35-
error: errorTransakLink
24+
isError: isErrorTransakLink,
25+
refetch: refetchTransakLink
3626
} = useTransakWidgetUrl({
3727
referrerDomain: window.location.origin,
3828
walletAddress: addFundsSettings.walletAddress,
@@ -43,13 +33,7 @@ export const AddFundsContentTransak = () => {
4333
defaultCryptoCurrency: addFundsSettings?.defaultCryptoCurrency || 'USDC',
4434
cryptoCurrencyList: addFundsSettings?.cryptoCurrencyList
4535
})
46-
=======
47-
const { transakApiUrl, transakApiKey, sequenceTransakApiUrl } = useEnvironmentContext()
48-
>>>>>>> 51730908 (feat(onramp): add optional Transak alternate flow with default and windowed modes)
4936
const iframeRef = useRef<HTMLIFrameElement | null>(null)
50-
const projectAccessKey = useProjectAccessKey()
51-
const [isLoading, setIsLoading] = useState(false)
52-
const [creationLinkFailed, setCreationLinkFailed] = useState(false)
5337
const { transakOnRampKind = 'default' } = addFundsSettings
5438
const isTransakOnRampKindWindowed = transakOnRampKind === 'windowed'
5539

@@ -82,71 +66,16 @@ export const AddFundsContentTransak = () => {
8266

8367
const link = transakLinkData?.url
8468

85-
if (isLoadingTransakLink) {
86-
return (
87-
<div className="flex items-center justify-center w-full px-4 pb-4 h-[200px]">
88-
<Spinner />
89-
</div>
90-
)
91-
}
92-
93-
if (errorTransakLink) {
94-
return (
95-
<div className="flex items-center justify-center w-full px-4 pb-4 h-[200px]">
96-
<Text color="text100">An error has occurred</Text>
97-
</div>
98-
)
99-
}
100-
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-
13269
useEffect(() => {
133-
if (!isTransakOnRampKindWindowed) {
134-
return
70+
if (isTransakOnRampKindWindowed && !isLoadingTransakLink && link) {
71+
window.open(link, '_blank', 'noopener')
13572
}
73+
}, [isTransakOnRampKindWindowed, isLoadingTransakLink, link])
13674

137-
handleTransakLink({ addFundsSettings, sequenceTransakApiUrl, projectAccessKey, setIsLoading, setCreationLinkFailed })
138-
}, [])
139-
140-
if (isLoading) {
75+
if (isLoadingTransakLink) {
14176
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" />
77+
<div className="flex items-center justify-center w-full px-4 pb-4 h-[200px]">
78+
<Spinner />
15079
</div>
15180
)
15281
}
@@ -160,33 +89,36 @@ export const AddFundsContentTransak = () => {
16089
paddingTop: HEADER_HEIGHT
16190
}}
16291
>
163-
{creationLinkFailed ? (
92+
{isErrorTransakLink ? (
16493
<div className="flex flex-col gap-2 items-center">
16594
<Text color="text100">The creation of the Transak link failed.</Text>
16695
<Button
16796
className="w-fit"
16897
onClick={() => {
169-
handleTransakLink({
170-
addFundsSettings,
171-
sequenceTransakApiUrl,
172-
projectAccessKey,
173-
setIsLoading,
174-
setCreationLinkFailed
175-
})
98+
// @ts-ignore-next-line
99+
refetchTransakLink()
176100
}}
177101
>
178102
Try Again
179103
</Button>
180104
</div>
181105
) : (
182106
<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>
107+
<Text color="text100">{addFundsSettings?.windowedOnRampMessage || 'Funds will be added from another window.'}</Text>
184108
</div>
185109
)}
186110
</div>
187111
)
188112
}
189113

114+
if (isErrorTransakLink) {
115+
return (
116+
<div className="flex items-center justify-center w-full px-4 pb-4 h-[200px]">
117+
<Text color="text100">An error has occurred</Text>
118+
</div>
119+
)
120+
}
121+
190122
return (
191123
<div
192124
className="flex items-center w-full px-4 pb-4 h-full"

packages/connect/src/styles.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,6 +2522,4 @@ export const styles = String.raw`
25222522
--tw-gradient-to-position: 100%;
25232523
}
25242524
}
2525-
}
2526-
2527-
`
2525+
}`

0 commit comments

Comments
 (0)