|
1 | | -import { useStage, usePlayer } from "@empirica/core/player/classic/react"; |
2 | | -import React, { useEffect, useMemo } from "react"; |
3 | | -import { |
4 | | - TimeConditionalRender, |
5 | | - PositionConditionalRender, |
6 | | - ConditionsConditionalRender, |
7 | | - SubmissionConditionalRender, |
8 | | -} from "./components/ConditionalRender"; |
9 | | -import { Discussion } from "./elements/Discussion"; |
10 | | -import { Element } from "./elements/Element"; |
11 | | - |
12 | | -export function Stage() { |
13 | | - console.log("Before use stage"); |
14 | | - |
15 | | - const stage = useStage(); |
16 | | - const player = usePlayer(); |
17 | | - |
18 | | - const progressLabel = useMemo( |
19 | | - () => |
20 | | - `game_${stage.get("index")}_${stage |
21 | | - .get("name") |
22 | | - .trim() |
23 | | - .replace(/ /g, "_")}`, // replace ALL spaces with underscores |
24 | | - [stage] |
25 | | - ); // memoize so we don't trigger the useEffect on every render |
26 | | - |
27 | | - useEffect(() => { |
28 | | - if (player.get("progressLabel") !== progressLabel) { |
29 | | - console.log(`Starting ${progressLabel}`); |
30 | | - player.set("progressLabel", progressLabel); |
31 | | - player.set("localStageStartTime", undefined); // force use of stageTimer |
32 | | - } |
33 | | - }, [progressLabel, player]); |
34 | | - |
35 | | - const discussion = stage?.get("discussion"); |
36 | | - const elements = stage?.get("elements") || []; |
37 | | - |
38 | | - // console.log("Elements in stage", elements); |
39 | | - |
40 | | - const renderElement = (element, index) => ( |
41 | | - <TimeConditionalRender |
42 | | - displayTime={element.displayTime} |
43 | | - hideTime={element.hideTime} |
44 | | - key={`element_${index}`} |
45 | | - > |
46 | | - <PositionConditionalRender |
47 | | - showToPositions={element.showToPositions} |
48 | | - hideFromPositions={element.hideFromPositions} |
49 | | - > |
50 | | - <ConditionsConditionalRender conditions={element.conditions}> |
51 | | - <Element |
52 | | - element={element} |
53 | | - onSubmit={() => player.stage.set("submit", true)} |
54 | | - /> |
55 | | - </ConditionsConditionalRender> |
56 | | - </PositionConditionalRender> |
57 | | - </TimeConditionalRender> |
58 | | - ); |
59 | | - |
60 | | - const renderDiscussionPage = () => ( |
61 | | - // If the page is larger than 'md', render two columns |
62 | | - // with the left being the discussion at a fixed location |
63 | | - // and the right being the elements. |
64 | | - // If the page is smaller than 'md' render the discussion at the top |
65 | | - // and the elements below it. |
66 | | - |
67 | | - <> |
68 | | - <div className="md:absolute md:left-0 md:top-0 md:bottom-0 md:right-150"> |
69 | | - <Discussion |
70 | | - chatType={discussion.chatType} |
71 | | - showNickname={discussion.showNickname ?? true} |
72 | | - showTitle={discussion.showTitle} |
73 | | - /> |
74 | | - </div> |
75 | | - |
76 | | - <div className="pb-4 px-4 md:absolute md:right-0 md:w-150 md:bottom-0 md:top-0 md:overflow-auto md:scroll-smooth"> |
77 | | - {elements.map(renderElement)} |
78 | | - </div> |
79 | | - </> |
80 | | - ); |
81 | | - |
82 | | - const renderNoDiscussionPage = () => ( |
83 | | - <div className="mx-auto max-w-2xl pb-2">{elements.map(renderElement)}</div> |
84 | | - ); |
85 | | - |
86 | | - return ( |
87 | | - <SubmissionConditionalRender> |
88 | | - {!!discussion && renderDiscussionPage()} |
89 | | - {!discussion && renderNoDiscussionPage()} |
90 | | - </SubmissionConditionalRender> |
91 | | - ); |
92 | | -} |
| 1 | +import { useStage, usePlayer } from "@empirica/core/player/classic/react"; |
| 2 | +import React, { useEffect, useMemo } from "react"; |
| 3 | +import { |
| 4 | + TimeConditionalRender, |
| 5 | + PositionConditionalRender, |
| 6 | + ConditionsConditionalRender, |
| 7 | + SubmissionConditionalRender, |
| 8 | +} from "./components/ConditionalRender"; |
| 9 | +import { Discussion } from "./elements/Discussion"; |
| 10 | +import { Element } from "./elements/Element"; |
| 11 | + |
| 12 | +export function Stage() { |
| 13 | + |
| 14 | + const stage = useStage(); |
| 15 | + const player = usePlayer(); |
| 16 | + |
| 17 | + const progressLabel = useMemo( |
| 18 | + () => |
| 19 | + `game_${stage.get("index")}_${stage |
| 20 | + .get("name") |
| 21 | + .trim() |
| 22 | + .replace(/ /g, "_")}`, // replace ALL spaces with underscores |
| 23 | + [stage] |
| 24 | + ); // memoize so we don't trigger the useEffect on every render |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + if (player.get("progressLabel") !== progressLabel) { |
| 28 | + console.log(`Starting ${progressLabel}`); |
| 29 | + player.set("progressLabel", progressLabel); |
| 30 | + player.set("localStageStartTime", undefined); // force use of stageTimer |
| 31 | + } |
| 32 | + }, [progressLabel, player]); |
| 33 | + |
| 34 | + const discussion = stage?.get("discussion"); |
| 35 | + const elements = stage?.get("elements") || []; |
| 36 | + |
| 37 | + const renderElement = (element, index) => ( |
| 38 | + <TimeConditionalRender |
| 39 | + displayTime={element.displayTime} |
| 40 | + hideTime={element.hideTime} |
| 41 | + key={`element_${index}`} |
| 42 | + > |
| 43 | + <PositionConditionalRender |
| 44 | + showToPositions={element.showToPositions} |
| 45 | + hideFromPositions={element.hideFromPositions} |
| 46 | + > |
| 47 | + <ConditionsConditionalRender conditions={element.conditions}> |
| 48 | + <Element |
| 49 | + element={element} |
| 50 | + onSubmit={() => player.stage.set("submit", true)} |
| 51 | + /> |
| 52 | + </ConditionsConditionalRender> |
| 53 | + </PositionConditionalRender> |
| 54 | + </TimeConditionalRender> |
| 55 | + ); |
| 56 | + |
| 57 | + const renderDiscussionPage = () => ( |
| 58 | + // If the page is larger than 'md', render two columns |
| 59 | + // with the left being the discussion at a fixed location |
| 60 | + // and the right being the elements. |
| 61 | + // If the page is smaller than 'md' render the discussion at the top |
| 62 | + // and the elements below it. |
| 63 | + |
| 64 | + <> |
| 65 | + <div className="md:absolute md:left-0 md:top-0 md:bottom-0 md:right-150"> |
| 66 | + <Discussion |
| 67 | + chatType={discussion.chatType} |
| 68 | + showNickname={discussion.showNickname ?? true} |
| 69 | + showTitle={discussion.showTitle} |
| 70 | + /> |
| 71 | + </div> |
| 72 | + |
| 73 | + <div className="pb-4 px-4 md:absolute md:right-0 md:w-150 md:bottom-0 md:top-0 md:overflow-auto md:scroll-smooth"> |
| 74 | + {elements.map(renderElement)} |
| 75 | + </div> |
| 76 | + </> |
| 77 | + ); |
| 78 | + |
| 79 | + const renderNoDiscussionPage = () => ( |
| 80 | + <div className="mx-auto max-w-2xl pb-2">{elements.map(renderElement)}</div> |
| 81 | + ); |
| 82 | + |
| 83 | + return ( |
| 84 | + <SubmissionConditionalRender> |
| 85 | + {!!discussion && renderDiscussionPage()} |
| 86 | + {!discussion && renderNoDiscussionPage()} |
| 87 | + </SubmissionConditionalRender> |
| 88 | + ); |
| 89 | +} |
0 commit comments