Skip to content

Commit aad3835

Browse files
committed
handle config in AppRouter so we can dispatch
1 parent 38968d5 commit aad3835

File tree

3 files changed

+61
-48
lines changed

3 files changed

+61
-48
lines changed

wormhole-connect/src/AppRouter.tsx

+19-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { RootState } from './store';
66
import { clearRedeem } from './store/redeem';
77
import { clearTransfer } from './store/transferInput';
88
import { usePrevious } from './utils';
9+
import { WormholeConnectConfig } from './config/types';
10+
import { setConfig } from './config';
911

1012
import Bridge from './views/Bridge/Bridge';
1113
import FAQ from './views/FAQ';
@@ -34,11 +36,27 @@ const useStyles = makeStyles()((theme: any) => ({
3436
},
3537
}));
3638

39+
interface Props {
40+
config?: WormholeConnectConfig;
41+
}
42+
3743
// since this will be embedded, we'll have to use pseudo routes instead of relying on the url
38-
function AppRouter() {
44+
function AppRouter({ config }: Props) {
3945
const { classes } = useStyles();
4046
const dispatch = useDispatch();
4147

48+
// We update the global config once when WormholeConnect is first mounted, if a custom
49+
// config was provided.
50+
//
51+
// We don't allow config changes afterwards because they could lead to lots of
52+
// broken and undesired behavior.
53+
React.useEffect(() => {
54+
if (config) {
55+
setConfig(config);
56+
dispatch(clearTransfer());
57+
}
58+
}, []);
59+
4260
const showWalletModal = useSelector(
4361
(state: RootState) => state.router.showWalletModal,
4462
);

wormhole-connect/src/WormholeConnect.tsx

+3-12
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { ThemeProvider, createTheme } from '@mui/material/styles';
55
import './App.css';
66
import { store } from './store';
77
import AppRouter from './AppRouter';
8-
import { getDesignTokens, CustomTheme, dark } from './theme';
8+
import { getDesignTokens, dark } from './theme';
99
import BackgroundImage from './components/Background/BackgroundImage';
1010
import ErrorBoundary from './components/ErrorBoundary';
1111
import { WormholeConnectConfig } from './config/types';
12-
import { setConfig } from './config';
12+
import { CustomTheme } from 'theme';
1313

1414
export interface WormholeConnectProps {
1515
// theme can be updated at any time to change the colors of Connect
@@ -22,15 +22,6 @@ export default function WormholeConnect({
2222
config,
2323
theme,
2424
}: WormholeConnectProps) {
25-
// We update the global config once when WormholeConnect is first mounted, if a custom
26-
// config was provided.
27-
//
28-
// We don't allow config changes afterwards because they could lead to lots of
29-
// broken and undesired behavior.
30-
React.useEffect(() => {
31-
if (config) setConfig(config);
32-
}, []);
33-
3425
// Handle theme changes at any time
3526
const muiTheme = React.useMemo(
3627
() => createTheme(getDesignTokens(theme ?? dark)),
@@ -43,7 +34,7 @@ export default function WormholeConnect({
4334
<ScopedCssBaseline enableColorScheme>
4435
<ErrorBoundary>
4536
<BackgroundImage>
46-
<AppRouter />
37+
<AppRouter config={config} />
4738
</BackgroundImage>
4839
</ErrorBoundary>
4940
</ScopedCssBaseline>

wormhole-connect/src/store/transferInput.ts

+39-35
Original file line numberDiff line numberDiff line change
@@ -127,43 +127,46 @@ export interface TransferInputState {
127127
supportedDestTokens: TokenConfig[];
128128
}
129129

130-
const initialState: TransferInputState = {
131-
showValidationState: false,
132-
validations: {
133-
fromChain: '',
134-
toChain: '',
135-
token: '',
130+
// This is a function because config might have changed since we last cleared this store
131+
function getInitialState(): TransferInputState {
132+
return {
133+
showValidationState: false,
134+
validations: {
135+
fromChain: '',
136+
toChain: '',
137+
token: '',
138+
destToken: '',
139+
amount: '',
140+
route: '',
141+
toNativeToken: '',
142+
sendingWallet: '',
143+
receivingWallet: '',
144+
foreignAsset: '',
145+
relayerFee: '',
146+
receiveAmount: '',
147+
},
148+
routeStates: undefined,
149+
fromChain: config.bridgeDefaults?.fromNetwork || undefined,
150+
toChain: config.bridgeDefaults?.toNetwork || undefined,
151+
token: config.bridgeDefaults?.token || '',
136152
destToken: '',
137153
amount: '',
138-
route: '',
139-
toNativeToken: '',
140-
sendingWallet: '',
141-
receivingWallet: '',
154+
receiveAmount: getEmptyDataWrapper(),
155+
route: undefined,
156+
balances: {},
142157
foreignAsset: '',
143-
relayerFee: '',
144-
receiveAmount: '',
145-
},
146-
routeStates: undefined,
147-
fromChain: config.bridgeDefaults?.fromNetwork || undefined,
148-
toChain: config.bridgeDefaults?.toNetwork || undefined,
149-
token: config.bridgeDefaults?.token || '',
150-
destToken: '',
151-
amount: '',
152-
receiveAmount: getEmptyDataWrapper(),
153-
route: undefined,
154-
balances: {},
155-
foreignAsset: '',
156-
associatedTokenAddress: '',
157-
gasEst: {
158-
send: '',
159-
claim: '',
160-
},
161-
isTransactionInProgress: false,
162-
receiverNativeBalance: '',
163-
supportedSourceTokens: [],
164-
allSupportedDestTokens: [],
165-
supportedDestTokens: [],
166-
};
158+
associatedTokenAddress: '',
159+
gasEst: {
160+
send: '',
161+
claim: '',
162+
},
163+
isTransactionInProgress: false,
164+
receiverNativeBalance: '',
165+
supportedSourceTokens: [],
166+
allSupportedDestTokens: [],
167+
supportedDestTokens: [],
168+
};
169+
}
167170

168171
const performModificationsIfFromChainChanged = (state: TransferInputState) => {
169172
const { fromChain, token, route } = state;
@@ -259,7 +262,7 @@ const establishRoute = (state: TransferInputState) => {
259262

260263
export const transferInputSlice = createSlice({
261264
name: 'transfer',
262-
initialState,
265+
initialState: getInitialState(),
263266
reducers: {
264267
// validations
265268
setValidations: (
@@ -409,6 +412,7 @@ export const transferInputSlice = createSlice({
409412
},
410413
// clear inputs
411414
clearTransfer: (state: TransferInputState) => {
415+
const initialState = getInitialState();
412416
Object.keys(state).forEach((key) => {
413417
// @ts-ignore
414418
state[key] = initialState[key];

0 commit comments

Comments
 (0)