11import { useMemo } from 'react'
2- import { rgbToHex , useTheme } from '@mui/material'
2+ import { rgbToHex , useTheme , type Theme } from '@mui/material'
33import { TRANSAK_API_KEY_PRODUCTION , TRANSAK_API_KEY_STAGING } from '../constants.js'
44import { formatEthereumAddress } from '@masknet/web3-shared-evm'
55import type { TransakConfig } from '../types.js'
@@ -23,20 +23,30 @@ const DEFAULT_PARAMETERS: TransakConfig = {
2323 excludeFiatCurrencies : 'KRW' ,
2424}
2525
26+ // Query params shared by the legacy direct URL and the session proxy.
27+ export function buildTransakSearchParams ( config ?: Partial < TransakConfig > , theme ?: Theme ) : URLSearchParams {
28+ const config_ : TransakConfig = {
29+ ...DEFAULT_PARAMETERS ,
30+ referrerDomain : location . origin ,
31+ themeColor : theme ? rgbToHex ( theme . palette . maskColor . dark ) . slice ( 1 ) : undefined ,
32+ exchangeScreenTitle :
33+ config ?. walletAddress ? `Buy Crypto to ${ formatEthereumAddress ( config . walletAddress , 4 ) } ` : undefined ,
34+ ...config ,
35+ }
36+ const params = new URLSearchParams ( )
37+ Object . entries ( config_ ) . forEach ( ( [ key , value ] ) => {
38+ if ( value === undefined || value === null ) return
39+ params . append ( key , String ( value ) )
40+ } )
41+ return params
42+ }
43+
2644export function useTransakURL ( config ?: Partial < TransakConfig > ) {
2745 const theme = useTheme ( )
28- const search = useMemo ( ( ) => {
29- const config_ : TransakConfig = {
30- ...DEFAULT_PARAMETERS ,
31- themeColor : rgbToHex ( theme . palette . maskColor . dark ) . slice ( 1 ) ,
32- exchangeScreenTitle :
33- config ?. walletAddress ? `Buy Crypto to ${ formatEthereumAddress ( config . walletAddress , 4 ) } ` : void 0 ,
34- ...config ,
35- }
36- const params = new URLSearchParams ( )
37- Object . entries ( config_ ) . forEach ( ( [ key , value = '' ] ) => params . append ( key , String ( value ) ) )
38- return params . toString ( )
46+ const search = useMemo (
47+ ( ) => buildTransakSearchParams ( config , theme ) . toString ( ) ,
3948 // eslint-disable-next-line react-compiler/react-compiler
40- } , [ theme . palette . primary . main , JSON . stringify ( config ) ] )
49+ [ theme . palette . primary . main , JSON . stringify ( config ) ] ,
50+ )
4151 return `${ HOST_MAP [ process . env . NODE_ENV ] } ?${ search } `
4252}
0 commit comments