Merge pull request #2475 from TheMas3212/refactor-gameroot-v2

Refactor GameRoot.tsx to use a switch case statement instead of 20 nested ternarys
This commit is contained in:
hydroflame 2022-01-08 15:29:47 -05:00 committed by GitHub
commit d74efd6a4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -300,81 +300,125 @@ export function GameRoot({ player, engine, terminal }: IProps): React.ReactEleme
if (page !== Page.Terminal) window.scrollTo(0, 0); if (page !== Page.Terminal) window.scrollTo(0, 0);
}); });
return ( let mainPage = <Typography>Cannot load</Typography>;
<Context.Player.Provider value={player}> let withSidebar = true;
<Context.Router.Provider value={Router}> let withPopups = true;
<SnackbarProvider> switch (page) {
<Overview> case Page.Recovery: {
{!ITutorial.isRunning ? ( mainPage = <RecoveryRoot router={Router} />;
<CharacterOverview save={() => saveObject.saveGame()} killScripts={killAllScripts} /> withSidebar = false;
) : ( withPopups = false;
<InteractiveTutorialRoot /> break;
)} }
</Overview> case Page.BitVerse: {
{page === Page.Recovery ? ( mainPage = <BitverseRoot flume={flume} enter={enterBitNode} quick={quick} />;
<RecoveryRoot router={Router} /> withSidebar = false;
) : page === Page.BitVerse ? ( withPopups = false;
<BitverseRoot flume={flume} enter={enterBitNode} quick={quick} /> break
) : page === Page.Infiltration ? ( }
<InfiltrationRoot location={location} /> case Page.Infiltration: {
) : page === Page.BladeburnerCinematic ? ( mainPage = <InfiltrationRoot location={location} />;
<BladeburnerCinematic /> withSidebar = false;
) : page === Page.Work ? ( withPopups = false;
<WorkInProgressRoot /> break;
) : ( }
<Box display="flex" flexDirection="row" width="100%"> case Page.BladeburnerCinematic: {
<SidebarRoot player={player} router={Router} page={page} /> mainPage = <BladeburnerCinematic />;
<Box className={classes.root} flexGrow={1} display="block" px={1} min-height="100vh"> withSidebar = false;
{page === Page.Terminal ? ( withPopups = false;
<TerminalRoot terminal={terminal} router={Router} player={player} /> break;
) : page === Page.Sleeves ? ( }
<SleeveRoot /> case Page.Work: {
) : page === Page.StaneksGift ? ( mainPage = <WorkInProgressRoot />;
<StaneksGiftRoot staneksGift={staneksGift} /> withSidebar = false;
) : page === Page.Stats ? ( break;
<CharacterStats /> }
) : page === Page.ScriptEditor ? ( case Page.Terminal: {
<ScriptEditorRoot mainPage = <TerminalRoot terminal={terminal} router={Router} player={player} />;
break;
}
case Page.Sleeves: {
mainPage = <SleeveRoot />;
break;
}
case Page.StaneksGift: {
mainPage = <StaneksGiftRoot staneksGift={staneksGift} />;
break;
}
case Page.Stats: {
mainPage = <CharacterStats />;
break;
}
case Page.ScriptEditor: {
mainPage = <ScriptEditorRoot
files={files} files={files}
hostname={player.getCurrentServer().hostname} hostname={player.getCurrentServer().hostname}
player={player} player={player}
router={Router} router={Router}
vim={vim} vim={vim}
/> />;
) : page === Page.ActiveScripts ? ( break;
<ActiveScriptsRoot workerScripts={workerScripts} /> }
) : page === Page.Hacknet ? ( case Page.ActiveScripts: {
<HacknetRoot player={player} /> mainPage = <ActiveScriptsRoot workerScripts={workerScripts} />;
) : page === Page.CreateProgram ? ( break;
<ProgramsRoot /> }
) : page === Page.Factions ? ( case Page.Hacknet: {
<FactionsRoot player={player} router={Router} /> mainPage = <HacknetRoot player={player} />;
) : page === Page.Faction ? ( break;
<FactionRoot faction={faction} /> }
) : page === Page.Milestones ? ( case Page.CreateProgram: {
<MilestonesRoot player={player} /> mainPage = <ProgramsRoot />;
) : page === Page.Tutorial ? ( break;
<TutorialRoot }
case Page.Factions: {
mainPage = <FactionsRoot player={player} router={Router} />;
break;
}
case Page.Faction: {
mainPage = <FactionRoot faction={faction} />;
break;
}
case Page.Milestones: {
mainPage = <MilestonesRoot player={player} />;
break;
}
case Page.Tutorial: {
mainPage = <TutorialRoot
reactivateTutorial={() => { reactivateTutorial={() => {
prestigeAugmentation(); prestigeAugmentation();
Router.toTerminal(); Router.toTerminal();
iTutorialStart(); iTutorialStart();
}} }}
/> />;
) : page === Page.DevMenu ? ( break;
<DevMenuRoot player={player} engine={engine} router={Router} /> }
) : page === Page.Gang ? ( case Page.DevMenu: {
<GangRoot /> mainPage = <DevMenuRoot player={player} engine={engine} router={Router} />;
) : page === Page.Corporation ? ( break;
<CorporationRoot /> }
) : page === Page.Bladeburner ? ( case Page.Gang: {
<BladeburnerRoot /> mainPage = <GangRoot />;
) : page === Page.Resleeves ? ( break;
<ResleeveRoot /> }
) : page === Page.Travel ? ( case Page.Corporation: {
<TravelAgencyRoot p={player} router={Router} /> mainPage = <CorporationRoot />;
) : page === Page.StockMarket ? ( break;
<StockMarketRoot }
case Page.Bladeburner: {
mainPage = <BladeburnerRoot />;
break;
}
case Page.Resleeves: {
mainPage = <ResleeveRoot />;
break;
}
case Page.Travel: {
mainPage = <TravelAgencyRoot p={player} router={Router} />;
break;
}
case Page.StockMarket: {
mainPage = <StockMarketRoot
buyStockLong={buyStock} buyStockLong={buyStock}
buyStockShort={shortStock} buyStockShort={shortStock}
cancelOrder={cancelOrder} cancelOrder={cancelOrder}
@ -385,15 +429,20 @@ export function GameRoot({ player, engine, terminal }: IProps): React.ReactEleme
sellStockLong={sellStock} sellStockLong={sellStock}
sellStockShort={sellShort} sellStockShort={sellShort}
stockMarket={StockMarket} stockMarket={StockMarket}
/> />;
) : page === Page.City ? ( break;
<LocationCity /> }
) : page === Page.Job ? ( case Page.City: {
<GenericLocation loc={location} /> mainPage = <LocationCity />;
) : page === Page.Location ? ( break;
<GenericLocation loc={location} /> }
) : page === Page.Options ? ( case Page.Job:
<GameOptionsRoot case Page.Location: {
mainPage = <GenericLocation loc={location} />;
break;
}
case Page.Options: {
mainPage = <GameOptionsRoot
player={player} player={player}
save={() => saveObject.saveGame()} save={() => saveObject.saveGame()}
export={() => { export={() => {
@ -407,9 +456,11 @@ export function GameRoot({ player, engine, terminal }: IProps): React.ReactEleme
prestigeAugmentation(); prestigeAugmentation();
Router.toTerminal(); Router.toTerminal();
}} }}
/> />;
) : page === Page.Augmentations ? ( break;
<AugmentationsRoot }
case Page.Augmentations: {
mainPage = <AugmentationsRoot
exportGameFn={() => { exportGameFn={() => {
// Apply the export bonus before saving the game // Apply the export bonus before saving the game
onExport(player); onExport(player);
@ -419,23 +470,44 @@ export function GameRoot({ player, engine, terminal }: IProps): React.ReactEleme
installAugmentations(); installAugmentations();
Router.toTerminal(); Router.toTerminal();
}} }}
/> />;
) : page === Page.Achievements ? ( break;
<AchievementsRoot /> }
case Page.Achievements: {
mainPage = <AchievementsRoot />;
break;
}
}
return (
<Context.Player.Provider value={player}>
<Context.Router.Provider value={Router}>
<SnackbarProvider>
<Overview>
{!ITutorial.isRunning ? (
<CharacterOverview save={() => saveObject.saveGame()} killScripts={killAllScripts} />
) : ( ) : (
<> <InteractiveTutorialRoot />
<Typography>Cannot load</Typography>
</>
)} )}
</Overview>
{withSidebar ? (
<Box display="flex" flexDirection="row" width="100%">
<SidebarRoot player={player} router={Router} page={page} />
<Box className={classes.root} flexGrow={1} display="block" px={1} min-height="100vh">
{mainPage}
</Box> </Box>
</Box> </Box>
)} ) : mainPage }
<Unclickable /> <Unclickable />
{withPopups && (
<>
<LogBoxManager /> <LogBoxManager />
<AlertManager /> <AlertManager />
<PromptManager /> <PromptManager />
<InvitationModal /> <InvitationModal />
<Snackbar /> <Snackbar />
</>
)}
</SnackbarProvider> </SnackbarProvider>
</Context.Router.Provider> </Context.Router.Provider>
</Context.Player.Provider> </Context.Player.Provider>