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

93 lines
2.7 KiB
TypeScript
Raw Normal View History

import React, { useState, useEffect } from "react";
2021-09-16 08:52:45 +02:00
import Typography from "@material-ui/core/Typography";
import List from "@material-ui/core/List";
import ListItem from "@material-ui/core/ListItem";
2021-09-16 20:43:39 +02:00
import { Link as MuiLink } from "@material-ui/core";
2021-09-16 08:52:45 +02:00
import { makeStyles, createStyles, Theme } from "@material-ui/core/styles";
import Box from "@material-ui/core/Box";
import { ITerminal, Output, Link } from "../ITerminal";
2021-09-16 08:52:45 +02:00
import { IEngine } from "../../IEngine";
import { IPlayer } from "../../PersonObjects/IPlayer";
import { TerminalInput } from "./TerminalInput";
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",
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-09-16 08:52:45 +02:00
}),
);
interface IProps {
terminal: ITerminal;
engine: IEngine;
player: IPlayer;
}
export function TerminalRoot({ terminal, engine, player }: IProps): React.ReactElement {
const setRerender = useState(false)[1];
function rerender(): void {
setRerender((old) => !old);
}
useEffect(() => {
const id = setInterval(() => {
if (terminal.pollChanges()) rerender();
}, 100);
return () => clearInterval(id);
}, []);
const classes = useStyles();
return (
2021-09-16 20:43:39 +02:00
<Box position="fixed" bottom="0" width="100%" px={1}>
2021-09-16 21:04:20 +02:00
<List classes={{ root: classes.list }}>
2021-09-16 20:43:39 +02:00
{terminal.outputHistory.map((item, i) => {
if (item instanceof Output)
return (
<ListItem key={i} classes={{ root: classes.nopadding }}>
<Typography classes={{ root: classes.preformatted }} color={item.color} paragraph={false}>
{item.text}
</Typography>
</ListItem>
);
if (item instanceof Link)
return (
<ListItem key={i} classes={{ root: classes.nopadding }}>
<MuiLink
classes={{ root: classes.preformatted }}
color={"secondary"}
paragraph={false}
onClick={() => terminal.connectToServer(player, item.hostname)}
>
&gt;&nbsp;{item.hostname}
</MuiLink>
</ListItem>
);
})}
2021-09-16 08:52:45 +02:00
</List>
2021-09-16 20:43:39 +02:00
{terminal.action !== null && <ActionTimer terminal={terminal} />}
<TerminalInput player={player} engine={engine} terminal={terminal} />
2021-09-16 08:52:45 +02:00
</Box>
);
}