Skip to content

Commit

Permalink
use combine key press
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeparticle committed Oct 27, 2023
1 parent d8f0810 commit dbba6c9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 52 deletions.
2 changes: 1 addition & 1 deletion ui/src/components/General/PopupSearch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const PopupSearch: React.FC = () => {
);
}, [input]);

useCombinedKeyPress(handleModalOpen, ["Meta", "k"]);
useCombinedKeyPress(handleModalOpen, ["control/meta", "k"]);

return (
<Modal
Expand Down
68 changes: 33 additions & 35 deletions ui/src/hooks/useCombinedKeyPress.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,42 @@
import { useCallback, useEffect, useState } from "react";
import useUserAgent from "./useUserAgent";

type CallBackFunction = () => void;

// mapping
const keymap = new Map([["Meta", "control"]]);

function useCombinedKeyPress(
callback: CallBackFunction,
keyCodes: string[]
): void {
const platform = useUserAgent();
const [pressedKeys, setPressedKeys] = useState<string[]>([]);

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(() => {
Expand All @@ -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;
2 changes: 0 additions & 2 deletions ui/src/pages/About/About.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 7 additions & 12 deletions ui/src/pages/About/components/Credits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down
4 changes: 2 additions & 2 deletions ui/src/pages/About/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const About = () => {
return (
<>
<div className={style.about}>
<div className={style.about__card}>
<Card className={style.about__card}>
<div className={style.about__card__intro}>
<Typography.Title
level={5}
Expand Down Expand Up @@ -50,7 +50,7 @@ const About = () => {
<p>{`— A node in the BinaryTree.`}</p>
</Typography.Title>
</div>
</div>
</Card>
<Card className={style.about__card} bordered={false}>
<Values />
</Card>
Expand Down

0 comments on commit dbba6c9

Please sign in to comment.