Skip to content

Commit 30a65b6

Browse files
committed
mvp: keep wallet
1 parent b0f29a7 commit 30a65b6

File tree

3 files changed

+135
-82
lines changed

3 files changed

+135
-82
lines changed

app/providers.tsx

Lines changed: 73 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -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) => {

storage.example.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"state": {
3+
"connections": {
4+
"__type": "Map",
5+
"value": [
6+
[
7+
"98931b6a87a",
8+
{
9+
"accounts": ["0xA398Bf36F3df52a0dCCFfe3299A88E178d7A8D17"],
10+
"chainId": 1,
11+
"connector": {
12+
"id": "walletConnect",
13+
"name": "WalletConnect",
14+
"type": "walletConnect",
15+
"uid": "98931b6a87a"
16+
}
17+
}
18+
]
19+
]
20+
},
21+
"chainId": 1,
22+
"current": "98931b6a87a"
23+
},
24+
"version": 2
25+
}

utils/parse-config.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
/**
3+
* Parses a JSON string and recursively revives all serialized Maps.
4+
* @param jsonString - The JSON string representing the serialized state.
5+
* @returns The parsed and revived state object with Maps restored.
6+
*/
7+
export function parseAndReviveMaps(jsonString: string): any {
8+
function reviveMaps(obj: any): any {
9+
if (obj === null || obj === undefined) {
10+
return obj
11+
}
12+
13+
if (Array.isArray(obj)) {
14+
// Detect if array is serialized Map: all elements are [key, value] pairs
15+
if (obj.every((item) => Array.isArray(item) && item.length === 2)) {
16+
return new Map(obj.map(([k, v]) => [k, reviveMaps(v)]))
17+
} else {
18+
return obj.map(reviveMaps)
19+
}
20+
}
21+
22+
if (typeof obj === 'object') {
23+
const revivedObj: { [key: string]: any } = {}
24+
for (const key in obj) {
25+
if (Object.hasOwn(obj, key)) {
26+
revivedObj[key] = reviveMaps(obj[key])
27+
}
28+
}
29+
return revivedObj
30+
}
31+
32+
return obj
33+
}
34+
35+
const parsed = JSON.parse(jsonString)
36+
return reviveMaps(parsed)
37+
}

0 commit comments

Comments
 (0)