frontpage/components/REPL/REPLHistory.tsx

50 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-12-16 00:21:14 +01:00
import { NextPage } from "next";
2021-12-26 15:37:08 +01:00
import Link from "next/link";
2021-12-16 01:15:43 +01:00
import { MutableRefObject } from "react";
2021-12-16 00:21:14 +01:00
import styles from "../../styles/REPL/REPLHistory.module.css";
2021-12-16 01:15:43 +01:00
interface REPLHistoryParams {
history: string[];
inputRef: MutableRefObject<HTMLInputElement|undefined>;
}
const REPLHistory: NextPage<REPLHistoryParams> = ({history, inputRef}) => {
const focusInput = () => {if (inputRef.current) inputRef.current.focus();};
2021-12-26 15:37:08 +01:00
const parseLine = (line: string) => {
if (line === "") return "\u00A0";
let idxStart = line.indexOf("#{");
let idxSep = line.indexOf("|", idxStart);
let idxEnd = line.indexOf("}", idxSep);
if (idxStart === -1 || idxSep === -1 || idxEnd === -1) return line;
const result = []
while (idxStart !== -1 && idxSep !== -1 && idxEnd !== -1) {
const linkText = line.substring(idxStart+2, idxSep);
const linkHref = line.substring(idxSep+1, idxEnd);
result.push(line.substring(0, idxStart));
result.push(<Link href={linkHref}><a className={styles.link}>{linkText}</a></Link>);
line = line.substring(idxEnd+1)
idxStart = line.indexOf("#{");
idxSep = line.indexOf("|", idxStart);
idxEnd = line.indexOf("}", idxSep);
}
result.push(line.substring(idxEnd+1))
return result
}
2021-12-16 01:15:43 +01:00
return <div className={styles.container} onClick={focusInput}>
2021-12-26 15:37:08 +01:00
{ history.map((value, idx) => {
return <div className={styles.line} key={idx}>
{parseLine(value)}
</div>}
)}
2021-12-16 00:21:14 +01:00
</div>;
};
export default REPLHistory;