App.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import React, { Fragment, useEffect, useState } from 'react';
  2. import './css/App.css';
  3. import './css/fontawesome.css';
  4. import './css/solid.css';
  5. import './css/bootstrap/bootstrap.css';
  6. import './css/main.css';
  7. import './css/ChuChubeFont.css'
  8. import useRoom from "./util/useRoom";
  9. import ChooseRoom from "./components/ChooseRoom";
  10. import Room from "./components/Room";
  11. import LoadingLogo from "./components/LoadingLogo";
  12. import NavBar from "./components/NavBar";
  13. function getPathFromUrl() {
  14. return new URLSearchParams(window.location.search).get("room") || null
  15. }
  16. function App() {
  17. const [path, setPath] = useState(getPathFromUrl());
  18. const { room, setRoom, resolver, socket } = useRoom(path);
  19. useEffect(() => {
  20. window.onpopstate = (_) => {
  21. setPath(getPathFromUrl());
  22. }
  23. })
  24. useEffect(() => {
  25. if (path === null) {
  26. const title = "ChuChube - Choose Room";
  27. window.history.pushState({}, title, "/")
  28. document.title = title;
  29. } else {
  30. const title = `ChuChube - ${path}`;
  31. window.history.pushState({}, title, `player?room=${path}`)
  32. document.title = title;
  33. }
  34. }, [path])
  35. if (path === null) {
  36. return <ChooseRoom setPath={setPath}/>
  37. } else if (socket.connected) {
  38. return <Room room={room}
  39. setRoom={setRoom}
  40. exitRoom={() => setPath(null)}
  41. socket={socket}
  42. resolver={resolver}
  43. />;
  44. } else {
  45. return <Fragment>
  46. <NavBar exitRoom={() => setPath(null)}/>
  47. <div className="absoluteCenter" style={{height: "200px"}}>
  48. <LoadingLogo/>
  49. </div>
  50. </Fragment>
  51. }
  52. }
  53. export default App;