Skip to content

Commit

Permalink
Add connect/disconnect keyboard shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-grace committed Dec 3, 2024
1 parent dcd9ebb commit 3bac4da
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/components/LiveGraphPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import AlertIcon from "./AlertIcon";
import InfoToolTip from "./InfoToolTip";
import LiveGraph from "./LiveGraph";
import PredictedAction from "./PredictedAction";
import { useHotkeys } from "react-hotkeys-hook";
import { globalShortcutConfig, keyboardShortcuts } from "../keyboard-shortcuts";

interface LiveGraphPanelProps {
showPredictedAction?: boolean;
Expand All @@ -42,6 +44,10 @@ const LiveGraphPanel = ({
!isConnected && !isReconnecting && status !== ConnectionStatus.Connecting;

const handleConnectOrReconnect = useCallback(() => {
if (isConnected) {
// Don't connect if already connected.
return;
}
if (
status === ConnectionStatus.NotConnected ||
status === ConnectionStatus.Connecting ||
Expand All @@ -56,15 +62,29 @@ const LiveGraphPanel = ({
});
void actions.reconnect();
}
}, [status, actions, logging]);
}, [isConnected, status, actions, logging]);

const handleDisconnect = useCallback(() => {
if (!isConnected) {
// Don't disconnect if already disconnected.
return;
}
logging.event({
type: "disconnect-user",
});
void actions.disconnect();
}, [actions, logging]);
}, [actions, isConnected, logging]);
const intl = useIntl();
useHotkeys(
keyboardShortcuts.connect,
handleConnectOrReconnect,
globalShortcutConfig
);
useHotkeys(
keyboardShortcuts.disconnect,
handleDisconnect,
globalShortcutConfig
);
return (
<HStack
role="region"
Expand Down
2 changes: 2 additions & 0 deletions src/keyboard-shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const keyboardShortcuts = {
nextAction: ["down"],
previousAction: ["up"],
renameAction: ["F2"],
connect: ["ctrl+shift+c", "meta+shift+c"],
disconnect: ["ctrl+shift+d", "meta+shift+d"],
};

export const globalShortcutConfig = {
Expand Down

0 comments on commit 3bac4da

Please sign in to comment.