Skip to content

Commit

Permalink
Fixed useGlobalShortcut deps
Browse files Browse the repository at this point in the history
  • Loading branch information
tangxianyun committed Jan 13, 2025
1 parent 83b7ce5 commit e87895f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useTerminalStore } from "../../../stores";
const TerminalShortcuts = () => {
const activeTerminal = useTerminalStore((state) => state.activeTerminal);
const setActiveTerminal = useTerminalStore(
(state) => state.setActiveTerminal,
(state) => state.setActiveTerminal
);
const openedTerminals = useTerminalStore((state) => state.openedTerminals);
const removeTerminal = useTerminalStore((state) => state.removeTerminal);
Expand Down Expand Up @@ -52,8 +52,8 @@ const TerminalShortcuts = () => {
borderRadius: theme.spacing(0.5),
position: "relative",
"&:hover .MuiListItemIcon-root": {
visibility: "visible", // hover 时显示 CloseIcon
opacity: 1, // 配合动画效果
visibility: "visible",
opacity: 1,
},
})}
onClick={() => handleShortcutClick(terminal)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ export const TerminalView = ({ terminal }: TerminalViewProps) => {
return activeTerminal === terminal;
}, [activeTerminal, terminal]);

useGlobalShortcut("CommandOrControl+I", () => {
setAgentOpen((v) => !v);
});
useGlobalShortcut(
"CommandOrControl+I",
() => {
if (isTerminalActive) {
setAgentOpen((v) => !v);
}
},
[isTerminalActive]
);

const { refetch, isError } = useQuery({
queryKey: [terminal, host],
Expand Down
11 changes: 8 additions & 3 deletions src/hooks/global-shortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import {
ShortcutEvent,
unregister,
} from "@tauri-apps/plugin-global-shortcut";
import { useEffect } from "react";
import { DependencyList, useEffect } from "react";

export const useGlobalShortcut = (shortcuts: string, onPressed: () => void) => {
export const useGlobalShortcut = (
shortcuts: string,
onPressed: () => void,
deps?: DependencyList
) => {
const handleEvent = useThrottleCallback((event: ShortcutEvent) => {
if (event.state === "Pressed") {
onPressed();
Expand All @@ -30,5 +34,6 @@ export const useGlobalShortcut = (shortcuts: string, onPressed: () => void) => {
return () => {
void unregisterFn();
};
}, [handleEvent, shortcuts]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [handleEvent, shortcuts, ...(deps || [])]);
};

0 comments on commit e87895f

Please sign in to comment.