Skip to content

Commit 060b30e

Browse files
committed
lift state up
1 parent b1a4989 commit 060b30e

4 files changed

Lines changed: 35 additions & 36 deletions

File tree

src/App.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
ScatterTrace,
2727
Section,
2828
TrajectoryStatus,
29+
ViewType,
2930
} from "./types";
3031
import LeftPanel from "./components/main-layout/LeftPanel";
3132
import RightPanel from "./components/main-layout/RightPanel";
@@ -56,6 +57,7 @@ import useModule from "./hooks/useModule";
5657
import LandingPage from "./components/LandingPage";
5758

5859
function App() {
60+
const [currentView, setCurrentView] = useState<ViewType>(ViewType.Lab);
5961
const [page, setPage] = useState(FIRST_PAGE[Module.A_B_AB]);
6062
const [time, setTime] = useState(0);
6163
const [isPlaying, setIsPlaying] = useState(false);
@@ -377,6 +379,7 @@ function App() {
377379
]
378380
);
379381
const totalReset = useCallback(() => {
382+
setCurrentView(ViewType.Lab);
380383
const activeAgents = [AgentName.A, AgentName.B];
381384
setCurrentModule(Module.A_B_AB);
382385
const concentrations = simulationData.getInitialConcentrations(
@@ -521,6 +524,12 @@ function App() {
521524
clearAllAnalysisState();
522525
setCurrentModule(module);
523526
setIsPlaying(false);
527+
// the first module is the only one that starts with the lab view
528+
if (module === Module.A_B_AB) {
529+
setCurrentView(ViewType.Lab);
530+
} else {
531+
setCurrentView(ViewType.Simulation);
532+
}
524533
};
525534

526535
const handleStartExperiment = () => {
@@ -618,6 +627,12 @@ function App() {
618627
}, 3000);
619628
};
620629

630+
const handleSwitchView = () => {
631+
setCurrentView((prevView) =>
632+
prevView === ViewType.Lab ? ViewType.Simulation : ViewType.Lab
633+
);
634+
};
635+
621636
const handleRecordEquilibrium = () => {
622637
if (!clientSimulator) {
623638
return false;
@@ -717,11 +732,13 @@ function App() {
717732
setModule,
718733
setPage,
719734
setViewportSize,
735+
setViewportType: handleSwitchView,
720736
simulariumController,
721737
timeFactor,
722738
timeUnit: simulationData.timeUnit,
723739
trajectoryName,
724740
viewportSize,
741+
viewportType: currentView,
725742
addCompletedModule,
726743
completedModules,
727744
}}

src/components/ViewSwitch.tsx

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useContext, useState } from "react";
1+
import React, { useContext } from "react";
22

33
import Viewer from "./Viewer";
44
import { SimulariumContext } from "../simulation/context";
@@ -9,47 +9,19 @@ import LabIcon from "./icons/Lab";
99
import Molecules from "./icons/Molecules";
1010
import LabView from "./LabView";
1111
import VisibilityControl from "./shared/VisibilityControl";
12-
import { Module, Section } from "../types";
12+
import { Module, ViewType } from "../types";
1313
import { FIRST_PAGE } from "../content";
14-
import useModule from "../hooks/useModule";
1514
import { VIEW_SWITCH_ID } from "../constants";
1615

17-
enum View {
18-
Lab = "lab",
19-
Simulation = "simulation",
20-
}
21-
2216
const ViewSwitch: React.FC = () => {
23-
const [currentView, setCurrentView] = useState<View>(View.Lab);
24-
const [previousModule, setPreviousModule] = useState<Module>(Module.A_B_AB);
17+
const { viewportType, setViewportType } = useContext(SimulariumContext);
2518

26-
const switchView = () => {
27-
setCurrentView((prevView) =>
28-
prevView === View.Lab ? View.Simulation : View.Lab
29-
);
30-
};
3119
const { page, isPlaying, setIsPlaying, handleTimeChange, module } =
3220
useContext(SimulariumContext);
3321

3422
const isFirstPageOfFirstModule =
3523
page === FIRST_PAGE[module] + 1 && module === Module.A_B_AB;
3624

37-
if (isFirstPageOfFirstModule && currentView === View.Simulation) {
38-
setCurrentView(View.Lab);
39-
}
40-
41-
const { contentData } = useModule(module);
42-
43-
// Show the sim view at the beginning of the module
44-
if (module !== previousModule) {
45-
setPreviousModule(module);
46-
if (contentData[page].section === Section.Experiment) {
47-
if (currentView === View.Lab) {
48-
setCurrentView(View.Simulation);
49-
}
50-
}
51-
}
52-
5325
let buttonStyle: React.CSSProperties = {
5426
top: 16,
5527
right: 16,
@@ -73,22 +45,23 @@ const ViewSwitch: React.FC = () => {
7345
<VisibilityControl notInBonusMaterial>
7446
<ProgressionControl elementId={VIEW_SWITCH_ID}>
7547
<OverlayButton
76-
onClick={switchView}
48+
onClick={setViewportType}
7749
style={buttonStyle}
7850
icon={
79-
currentView === View.Lab ? (
51+
viewportType === ViewType.Lab ? (
8052
<Molecules />
8153
) : (
8254
<LabIcon />
8355
)
8456
}
8557
>
86-
{currentView === View.Lab ? "Molecular" : "Lab"} view
58+
{viewportType === ViewType.Lab ? "Molecular" : "Lab"}{" "}
59+
view
8760
</OverlayButton>
8861
</ProgressionControl>
8962
</VisibilityControl>
9063
<PlayButton />
91-
{currentView === View.Lab ? <LabView /> : null}
64+
{viewportType === ViewType.Lab ? <LabView /> : null}
9265
<Viewer
9366
isPlaying={isPlaying}
9467
setIsPlaying={setIsPlaying}

src/simulation/context.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
NANO,
1111
ProgressionElement,
1212
} from "../constants";
13-
import { AgentName, Module, ProductName, Section } from "../types";
13+
import { AgentName, Module, ProductName, Section, ViewType } from "../types";
1414

1515
interface SimulariumContextType {
1616
adjustableAgentName: AgentName;
@@ -34,11 +34,13 @@ interface SimulariumContextType {
3434
setModule: (value: Module) => void;
3535
setPage: (value: number) => void;
3636
setViewportSize: (value: { width: number; height: number }) => void;
37+
setViewportType: () => void;
3738
simulariumController: SimulariumController | null;
3839
timeFactor: number;
3940
timeUnit: string;
4041
trajectoryName: string;
4142
viewportSize: { width: number; height: number };
43+
viewportType: ViewType;
4244
addCompletedModule: (value: Module) => void;
4345
completedModules: Set<Module>;
4446
}
@@ -65,11 +67,13 @@ export const SimulariumContext = createContext({
6567
setModule: () => {},
6668
setPage: () => {},
6769
setViewportSize: () => {},
70+
setViewportType: () => {},
6871
simulariumController: null,
6972
timeFactor: 30,
7073
timeUnit: NANO,
7174
trajectoryName: LIVE_SIMULATION_NAME,
7275
viewportSize: DEFAULT_VIEWPORT_SIZE,
76+
viewportType: ViewType.Lab,
7377
addCompletedModule: () => {},
7478
completedModules: new Set(),
7579
} as SimulariumContextType);

src/types/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,8 @@ export enum TrajectoryStatus {
153153
LOADED,
154154
ERROR,
155155
}
156+
157+
export enum ViewType {
158+
Lab = "lab",
159+
Simulation = "simulation",
160+
}

0 commit comments

Comments
 (0)