Skip to content

Commit

Permalink
feat: Hot keys: Escape to close settings, Up Arrow to get last input
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongmeizhi committed Apr 7, 2023
1 parent fec055d commit 58b956f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ export function Chat(props: {

const inputRef = useRef<HTMLTextAreaElement>(null);
const [userInput, setUserInput] = useState("");
const [beforeInput, setBeforeInput] = useState("");
const [isLoading, setIsLoading] = useState(false);
const { submitKey, shouldSubmit } = useSubmitHandler();
const { scrollRef, setAutoScroll } = useScrollToBottom();
Expand Down Expand Up @@ -407,6 +408,7 @@ export function Chat(props: {
if (userInput.length <= 0) return;
setIsLoading(true);
chatStore.onUserInput(userInput).then(() => setIsLoading(false));
setBeforeInput(userInput);
setUserInput("");
setPromptHints([]);
if (!isMobileScreen()) inputRef.current?.focus();
Expand All @@ -420,6 +422,12 @@ export function Chat(props: {

// check if should send message
const onInputKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
// if ArrowUp and no userInput
if (e.key === "ArrowUp" && userInput.length <= 0) {
setUserInput(beforeInput);
e.preventDefault();
return;
}
if (shouldSubmit(e)) {
onUserSubmit();
e.preventDefault();
Expand Down
13 changes: 13 additions & 0 deletions app/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ export function Settings(props: { closeSettings: () => void }) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
const keydownEvent = (e: KeyboardEvent) => {
if (e.key === "Escape") {
props.closeSettings();
}
};
document.addEventListener("keydown", keydownEvent);
return () => {
document.removeEventListener("keydown", keydownEvent);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return (
<ErrorBoundary>
<div className={styles["window-header"]}>
Expand Down

0 comments on commit 58b956f

Please sign in to comment.