Skip to content

Commit

Permalink
Konami code toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-sherman committed Nov 16, 2023
1 parent 7ea2049 commit f16c8c3
Show file tree
Hide file tree
Showing 6 changed files with 415 additions and 31 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,11 @@
],
"editor.quickSuggestions": {
"strings": true
},
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[typescriptreact]": {
"editor.defaultFormatter": "biomejs.biome"
}
}
6 changes: 5 additions & 1 deletion apps/maestros/app/(maestros)/monorepos/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import {
import Link from 'next/link';
import { Analytics } from '@repo/analytics';
import { Twitter, SidebarOpen, Music } from 'lucide-react';
import { Fragment } from 'react';
import { Fragment, Suspense } from 'react';
import { linkStyles } from '../navLinks';
import { Toolbar } from './toolbar';
import { inter } from '#/app/fonts';
import { buildMeta, metadataBaseURI } from '#/app/metadata';
import '#/app/globals.css';
Expand Down Expand Up @@ -162,6 +163,9 @@ export default function RootLayout({
</ThemeWrapper>
</AuthSessionProvider>
<Analytics />
<Suspense>
<Toolbar />
</Suspense>
</body>
</html>
);
Expand Down
81 changes: 81 additions & 0 deletions apps/maestros/app/(maestros)/monorepos/toolbar.tsx
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;
}
3 changes: 2 additions & 1 deletion apps/maestros/next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { withContentlayer } = require('next-contentlayer');
const withVercelToolbar = require('@vercel/toolbar/plugins/next')();

/** @type {import('next').NextConfig} */
const moduleExports = {
Expand All @@ -8,4 +9,4 @@ const moduleExports = {
typescript: { ignoreBuildErrors: true },
};

module.exports = withContentlayer(moduleExports);
module.exports = withVercelToolbar(withContentlayer(moduleExports));
1 change: 1 addition & 0 deletions apps/maestros/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@repo/db": "workspace:*",
"@repo/ui": "workspace:*",
"@vercel/og": "^0.0.27",
"@vercel/toolbar": "^0.1.5",
"bright": "^0.8.2",
"class-variance-authority": "^0.6.1",
"contentlayer": "latest",
Expand Down
Loading

0 comments on commit f16c8c3

Please sign in to comment.