press esc to close settings window

This commit is contained in:
2023-07-27 11:09:04 +08:00
parent 69e1c013b3
commit b5ec1fe518

View File

@@ -1,5 +1,5 @@
import { createRef } from "preact";
import { StateUpdater, useState } from "preact/hooks";
import { StateUpdater, useEffect, useState } from "preact/hooks";
import { ChatStore, clearTotalCost, getTotalCost } from "./app";
import models from "./models";
import { TemplateChatStore } from "./chatbox";
@@ -177,6 +177,22 @@ export default (props: {
const importFileRef = createRef();
const [totalCost, setTotalCost] = useState(getTotalCost());
useEffect(() => {
const handleKeyPress = (event: any) => {
if (event.keyCode === 27) {
// keyCode for ESC key is 27
props.setShow(false);
}
};
document.addEventListener("keydown", handleKeyPress);
return () => {
document.removeEventListener("keydown", handleKeyPress);
};
}, []); // The empty dependency array ensures that the effect runs only once
return (
<div
onClick={() => props.setShow(false)}