Skip to content

Commit 22ff991

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

File tree

3 files changed

+60
-48
lines changed

3 files changed

+60
-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

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

130-
const initialState: TransferInputState = {
131-
showValidationState: false,
132-
validations: {
133-
fromChain: '',
134-
toChain: '',
135-
token: '',
130+
function getInitialState(): TransferInputState {
131+
return {
132+
showValidationState: false,
133+
validations: {
134+
fromChain: '',
135+
toChain: '',
136+
token: '',
137+
destToken: '',
138+
amount: '',
139+
route: '',
140+
toNativeToken: '',
141+
sendingWallet: '',
142+
receivingWallet: '',
143+
foreignAsset: '',
144+
relayerFee: '',
145+
receiveAmount: '',
146+
},
147+
routeStates: undefined,
148+
fromChain: config.bridgeDefaults?.fromNetwork || undefined,
149+
toChain: config.bridgeDefaults?.toNetwork || undefined,
150+
token: config.bridgeDefaults?.token || '',
136151
destToken: '',
137152
amount: '',
138-
route: '',
139-
toNativeToken: '',
140-
sendingWallet: '',
141-
receivingWallet: '',
153+
receiveAmount: getEmptyDataWrapper(),
154+
route: undefined,
155+
balances: {},
142156
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-
};
157+
associatedTokenAddress: '',
158+
gasEst: {
159+
send: '',
160+
claim: '',
161+
},
162+
isTransactionInProgress: false,
163+
receiverNativeBalance: '',
164+
supportedSourceTokens: [],
165+
allSupportedDestTokens: [],
166+
supportedDestTokens: [],
167+
};
168+
}
167169

168170
const performModificationsIfFromChainChanged = (state: TransferInputState) => {
169171
const { fromChain, token, route } = state;
@@ -259,7 +261,7 @@ const establishRoute = (state: TransferInputState) => {
259261

260262
export const transferInputSlice = createSlice({
261263
name: 'transfer',
262-
initialState,
264+
initialState: getInitialState(),
263265
reducers: {
264266
// validations
265267
setValidations: (
@@ -409,6 +411,7 @@ export const transferInputSlice = createSlice({
409411
},
410412
// clear inputs
411413
clearTransfer: (state: TransferInputState) => {
414+
const initialState = getInitialState();
412415
Object.keys(state).forEach((key) => {
413416
// @ts-ignore
414417
state[key] = initialState[key];

0 commit comments

Comments
 (0)