Add useKeyPress hook for keyboard event handling

This commit is contained in:
Tushar Daiya 2024-07-29 21:26:57 +05:30
parent 091ac1312b
commit f45d7fb9ba
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]);
};