1
Fork 0
mirror of https://github.com/Steffo99/steffoweb.git synced 2024-12-18 04:44:19 +00:00
steffoweb/hooks/useKonamiCode.ts

46 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-02-18 03:12:46 +00:00
import {useCallback, useEffect, useState} from "react"
const KONAMI_CODE = ["ArrowUp", "ArrowUp", "ArrowDown", "ArrowDown", "ArrowLeft", "ArrowRight", "ArrowLeft", "ArrowRight", "KeyB", "KeyA"]
export function useKonamiCode(onDone: () => void) {
const [remainingKeys, setRemainingKeys] = useState(() => [...KONAMI_CODE])
const nextKey = remainingKeys?.[0];
const checkInput = useCallback(
(event: KeyboardEvent) => {
if(event.code === nextKey) {
setRemainingKeys(rem => {
rem = [...rem]
rem.shift()
return rem
})
}
else {
setRemainingKeys([...KONAMI_CODE])
}
},
[nextKey]
)
useEffect(
() => {
if(!nextKey) {
onDone()
setRemainingKeys([...KONAMI_CODE])
}
},
[nextKey, onDone]
)
useEffect(
() => {
document.addEventListener("keydown", checkInput)
return () => {
document.removeEventListener("keydown", checkInput)
}
},
[checkInput]
)
}