@@ -22,6 +22,12 @@ import { ConnectionAction, ConnectionStatus } from "../device/device";
2222import { useConnectionStatus } from "../device/device-hooks" ;
2323import MoreMenuButton from "./MoreMenuButton" ;
2424import { useProjectActions } from "./project-hooks" ;
25+ import { useHotkeys } from "react-hotkeys-hook" ;
26+ import {
27+ globalShortcutConfig ,
28+ keyboardShortcuts ,
29+ } from "../common/keyboard-shortcuts" ;
30+ import { FinalFocusRef } from "./project-actions" ;
2531
2632interface SendButtonProps {
2733 size ?: ThemeTypings [ "components" ] [ "Button" ] [ "sizes" ] ;
@@ -48,24 +54,27 @@ const SendButton = React.forwardRef(
4854 flashing : false ,
4955 lastCompleteFlash : 0 ,
5056 } ) ;
51- const handleSendToMicrobit = useCallback ( async ( ) => {
52- if ( flashing . current . flashing ) {
53- // Ignore repeated clicks.
54- return ;
55- }
56- flashing . current = {
57- flashing : true ,
58- lastCompleteFlash : flashing . current . lastCompleteFlash ,
59- } ;
60- try {
61- await actions . flash ( sendButtonRef ) ;
62- } finally {
57+ const handleSendToMicrobit = useCallback (
58+ async ( finalFocusRef : FinalFocusRef ) => {
59+ if ( flashing . current . flashing ) {
60+ // Ignore repeated clicks.
61+ return ;
62+ }
6363 flashing . current = {
64- flashing : false ,
65- lastCompleteFlash : new Date ( ) . getTime ( ) ,
64+ flashing : true ,
65+ lastCompleteFlash : flashing . current . lastCompleteFlash ,
6666 } ;
67- }
68- } , [ flashing , actions , sendButtonRef ] ) ;
67+ try {
68+ await actions . flash ( finalFocusRef ) ;
69+ } finally {
70+ flashing . current = {
71+ flashing : false ,
72+ lastCompleteFlash : new Date ( ) . getTime ( ) ,
73+ } ;
74+ }
75+ } ,
76+ [ flashing , actions ]
77+ ) ;
6978 const handleFocus = useCallback (
7079 ( e : FocusEvent < unknown > ) => {
7180 const inProgress = flashing . current . flashing ;
@@ -79,6 +88,16 @@ const SendButton = React.forwardRef(
7988 [ flashing ]
8089 ) ;
8190 const menuButtonRef = useRef < HTMLButtonElement > ( null ) ;
91+ const activeElementRef = useRef < HTMLElement | null > ( null ) ;
92+ const handleSendToMicrobitShortcut = useCallback ( ( ) => {
93+ activeElementRef . current = document . activeElement as HTMLElement ;
94+ handleSendToMicrobit ( activeElementRef ) ;
95+ } , [ handleSendToMicrobit ] ) ;
96+ useHotkeys (
97+ keyboardShortcuts . sendToMicrobit ,
98+ handleSendToMicrobitShortcut ,
99+ globalShortcutConfig
100+ ) ;
82101 return (
83102 < HStack >
84103 < Menu >
@@ -96,7 +115,7 @@ const SendButton = React.forwardRef(
96115 size = { size }
97116 variant = "solid"
98117 leftIcon = { < RiUsbLine /> }
99- onClick = { handleSendToMicrobit }
118+ onClick = { ( ) => handleSendToMicrobit ( sendButtonRef ) }
100119 >
101120 < FormattedMessage id = "send-action" />
102121 </ Button >
0 commit comments