MainRoute.jsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import React from "react";
  2. import { Route, Switch, Redirect } from "react-router-dom";
  3. import "react-toastify/dist/ReactToastify.css";
  4. import "./react-confirm-alert.css";
  5. import { uid } from "./utils";
  6. import Home from "./views/Home";
  7. import GameView from "./views/GameView";
  8. import Session from "./views/Session";
  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 (
  30. <Redirect
  31. to={{
  32. pathName: `/session/${uid()}/`,
  33. state: { fromGame: gameId },
  34. }}
  35. />
  36. );
  37. }}
  38. </Route>
  39. {/* for compat with old url scheme */}
  40. <Route path="/game/:gameId/session/:sessionId/">
  41. {({
  42. match: {
  43. params: { gameId, sessionId },
  44. },
  45. }) => {
  46. return (
  47. <Redirect
  48. to={{
  49. pathName: `/session/${sessionId}`,
  50. state: { fromGame: gameId },
  51. }}
  52. />
  53. );
  54. }}
  55. </Route>
  56. {/* Start a new session from this game */}
  57. <Route path="/playgame/:gameId" exact>
  58. {({
  59. match: {
  60. params: { gameId },
  61. },
  62. }) => {
  63. // Redirect to new session id
  64. return (
  65. <Redirect
  66. to={{
  67. pathname: `/session/${uid()}/`,
  68. state: {
  69. fromGame: gameId,
  70. },
  71. }}
  72. />
  73. );
  74. }}
  75. </Route>
  76. <Route path="/session/:sessionId">
  77. {({
  78. match: {
  79. params: { sessionId },
  80. },
  81. location: { state: { fromGame } = {} } = {},
  82. }) => {
  83. // Redirect to new session id
  84. return (
  85. <WithSocketIO>
  86. <Session sessionId={sessionId} fromGame={fromGame} />
  87. </WithSocketIO>
  88. );
  89. }}
  90. </Route>
  91. {/* Game edition */}
  92. <Route path="/game/:gameId?">
  93. {({
  94. match: {
  95. params: { gameId },
  96. },
  97. }) => {
  98. return (
  99. <WithSocketIO>
  100. <GameView gameId={gameId} />
  101. </WithSocketIO>
  102. );
  103. }}
  104. </Route>
  105. {/*Room routes*/}
  106. <Route path="/room/:roomId">
  107. {({
  108. match: {
  109. params: { roomId },
  110. },
  111. location: { state: { showInvite = false } = {} } = {},
  112. }) => (
  113. <WithSocketIO>
  114. <RoomView roomId={roomId} showInvite={showInvite} />
  115. </WithSocketIO>
  116. )}
  117. </Route>
  118. <Route path="/room/" exact>
  119. {() => {
  120. // Redirect to new room
  121. return (
  122. <Redirect
  123. to={{
  124. pathname: `/room/${uid()}/`,
  125. state: { showInvite: true },
  126. }}
  127. />
  128. );
  129. }}
  130. </Route>
  131. {/* Auth rout */}
  132. <Route exact path="/login/:userHash/:token">
  133. <AuthView />
  134. </Route>
  135. {/* Default route */}
  136. <Route path="/" exact>
  137. <Redirect to="/games/" />
  138. </Route>
  139. <Route path="/">
  140. <Home />
  141. </Route>
  142. </Switch>
  143. );
  144. };
  145. export default MainRoute;