MainRoute.jsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <Redirect to={`/session/${uid()}/?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/${uid()}/`,
  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. <Session sessionId={sessionId} fromGame={fromGame} />
  73. </WithSocketIO>
  74. );
  75. }}
  76. </Route>
  77. {/* Game edition */}
  78. <Route path="/game/:gameId?">
  79. {({
  80. match: {
  81. params: { gameId },
  82. },
  83. }) => {
  84. return (
  85. <WithSocketIO>
  86. <GameView gameId={gameId} />
  87. </WithSocketIO>
  88. );
  89. }}
  90. </Route>
  91. {/*Room routes*/}
  92. <Route path="/room/:roomId">
  93. {({
  94. match: {
  95. params: { roomId },
  96. },
  97. }) => (
  98. <WithSocketIO>
  99. <RoomView roomId={roomId} />
  100. </WithSocketIO>
  101. )}
  102. </Route>
  103. {/* Auth rout */}
  104. <Route exact path="/login/:userHash/:token">
  105. <AuthView />
  106. </Route>
  107. {/* Default route */}
  108. <Redirect from="/" to="/games/" exact />
  109. <Route path="/">
  110. <Home />
  111. </Route>
  112. </Switch>
  113. );
  114. };
  115. export default MainRoute;