Merge pull request #187 from tushar-daiya/keymaps/addmemory

Add useKeyPress hook for keyboard event handling
This commit is contained in:
Dhravya Shah 2024-07-29 20:19:08 -07:00 committed by GitHub
commit 1465bca983
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 1 deletions

View file

@ -30,6 +30,7 @@ import { createMemory, createSpace } from "../actions/doers";
import ComboboxWithCreate from "@repo/ui/shadcn/combobox";
import { StoredSpace } from "@/server/db/schema";
import useMeasure from "react-use-measure";
import { useKeyPress } from "@/lib/useKeyPress";
function Menu() {
const [spaces, setSpaces] = useState<StoredSpace[]>([]);
@ -48,7 +49,11 @@ function Menu() {
setSpaces(spaces.data);
})();
}, []);
useKeyPress("a", () => {
if (!dialogOpen) {
setDialogOpen(true);
}
});
const menuItems = [
{
icon: HomeIconWeb,

View file

@ -0,0 +1,15 @@
import { useEffect } from "react";
export const useKeyPress = (key: string, callback: () => void) => {
useEffect(() => {
const handler = (e: KeyboardEvent) => {
if (e.key === key && e.altKey) {
callback();
}
};
window.addEventListener("keydown", handler);
return () => {
window.removeEventListener("keydown", handler);
};
}, [key, callback]);
};