@@ -42,106 +42,97 @@ export const Providers = ({ children }: { children: ReactNode }) => {
4242 const handleMessage = ( event : MessageEvent ) => {
4343 if ( event . data ?. type === 'WALLETCONNECT_CONFIG_DATA' ) {
4444 const parentConfig = event . data . payload . config
45- if (
46- ! parentConfig ||
47- parentConfig === 'null' ||
48- parentConfig === 'undefined' ||
49- parentConfig === ''
50- ) {
51- // No config from parent, create new config
52- const cfg = getConfig ( projectId )
53- setConfig ( cfg )
54- setInitialState ( undefined )
45+ // if (
46+ // !config &&
47+ // !!!parentConfig
48+ // // (!parentConfig ||
49+ // // parentConfig === 'null' ||
50+ // // parentConfig === 'undefined' ||
51+ // // parentConfig === '')
52+ // ) {
53+ // // No config from parent, create new config
54+ // const cfg = getConfig(projectId)
55+ // setConfig(cfg)
56+ // setInitialState(undefined)
57+ // console.info(
58+ // '[ TAPPLET-BRIDGE ] No config from parent, created new config:',
59+ // cfg,
60+ // )
61+ // } else {
62+ if ( ! config && ! initialState && parentConfig ) {
63+ // Config exists from parent, parse and use it
64+
5565 console . info (
56- '[ TAPPLET-BRIDGE ] No config from parent, created new config:' ,
57- cfg ,
66+ '[ TAPPLET-BRIDGE ] Using config from parent:' ,
67+ typeof parentConfig === 'string' ,
68+ parentConfig ,
5869 )
59- } else {
60- // Config exists from parent, parse and use it
6170 try {
62- console . info (
63- '[ TAPPLET-BRIDGE ] Using config from parent:' ,
64- typeof parentConfig === 'string' ,
65- parentConfig ,
66- )
67- try {
68- const parsedState : State =
69- typeof parentConfig === 'string'
70- ? JSON . parse ( parentConfig )
71- : parentConfig
72- // Revive connections into a Map
73- if (
74- parsedState . connections &&
75- ! ( parsedState . connections instanceof Map )
76- ) {
77- parsedState . connections = new Map ( parsedState . connections )
78- }
79- setInitialState ( parsedState )
80- console . info ( '[ TAPPLET-BRIDGE ] parsed state:' , parsedState )
81- // setConfig(parsedState)
82- } catch ( error ) {
83- console . error (
84- '[ TAPPLET-BRIDGE ] Failed to set init state:' ,
85- error ,
86- )
71+ const parsedState : State =
72+ typeof parentConfig === 'string'
73+ ? JSON . parse ( parentConfig )
74+ : parentConfig
75+ // Revive connections into a Map
76+ if (
77+ parsedState . connections &&
78+ ! ( parsedState . connections instanceof Map )
79+ ) {
80+ parsedState . connections = new Map ( parsedState . connections )
8781 }
88- // If parent also sent session state, set it here
89- // if (event.data.session) {
90- // setInitialState(event.data.session)
91- // }
92- } catch ( error ) {
93- console . error (
94- '[ TAPPLET-BRIDGE ] Failed to parse parent config:' ,
95- error ,
96- )
97- // fallback: create new config
82+ setInitialState ( parsedState )
83+ console . info ( '[ TAPPLET-BRIDGE ] parsed state:' , parsedState )
9884 const cfg = getConfig ( projectId )
9985 setConfig ( cfg )
100- setInitialState ( undefined )
101- }
102- }
103- // If configData is present, set initial state
104- const configData : string = event . data . payload ?. config || ''
105- if ( projectId && configData ) {
106- try {
107- const parsedState = JSON . parse ( configData ) as State
108- setInitialState ( parsedState )
10986 } catch ( error ) {
110- console . error (
111- '[ TAPPLET-BRIDGE ] Failed to parse configData:' ,
112- error ,
113- )
87+ console . error ( '[ TAPPLET-BRIDGE ] Failed to set init state:' , error )
11488 }
89+ // If parent also sent session state, set it here
90+ // if (event.data.session) {
91+ // setInitialState(event.data.session)
92+ // }
11593 }
94+ // If configData is present, set initial state
95+ // const configData: string = event.data.payload?.config || ''
96+ // if (projectId && configData) {
97+ // try {
98+ // const parsedState = JSON.parse(configData) as State
99+ // setInitialState(parsedState)
100+ // } catch (error) {
101+ // console.error(
102+ // '[ TAPPLET-BRIDGE ] Failed to parse configData:',
103+ // error,
104+ // )
105+ // }
106+ // }
116107 } else if ( event . data ?. type === 'SET_THEME' ) {
117108 setTheme ( event . data . payload )
118109 }
119110 }
120111
121112 window . addEventListener ( 'message' , handleMessage )
122113 return ( ) => window . removeEventListener ( 'message' , handleMessage )
123- } , [ projectId , setTheme , config ] )
114+ } , [ projectId , setTheme , config , initialState ] )
124115
125116 // If no config event is sent after a short delay, create a new config
126- useEffect ( ( ) => {
127- let timeout : NodeJS . Timeout | null = null
128- if ( ! config && projectId ) {
129- timeout = setTimeout ( ( ) => {
130- if ( ! config ) {
131- const cfg = getConfig ( projectId )
132- setConfig ( cfg )
133- // setInitialState(undefined)
134- console . info (
135- '[ TAPPLET-BRIDGE ] No config event received, created new config:' ,
136- cfg ,
137- )
138- }
139- } , 1000 ) // 1 second timeout, adjust as needed
140- }
141- return ( ) => {
142- if ( timeout ) clearTimeout ( timeout )
143- }
144- } , [ config , projectId ] )
117+ // useEffect(() => {
118+ // let timeout: NodeJS.Timeout | null = null
119+ // if (!config && projectId) {
120+ // timeout = setTimeout(() => {
121+ // if (!config) {
122+ // const cfg = getConfig(projectId)
123+ // setConfig(cfg)
124+ // // setInitialState(undefined)
125+ // console.info(
126+ // '[ TAPPLET-BRIDGE ] No config event received, created new config:',
127+ // cfg,
128+ // )
129+ // }
130+ // }, 5000 ) // 1 second timeout, adjust as needed
131+ // }
132+ // return () => {
133+ // if (timeout) clearTimeout(timeout)
134+ // }
135+ // }, [config, initialState , projectId])
145136
146137 // 3. When connected, send session data to parent for storage
147138 // const sendSessionToParent = useCallback((state: State | undefined) => {
0 commit comments