App.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import React, { useState } from 'react';
  2. import { CSSTransition } from 'react-transition-group';
  3. import styles from './App.module.css';
  4. import GamePanel from './components/screens/GamePanel';
  5. import HomePage from './components/screens/HomePage';
  6. import Lobby from './components/screens/Lobby';
  7. import PlayerScores from './components/screens/PlayerScores';
  8. import RoundSummary from './components/screens/RoundSummary';
  9. import ApiInfo from './components/util/ApiInfo';
  10. import { ERROR, IN_ROUND, POST_GAME, POST_ROUND, PRE_GAME, PRE_ROUND } from './domain/GameState';
  11. import { useGameState } from './domain/gameStore';
  12. import useDirectGameLinks from './hooks/useDirectGameLinks';
  13. const needsHeaderFooter = {
  14. [PRE_GAME]: true,
  15. [PRE_ROUND]: true,
  16. [IN_ROUND]: false,
  17. [POST_ROUND]: false,
  18. [POST_GAME]: false,
  19. [ERROR]: true
  20. };
  21. const Header = ({ show }) => (
  22. <CSSTransition
  23. in={show}
  24. timeout={500}
  25. mountOnEnter
  26. unmountOnExit
  27. classNames="state"
  28. >
  29. <div className={styles.header}>
  30. <p>TerrAssumptions!</p>
  31. </div>
  32. </CSSTransition>
  33. )
  34. const Footer = ({ show }) => (
  35. <CSSTransition
  36. in={show}
  37. timeout={500}
  38. mountOnEnter
  39. unmountOnExit
  40. classNames="state"
  41. >
  42. <div className={styles.footer}>
  43. <ApiInfo />
  44. </div>
  45. </CSSTransition>
  46. )
  47. const State = ({ show, children, setTransitioning }) => (
  48. <CSSTransition
  49. in={show}
  50. timeout={500}
  51. mountOnEnter
  52. unmountOnExit
  53. classNames="state"
  54. onEnter={() => setTransitioning(true)}
  55. onExited={() => setTransitioning(false)}
  56. >
  57. <div className="state">
  58. {children}
  59. </div>
  60. </CSSTransition>
  61. )
  62. export default () => {
  63. const [transitioning, setTransitioning] = useState(true);
  64. const gameState = useGameState();
  65. useDirectGameLinks();
  66. const needsHF = needsHeaderFooter[gameState];
  67. return (
  68. <React.StrictMode>
  69. <div className={styles.page}>
  70. <Header show={needsHF} />
  71. <State show={gameState === PRE_GAME} setTransitioning={setTransitioning}>
  72. <HomePage />
  73. </State>
  74. <State show={gameState === PRE_ROUND} setTransitioning={setTransitioning}>
  75. <Lobby />
  76. </State>
  77. {!transitioning && gameState === IN_ROUND && <GamePanel />}
  78. <State show={gameState === POST_ROUND} setTransitioning={setTransitioning}>
  79. <RoundSummary />
  80. </State>
  81. <State show={gameState === POST_GAME} setTransitioning={setTransitioning}>
  82. <PlayerScores />
  83. </State>
  84. <State show={gameState === ERROR}>
  85. <p>Application encountered unrecoverable error, please refresh the page.</p>
  86. </State>
  87. <Footer show={needsHF} />
  88. </div>
  89. </React.StrictMode>
  90. );
  91. };