Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Apr 18, 2024
1 parent 0929c5f commit 1fde6af
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 85 deletions.
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const nextConfig = {
},
env: {
WS_ROCOCO_CORETIME_CHAIN: process.env.WS_ROCOCO_CORETIME_CHAIN || '',
WS_KUSAMA_COREITME_CHAIN: process.env.WS_KUSAMA_COREITME_CHAIN || '',
WS_KUSAMA_CORETIME_CHAIN: process.env.WS_KUSAMA_CORETIME_CHAIN || '',
WS_ROCOCO_RELAY_CHAIN: process.env.WS_ROCOCO_RELAY_CHAIN,
WS_KUSAMA_RELAY_CHAIN: process.env.WS_KUSAMA_RELAY_CHAIN,
},
Expand Down
15 changes: 10 additions & 5 deletions src/components/Elements/NetworkSelect/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { useNetwork } from '@/contexts/network';
import { FormControl, InputLabel, MenuItem, Select } from '@mui/material';
import { useRouter } from 'next/router';

const RelaySelect = () => {
const { network, setNetwork } = useNetwork();
const router = useRouter();
const { network } = router.query;

const handleChange = (e: any) => {
setNetwork(e.target.value);
router.push({
pathname: router.pathname,
query: { ...router.query, network: e.target.value }
}, undefined, { shallow: false });
};

return (
<FormControl sx={{ m: 2, minWidth: 200 }} fullWidth>
<InputLabel id='demo-simple-select-label'>Network</InputLabel>
<InputLabel>Network</InputLabel>
<Select
id='network-select'
value={network}
value={network ? network : "rococo"}
label='Relay chain'
onChange={handleChange}
>
Expand Down
22 changes: 17 additions & 5 deletions src/contexts/apis/CoretimeApi/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useToast } from '@/contexts/toast';

import { connect, disconnect, initialState, reducer } from '../common';
import { WS_ROCOCO_CORETIME_CHAIN, WS_KUSAMA_CORETIME_CHAIN } from '../consts';
import { useNetwork } from '@/contexts/network';
import { useRouter } from 'next/router';

const types = {
CoreIndex: 'u32',
Expand Down Expand Up @@ -37,9 +37,11 @@ const CoretimeApiContext = React.createContext(defaultValue);

const CoretimeApiContextProvider = (props: any) => {
const [state, dispatch] = useReducer(reducer, initialState);
const { network } = useNetwork();
const { toastError, toastSuccess } = useToast();

const router = useRouter();
const { network } = router.query;

useEffect(() => {
state.apiError && toastError(`Failed to connect to Coretime chain`);
}, [state.apiError, toastError]);
Expand All @@ -49,12 +51,22 @@ const CoretimeApiContextProvider = (props: any) => {
toastSuccess('Successfully connected to the Coretime chain');
}, [state.apiState, toastSuccess]);

const getUrl = (network: any): string => {
if (!network || network == "rococo") {
return WS_ROCOCO_CORETIME_CHAIN;
} else if (network == "kusama") {
return WS_KUSAMA_CORETIME_CHAIN;
} else {
console.error(`Network: ${network} not recognized`);
// Default to rococo.
return WS_ROCOCO_CORETIME_CHAIN;
}
}

const connectCoretime = () =>
connect(
state,
network === 'rococo'
? WS_ROCOCO_CORETIME_CHAIN
: WS_KUSAMA_CORETIME_CHAIN,
getUrl(network),
dispatch,
types
);
Expand Down
6 changes: 2 additions & 4 deletions src/contexts/apis/RelayApi/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { ParaId } from '@/models';

import { connect, disconnect, initialState, reducer } from '../common';
import { WS_ROCOCO_RELAY_CHAIN, WS_KUSAMA_RELAY_CHAIN } from '../consts';
import { useNetwork } from '@/contexts/network';

const defaultValue = {
state: initialState,
Expand All @@ -27,7 +26,6 @@ const RelayApiContextProvider = (props: any) => {
const [state, dispatch] = useReducer(reducer, initialState);
const { toastError, toastSuccess } = useToast();
const [paraIds, setParaIds] = useState<ParaId[]>([]);
const { network } = useNetwork();

useEffect(() => {
state.apiError && toastError(`Failed to connect to relay chain`);
Expand All @@ -41,7 +39,7 @@ const RelayApiContextProvider = (props: any) => {
const connectRelay = () =>
connect(
state,
network === 'rococo' ? WS_ROCOCO_RELAY_CHAIN : WS_KUSAMA_RELAY_CHAIN,
WS_ROCOCO_RELAY_CHAIN,
dispatch
);
const disconnectRelay = () => disconnect(state);
Expand All @@ -58,7 +56,7 @@ const RelayApiContextProvider = (props: any) => {
setParaIds(paraIds);
};
fetchParaIds();
}, [state, network]);
}, [state]);

return (
<RelayApiContext.Provider
Expand Down
30 changes: 0 additions & 30 deletions src/contexts/network/index.tsx

This file was deleted.

6 changes: 3 additions & 3 deletions src/contexts/regions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import * as NativeRegions from './native';
import { useCoretimeApi } from '../apis';
import { useCommon } from '../common';
import { useTasks } from '../tasks';
import { useNetwork } from '../network';

interface RegionsData {
regions: Array<RegionMetadata>;
Expand Down Expand Up @@ -52,7 +51,6 @@ const RegionDataProvider = ({ children }: Props) => {
const { fetchWorkplan, fetchRegionWorkload } = useTasks();

const context = useCommon();
const { network } = useNetwork();

const [regions, setRegions] = useState<Array<RegionMetadata>>([]);
const [loading, setLoading] = useState(false);
Expand All @@ -70,6 +68,7 @@ const RegionDataProvider = ({ children }: Props) => {
setRegions([]);
return;
}
console.log('hey');
setLoading(true);

const tasks = await fetchWorkplan();
Expand Down Expand Up @@ -109,14 +108,15 @@ const RegionDataProvider = ({ children }: Props) => {
);
}

console.log(_regions);

setRegions(_regions);
setLoading(false);
}, [
activeAccount,
context,
coretimeApi,
api,
network,
fetchWorkplan,
_getTaskFromWorkloadId,
]);
Expand Down
7 changes: 0 additions & 7 deletions src/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ export type ParaId = number;

export type BlockNumber = number;

export type Network = 'rococo' | 'kusama';

export type NetworkContext = {
network: string;
setNetwork: (_n: Network) => void;
};

export type Sender = {
address: string;
signer: Signer;
Expand Down
73 changes: 43 additions & 30 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import {
CoretimeApiContextProvider,
RelayApiContextProvider,
} from '@/contexts/apis';
import { WS_ROCOCO_CORETIME_CHAIN } from '@/contexts/apis/consts';
import { WS_KUSAMA_CORETIME_CHAIN, WS_ROCOCO_CORETIME_CHAIN } from '@/contexts/apis/consts';
import { ContextDataProvider } from '@/contexts/common';
import { MarketProvider } from '@/contexts/market';
import { RegionDataProvider } from '@/contexts/regions';
import { SaleInfoProvider } from '@/contexts/sales';
import { TaskDataProvider } from '@/contexts/tasks';
import { ToastProvider } from '@/contexts/toast';
import { NetworkDataProvider } from '@/contexts/network';
import { useRouter } from 'next/router';

// Client-side cache, shared for the whole session of the user in the browser.
const clientSideEmotionCache = createEmotionCache();
Expand All @@ -38,9 +38,24 @@ interface MyAppProps extends AppProps {
}

export default function MyApp(props: MyAppProps) {
const router = useRouter();
const { network } = router.query;

const { Component, emotionCache = clientSideEmotionCache, pageProps } = props;
const getLayout = Component.getLayout ?? ((page) => <Layout>{page}</Layout>);

const getUrl = (network: any): string => {
if (!network || network === "rococo") {
return WS_ROCOCO_CORETIME_CHAIN;
} else if (network === "kusama") {
return WS_KUSAMA_CORETIME_CHAIN;
} else {
console.error(`Network: ${network} not recognized`);
// default to rococo.
return WS_ROCOCO_CORETIME_CHAIN;
}
}

return (
<CacheProvider value={emotionCache}>
<Head>
Expand All @@ -50,34 +65,32 @@ export default function MyApp(props: MyAppProps) {
<ThemeProvider theme={theme}>
<CssBaseline />
<ToastProvider>
<NetworkDataProvider>
<CoretimeApiContextProvider>
<RelayApiContextProvider>
<UseInkathonProvider
appName='CoreHub'
connectOnInit={false}
defaultChain={{
network: '',
name: '',
rpcUrls: [WS_ROCOCO_CORETIME_CHAIN],
}}
apiOptions={{ types: { Id } }}
>
<ContextDataProvider>
<TaskDataProvider>
<RegionDataProvider>
<MarketProvider>
<SaleInfoProvider>
{getLayout(<Component {...pageProps} />)}
</SaleInfoProvider>
</MarketProvider>
</RegionDataProvider>
</TaskDataProvider>
</ContextDataProvider>
</UseInkathonProvider>
</RelayApiContextProvider>
</CoretimeApiContextProvider>
</NetworkDataProvider>
<CoretimeApiContextProvider>
<RelayApiContextProvider>
<UseInkathonProvider
appName='CoreHub'
connectOnInit={false}
defaultChain={{
network: '',
name: '',
rpcUrls: [getUrl(network)],
}}
apiOptions={{ types: { Id } }}
>
<ContextDataProvider>
<TaskDataProvider>
<RegionDataProvider>
<MarketProvider>
<SaleInfoProvider>
{getLayout(<Component {...pageProps} />)}
</SaleInfoProvider>
</MarketProvider>
</RegionDataProvider>
</TaskDataProvider>
</ContextDataProvider>
</UseInkathonProvider>
</RelayApiContextProvider>
</CoretimeApiContextProvider>
</ToastProvider>
</ThemeProvider>
</CacheProvider>
Expand Down

0 comments on commit 1fde6af

Please sign in to comment.