Skip to content

Commit bba3fcb

Browse files
Thibault-MonnierWolverinDEV
authored andcommitted
feat: undo / redo buttons in sandbox + keyboard shorcuts
1 parent eb2c6dc commit bba3fcb

3 files changed

Lines changed: 102 additions & 86 deletions

File tree

packages/frontend/src/components/sandbox/SandboxHud.tsx

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from 'react';
1+
import { useEffect, useState } from 'react';
22

33
import GameHudShell from '../game-screen/GameHudShell';
44

@@ -8,9 +8,11 @@ type SandboxHudProps = {
88
occupiedCellCount: number
99
renderableCellCount: number
1010
onResetBoard: () => void
11-
onTakeBack: () => void
11+
onUndo: () => void
12+
onRedo: () => void
1213
onResetView: () => void
13-
canTakeBack: boolean
14+
canUndo: boolean
15+
canRedo: boolean
1416
onSharePosition: () => void
1517
canSharePosition: boolean
1618
isSharingPosition: boolean
@@ -21,10 +23,8 @@ function SandboxHud({
2123
isAuthenticated,
2224
occupiedCellCount,
2325
renderableCellCount,
24-
onResetBoard,
25-
onTakeBack,
26-
onResetView,
27-
canTakeBack,
26+
onResetBoard, onUndo, onRedo,
27+
onResetView, canUndo, canRedo,
2828
onSharePosition,
2929
canSharePosition,
3030
isSharingPosition,
@@ -37,6 +37,21 @@ function SandboxHud({
3737
? `Play from this saved position locally with no clock. Assign either side to a bot or control both players yourself.`
3838
: `Local sandbox with no clock. Control both players yourself or let a bot take either side.`;
3939

40+
useEffect(() => {
41+
const handleKeyDown = (event: KeyboardEvent) => {
42+
if (event.key === `ArrowLeft` && canUndo) {
43+
onUndo();
44+
} else if (event.key === `ArrowRight` && canRedo) {
45+
onRedo();
46+
}
47+
};
48+
49+
window.addEventListener(`keydown`, handleKeyDown);
50+
return () => window.removeEventListener(`keydown`, handleKeyDown);
51+
}, [
52+
canUndo, canRedo, onUndo, onRedo,
53+
]);
54+
4055
return (
4156
<GameHudShell
4257
isOpen={isHudOpen}
@@ -156,16 +171,26 @@ function SandboxHud({
156171
>
157172
{isSharingPosition ? `Sharing...` : `Share Link`}
158173
</button>
174+
</div>
175+
176+
<div className="pointer-events-auto mt-4 grid grid-cols-2 gap-2">
177+
<button
178+
onClick={onUndo}
179+
disabled={!canUndo}
180+
className={`min-w-[9rem] flex-1 rounded-full px-4 py-2 font-medium shadow-lg transition md:flex-none ${canUndo ? enabledButtonClassName : disabledButtonClassName}`}
181+
>
182+
Undo
183+
</button>
159184

160185
<button
161-
onClick={onTakeBack}
162-
disabled={!canTakeBack}
163-
className={`min-w-[9rem] flex-1 rounded-full px-4 py-2 font-medium shadow-lg transition md:flex-none ${canTakeBack
186+
onClick={onRedo}
187+
disabled={!canRedo}
188+
className={`min-w-[9rem] flex-1 rounded-full px-4 py-2 font-medium shadow-lg transition md:flex-none ${canRedo
164189
? enabledButtonClassName
165190
: disabledButtonClassName
166191
}`}
167192
>
168-
Take Back
193+
Redo
169194
</button>
170195
</div>
171196

0 commit comments

Comments
 (0)