bitburner-src/src/Terminal/ui/TerminalRoot.tsx

207 lines
5.8 KiB
TypeScript
Raw Normal View History

2021-09-17 02:14:09 +02:00
import React, { useState, useEffect, useRef } from "react";
2021-09-17 01:23:03 +02:00
import Typography from "@mui/material/Typography";
import List from "@mui/material/List";
import ListItem from "@mui/material/ListItem";
import { Link as MuiLink } from "@mui/material";
import { Theme } from "@mui/material/styles";
2021-09-17 02:14:09 +02:00
import makeStyles from "@mui/styles/makeStyles";
import createStyles from "@mui/styles/createStyles";
2021-09-17 01:23:03 +02:00
import Box from "@mui/material/Box";
2021-10-27 02:26:05 +02:00
import { ITerminal, Output, Link, RawOutput } from "../ITerminal";
2021-09-17 08:58:02 +02:00
import { IRouter } from "../../ui/Router";
2021-09-16 08:52:45 +02:00
import { IPlayer } from "../../PersonObjects/IPlayer";
import { TerminalInput } from "./TerminalInput";
2021-09-20 06:14:30 +02:00
import { TerminalEvents, TerminalClearEvents } from "../TerminalEvents";
2021-10-01 19:08:37 +02:00
import { BitFlumeModal } from "../../BitNode/ui/BitFlumeModal";
import { CodingContractModal } from "../../ui/React/CodingContractModal";
2021-09-24 00:47:43 +02:00
import _ from "lodash";
2021-09-16 20:43:39 +02:00
interface IActionTimerProps {
terminal: ITerminal;
}
function ActionTimer({ terminal }: IActionTimerProps): React.ReactElement {
return (
<Typography color={"primary"} paragraph={false}>
{terminal.getProgressText()}
</Typography>
);
}
2021-09-16 08:52:45 +02:00
const useStyles = makeStyles((theme: Theme) =>
createStyles({
nopadding: {
padding: theme.spacing(0),
2021-09-16 08:52:45 +02:00
},
preformatted: {
2021-09-16 20:55:55 +02:00
whiteSpace: "pre-wrap",
2021-09-18 08:21:48 +02:00
overflowWrap: "anywhere",
margin: theme.spacing(0),
2021-09-16 08:52:45 +02:00
},
2021-09-16 21:04:20 +02:00
list: {
padding: theme.spacing(0),
2021-09-16 21:04:20 +02:00
height: "100%",
},
2021-11-19 21:44:12 +01:00
success: {
whiteSpace: "pre-wrap",
overflowWrap: "anywhere",
margin: theme.spacing(0),
color: theme.colors.success,
},
error: {
whiteSpace: "pre-wrap",
overflowWrap: "anywhere",
margin: theme.spacing(0),
color: theme.palette.error.main,
},
primary: {
whiteSpace: "pre-wrap",
overflowWrap: "anywhere",
margin: theme.spacing(0),
color: theme.palette.primary.main,
},
info: {
whiteSpace: "pre-wrap",
overflowWrap: "anywhere",
margin: theme.spacing(0),
color: theme.palette.info.main,
},
warning: {
whiteSpace: "pre-wrap",
overflowWrap: "anywhere",
margin: theme.spacing(0),
color: theme.palette.warning.main,
},
2021-09-16 08:52:45 +02:00
}),
);
interface IProps {
terminal: ITerminal;
2021-09-17 08:58:02 +02:00
router: IRouter;
2021-09-16 08:52:45 +02:00
player: IPlayer;
}
2021-09-17 08:58:02 +02:00
export function TerminalRoot({ terminal, router, player }: IProps): React.ReactElement {
2021-09-17 02:14:09 +02:00
const scrollHook = useRef<HTMLDivElement>(null);
2021-09-19 06:46:39 +02:00
const setRerender = useState(0)[1];
2021-09-20 06:14:30 +02:00
const [key, setKey] = useState(0);
2021-09-16 08:52:45 +02:00
function rerender(): void {
2021-09-19 06:46:39 +02:00
setRerender((old) => old + 1);
2021-09-16 08:52:45 +02:00
}
2021-09-20 06:14:30 +02:00
function clear(): void {
setKey((key) => key + 1);
}
useEffect(() => {
const debounced = _.debounce(async () => rerender(), 25, { maxWait: 50 });
const unsubscribe = TerminalEvents.subscribe(debounced);
return () => {
debounced.cancel();
unsubscribe();
}
}, []);
useEffect(() => {
const debounced = _.debounce(async () => clear(), 25, { maxWait: 50 });
const unsubscribe = TerminalClearEvents.subscribe(debounced);
return () => {
debounced.cancel();
unsubscribe();
}
}, []);
2021-09-16 08:52:45 +02:00
function doScroll(): number | undefined {
2021-09-17 03:49:38 +02:00
const hook = scrollHook.current;
if (hook !== null) {
return window.setTimeout(() => hook.scrollIntoView(true), 50);
2021-09-17 03:49:38 +02:00
}
2021-09-17 02:14:09 +02:00
}
2021-09-17 03:49:38 +02:00
doScroll();
useEffect(() => {
let scrollId: number;
const id = setTimeout(() => {
scrollId = doScroll() ?? 0;
}, 50);
return () => {
clearTimeout(id);
clearTimeout(scrollId);
}
2021-09-17 03:49:38 +02:00
}, []);
2021-11-19 21:44:12 +01:00
function lineClass(s: string): string {
if (s === "error") {
return classes.error;
}
if (s === "success") {
return classes.success;
}
if (s === "info") {
return classes.info;
}
if (s === "warn") {
return classes.warning;
}
return classes.primary;
}
2021-09-16 08:52:45 +02:00
const classes = useStyles();
return (
2021-09-17 02:14:09 +02:00
<>
2021-09-17 08:04:44 +02:00
<Box width="100%" minHeight="100vh" display={"flex"} alignItems={"flex-end"}>
2021-09-20 06:14:30 +02:00
<List key={key} id="terminal" classes={{ root: classes.list }}>
2021-09-17 02:14:09 +02:00
{terminal.outputHistory.map((item, i) => {
if (item instanceof Output)
return (
<ListItem key={i} classes={{ root: classes.nopadding }}>
2021-11-19 21:44:12 +01:00
<Typography classes={{ root: lineClass(item.color) }} paragraph={false}>
2021-09-17 02:14:09 +02:00
{item.text}
</Typography>
</ListItem>
);
2021-10-27 02:26:05 +02:00
if (item instanceof RawOutput)
return (
<ListItem key={i} classes={{ root: classes.nopadding }}>
<Typography classes={{ root: classes.preformatted }} paragraph={false}>
{item.raw}
</Typography>
</ListItem>
);
2021-09-17 02:14:09 +02:00
if (item instanceof Link)
return (
<ListItem key={i} classes={{ root: classes.nopadding }}>
2021-09-22 18:56:55 +02:00
<Typography>{item.dashes}&gt;&nbsp;</Typography>
<MuiLink
2021-09-17 02:14:09 +02:00
classes={{ root: classes.preformatted }}
color={"secondary"}
paragraph={false}
onClick={() => terminal.connectToServer(player, item.hostname)}
>
2021-10-01 22:42:07 +02:00
<Typography>{item.hostname}</Typography>
2021-09-17 02:14:09 +02:00
</MuiLink>
</ListItem>
);
})}
2021-09-17 09:08:15 +02:00
{terminal.action !== null && (
<ListItem classes={{ root: classes.nopadding }}>
<ActionTimer terminal={terminal} />{" "}
</ListItem>
)}
2021-09-17 02:14:09 +02:00
</List>
<div ref={scrollHook}></div>
</Box>
2021-09-17 08:04:44 +02:00
<Box position="sticky" bottom={0} width="100%" px={0}>
2021-09-17 08:58:02 +02:00
<TerminalInput player={player} router={router} terminal={terminal} />
2021-09-17 02:14:09 +02:00
</Box>
2021-10-01 19:08:37 +02:00
<BitFlumeModal />
<CodingContractModal />
2021-09-17 02:14:09 +02:00
</>
2021-09-16 08:52:45 +02:00
);
}