|
1 | | -import React, { useCallback, useContext, useEffect, useRef } from 'react'; |
| 1 | +import React, { useContext, useEffect, useRef } from 'react'; |
2 | 2 | import { useDispatch, useSelector } from 'react-redux'; |
3 | 3 | import { useTheme } from '@mui/material/styles'; |
4 | 4 |
|
5 | 5 | import './App.css'; |
6 | 6 | import type { RootState } from './store'; |
7 | 7 | import { clearRedeem } from './store/redeem'; |
8 | 8 | import { clearTransfer } from './store/transferInput'; |
9 | | -import { isEmptyObject, usePrevious } from './utils'; |
10 | | -import type { WormholeConnectConfig } from './config/types'; |
11 | | -import { setConfig } from './config'; |
12 | | -import config from './config'; |
| 9 | +import { usePrevious } from './utils'; |
| 10 | +import { useConfig } from './contexts/ConfigContext'; |
13 | 11 |
|
14 | 12 | import Terms from './views/Terms'; |
15 | 13 | import TxSearch from './views/TxSearch'; |
@@ -80,56 +78,32 @@ const AppRouterContent = () => { |
80 | 78 | ); |
81 | 79 | }; |
82 | 80 |
|
83 | | -interface Props { |
84 | | - config?: WormholeConnectConfig; |
85 | | -} |
86 | | - |
87 | 81 | // since this will be embedded, we'll have to use pseudo routes instead of relying on the url |
88 | | -function AppRouter(props: Props) { |
| 82 | +function AppRouter() { |
89 | 83 | const dispatch = useDispatch(); |
90 | | - |
91 | | - const hasSetSsgConfig = useRef(false); |
92 | | - const isInitialLoad = useRef(true); |
| 84 | + const config = useConfig(); |
93 | 85 | const route = useSelector((state: RootState) => state.router.route); |
94 | 86 |
|
95 | | - const loadConfig = useCallback((customConfig: WormholeConnectConfig) => { |
96 | | - if (!isEmptyObject(customConfig)) { |
97 | | - setConfig(customConfig); |
98 | | - } |
99 | | - |
100 | | - hasSetSsgConfig.current = true; |
101 | | - config.triggerEvent({ |
102 | | - type: 'config', |
103 | | - config: customConfig, |
104 | | - }); |
105 | | - }, []); |
| 87 | + // Track config changes to clear transfer state when config is updated |
| 88 | + const prevConfig = usePrevious(config); |
| 89 | + const hasInitialized = useRef(false); |
106 | 90 |
|
107 | | - if (!hasSetSsgConfig.current) { |
108 | | - // This runs once in SSG step (server-side pre-rendering) |
109 | | - if (props.config) { |
110 | | - loadConfig(props.config); |
111 | | - } |
112 | | - if (route !== 'bridge') { |
113 | | - // The route may not be bridge on initial load if the component was re-rendered after client side navigation |
114 | | - dispatch(setRoute('bridge')); |
| 91 | + // SSG route initialization - ensure we start on bridge route |
| 92 | + useEffect(() => { |
| 93 | + if (!hasInitialized.current) { |
| 94 | + hasInitialized.current = true; |
| 95 | + if (route !== 'bridge') { |
| 96 | + dispatch(setRoute('bridge')); |
| 97 | + } |
115 | 98 | } |
116 | | - } |
| 99 | + }, [route, dispatch]); |
117 | 100 |
|
| 101 | + // Clear transfer state when config changes (after initial load) |
118 | 102 | useEffect(() => { |
119 | | - if (isInitialLoad.current) { |
120 | | - isInitialLoad.current = false; |
121 | | - config.triggerEvent({ |
122 | | - type: 'load', |
123 | | - config: props.config, |
124 | | - }); |
125 | | - } else { |
126 | | - if (props.config) { |
127 | | - loadConfig(props.config); |
128 | | - dispatch(clearTransfer()); |
129 | | - } |
| 103 | + if (prevConfig && prevConfig !== config) { |
| 104 | + dispatch(clearTransfer()); |
130 | 105 | } |
131 | | - }, [props.config, loadConfig, dispatch]); |
132 | | - // END config loading code |
| 106 | + }, [config, prevConfig, dispatch]); |
133 | 107 |
|
134 | 108 | return <AppRouterContent />; |
135 | 109 | } |
|
0 commit comments