frontpage/components/Terminal/REPL/index.tsx

34 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

2022-02-05 00:47:36 +01:00
import { MutableRefObject, useEffect, useRef, useState } from "react";
2021-12-15 18:56:32 +01:00
import REPLInput from "./REPLInput";
2021-12-16 00:21:14 +01:00
import REPLHistory from "./REPLHistory";
2022-09-30 19:38:25 +02:00
import styles from "../../../styles/Terminal/REPL/REPLComplete.module.css";
2022-06-21 18:44:20 +02:00
import type { NextPage } from "next";
2021-12-15 18:56:32 +01:00
2022-01-14 14:27:19 +01:00
interface IREPLProps {
2022-02-05 00:47:36 +01:00
inputRef: MutableRefObject<HTMLInputElement|null>;
2022-06-14 19:07:21 +02:00
buildTime: string;
2022-01-14 14:27:19 +01:00
}
2022-06-14 19:07:21 +02:00
const REPL: NextPage<IREPLProps> = ({ inputRef, buildTime }) => {
const [history, manipulateHistory] = useState<string[]>([`cer0 0S - Build ${buildTime}`]);
2022-02-05 00:47:36 +01:00
const containerRef = useRef<HTMLDivElement>(null);
2022-06-21 18:44:20 +02:00
2021-12-16 00:21:14 +01:00
const onCommandExecuted = (result: string[]) => manipulateHistory(result.reverse().concat(history).slice(0, 1000));
2021-12-26 14:17:11 +01:00
const onClearHistory = () => manipulateHistory([]);
2021-12-16 00:21:14 +01:00
2021-12-26 13:59:22 +01:00
const focusInput = () => {
if (inputRef.current) inputRef.current.focus();
2022-01-05 17:52:20 +01:00
};
2021-12-26 13:59:22 +01:00
2022-02-05 00:47:36 +01:00
useEffect(() => {
if(containerRef && containerRef.current) containerRef.current.scrollTo(0, containerRef.current.scrollHeight);
}, [history]);
return (<div className={styles.container} ref={containerRef}>
2021-12-16 01:15:43 +01:00
<REPLHistory history={history} inputRef={inputRef} />
2022-06-12 14:22:15 +02:00
<REPLInput historyCallback={onCommandExecuted} historyClear={onClearHistory} inputRef={inputRef} />
2021-12-26 13:59:22 +01:00
<div style={{flexGrow: 2}} onClick={focusInput}></div>
2021-12-26 15:16:09 +01:00
</div>);
2021-12-15 18:56:32 +01:00
};
export default REPL;