From dbba6c90a19ad66fd050bd15d9ed2c1ce329f052 Mon Sep 17 00:00:00 2001 From: lifeparticle Date: Fri, 27 Oct 2023 22:47:30 +1100 Subject: [PATCH] use combine key press --- .../components/General/PopupSearch/index.tsx | 2 +- ui/src/hooks/useCombinedKeyPress.ts | 68 +++++++++---------- ui/src/pages/About/About.module.scss | 2 - ui/src/pages/About/components/Credits.tsx | 19 ++---- ui/src/pages/About/index.tsx | 4 +- 5 files changed, 43 insertions(+), 52 deletions(-) diff --git a/ui/src/components/General/PopupSearch/index.tsx b/ui/src/components/General/PopupSearch/index.tsx index 9f294e37..6e8a3bd5 100644 --- a/ui/src/components/General/PopupSearch/index.tsx +++ b/ui/src/components/General/PopupSearch/index.tsx @@ -41,7 +41,7 @@ const PopupSearch: React.FC = () => { ); }, [input]); - useCombinedKeyPress(handleModalOpen, ["Meta", "k"]); + useCombinedKeyPress(handleModalOpen, ["control/meta", "k"]); return ( void; -// mapping -const keymap = new Map([["Meta", "control"]]); - function useCombinedKeyPress( callback: CallBackFunction, keyCodes: string[] ): void { - const platform = useUserAgent(); const [pressedKeys, setPressedKeys] = useState([]); + useEffect(() => { + const isMatched = compareArrays(pressedKeys, keyCodes); + if (isMatched) { + callback(); + setPressedKeys([]); + } + }, [pressedKeys, keyCodes, callback]); + const handleKeyDown = useCallback( (event: KeyboardEvent) => { - if (!pressedKeys.includes(event.key)) { - const key = - platform === "win" ? keymap.get(event.key) : event.key; - setPressedKeys((prev) => [...prev, key || ""]); + event.preventDefault(); + const key = event.key.toLowerCase(); + if (!pressedKeys.includes(key)) { + setPressedKeys((prev) => [...prev, key]); } - - console.log("handleKeyDown", event.key, callback); }, - [pressedKeys, platform] + [pressedKeys] ); - useEffect(() => { - console.log("pressedKeys", pressedKeys); - const identifyKey = () => { - for (const key of keyCodes) { - if (!pressedKeys.includes(key)) { - return false; - } - } - return true; - }; - const val = identifyKey(); - console.log("val", val); - }, [pressedKeys, keyCodes]); - const handleKeyUp = useCallback( (event: KeyboardEvent) => { - const keyUp = - platform === "win" ? keymap.get(event.key) : event.key; - console.log("keyUp", keyUp); - const filteredKeys = pressedKeys.filter((key) => key !== keyUp); - - setPressedKeys(filteredKeys); - - console.log("handleKeyUp", event.key); + event.preventDefault(); + const key = event.key.toLowerCase(); + if (pressedKeys.includes(key)) { + const filteredKeys = pressedKeys.filter((key) => key !== key); + setPressedKeys(filteredKeys); + } }, - [pressedKeys, platform] + [pressedKeys] ); useEffect(() => { @@ -62,6 +47,19 @@ function useCombinedKeyPress( window.removeEventListener("keyup", handleKeyUp); }; }, [handleKeyDown, handleKeyUp]); + + function compareArrays(pressedArray: string[], mainArray: string[]) { + if (pressedArray?.length !== mainArray?.length) return false; + + for (const iterator of mainArray) { + const filteredArray = pressedArray.filter((key) => + iterator.includes(key) + ); + if (!(filteredArray?.length > 0)) return false; + } + + return true; + } } export default useCombinedKeyPress; diff --git a/ui/src/pages/About/About.module.scss b/ui/src/pages/About/About.module.scss index 24c16684..b8641d34 100644 --- a/ui/src/pages/About/About.module.scss +++ b/ui/src/pages/About/About.module.scss @@ -13,8 +13,6 @@ display: flex; align-items: center; justify-content: center; - background: url("../../assets/About/card_bg.jpg") no-repeat left / - cover; &_text { backdrop-filter: blur(100px); diff --git a/ui/src/pages/About/components/Credits.tsx b/ui/src/pages/About/components/Credits.tsx index f40d3d85..04fcad20 100644 --- a/ui/src/pages/About/components/Credits.tsx +++ b/ui/src/pages/About/components/Credits.tsx @@ -43,33 +43,28 @@ const CREDITS_DATA: Credits[] = [ name: "Movie for hackers", url: "https://github.com/k4m4/movies-for-hackers", }, - // { - // key: "5", - // name: "", - // url: "", - // }, { - key: "6", - name: "Hassaan Here", - url: "https://unsplash.com/photos/bKfkhVRAJTQ", + key: "5", + name: "unDraw", + url: "https://undraw.co/", }, { - key: "7", + key: "6", name: "Frontend Focus", url: "https://frontendfoc.us/", }, { - key: "8", + key: "7", name: "React Status", url: "https://react.statuscode.com/", }, { - key: "9", + key: "8", name: "Lucide", url: "https://lucide.dev/", }, { - key: "10", + key: "9", name: "Dudeowl", url: "https://99designs.com.au/profiles/dudeowl", }, diff --git a/ui/src/pages/About/index.tsx b/ui/src/pages/About/index.tsx index 19ba9182..fa34c776 100644 --- a/ui/src/pages/About/index.tsx +++ b/ui/src/pages/About/index.tsx @@ -10,7 +10,7 @@ const About = () => { return ( <>
-
+
{

{`— A node in the BinaryTree.`}

-
+