Skip to content

Commit

Permalink
feat: close #1055 cmd/alt/ctrl + arrow up/down to switch window
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa committed May 9, 2023
1 parent 9b1f251 commit 2b7f72d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
14 changes: 4 additions & 10 deletions app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import chatStyle from "./chat.module.scss";

import { ListItem, Modal, showModal } from "./ui-lib";
import { useLocation, useNavigate } from "react-router-dom";
import { Path } from "../constant";
import { LAST_INPUT_KEY, Path } from "../constant";
import { Avatar } from "./emoji";
import { MaskAvatar, MaskConfig } from "./mask";
import { useMaskStore } from "../store/mask";
Expand Down Expand Up @@ -404,7 +404,6 @@ export function Chat() {

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, scrollToBottom } = useScrollToBottom();
Expand Down Expand Up @@ -477,7 +476,7 @@ export function Chat() {
if (userInput.trim() === "") return;
setIsLoading(true);
chatStore.onUserInput(userInput).then(() => setIsLoading(false));
setBeforeInput(userInput);
localStorage.setItem(LAST_INPUT_KEY, userInput);
setUserInput("");
setPromptHints([]);
if (!isMobileScreen) inputRef.current?.focus();
Expand All @@ -491,9 +490,9 @@ export function Chat() {

// check if should send message
const onInputKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
// if ArrowUp and no userInput
// if ArrowUp and no userInput, fill with last input
if (e.key === "ArrowUp" && userInput.length <= 0) {
setUserInput(beforeInput);
setUserInput(localStorage.getItem(LAST_INPUT_KEY) ?? "");
e.preventDefault();
return;
}
Expand All @@ -503,11 +502,6 @@ export function Chat() {
}
};
const onRightClick = (e: any, message: Message) => {
// auto fill user input
if (message.role === "user") {
setUserInput(message.content);
}

// copy to clipboard
if (selectOrCopy(e.currentTarget, message.content)) {
e.preventDefault();
Expand Down
25 changes: 24 additions & 1 deletion app/components/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ const ChatList = dynamic(async () => (await import("./chat-list")).ChatList, {
loading: () => null,
});

function useHotKey() {
const chatStore = useChatStore();

useEffect(() => {
const onKeyDown = (e: KeyboardEvent) => {
if (e.metaKey || e.altKey || e.ctrlKey) {
const n = chatStore.sessions.length;
const limit = (x: number) => (x + n) % n;
const i = chatStore.currentSessionIndex;
if (e.key === "ArrowUp") {
chatStore.selectSession(limit(i - 1));
} else if (e.key === "ArrowDown") {
chatStore.selectSession(limit(i + 1));
}
}
};

window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
});
}

function useDragSideBar() {
const limit = (x: number) => Math.min(MAX_SIDEBAR_WIDTH, x);

Expand Down Expand Up @@ -86,9 +108,10 @@ export function SideBar(props: { className?: string }) {
// drag side bar
const { onDragMouseDown, shouldNarrow } = useDragSideBar();
const navigate = useNavigate();

const config = useAppConfig();

useHotKey();

return (
<div
className={`${styles.sidebar} ${props.className} ${
Expand Down
2 changes: 2 additions & 0 deletions app/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ export const MIN_SIDEBAR_WIDTH = 230;
export const NARROW_SIDEBAR_WIDTH = 100;

export const ACCESS_CODE_PREFIX = "ak-";

export const LAST_INPUT_KEY = "last-input";

0 comments on commit 2b7f72d

Please sign in to comment.