index.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2017-2019 @polkadot/apps authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import { I18nProps } from '@polkadot/react-components/types';
  5. import React, { useContext } from 'react';
  6. import { withRouter, RouteComponentProps } from 'react-router';
  7. import styled from 'styled-components';
  8. import routing from '@polkadot/apps-routing';
  9. import { ApiContext } from '@polkadot/react-api';
  10. import { StatusContext } from '@polkadot/react-components';
  11. import Status from './Status';
  12. import translate from '../translate';
  13. import NotFound from './NotFound';
  14. import TopBar from '../TopBar';
  15. import { MyAccountProvider } from '@polkadot/joy-utils/MyAccountContext';
  16. interface Props extends I18nProps, RouteComponentProps {}
  17. const Wrapper = styled.div`
  18. background: #fafafa;
  19. display: flex;
  20. flex-direction: column;
  21. flex-grow: 1;
  22. height: 100%;
  23. min-height: 100vh;
  24. overflow-x: hidden;
  25. overflow-y: auto;
  26. width: 100%;
  27. padding: 0 2rem;
  28. @media(max-width: 768px) {
  29. padding: 0 0.5rem;
  30. }
  31. `;
  32. const unknown = {
  33. display: {
  34. needsApi: undefined
  35. },
  36. Component: NotFound,
  37. name: ''
  38. };
  39. function Content ({ className, location, t }: Props): React.ReactElement<Props> {
  40. const { isApiConnected, isApiReady } = useContext(ApiContext);
  41. const { queueAction, stqueue, txqueue } = useContext(StatusContext);
  42. const app = location.pathname.slice(1) || '';
  43. const { Component, display: { needsApi }, name } = routing.routes.find((route): boolean =>
  44. !!(route && app.startsWith(route.name))
  45. ) || unknown;
  46. return (
  47. <div className={className}>
  48. {needsApi && (!isApiReady || !isApiConnected)
  49. ? <div className='connecting'>{t('Waiting for API to be connected and ready.')}</div>
  50. : (
  51. <>
  52. <Wrapper>
  53. <MyAccountProvider>
  54. <TopBar />
  55. <Component
  56. basePath={`/${name}`}
  57. location={location}
  58. onStatusChange={queueAction}
  59. />
  60. <Status
  61. queueAction={queueAction}
  62. stqueue={stqueue}
  63. txqueue={txqueue}
  64. />
  65. </MyAccountProvider>
  66. </Wrapper>
  67. </>
  68. )
  69. }
  70. </div>
  71. );
  72. }
  73. // React-router needs to be first, otherwise we have blocked updates
  74. export default translate(
  75. withRouter(
  76. styled(Content)`
  77. background: #fafafa;
  78. display: flex;
  79. flex-direction: column;
  80. flex-grow: 1;
  81. height: 100%;
  82. min-height: 100vh;
  83. overflow-x: hidden;
  84. overflow-y: auto;
  85. width: 100%;
  86. padding: 0;
  87. @media(max-width: 768px) {
  88. padding: 0 0.5rem;
  89. }
  90. .connecting {
  91. padding: 1rem 0;
  92. }
  93. `
  94. )
  95. );