-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #551 from soroswap/refactor/ProtocolsToggle
Refactor protocols toggle logic
- Loading branch information
Showing
9 changed files
with
169 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Analytics } from '@vercel/analytics/react'; | ||
import { AppContext, AppContextType, SnackbarIconType, ProtocolsStatus } from 'contexts'; | ||
import { useEffect, useMemo, useState } from 'react'; | ||
import MainLayout from '../Layout/MainLayout'; | ||
import { useSorobanReact } from '@soroban-react/core'; | ||
import config from 'configs/protocols.config.json' | ||
import { Protocols } from 'soroswap-router-sdk'; | ||
import { PlatformType } from 'state/routing/types'; | ||
import { useAggregator } from 'hooks/useAggregator'; | ||
|
||
|
||
export default function ContextProvider({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
const sorobanContext = useSorobanReact(); | ||
const { activeChain } = sorobanContext; | ||
const { isEnabled: isAggregator } = useAggregator(); | ||
const [isConnectWalletModal, setConnectWalletModal] = useState<boolean>(false); | ||
const [maxHops, setMaxHops] = useState<number>(2); | ||
const [protocolsStatus, setProtocolsStatus] = useState<ProtocolsStatus[]>([]); | ||
const [openSnackbar, setOpenSnackbar] = useState<boolean>(false); | ||
const [snackbarMessage, setSnackbarMessage] = useState<string>(''); | ||
const [snackbarTitle, setSnackbarTitle] = useState<string>('Swapped'); | ||
const [snackbarType, setSnackbarType] = useState<SnackbarIconType>(SnackbarIconType.SWAP); | ||
|
||
const appContextValues: AppContextType = { | ||
ConnectWalletModal: { | ||
isConnectWalletModalOpen: isConnectWalletModal, | ||
setConnectWalletModalOpen: setConnectWalletModal, | ||
}, | ||
SnackbarContext: { | ||
openSnackbar, | ||
snackbarMessage, | ||
snackbarTitle, | ||
snackbarType, | ||
setOpenSnackbar, | ||
setSnackbarMessage, | ||
setSnackbarTitle, | ||
setSnackbarType, | ||
}, | ||
Settings: { | ||
maxHops, | ||
setMaxHops, | ||
protocolsStatus, | ||
setProtocolsStatus, | ||
}, | ||
}; | ||
|
||
const defaultProtocolsStatus: ProtocolsStatus[] = useMemo(() => { | ||
if (activeChain?.id === 'testnet') { | ||
return config['testnet'] as ProtocolsStatus[]; | ||
} else { | ||
return config['mainnet'] as ProtocolsStatus[]; | ||
} | ||
}, [activeChain]); | ||
|
||
useEffect(() => { | ||
let protocols: ProtocolsStatus[] = []; | ||
for (let protocol of defaultProtocolsStatus) { | ||
const key = protocol.key; | ||
protocols.push({ | ||
key: key.toLocaleLowerCase() === 'sdex' ? PlatformType.STELLAR_CLASSIC : key, | ||
value: protocol.value, | ||
}); | ||
} | ||
if (isAggregator === false) { | ||
switch (activeChain?.id) { | ||
case 'testnet': | ||
protocols = protocols.filter((protocol) => protocol.key == Protocols.SOROSWAP); | ||
break; | ||
case 'mainnet': | ||
protocols = protocols.filter((protocol) => protocol.key == Protocols.SOROSWAP || protocol.key == PlatformType.STELLAR_CLASSIC); | ||
break; | ||
} | ||
} | ||
if (activeChain?.id == 'testnet') { | ||
protocols = protocols.filter((protocol) => protocol.key != PlatformType.STELLAR_CLASSIC); | ||
} | ||
setProtocolsStatus(protocols); | ||
}, [defaultProtocolsStatus, isAggregator, activeChain?.id]); | ||
|
||
return ( | ||
<AppContext.Provider value={appContextValues}> | ||
<MainLayout> | ||
{children} | ||
<Analytics /> | ||
</MainLayout> | ||
</AppContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { ColorModeContext } from 'contexts'; | ||
import { Provider } from 'react-redux'; | ||
import { useMemo, useState } from 'react'; | ||
import InkathonProvider from 'inkathon/InkathonProvider'; | ||
import MySorobanReactProvider from 'soroban/MySorobanReactProvider'; | ||
import store from 'state'; | ||
import { SorobanContextType } from '@soroban-react/core'; | ||
import { SoroswapThemeProvider } from 'soroswap-ui'; | ||
import ContextProvider from './ContextProvider'; | ||
|
||
export default function Providers({ | ||
children, | ||
sorobanReactProviderProps, | ||
}: { | ||
children: React.ReactNode; | ||
sorobanReactProviderProps?: Partial<SorobanContextType>; | ||
}) { | ||
const [mode, setMode] = useState<'light' | 'dark'>('dark'); | ||
const colorMode = useMemo( | ||
() => ({ | ||
toggleColorMode: () => { | ||
setMode((prevMode) => (prevMode === 'light' ? 'dark' : 'light')); | ||
}, | ||
}), | ||
[], | ||
); | ||
|
||
return ( | ||
<Provider store={store}> | ||
<InkathonProvider> | ||
<ColorModeContext.Provider value={colorMode}> | ||
<SoroswapThemeProvider theme={mode}> | ||
<MySorobanReactProvider {...sorobanReactProviderProps}> | ||
<ContextProvider> | ||
{children} | ||
</ContextProvider> | ||
</MySorobanReactProvider> | ||
</SoroswapThemeProvider> | ||
</ColorModeContext.Provider> | ||
</InkathonProvider> | ||
</Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"mainnet": [ | ||
{ | ||
"key": "sdex", | ||
"value": true | ||
}, | ||
{ | ||
"key": "soroswap", | ||
"value": true | ||
}, | ||
{ | ||
"key": "phoenix", | ||
"value": false | ||
} | ||
], | ||
"testnet": [ | ||
{ | ||
"key": "soroswap", | ||
"value": true | ||
}, | ||
{ | ||
"key": "phoenix", | ||
"value": true | ||
}, | ||
{ | ||
"key": "sdex", | ||
"value": false | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters