-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(feature menu): add a hidden feature menu.
- Loading branch information
1 parent
821dd6b
commit 8b04e47
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import React, { useState, useEffect, useCallback } from "react"; | ||
import { Dialog, Flex, Button, Checkbox, Text } from "@radix-ui/themes"; | ||
import { useAppDispatch, useAppSelector } from "../../hooks"; | ||
import { selectFeatures, changeFeature } from "./configSlice"; | ||
|
||
const useInputEvent = () => { | ||
const [key, setKey] = useState<string | null>(null); | ||
useEffect(() => { | ||
const keyDownHandler = (event: KeyboardEvent) => setKey(event.code); | ||
const keyUpHandler = () => setKey(null); | ||
window.addEventListener("keydown", keyDownHandler); | ||
window.addEventListener("keyup", keyUpHandler); | ||
return () => { | ||
window.removeEventListener("keydown", keyDownHandler); | ||
window.removeEventListener("keyup", keyUpHandler); | ||
}; | ||
}, []); | ||
|
||
return key; | ||
}; | ||
|
||
const konamiCode = [ | ||
"ArrowUp", | ||
"ArrowUp", | ||
"ArrowDown", | ||
"ArrowDown", | ||
"ArrowLeft", | ||
"ArrowRight", | ||
"ArrowLeft", | ||
"ArrowRight", | ||
"Escape", | ||
"Enter", | ||
]; | ||
|
||
const useKonamiCode = () => { | ||
const [count, setCount] = useState(0); | ||
const [success, setSuccess] = useState(false); | ||
const key = useInputEvent(); | ||
|
||
useEffect(() => { | ||
if (success) { | ||
return; | ||
} else if (count === konamiCode.length) { | ||
setSuccess(true); | ||
} else if (key === konamiCode[count]) { | ||
setCount((n) => n + 1); | ||
} | ||
}, [key, count, success]); | ||
|
||
const reset = useCallback(() => { | ||
setSuccess(false); | ||
setCount(0); | ||
}, []); | ||
|
||
return { success, reset }; | ||
}; | ||
|
||
export const FeatureMenu: React.FC = () => { | ||
const { success, reset } = useKonamiCode(); | ||
const dispatch = useAppDispatch(); | ||
const features = useAppSelector(selectFeatures); | ||
|
||
// if (!success) return false; | ||
|
||
const keysAndValues = Object.entries(features ?? {}); | ||
|
||
return ( | ||
<Dialog.Root open={success} onOpenChange={reset}> | ||
<Dialog.Content> | ||
<Dialog.Title>Hidden Features Menu</Dialog.Title> | ||
{keysAndValues.length === 0 && ( | ||
<Dialog.Description>No hidden features</Dialog.Description> | ||
)} | ||
<Flex direction="column" gap="3"> | ||
{keysAndValues.map(([feature, value]) => { | ||
return ( | ||
<Text key={feature} as="label" size="2"> | ||
<Flex as="span" gap="2"> | ||
<Checkbox | ||
checked={value} | ||
onCheckedChange={() => { | ||
dispatch(changeFeature({ feature, value: !value })); | ||
}} | ||
/>{" "} | ||
{feature} | ||
</Flex> | ||
</Text> | ||
); | ||
})} | ||
</Flex> | ||
|
||
<Flex gap="3" mt="4" justify="end"> | ||
<Dialog.Close> | ||
<Button variant="soft" color="gray"> | ||
Close | ||
</Button> | ||
</Dialog.Close> | ||
</Flex> | ||
</Dialog.Content> | ||
</Dialog.Root> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters