frontpage/components/REPL/index.tsx

37 lines
1.5 KiB
TypeScript
Raw 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";
2021-12-26 15:16:09 +01:00
import styles from "../../styles/REPL/REPLComplete.module.css";
2021-12-26 16:51:35 +01: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-01-14 14:27:19 +01:00
modalManipulation: {
setModalVisible: CallableFunction;
setModalProject: CallableFunction;
}
}
const REPL: NextPage<IREPLProps> = ({ inputRef, modalManipulation }) => {
2022-02-05 00:47:36 +01:00
const date = new Date();
const [history, manipulateHistory] = useState<string[]>([`cer0 0S - ${date.toLocaleDateString()}`]);
const containerRef = useRef<HTMLDivElement>(null);
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-01-14 14:27:19 +01:00
<REPLInput historyCallback={onCommandExecuted} historyClear={onClearHistory} inputRef={inputRef} modalManipulation={modalManipulation} />
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;