ghost-portal/packages/apps/src/Root.tsx
Jaco 1e7ebdee40
Break (some) circular dependencies (#9464)
* Break (some) circular dependencies

* Rebuild translations
2023-05-19 10:26:23 +03:00

73 lines
2.3 KiB
TypeScript

// Copyright 2017-2023 @polkadot/apps authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ThemeDef } from '@polkadot/react-components/types';
import type { KeyringStore } from '@polkadot/ui-keyring/types';
import React, { Suspense, useEffect, useState } from 'react';
import { HashRouter } from 'react-router-dom';
import { ThemeProvider } from 'styled-components';
import { ApiCtxRoot } from '@polkadot/react-api';
import { darkTheme, lightTheme } from '@polkadot/react-components';
import { ApiStatsCtxRoot, BlockAuthorsCtxRoot, BlockEventsCtxRoot, KeyringCtxRoot, QueueCtxRoot, WindowSizeCtxRoot } from '@polkadot/react-hooks';
import { settings } from '@polkadot/ui-settings';
import Apps from './Apps.js';
interface Props {
isElectron: boolean;
store?: KeyringStore;
}
function createTheme ({ uiTheme }: { uiTheme: string }): ThemeDef {
const validTheme = uiTheme === 'dark' ? 'dark' : 'light';
document && document.documentElement &&
document.documentElement.setAttribute('data-theme', validTheme);
return uiTheme === 'dark'
? darkTheme
: lightTheme;
}
function Root ({ isElectron, store }: Props): React.ReactElement<Props> {
const [theme, setTheme] = useState(() => createTheme(settings));
useEffect((): void => {
settings.on('change', (settings) => setTheme(createTheme(settings)));
}, []);
// The ordering here is critical. It defines the hierarchy of dependencies,
// i.e. Block* could from Api. Certainly no cross-deps allowed
return (
<Suspense fallback='...'>
<ThemeProvider theme={theme}>
<QueueCtxRoot>
<ApiCtxRoot
apiUrl={settings.apiUrl}
isElectron={isElectron}
store={store}
>
<KeyringCtxRoot>
<ApiStatsCtxRoot>
<BlockAuthorsCtxRoot>
<BlockEventsCtxRoot>
<HashRouter>
<WindowSizeCtxRoot>
<Apps />
</WindowSizeCtxRoot>
</HashRouter>
</BlockEventsCtxRoot>
</BlockAuthorsCtxRoot>
</ApiStatsCtxRoot>
</KeyringCtxRoot>
</ApiCtxRoot>
</QueueCtxRoot>
</ThemeProvider>
</Suspense>
);
}
export default React.memo(Root);