Skip to content

Commit

Permalink
Fix: Error: React Hook "useEffect" is called conditionally.
Browse files Browse the repository at this point in the history
  • Loading branch information
llaoj committed May 24, 2024
1 parent ac8ddfc commit 456a8ae
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions frontend/lib/use-shortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ import { useEffect } from "react";
import { isDesktop, isMacOs } from "react-device-detect";

export function useShortcut({ eventHandler }: { eventHandler: () => void }) {
if (isDesktop) {
useEffect(() => {
useEffect(() => {
function keyDownHandler(e: globalThis.KeyboardEvent) {
if (!isDesktop) {
return;
}
// refer: https://support.google.com/chrome/answer/157179
function keyDownHandler(e: globalThis.KeyboardEvent) {
if (
e.key === "Enter" ||
(isMacOs && e.metaKey && e.key == "1") ||
(!isMacOs && e.ctrlKey && e.key == "1")
) {
e.preventDefault();
eventHandler();
}
if (
e.key === "Enter" ||
(isMacOs && e.metaKey && e.key == "1") ||
(!isMacOs && e.ctrlKey && e.key == "1")
) {
e.preventDefault();
eventHandler();
}
document.addEventListener("keydown", keyDownHandler);
return () => document.removeEventListener("keydown", keyDownHandler);
});
}
}
document.addEventListener("keydown", keyDownHandler);
return () => document.removeEventListener("keydown", keyDownHandler);
});
}

0 comments on commit 456a8ae

Please sign in to comment.