frontpage/components/REPL/index.tsx

22 lines
809 B
TypeScript
Raw Normal View History

2021-12-16 01:15:43 +01:00
import { 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-15 18:56:32 +01:00
const REPL = () => {
2021-12-16 00:21:14 +01:00
const [history, manipulateHistory] = useState<string[]>([]);
2021-12-16 01:15:43 +01:00
const inputRef = useRef<HTMLInputElement>();
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();
}
2021-12-16 00:21:14 +01:00
return (<>
2021-12-16 01:15:43 +01:00
<REPLHistory history={history} inputRef={inputRef} />
2021-12-26 14:17:11 +01: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-16 00:21:14 +01:00
</>);
2021-12-15 18:56:32 +01:00
};
export default REPL;