bitburner-src/src/ui/React/CinematicLine.tsx

45 lines
971 B
TypeScript
Raw Normal View History

2021-09-13 00:03:07 +02:00
import React, { useState, useEffect } from "react";
2021-09-27 23:09:48 +02:00
import Typography from "@mui/material/Typography";
2021-09-13 00:03:07 +02:00
interface IProps {
text: string;
onDone?: () => void;
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export function CinematicLine(props: IProps): React.ReactElement {
const [length, setLength] = useState(0);
const [done, setDone] = useState(false);
function advance(): void {
const newLength = length + 1;
setLength(newLength);
setDone(newLength >= props.text.length);
}
useEffect(() => {
if (done && props.onDone) {
props.onDone();
return;
}
let cancel = false;
(async () => {
await sleep(10).then(() => !cancel && advance());
})();
return () => {
cancel = true;
};
});
return (
<>
2021-09-27 23:09:48 +02:00
<Typography>
2021-09-13 00:03:07 +02:00
{props.text.slice(0, length)}
{!done && <span>&#9608;</span>}
2021-09-27 23:09:48 +02:00
</Typography>
2021-09-13 00:03:07 +02:00
</>
);
}