SessionView_old.jsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import React from "react";
  2. import useC2C, { C2CProvider } from "../components/hooks/useC2C";
  3. import { itemMap, useGameItemActionMap, ItemForm } from "../gameComponents";
  4. import BoardView from "./BoardView";
  5. import Waiter from "./Waiter";
  6. import { useUsers } from "../components/users";
  7. import useSession, { SessionProvider } from "../hooks/useSession";
  8. import AutoSaveSession from "./AutoSaveSession";
  9. import { useTranslation } from "react-i18next";
  10. import useAsyncEffect from "use-async-effect";
  11. export const SessionView = () => {
  12. const {
  13. loadSession,
  14. setSession,
  15. sessionLoaded,
  16. gameId,
  17. sessionId,
  18. } = useSession();
  19. const { c2c, isMaster } = useC2C("board");
  20. const { actionMap } = useGameItemActionMap();
  21. const { setCurrentUser } = useUsers();
  22. const gameLoadingRef = React.useRef(false);
  23. const { t } = useTranslation();
  24. React.useEffect(() => {
  25. setCurrentUser((prev) => ({ ...prev, space: sessionId }));
  26. }, [sessionId, setCurrentUser]);
  27. useAsyncEffect(
  28. async (isMounted) => {
  29. if (isMaster && !sessionLoaded && !gameLoadingRef.current) {
  30. gameLoadingRef.current = true;
  31. const sessionData = await loadSession();
  32. if (!isMounted) return;
  33. setSession(sessionData, true);
  34. }
  35. },
  36. [sessionLoaded, isMaster]
  37. );
  38. // Load game from master if any
  39. React.useEffect(() => {
  40. if (!isMaster && !sessionLoaded && !gameLoadingRef.current) {
  41. gameLoadingRef.current = true;
  42. const onReceiveGame = (receivedSession) => {
  43. setSession(receivedSession);
  44. };
  45. c2c.call("getSession").then(onReceiveGame, () => {
  46. setTimeout(
  47. () =>
  48. c2c
  49. .call("getSession")
  50. .then(onReceiveGame, (error) =>
  51. console.log("Failed to call getSession with error", error)
  52. ),
  53. 1000
  54. );
  55. });
  56. }
  57. }, [c2c, isMaster, sessionLoaded, setSession]);
  58. const mediaLibraries = React.useMemo(
  59. () =>
  60. gameId
  61. ? [
  62. {
  63. id: "session",
  64. name: t("Session"),
  65. boxId: "session",
  66. resourceId: sessionId,
  67. },
  68. { id: "game", name: t("Game"), boxId: "game", resourceId: gameId },
  69. ]
  70. : [
  71. {
  72. id: "session",
  73. name: t("Session"),
  74. boxId: "session",
  75. resourceId: sessionId,
  76. },
  77. ],
  78. [gameId, sessionId, t]
  79. );
  80. if (!sessionLoaded) {
  81. return <Waiter message={t("Session loading...")} />;
  82. }
  83. return (
  84. <>
  85. <AutoSaveSession />
  86. <BoardView
  87. mediaLibraries={mediaLibraries}
  88. itemMap={itemMap}
  89. actionMap={actionMap}
  90. ItemFormComponent={ItemForm}
  91. />
  92. </>
  93. );
  94. };
  95. const ConnectedSessionView = ({ sessionId, fromGame }) => {
  96. return (
  97. <C2CProvider room={sessionId} channel="board">
  98. <SessionProvider sessionId={sessionId} fromGameId={fromGame}>
  99. <SessionView />
  100. </SessionProvider>
  101. </C2CProvider>
  102. );
  103. };
  104. export default ConnectedSessionView;