-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ea2049
commit f16c8c3
Showing
6 changed files
with
415 additions
and
31 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
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,81 @@ | ||
'use client'; | ||
|
||
import { VercelToolbar } from '@vercel/toolbar/next'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
const konamiKeyCodes = [ | ||
'ArrowUp', | ||
'ArrowUp', | ||
'ArrowDown', | ||
'ArrowDown', | ||
'ArrowLeft', | ||
'ArrowRight', | ||
'ArrowLeft', | ||
'ArrowRight', | ||
'b', | ||
'a', | ||
]; | ||
|
||
type State = | ||
| { | ||
complete: false; | ||
sequence: string[]; | ||
} | ||
| { | ||
complete: true; | ||
}; | ||
|
||
const LOCAL_STORAGE_KEY = 'konami-state'; | ||
|
||
export function Toolbar() { | ||
const [state, setState] = useState<State>({ complete: false, sequence: [] }); | ||
|
||
useEffect(() => { | ||
if (state.complete) { | ||
console.log('Konami code complete!'); | ||
localStorage.setItem(LOCAL_STORAGE_KEY, 'yes'); | ||
return; | ||
} | ||
|
||
const storedState = localStorage.getItem(LOCAL_STORAGE_KEY); | ||
|
||
if (storedState === 'yes') { | ||
setState({ complete: true }); | ||
} | ||
|
||
function onKeyDown(e: KeyboardEvent) { | ||
setState((prevState) => { | ||
// This check is to please the typescript compiler | ||
if (prevState.complete) return prevState; | ||
|
||
const newSequence = [...prevState.sequence, e.key]; | ||
if (arrayStartsWith(konamiKeyCodes, newSequence)) { | ||
if (newSequence.length === konamiKeyCodes.length) { | ||
return { complete: true }; | ||
} | ||
return { complete: false, sequence: newSequence }; | ||
} | ||
|
||
return { complete: false, sequence: [] }; | ||
}); | ||
} | ||
|
||
window.addEventListener('keydown', onKeyDown); | ||
|
||
return () => { | ||
window.removeEventListener('keydown', onKeyDown); | ||
}; | ||
}, [state.complete]); | ||
|
||
return state.complete ? <VercelToolbar /> : null; | ||
} | ||
|
||
function arrayStartsWith<T>(arr: T[], prefix: T[]) { | ||
if (arr.length < prefix.length) return false; | ||
|
||
for (let i = 0; i < prefix.length; i++) { | ||
if (arr[i] !== prefix[i]) return false; | ||
} | ||
|
||
return true; | ||
} |
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
Oops, something went wrong.