bitburner-src/src/ui/LoadingScreen.tsx

77 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-09-20 00:04:12 +02:00
import React, { useState, useEffect } from "react";
import CircularProgress from "@mui/material/CircularProgress";
import Typography from "@mui/material/Typography";
import Grid from "@mui/material/Grid";
2021-09-21 22:49:38 +02:00
import { load } from "../db";
import { Engine } from "../engine";
import { GameRoot } from "./GameRoot";
2021-09-20 00:04:12 +02:00
import { CONSTANTS } from "../Constants";
2021-11-02 22:28:19 +01:00
import { ActivateRecoveryMode } from "./React/RecoveryRoot";
2021-12-17 02:07:34 +01:00
import { hash } from "../hash/hash";
import { pushGameReady } from "../Electron";
2021-09-20 00:04:12 +02:00
export function LoadingScreen(): React.ReactElement {
const [show, setShow] = useState(false);
const [loaded, setLoaded] = useState(false);
2021-09-20 00:04:12 +02:00
const version = `v${CONSTANTS.VersionString} (${hash()})`;
if (process.env.NODE_ENV === "development") {
document.title = `[dev] Bitburner ${version}`;
} else {
document.title = `Bitburner ${version}`;
}
2021-09-20 00:04:12 +02:00
useEffect(() => {
const id = setTimeout(() => {
if (!loaded) setShow(true);
2021-09-20 00:04:12 +02:00
}, 2000);
return () => clearTimeout(id);
});
useEffect(() => {
2021-09-25 07:26:03 +02:00
async function doLoad(): Promise<void> {
2021-09-21 22:49:38 +02:00
await load()
.then((saveString) => {
2021-11-02 22:28:19 +01:00
try {
Engine.load(saveString);
2022-07-15 07:51:30 +02:00
} catch (err: unknown) {
2021-11-02 22:28:19 +01:00
ActivateRecoveryMode();
setLoaded(true);
throw err;
}
pushGameReady();
2021-09-21 22:49:38 +02:00
setLoaded(true);
})
.catch((reason) => {
console.error(reason);
Engine.load("");
setLoaded(true);
});
}
doLoad();
2021-09-20 00:04:12 +02:00
}, []);
2022-08-16 08:32:25 +02:00
return loaded ? (
2022-08-29 08:41:17 +02:00
<GameRoot />
2022-08-16 08:32:25 +02:00
) : (
<Grid container direction="column" justifyContent="center" alignItems="center" style={{ minHeight: "100vh" }}>
<Grid item>
<CircularProgress size={150} color="primary" />
</Grid>
<Grid item>
<Typography variant="h3">Loading Bitburner {version}</Typography>
</Grid>
{show && (
<Grid item>
<Typography>
If the game fails to load, consider <a href="?noScripts">killing all scripts</a>
</Typography>
2021-09-20 00:04:12 +02:00
</Grid>
)}
2022-08-16 08:32:25 +02:00
</Grid>
2021-09-20 00:04:12 +02:00
);
}