frontpage/components/Terminal/REPL/REPLHistory.tsx

101 lines
3.6 KiB
TypeScript
Raw Permalink 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-06-12 14:22:15 +02:00
import type { BaseSyntheticEvent, MutableRefObject } from "react";
2022-09-30 19:38:25 +02:00
import styles from "../../../styles/Terminal/REPL/REPLHistory.module.css";
2021-12-16 00:21:14 +01:00
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);
const isLocal = linkHref.startsWith("https://c0ntroller.de") || linkHref.startsWith("/") || linkHref.startsWith("#");
2021-12-26 15:37:08 +01:00
result.push(line.substring(0, idxStart));
2022-08-10 16:55:46 +02:00
result.push(<Link href={linkHref} key={`${linkHref}${line.length}`}><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);
}
// Its already cut off
result.push(line);
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
2022-08-10 16:55:46 +02:00
const makeCommands = (line: string|JSX.Element) => {
2021-12-26 16:17:40 +01:00
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));
2022-08-10 16:55:46 +02:00
result.push(<span className={styles.cmd} onClick={forceInput} key={`${cmdText}${line.length}${cmdText}`}>{cmdText}</span>);
2021-12-26 16:17:40 +01:00
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);
}
// Its already cut off
result.push(line);
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-06-21 18:44:20 +02:00
{ history.map((value, idx) => <div className={styles.line} key={`${idx}${value}`}>
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;