2022-01-30 15:06:18 +01:00
|
|
|
import React, { useState, useEffect, Dispatch, SetStateAction } from "react";
|
2021-10-02 04:53:23 +02:00
|
|
|
import { EventEmitter } from "../../utils/EventEmitter";
|
2022-03-06 05:05:55 +01:00
|
|
|
import { Modal } from "./Modal";
|
2021-10-02 04:53:23 +02:00
|
|
|
import Typography from "@mui/material/Typography";
|
|
|
|
import Button from "@mui/material/Button";
|
2022-01-30 15:06:18 +01:00
|
|
|
import Select, { SelectChangeEvent } from "@mui/material/Select";
|
|
|
|
import TextField from '@mui/material/TextField';
|
|
|
|
import { KEY } from '../../utils/helpers/keyCodes';
|
|
|
|
import MenuItem from '@mui/material/MenuItem';
|
2021-10-02 04:53:23 +02:00
|
|
|
|
|
|
|
export const PromptEvent = new EventEmitter<[Prompt]>();
|
|
|
|
|
|
|
|
interface Prompt {
|
|
|
|
txt: string;
|
2022-01-31 19:15:13 +01:00
|
|
|
options?: { type?: string; choices?: string[] | { [key: string]: string | number } };
|
2022-01-30 15:06:18 +01:00
|
|
|
resolve: (result: boolean | string) => void;
|
2021-10-02 04:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function PromptManager(): React.ReactElement {
|
|
|
|
const [prompt, setPrompt] = useState<Prompt | null>(null);
|
|
|
|
useEffect(
|
|
|
|
() =>
|
|
|
|
PromptEvent.subscribe((p: Prompt) => {
|
|
|
|
setPrompt(p);
|
|
|
|
}),
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
|
2022-01-30 15:21:46 +01:00
|
|
|
const valueState = useState('')
|
|
|
|
|
2021-10-02 04:53:23 +02:00
|
|
|
function close(): void {
|
|
|
|
if (prompt === null) return;
|
|
|
|
prompt.resolve(false);
|
2022-01-30 15:21:46 +01:00
|
|
|
valueState[1]('')
|
2021-10-02 04:53:23 +02:00
|
|
|
setPrompt(null);
|
|
|
|
}
|
|
|
|
|
2022-01-30 15:06:18 +01:00
|
|
|
let promptRenderer;
|
|
|
|
switch (prompt?.options?.type) {
|
|
|
|
case 'text': {
|
|
|
|
promptRenderer = promptMenuText;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'select': {
|
|
|
|
promptRenderer = promptMenuSelect;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
promptRenderer = promptMenuBoolean;
|
|
|
|
}
|
2021-10-02 04:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{prompt != null && (
|
|
|
|
<Modal open={true} onClose={close}>
|
|
|
|
<Typography>{prompt.txt}</Typography>
|
2022-01-30 15:06:18 +01:00
|
|
|
{promptRenderer(prompt, setPrompt, valueState)}
|
2021-10-02 04:53:23 +02:00
|
|
|
</Modal>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2022-01-30 15:06:18 +01:00
|
|
|
|
|
|
|
function promptMenuBoolean(prompt: Prompt | null, setPrompt: Dispatch<SetStateAction<Prompt | null>>): React.ReactElement {
|
|
|
|
const yes = (): void => {
|
|
|
|
if (prompt !== null) {
|
|
|
|
prompt.resolve(true);
|
|
|
|
setPrompt(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const no = (): void => {
|
|
|
|
if (prompt !== null) {
|
|
|
|
prompt.resolve(false);
|
|
|
|
setPrompt(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', paddingTop: '10px' }}>
|
|
|
|
<Button style={{ marginRight: 'auto' }} onClick={yes}>Yes</Button>
|
|
|
|
<Button onClick={no}>No</Button>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function promptMenuText(prompt: Prompt | null, setPrompt: Dispatch<SetStateAction<Prompt | null>>, valueState: [string, Dispatch<SetStateAction<string>>]): React.ReactElement {
|
|
|
|
const [value, setValue] = valueState
|
|
|
|
|
|
|
|
const submit = (): void => {
|
|
|
|
if (prompt !== null) {
|
|
|
|
prompt.resolve(value);
|
|
|
|
setValue('')
|
|
|
|
setPrompt(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const onInput = (event: React.ChangeEvent<HTMLInputElement>): void => {
|
|
|
|
setValue(event.target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
const onKeyDown = (event: React.KeyboardEvent<HTMLInputElement>): void => {
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
if (prompt !== null && event.keyCode === KEY.ENTER) {
|
|
|
|
event.preventDefault();
|
|
|
|
submit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div style={{ display: 'flex', alignItems: 'center', paddingTop: '10px' }}>
|
|
|
|
<TextField
|
|
|
|
autoFocus
|
|
|
|
value={value}
|
|
|
|
onInput={onInput}
|
|
|
|
onKeyDown={onKeyDown}
|
|
|
|
style={{ flex: '1 0 auto' }}
|
|
|
|
InputProps={{
|
|
|
|
endAdornment: (
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
submit();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Confirm
|
|
|
|
</Button>
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function promptMenuSelect(prompt: Prompt | null, setPrompt: Dispatch<SetStateAction<Prompt | null>>, valueState: [string, Dispatch<SetStateAction<string>>]): React.ReactElement {
|
|
|
|
const [value, setValue] = valueState
|
|
|
|
|
|
|
|
const submit = (): void => {
|
|
|
|
if (prompt !== null) {
|
|
|
|
prompt.resolve(value);
|
|
|
|
setValue('');
|
|
|
|
setPrompt(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const onChange = (event: SelectChangeEvent<string>): void => {
|
|
|
|
setValue(event.target.value);
|
|
|
|
}
|
|
|
|
|
2022-01-31 19:15:13 +01:00
|
|
|
const getItems = (choices: string[] | { [key: string]: string | number }) : React.ReactElement[] => {
|
2022-01-30 15:06:18 +01:00
|
|
|
const content = [];
|
2022-01-31 19:15:13 +01:00
|
|
|
for (const i in choices) {
|
2022-01-30 15:06:18 +01:00
|
|
|
// @ts-ignore
|
2022-01-31 19:15:13 +01:00
|
|
|
content.push(<MenuItem value={i}>{choices[i]}</MenuItem>);
|
2022-01-30 15:06:18 +01:00
|
|
|
}
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div style={{ display: 'flex', alignItems: 'center', paddingTop: '10px' }}>
|
|
|
|
<Select onChange={onChange} value={value} style={{ flex: '1 0 auto' }}>
|
2022-01-31 19:15:13 +01:00
|
|
|
{getItems(prompt?.options?.choices || [])}
|
2022-01-30 15:06:18 +01:00
|
|
|
</Select>
|
|
|
|
<Button onClick={submit}>Confirm</Button>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|