frontpage/components/REPL/REPLHistory.tsx

97 lines
3.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";
2022-02-05 00:47:36 +01:00
import { BaseSyntheticEvent, MutableRefObject, useEffect, useRef } 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[];
2022-02-05 00:47:36 +01:00
inputRef: MutableRefObject<HTMLInputElement|null>;
2021-12-16 01:15:43 +01:00
}
const REPLHistory: NextPage<REPLHistoryParams> = ({history, inputRef}) => {
const focusInput = () => {if (inputRef.current) inputRef.current.focus();};
2021-12-26 16:17:40 +01:00
const forceInput = (e: BaseSyntheticEvent) => {
const command = (e.target as HTMLSpanElement).innerHTML;
if (inputRef.current) {
inputRef.current.value = command;
// TODO
// Fix this as this currently doesn't work
inputRef.current.dispatchEvent(new KeyboardEvent("keydown", {
key: "Enter",
keyCode: 13,
}));
}
return true;
2022-01-05 17:52:20 +01:00
};
2021-12-26 16:17:40 +01:00
const makeLinks = (line: string) => {
2021-12-26 15:37:08 +01:00
let idxStart = line.indexOf("#{");
let idxSep = line.indexOf("|", idxStart);
let idxEnd = line.indexOf("}", idxSep);
2021-12-26 16:17:40 +01:00
if (idxStart === -1 || idxSep === -1 || idxEnd === -1) return [line];
2021-12-26 15:37:08 +01:00
2022-01-05 17:52:20 +01:00
const result = [];
2021-12-26 15:37:08 +01:00
while (idxStart !== -1 && idxSep !== -1 && idxEnd !== -1) {
const linkText = line.substring(idxStart+2, idxSep);
const linkHref = line.substring(idxSep+1, idxEnd);
2022-03-14 11:06:13 +01:00
const isLocal = linkHref.startsWith("https://c0ntroller.de") || linkHref.startsWith("/");
2021-12-26 15:37:08 +01:00
result.push(line.substring(0, idxStart));
2022-03-14 11:06:13 +01:00
result.push(<Link href={linkHref}><a className={styles.link} target={isLocal ? "_self" : "_blank"} rel={isLocal ? "" : "noreferrer"}>{linkText}</a></Link>);
2021-12-26 15:37:08 +01:00
2022-01-05 17:52:20 +01:00
line = line.substring(idxEnd+1);
2021-12-26 15:37:08 +01:00
idxStart = line.indexOf("#{");
idxSep = line.indexOf("|", idxStart);
idxEnd = line.indexOf("}", idxSep);
}
2022-01-05 17:52:20 +01:00
result.push(line.substring(idxEnd+1));
2021-12-26 16:17:40 +01:00
return result;
2022-01-05 17:52:20 +01:00
};
2021-12-26 16:17:40 +01:00
const makeCommands = (line: string|JSX.Element, indexKey: number) => {
if (typeof line !== "string") return line;
let idxStart = line.indexOf("%{");
let idxEnd = line.indexOf("}", idxStart);
if (idxStart === -1 || idxEnd === -1) return line;
2022-01-05 17:52:20 +01:00
const result = [];
2021-12-26 16:17:40 +01:00
while (idxStart !== -1 && idxEnd !== -1) {
const cmdText = line.substring(idxStart+2, idxEnd);
result.push(line.substring(0, idxStart));
result.push(<span className={styles.cmd} onClick={forceInput} key={`${indexKey}${line.length}${cmdText}`}>{cmdText}</span>);
2022-01-05 17:52:20 +01:00
line = line.substring(idxEnd+1);
2021-12-26 16:17:40 +01:00
idxStart = line.indexOf("%{");
idxEnd = line.indexOf("}", idxStart);
}
2022-01-05 17:52:20 +01:00
result.push(line.substring(idxEnd+1));
2021-12-26 16:17:40 +01:00
return result;
2022-01-05 17:52:20 +01:00
};
2021-12-26 16:17:40 +01:00
const parseLine = (line: string) => {
if (line === "") return "\u00A0";
const resultLinks = makeLinks(line);
2022-01-05 17:52:20 +01:00
const resultAll = resultLinks.map(makeCommands);
return resultAll.flat();
};
2021-12-26 15:37:08 +01:00
2021-12-16 01:15:43 +01:00
return <div className={styles.container} onClick={focusInput}>
2022-02-17 16:04:48 +01:00
{ history.map((value, idx) => <div className={styles.line} key={idx}>
2021-12-26 15:37:08 +01:00
{parseLine(value)}
2022-02-17 16:04:48 +01:00
</div>)
}
{<noscript>
<div className={styles.line}>JavaScript must be enabled, else this site won&apos;t work.</div>
<div className={styles.line}>This site doesn&apos;t use any trackers, so please enable JS!</div>
</noscript>}
2021-12-16 00:21:14 +01:00
</div>;
};
export default REPLHistory;