From 8cd3b2d767a74c9d5e557279b8721b0e24147abc Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Thu, 20 Jan 2022 22:21:25 -0800 Subject: [PATCH] nicer hotkeys library --- components/demo/TeamVoiceLine.tsx | 68 +++++++++++++------------------ package-lock.json | 20 +++++++++ package.json | 1 + 3 files changed, 49 insertions(+), 40 deletions(-) diff --git a/components/demo/TeamVoiceLine.tsx b/components/demo/TeamVoiceLine.tsx index 6854545..890cf73 100644 --- a/components/demo/TeamVoiceLine.tsx +++ b/components/demo/TeamVoiceLine.tsx @@ -9,6 +9,8 @@ import { useCallback, useEffect, useState } from "react"; import { KeyCode } from "../../globals/keycode"; import toast from "react-hot-toast"; +import { GlobalHotKeys, HotKeys, KeySequence, KeyMap } from "react-hotkeys"; + let testFriends = [ { name: "Liam", @@ -78,7 +80,7 @@ function renderPulse(status: UserStatus) { } } -export default function TeamVoiceLine(props: IVoiceDemoProps) { +function TeamVoiceLine(props: IVoiceDemoProps) { const isVoiceLineTurn = props.demoStep == DemoStep.playIncomingMessage || props.demoStep == DemoStep.hearReply || @@ -104,53 +106,37 @@ export default function TeamVoiceLine(props: IVoiceDemoProps) { const [isRecording, setIsRecording] = useState(false); - const handleKeyUp = useCallback( - (event) => { - if (event.repeat) { - return; - } - console.log("on key up"); - console.log(event.keyCode); + function startRecord(event) { + toast("listening"); - if (isRecording && event.keyCode == KeyCode.R) { - setIsRecording(false); - } + setIsRecording(true); + } + + function endRecording(event) { + toast.success("Paul heard it live!"); + + setIsRecording(false); + } + + const keyMap: KeyMap = { + RECORD: "r", + STOP_RECORDING: { + name: "Stop recording", + sequence: "r", + action: "keyup", }, - [isRecording] - ); - - const handleKeyDown = useCallback( - (event) => { - if (event.repeat) { - return; - } - console.log(event.keyCode); - - if (event.keyCode == KeyCode.R) { - setIsRecording(true); - } - }, - [isRecording] - ); - - useEffect(() => { - document.addEventListener("keydown", handleKeyDown); - document.addEventListener("keyup", handleKeyUp); - - return () => { - document.removeEventListener("keydown", handleKeyDown); - document.removeEventListener("keyup", handleKeyUp); - }; - }, [handleKeyDown, handleKeyUp]); + }; + const handlers = { RECORD: startRecord, STOP_RECORDING: endRecording }; return (
console.log("yayayayay")} > + {/* keyboard shortcut handler */} + + @@ -256,3 +242,5 @@ export default function TeamVoiceLine(props: IVoiceDemoProps) {
); } + +export default TeamVoiceLine; diff --git a/package-lock.json b/package-lock.json index 3585c9b..0d934f4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "react-dom": "17.0.2", "react-h5-audio-player": "^3.8.1", "react-hot-toast": "^2.2.0", + "react-hotkeys": "^2.0.0", "react-icons": "^4.3.1", "react-moment": "^1.1.1", "uuid": "^8.3.2" @@ -7833,6 +7834,17 @@ "csstype": "^2.6.2" } }, + "node_modules/react-hotkeys": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/react-hotkeys/-/react-hotkeys-2.0.0.tgz", + "integrity": "sha512-3n3OU8vLX/pfcJrR3xJ1zlww6KS1kEJt0Whxc4FiGV+MJrQ1mYSYI3qS/11d2MJDFm8IhOXMTFQirfu6AVOF6Q==", + "dependencies": { + "prop-types": "^15.6.1" + }, + "peerDependencies": { + "react": ">= 0.14.0" + } + }, "node_modules/react-icons": { "version": "4.3.1", "resolved": "https://registry.npmjs.org/react-icons/-/react-icons-4.3.1.tgz", @@ -15280,6 +15292,14 @@ } } }, + "react-hotkeys": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/react-hotkeys/-/react-hotkeys-2.0.0.tgz", + "integrity": "sha512-3n3OU8vLX/pfcJrR3xJ1zlww6KS1kEJt0Whxc4FiGV+MJrQ1mYSYI3qS/11d2MJDFm8IhOXMTFQirfu6AVOF6Q==", + "requires": { + "prop-types": "^15.6.1" + } + }, "react-icons": { "version": "4.3.1", "resolved": "https://registry.npmjs.org/react-icons/-/react-icons-4.3.1.tgz", diff --git a/package.json b/package.json index d63edad..b250d0d 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "react-dom": "17.0.2", "react-h5-audio-player": "^3.8.1", "react-hot-toast": "^2.2.0", + "react-hotkeys": "^2.0.0", "react-icons": "^4.3.1", "react-moment": "^1.1.1", "uuid": "^8.3.2"