Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit e84b32a

Browse files
committed
fix: Google Cloud Build
1 parent 3ffd681 commit e84b32a

File tree

3 files changed

+58
-72
lines changed

3 files changed

+58
-72
lines changed

src/pages/index.tsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,7 @@ import React from 'react';
22
import Hero from '../components/hero';
33
import Layout from '../components/layout';
44

5-
import './konami.js';
6-
7-
let discoMode: NodeJS.Timeout | null = null;
8-
document.addEventListener('konamiCode', (): void => {
9-
if (discoMode) {
10-
return clearInterval(discoMode);
11-
}
12-
discoMode = setInterval(
13-
(): boolean => document.body.classList.toggle('dark-mode'),
14-
300
15-
);
16-
});
5+
import '../util/konami';
176

187
export default function HomePage(): JSX.Element {
198
const title = 'Home Page';

src/pages/konami.js

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/util/konami.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const enum KEY {
2+
LEFT = 37,
3+
UP = 38,
4+
RIGHT = 39,
5+
DOWN = 40,
6+
A = 65,
7+
B = 66,
8+
}
9+
10+
// eslint-disable-next-line no-bitwise
11+
const contains = (a: string, b: string): boolean => !!~a.indexOf(b);
12+
13+
function initKonami(): void {
14+
const CODE_SEQUENCE = '38384040373937396665'; // ⬆ ⬆ ⬇ ⬇ ⬅ ➡ ⬅ ➡ b a
15+
const maxDelay = 1500;
16+
const konamiCodeEvent = new Event('konamiCode');
17+
let buffer = '';
18+
let lastDate = Date.now();
19+
20+
const validKeys = new Set([
21+
KEY.LEFT,
22+
KEY.UP,
23+
KEY.RIGHT,
24+
KEY.DOWN,
25+
KEY.A,
26+
KEY.B,
27+
]);
28+
29+
document.addEventListener('keyup', function triggerKonami(ev): void {
30+
if (!validKeys.has(ev.keyCode)) {
31+
return;
32+
}
33+
buffer =
34+
Date.now() - lastDate <= maxDelay ? `${ev.keyCode}` : buffer + ev.keyCode;
35+
lastDate = Date.now();
36+
if (!contains(buffer, CODE_SEQUENCE)) {
37+
return;
38+
}
39+
document.dispatchEvent(konamiCodeEvent);
40+
buffer = '';
41+
});
42+
}
43+
44+
// SSR Protection
45+
if (document) {
46+
initKonami();
47+
let discoMode: NodeJS.Timeout | null = null;
48+
document.addEventListener('konamiCode', (): void => {
49+
if (discoMode) {
50+
return clearInterval(discoMode);
51+
}
52+
discoMode = setInterval(
53+
(): boolean => document.body.classList.toggle('dark-mode'),
54+
300
55+
);
56+
});
57+
}

0 commit comments

Comments
 (0)