2024-07-19 00:21:33 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2024-07-30 20:44:19 +02:00
|
|
|
import { Modal } from "@/app/modals/modal";
|
2024-07-19 00:21:33 +02:00
|
|
|
import { Markdown } from "@/element/markdown";
|
2024-07-30 20:44:19 +02:00
|
|
|
import { modalsModel } from "@/store/modalmodel";
|
2024-07-19 00:21:33 +02:00
|
|
|
import * as keyutil from "@/util/keyutil";
|
|
|
|
import { UserInputService } from "../store/services";
|
|
|
|
|
2024-09-06 00:35:57 +02:00
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
2024-07-19 00:21:33 +02:00
|
|
|
import "./userinputmodal.less";
|
|
|
|
|
2024-07-30 20:44:19 +02:00
|
|
|
const UserInputModal = (userInputRequest: UserInputRequest) => {
|
2024-09-06 00:35:57 +02:00
|
|
|
const [responseText, setResponseText] = useState("");
|
|
|
|
const [countdown, setCountdown] = useState(Math.floor(userInputRequest.timeoutms / 1000));
|
|
|
|
const checkboxStatus = useRef(false);
|
2024-07-19 00:21:33 +02:00
|
|
|
|
2024-09-06 00:35:57 +02:00
|
|
|
const handleSendCancel = useCallback(() => {
|
2024-07-19 00:21:33 +02:00
|
|
|
UserInputService.SendUserInputResponse({
|
|
|
|
type: "userinputresp",
|
|
|
|
requestid: userInputRequest.requestid,
|
|
|
|
errormsg: "Canceled by the user",
|
|
|
|
});
|
2024-07-30 20:44:19 +02:00
|
|
|
modalsModel.popModal();
|
2024-07-19 00:21:33 +02:00
|
|
|
}, [responseText, userInputRequest]);
|
|
|
|
|
2024-09-06 00:35:57 +02:00
|
|
|
const handleSendText = useCallback(() => {
|
2024-07-19 00:21:33 +02:00
|
|
|
UserInputService.SendUserInputResponse({
|
|
|
|
type: "userinputresp",
|
|
|
|
requestid: userInputRequest.requestid,
|
|
|
|
text: responseText,
|
|
|
|
checkboxstat: checkboxStatus.current,
|
|
|
|
});
|
2024-07-30 20:44:19 +02:00
|
|
|
modalsModel.popModal();
|
2024-07-19 00:21:33 +02:00
|
|
|
}, [responseText, userInputRequest]);
|
|
|
|
|
2024-09-06 00:35:57 +02:00
|
|
|
const handleSendConfirm = useCallback(() => {
|
2024-07-19 00:21:33 +02:00
|
|
|
UserInputService.SendUserInputResponse({
|
|
|
|
type: "userinputresp",
|
|
|
|
requestid: userInputRequest.requestid,
|
|
|
|
confirm: true,
|
|
|
|
checkboxstat: checkboxStatus.current,
|
|
|
|
});
|
2024-07-30 20:44:19 +02:00
|
|
|
modalsModel.popModal();
|
2024-07-19 00:21:33 +02:00
|
|
|
}, [userInputRequest]);
|
|
|
|
|
2024-09-06 00:35:57 +02:00
|
|
|
const handleSubmit = useCallback(() => {
|
2024-07-19 00:21:33 +02:00
|
|
|
switch (userInputRequest.responsetype) {
|
|
|
|
case "text":
|
|
|
|
handleSendText();
|
|
|
|
break;
|
|
|
|
case "confirm":
|
|
|
|
handleSendConfirm();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}, [handleSendConfirm, handleSendText, userInputRequest.responsetype]);
|
|
|
|
|
2024-09-06 00:35:57 +02:00
|
|
|
const handleKeyDown = useCallback(
|
2024-07-19 00:21:33 +02:00
|
|
|
(waveEvent: WaveKeyboardEvent): boolean => {
|
|
|
|
if (keyutil.checkKeyPressed(waveEvent, "Escape")) {
|
|
|
|
handleSendCancel();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (keyutil.checkKeyPressed(waveEvent, "Enter")) {
|
|
|
|
handleSubmit();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[handleSendCancel, handleSubmit]
|
|
|
|
);
|
|
|
|
|
2024-09-06 00:35:57 +02:00
|
|
|
const queryText = useMemo(() => {
|
2024-07-19 00:21:33 +02:00
|
|
|
if (userInputRequest.markdown) {
|
|
|
|
return <Markdown text={userInputRequest.querytext} className="userinput-markdown" />;
|
|
|
|
}
|
|
|
|
return <span className="userinput-text">{userInputRequest.querytext}</span>;
|
|
|
|
}, [userInputRequest.markdown, userInputRequest.querytext]);
|
|
|
|
|
2024-09-06 00:35:57 +02:00
|
|
|
const inputBox = useMemo(() => {
|
2024-07-19 00:21:33 +02:00
|
|
|
if (userInputRequest.responsetype === "confirm") {
|
|
|
|
return <></>;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<input
|
|
|
|
type={userInputRequest.publictext ? "text" : "password"}
|
|
|
|
onChange={(e) => setResponseText(e.target.value)}
|
|
|
|
value={responseText}
|
|
|
|
maxLength={400}
|
|
|
|
className="userinput-inputbox"
|
|
|
|
autoFocus={true}
|
|
|
|
onKeyDown={(e) => keyutil.keydownWrapper(handleKeyDown)(e)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}, [userInputRequest.responsetype, userInputRequest.publictext, responseText, handleKeyDown, setResponseText]);
|
|
|
|
|
2024-09-06 00:35:57 +02:00
|
|
|
useEffect(() => {
|
2024-07-19 00:21:33 +02:00
|
|
|
let timeout: ReturnType<typeof setTimeout>;
|
2024-09-06 13:15:42 +02:00
|
|
|
if (countdown <= 0) {
|
2024-07-19 00:21:33 +02:00
|
|
|
timeout = setTimeout(() => {
|
|
|
|
handleSendCancel();
|
|
|
|
}, 300);
|
|
|
|
} else {
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
setCountdown(countdown - 1);
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
return () => clearTimeout(timeout);
|
|
|
|
}, [countdown]);
|
|
|
|
|
|
|
|
return (
|
2024-09-06 13:15:42 +02:00
|
|
|
<Modal onOk={() => handleSubmit()} onCancel={() => handleSendCancel()} onClose={() => handleSendCancel()}>
|
|
|
|
<div className="userinput-header">{userInputRequest.title + ` (${countdown}s)`}</div>
|
2024-07-19 00:21:33 +02:00
|
|
|
<div className="userinput-body">
|
|
|
|
{queryText}
|
|
|
|
{inputBox}
|
|
|
|
</div>
|
2024-07-30 20:44:19 +02:00
|
|
|
</Modal>
|
2024-07-19 00:21:33 +02:00
|
|
|
);
|
|
|
|
};
|
2024-07-30 20:44:19 +02:00
|
|
|
|
|
|
|
UserInputModal.displayName = "UserInputModal";
|
|
|
|
|
|
|
|
export { UserInputModal };
|