bitburner-src/src/ui/React/CodingContractPopup.tsx

67 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-09-05 01:09:30 +02:00
import React, { useState } from "react";
import { KEY } from "../../../utils/helpers/keyCodes";
2021-09-09 05:47:34 +02:00
import { CodingContract, CodingContractType, CodingContractTypes } from "../../CodingContracts";
import { ClickableTag, CopyableText } from "./CopyableText";
import { PopupCloseButton } from "./PopupCloseButton";
type IProps = {
2021-09-05 01:09:30 +02:00
c: CodingContract;
popupId: string;
onClose: () => void;
onAttempt: (answer: string) => void;
};
2021-06-14 21:42:38 +02:00
export function CodingContractPopup(props: IProps): React.ReactElement {
2021-09-05 01:09:30 +02:00
const [answer, setAnswer] = useState("");
2021-09-05 01:09:30 +02:00
function onChange(event: React.ChangeEvent<HTMLInputElement>): void {
setAnswer(event.target.value);
}
2021-09-05 01:09:30 +02:00
function onKeyDown(event: React.KeyboardEvent<HTMLInputElement>): void {
// React just won't cooperate on this one.
// "React.KeyboardEvent<HTMLInputElement>" seems like the right type but
// whatever ...
const value = (event.target as any).value;
2021-05-29 20:48:56 +02:00
2021-09-05 01:09:30 +02:00
if (event.keyCode === KEY.ENTER && value !== "") {
event.preventDefault();
props.onAttempt(answer);
} else if (event.keyCode === KEY.ESC) {
event.preventDefault();
props.onClose();
}
2021-09-05 01:09:30 +02:00
}
2021-09-05 01:09:30 +02:00
const contractType: CodingContractType = CodingContractTypes[props.c.type];
const description = [];
2021-09-09 05:47:34 +02:00
for (const [i, value] of contractType.desc(props.c.data).split("\n").entries())
description.push(<span key={i} dangerouslySetInnerHTML={{ __html: value + "<br />" }}></span>);
2021-09-05 01:09:30 +02:00
return (
<div>
<CopyableText value={props.c.type} tag={ClickableTag.Tag_h1} />
<br />
<br />
<p>
2021-09-09 05:47:34 +02:00
You are attempting to solve a Coding Contract. You have {props.c.getMaxNumTries() - props.c.tries} tries
remaining, after which the contract will self-destruct.
2021-09-05 01:09:30 +02:00
</p>
<br />
<p>{description}</p>
<br />
<input
className="text-input"
style={{ width: "50%", marginTop: "8px" }}
autoFocus={true}
placeholder="Enter Solution here"
value={answer}
onChange={onChange}
onKeyDown={onKeyDown}
/>
2021-09-09 05:47:34 +02:00
<PopupCloseButton popup={props.popupId} onClose={() => props.onAttempt(answer)} text={"Solve"} />
<PopupCloseButton popup={props.popupId} onClose={props.onClose} text={"Close"} />
2021-09-05 01:09:30 +02:00
</div>
);
}