MainRoute.jsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import React from "react";
  2. import { Route, Switch, Redirect } from "react-router-dom";
  3. import { nanoid } from "nanoid";
  4. import "react-toastify/dist/ReactToastify.css";
  5. import "./react-confirm-alert.css";
  6. import Home from "./views/Home";
  7. // import GameView from "./views/GameView";
  8. // import RoomWrapperView from "./views/RoomWrapperView";
  9. import AuthView from "./views/AuthView";
  10. // import RoomView from "./views/RoomView";
  11. import { Provider as SocketIOProvider } from "@scripters/use-socket.io";
  12. import { SOCKET_URL, SOCKET_OPTIONS } from "./utils/settings";
  13. const WithSocketIO = ({ children }) => (
  14. <SocketIOProvider url={SOCKET_URL} options={SOCKET_OPTIONS}>
  15. {children}
  16. </SocketIOProvider>
  17. );
  18. const MainRoute = () => {
  19. return (
  20. <Switch>
  21. {/* for compat with old url scheme */}
  22. <Route path="/game/:gameId/session/" exact>
  23. {({
  24. match: {
  25. params: { gameId },
  26. },
  27. }) => {
  28. // Redirect to new session id
  29. return <Redirect to={`/session/${nanoid()}/?fromGame=${gameId}`} />;
  30. }}
  31. </Route>
  32. {/* for compat with old url scheme */}
  33. <Route path="/game/:gameId/session/:sessionId/">
  34. {({
  35. match: {
  36. params: { gameId, sessionId },
  37. },
  38. }) => {
  39. return <Redirect to={`/session/${sessionId}/?fromGame=${gameId}`} />;
  40. }}
  41. </Route>
  42. {/* Start a new session from this game */}
  43. <Route path="/playgame/:gameId" exact>
  44. {({
  45. match: {
  46. params: { gameId },
  47. },
  48. }) => {
  49. // Redirect to new session id
  50. return (
  51. <Redirect
  52. to={{
  53. pathname: `/session/${nanoid()}/`,
  54. search: `?fromGame=${gameId}`,
  55. }}
  56. />
  57. );
  58. }}
  59. </Route>
  60. {/*<Route path="/session/:sessionId">
  61. {({
  62. location: { search },
  63. match: {
  64. params: { sessionId },
  65. },
  66. }) => {
  67. const params = new URLSearchParams(search);
  68. const fromGame = params.get("fromGame");
  69. // Redirect to new session id
  70. return (
  71. <WithSocketIO>
  72. <RoomWrapperView sessionId={sessionId} fromGame={fromGame} />
  73. </WithSocketIO>
  74. );
  75. }}
  76. </Route>*/}
  77. {/* Game edition/}
  78. <Route path="/game/:gameId?">
  79. <WithSocketIO>
  80. <GameView />
  81. </WithSocketIO>
  82. </Route>
  83. {/* Room routes}
  84. <Route path="/room/:roomId">
  85. {({
  86. match: {
  87. params: { roomId },
  88. },
  89. }) => (
  90. <WithSocketIO>
  91. <RoomView roomId={roomId} />
  92. </WithSocketIO>
  93. )}
  94. </Route>
  95. {/* Auth rout */}
  96. <Route exact path="/login/:userHash/:token">
  97. <AuthView />
  98. </Route>
  99. {/* Default route */}
  100. <Redirect from="/" to="/games/" exact />
  101. <Route path="/">
  102. <Home />
  103. </Route>
  104. </Switch>
  105. );
  106. };
  107. export default MainRoute;