Skip to content

Commit

Permalink
✨ Add in app tetris (#121)
Browse files Browse the repository at this point in the history
* ✨ Add in app tetris

* 🚨 Supress lint issue

* 💄 Change command from 'surprise me' to tetris
  • Loading branch information
foysalit authored Jun 29, 2023
1 parent d6634ac commit 09b455a
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 12 deletions.
118 changes: 118 additions & 0 deletions app/surprise-me/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
'use client'
import { useState } from 'react'
import Tetris from 'react-tetris'
import { useInterval } from 'react-use'

function getDuration(totalSeconds: number): string {
const hours = Math.floor(totalSeconds / 3600)
const minutes = Math.floor((totalSeconds % 3600) / 60)
const seconds = totalSeconds % 60

const formattedHours = String(hours).padStart(2, '0')
const formattedMinutes = String(minutes).padStart(2, '0')
const formattedSeconds = String(seconds).padStart(2, '0')

return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`
}

const Timer = () => {
const [seconds, setSeconds] = useState(0)
useInterval(() => {
setSeconds((sec) => sec + 1)
}, 1000)

return (
<div className="flex flex-row justify-center py-4">
<p className="font-bold text-xl">{getDuration(seconds)}</p>
</div>
)
}

// Right now, we only serve the tetris game here, in the future, we want to rotate
// between a few games/fun activities which is why it's named "surprise me"
export default function SurpriseMePage() {
return (
<>
{/* This is valid jsx but because of a known bug, typescript is confused */}
{/* @ts-ignore:next-line */}
<style global jsx>
{`
.game-block {
margin: 0;
padding: 0;
width: 1.5em;
height: 1.5em;
border: 1px solid #ddd;
}
.piece-i {
background-color: #ec858b;
}
.piece-j {
background-color: #f1b598;
}
.piece-l {
background-color: #f8efae;
}
.piece-o {
background-color: #b5a677;
}
.piece-s {
background-color: #816e56;
}
.piece-t {
background-color: #b77c72;
}
.piece-z {
background-color: #e3be58;
}
.piece-preview {
background-color: #eee;
}
`}
</style>
<Timer />
<Tetris
keyboardControls={{
// Default values shown here. These will be used if no
// `keyboardControls` prop is provided.
down: 'MOVE_DOWN',
left: 'MOVE_LEFT',
right: 'MOVE_RIGHT',
space: 'HARD_DROP',
z: 'FLIP_COUNTERCLOCKWISE',
x: 'FLIP_CLOCKWISE',
up: 'FLIP_CLOCKWISE',
p: 'TOGGLE_PAUSE',
c: 'HOLD',
shift: 'HOLD',
}}
>
{({
HeldPiece,
Gameboard,
PieceQueue,
points,
linesCleared,
state,
controller,
}) => (
<div className="flex flex-row gap-x-4 items-start pt-4 mx-auto">
<HeldPiece />
<div>
<p>Points: {points}</p>
<p>Lines Cleared: {linesCleared}</p>
</div>
<Gameboard />
<PieceQueue />
{state === 'LOST' && (
<div>
<h2>Game Over</h2>
<button onClick={controller.restart}>New game</button>
</div>
)}
</div>
)}
</Tetris>
</>
)
}
15 changes: 13 additions & 2 deletions components/shell/CommandPalette/useAsyncSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
ChatBubbleLeftIcon,
ExclamationCircleIcon,
UserGroupIcon,
PuzzlePieceIcon,
} from '@heroicons/react/24/outline'
import { useKBar, Action, useRegisterActions, createAction } from 'kbar'
import { AppRouterInstance } from 'next/dist/shared/lib/app-router-context'
Expand Down Expand Up @@ -73,7 +73,18 @@ export const useCommandPaletteAsyncSearch = () => {
return actions
}

if (isBlueSkyAppUrl(search)) {
if (search.trim() === 'tetris') {
actions.push({
id: 'tetris',
name: `Tetris`,
icon: <PuzzlePieceIcon className={iconClassName} />,
keywords: `tetris, surprise`,
subtitle: 'Take a quick break!',
perform: () => {
router.push(`/surprise-me`)
},
})
} else if (isBlueSkyAppUrl(search)) {
const fragments = getFragmentsFromBlueSkyAppUrl(search)

if (fragments?.cid) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"next": "13.0.7",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-tetris": "^0.3.0",
"react-toastify": "^9.1.1",
"react-use": "^17.4.0",
"typescript": "4.9.4"
Expand Down
34 changes: 24 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@
dependencies:
regenerator-runtime "^0.13.11"

"@babel/runtime@^7.13.10":
version "7.22.3"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.22.3.tgz#0a7fce51d43adbf0f7b517a71f4c3aaca92ebcbb"
integrity sha512-XsDuspWKLUsxwCp6r7EhsExHtYfbe5oAGQ19kqngTdCPUoPQzOPdUbD/pB9PJiwb2ptYKQDjSJT3R6dC+EPqfQ==
dependencies:
regenerator-runtime "^0.13.11"

"@colors/colors@1.5.0":
version "1.5.0"
resolved "https://registry.yarnpkg.com/@colors/colors/-/colors-1.5.0.tgz#bb504579c1cae923e6576a4f5da43d25f97bdbd9"
Expand Down Expand Up @@ -121,12 +128,6 @@
dependencies:
debug "^3.1.0"
lodash.once "^4.1.1"
"@babel/runtime@^7.13.10":
version "7.22.3"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.22.3.tgz#0a7fce51d43adbf0f7b517a71f4c3aaca92ebcbb"
integrity sha512-XsDuspWKLUsxwCp6r7EhsExHtYfbe5oAGQ19kqngTdCPUoPQzOPdUbD/pB9PJiwb2ptYKQDjSJT3R6dC+EPqfQ==
dependencies:
regenerator-runtime "^0.13.11"

"@eslint/eslintrc@^1.4.0":
version "1.4.0"
Expand Down Expand Up @@ -901,6 +902,11 @@ combined-stream@^1.0.6, combined-stream@~1.0.6:
dependencies:
delayed-stream "~1.0.0"

command-score@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/command-score/-/command-score-0.1.2.tgz#b986ad7e8c0beba17552a56636c44ae38363d381"
integrity sha512-VtDvQpIJBvBatnONUsPzXYFVKQQAhuf3XTNOAsdBxCNO/QCtUUd8LSgjn0GVarBkCad6aJCZfXgrjYbl/KRr7w==

commander@^6.2.1:
version "6.2.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-6.2.1.tgz#0792eb682dfbc325999bb2b84fddddba110ac73c"
Expand All @@ -910,10 +916,6 @@ common-tags@^1.8.0:
version "1.8.2"
resolved "https://registry.yarnpkg.com/common-tags/-/common-tags-1.8.2.tgz#94ebb3c076d26032745fd54face7f688ef5ac9c6"
integrity sha512-gk/Z852D2Wtb//0I+kRFNKKE9dIIVirjoqPoA1wJU+XePVXZfGeBpk45+A1rKO4Q43prqWBNY/MiIeRLbPWUaA==
command-score@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/command-score/-/command-score-0.1.2.tgz#b986ad7e8c0beba17552a56636c44ae38363d381"
integrity sha512-VtDvQpIJBvBatnONUsPzXYFVKQQAhuf3XTNOAsdBxCNO/QCtUUd8LSgjn0GVarBkCad6aJCZfXgrjYbl/KRr7w==

concat-map@0.0.1:
version "0.0.1"
Expand Down Expand Up @@ -2201,6 +2203,11 @@ kbar@^0.1.0-beta.40:
react-virtual "^2.8.2"
tiny-invariant "^1.2.0"

keymaster@1.6.2:
version "1.6.2"
resolved "https://registry.yarnpkg.com/keymaster/-/keymaster-1.6.2.tgz#e1ae54d0ea9488f9f60b66b668f02e9a1946c6eb"
integrity sha512-OvA/AALN8IDKKkTk2Z+bDrzs/SQao4lo/QPbwSdDvm+frxfiYiYCSn1aHFUypJY3SruAO1y/c771agBmTXqUtg==

language-subtag-registry@^0.3.20:
version "0.3.22"
resolved "https://registry.npmjs.org/language-subtag-registry/-/language-subtag-registry-0.3.22.tgz"
Expand Down Expand Up @@ -2792,6 +2799,13 @@ react-is@^16.13.1:
resolved "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz"
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==

react-tetris@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/react-tetris/-/react-tetris-0.3.0.tgz#b846e1dff9af37880a54877783aedddc2258bed5"
integrity sha512-tb5vcF/SwNbLmUSEgLfJxXzLc4laG0elbJ4RNQpZ0iVFm6YFz5z5Sy6zMd9nXYProCAJwgheWd5OdgOxwTujBA==
dependencies:
keymaster "1.6.2"

react-toastify@^9.1.1:
version "9.1.1"
resolved "https://registry.yarnpkg.com/react-toastify/-/react-toastify-9.1.1.tgz#9280caea4a13dc1739c350d90660a630807bf10b"
Expand Down

0 comments on commit 09b455a

Please sign in to comment.