Skip to content

Commit

Permalink
feat: updated keyboard event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhkeshan committed Dec 20, 2024
1 parent 8da5973 commit 2162acd
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 20 deletions.
70 changes: 59 additions & 11 deletions frontend/src/components/NewPlayer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BoxCentered, Button, Link } from "@fuel-ui/react";
import { useWallet } from "@fuels/react";
import { useEffect, useState } from "react";
import { useEffect, useState, useCallback } from "react";
import type { Dispatch, SetStateAction } from "react";
import type { Modals } from "../constants";

Expand Down Expand Up @@ -41,11 +41,7 @@ export default function NewPlayer({
const paymaster = usePaymaster();
const isGaslessSupported = useGaslessWalletSupported();

useEffect(() => {
getBalance();
}, [wallet]);

async function getBalance() {
const getBalance = useCallback(async () => {
const thisWallet = wallet ?? contract?.account;
console.log(wallet, "wallet");
const baseAssetId = thisWallet?.provider.getBaseAssetId();
Expand All @@ -55,7 +51,33 @@ export default function NewPlayer({
if (balanceNum) {
setHasFunds(balanceNum > 0);
}
}
}, [wallet, contract]);

useEffect(() => {
getBalance();
}, [wallet, getBalance]);

useEffect(() => {
const handleGlobalKeyDown = (e: globalThis.KeyboardEvent) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();

if (status === "error") {
setStatus("none");
updatePageNum();
} else if (status === "none" && !showNoFunds) {
handleNewPlayer();
} else if (status === "none" && !hasFunds && showNoFunds) {
getBalance();
}
}
};

window.addEventListener("keydown", handleGlobalKeyDown);
return () => {
window.removeEventListener("keydown", handleGlobalKeyDown);
};
}, [status, showNoFunds, hasFunds]);

async function createPlayerWithoutGasStation() {
if (!wallet || !contract) throw new Error("Wallet or contract not found");
Expand Down Expand Up @@ -202,19 +224,42 @@ export default function NewPlayer({
return (
<div className="new-player-modal">
{status === "none" && !showNoFunds && (
<Button css={buttonStyle} onPress={handleNewPlayer}>
<Button
css={buttonStyle}
onPress={handleNewPlayer}
role="button"
tabIndex={0}
aria-label="Create new player"
>
Make A New Player
</Button>
)}
{status === "none" && !hasFunds && showNoFunds && (
<BoxCentered css={styles.container}>
You need some ETH to play:
<Link isExternal href={`https://app.fuel.network/bridge`}>
<Button css={styles.link} variant="link">
<Link
isExternal
href={`https://app.fuel.network/bridge`}
role="link"
tabIndex={0}
>
<Button
css={styles.link}
variant="link"
role="button"
tabIndex={0}
aria-label="Go to Bridge"
>
Go to Bridge
</Button>
</Link>
<Button css={buttonStyle} onPress={getBalance}>
<Button
css={buttonStyle}
onPress={getBalance}
role="button"
tabIndex={0}
aria-label="Recheck balance"
>
Recheck balance
</Button>
</BoxCentered>
Expand All @@ -238,6 +283,9 @@ export default function NewPlayer({
setStatus("none");
updatePageNum();
}}
role="button"
tabIndex={0}
aria-label="Try again"
>
Try Again
</Button>
Expand Down
34 changes: 32 additions & 2 deletions frontend/src/components/modals/BuySeeds.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, Spinner, BoxCentered } from "@fuel-ui/react";
import { type BytesLike, Address, bn, Provider } from "fuels";
import type { Dispatch, SetStateAction } from "react";
import { useState } from "react";
import { useState, useEffect } from "react";
import { useWalletFunds } from "../../hooks/useWalletFunds";
import { NoFundsMessage } from "./NoFundsMessage";
import {
Expand Down Expand Up @@ -39,6 +39,26 @@ export default function BuySeeds({
const { hasFunds, showNoFunds, getBalance, showNoFundsMessage } =
useWalletFunds(contract);

useEffect(() => {
const handleGlobalKeyDown = (e: KeyboardEvent) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();

if (status === "error") {
setStatus("none");
updatePageNum();
} else if (status === "none" && !showNoFunds) {
buySeeds();
}
}
};

window.addEventListener("keydown", handleGlobalKeyDown);
return () => {
window.removeEventListener("keydown", handleGlobalKeyDown);
};
}, [status, showNoFunds]);

async function buyWithGasStation() {
if (!wallet) {
throw new Error("No wallet found");
Expand Down Expand Up @@ -219,6 +239,9 @@ export default function BuySeeds({
setStatus("none");
updatePageNum();
}}
role="button"
tabIndex={0}
aria-label="Try again"
>
Try Again
</Button>
Expand All @@ -227,7 +250,14 @@ export default function BuySeeds({
{status === "none" && !showNoFunds && (
<>
<div className="market-header">Buy Seeds</div>
<Button css={buttonStyle} variant="outlined" onPress={buySeeds}>
<Button
css={buttonStyle}
variant="outlined"
onPress={buySeeds}
role="button"
tabIndex={0}
aria-label="Buy 10 seeds"
>
Buy 10 seeds
</Button>
</>
Expand Down
35 changes: 32 additions & 3 deletions frontend/src/components/modals/HarvestModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Spinner, Button, BoxCentered } from "@fuel-ui/react";
import type { Dispatch, SetStateAction } from "react";
import { useState } from "react";
import { useState, useEffect } from "react";
import { cssObj } from "@fuel-ui/css";

import {
Expand Down Expand Up @@ -44,6 +44,27 @@ export default function HarvestModal({
const { hasFunds, showNoFunds, getBalance, showNoFundsMessage } =
useWalletFunds(contract);

// Add global keyboard event listener
useEffect(() => {
const handleGlobalKeyDown = (e: KeyboardEvent) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();

if (status === "error") {
setStatus("none");
updatePageNum();
} else if (status === "none" && !hasFunds && !showNoFunds) {
harvestItem();
}
}
};

window.addEventListener("keydown", handleGlobalKeyDown);
return () => {
window.removeEventListener("keydown", handleGlobalKeyDown);
};
}, [status, hasFunds, showNoFunds]);

async function harvestWithoutGasStation() {
if (!wallet || !contract) throw new Error("Wallet or contract not found");

Expand Down Expand Up @@ -99,7 +120,6 @@ export default function HarvestModal({
const tx = await wallet.sendTransaction(request);

if (tx) {
console.log("tx", tx);
onHarvestSuccess(tileArray[0]);
await paymaster.postJobComplete(jobId);
setModal("plant");
Expand Down Expand Up @@ -184,6 +204,9 @@ export default function HarvestModal({
setStatus("none");
updatePageNum();
}}
role="button"
tabIndex={0}
aria-label="Try again"
>
Try Again
</Button>
Expand All @@ -195,7 +218,13 @@ export default function HarvestModal({
{status === "none" && !hasFunds && !showNoFunds && (
<>
<div style={styles.items}>Harvest this item?</div>
<Button css={buttonStyle} onPress={harvestItem}>
<Button
css={buttonStyle}
onPress={harvestItem}
role="button"
tabIndex={0}
aria-label="Harvest item"
>
Harvest
</Button>
</>
Expand Down
32 changes: 30 additions & 2 deletions frontend/src/components/modals/PlantModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button } from "@fuel-ui/react";
import type { Dispatch, SetStateAction } from "react";
import { useState } from "react";
import { useState, useEffect } from "react";
import { useWalletFunds } from "../../hooks/useWalletFunds";
import { NoFundsMessage } from "./NoFundsMessage";

Expand Down Expand Up @@ -47,6 +47,25 @@ export default function PlantModal({
const { hasFunds, showNoFunds, getBalance, showNoFundsMessage } =
useWalletFunds(contract);

useEffect(() => {
const handleGlobalKeyDown = (e: globalThis.KeyboardEvent) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();

if (status === "error") {
setStatus("none");
} else if (status === "none" && !showNoFunds && seeds > 0) {
handlePlant();
}
}
};

window.addEventListener("keydown", handleGlobalKeyDown);
return () => {
window.removeEventListener("keydown", handleGlobalKeyDown);
};
}, [status, showNoFunds, seeds]);

async function plantWithoutGasStation() {
if (!wallet || !contract) throw new Error("Wallet or contract not found");

Expand Down Expand Up @@ -183,6 +202,9 @@ export default function PlantModal({
onPress={() => {
setStatus("none");
}}
role="button"
tabIndex={0}
aria-label="Try again"
>
Try Again
</Button>
Expand All @@ -193,7 +215,13 @@ export default function PlantModal({
{seeds > 0 ? (
<>
<div style={styles.seeds}>Plant a seed here?</div>
<Button css={buttonStyle} onPress={handlePlant}>
<Button
css={buttonStyle}
onPress={handlePlant}
role="button"
tabIndex={0}
aria-label="Plant seed"
>
Plant
</Button>
</>
Expand Down
34 changes: 32 additions & 2 deletions frontend/src/components/modals/SellItem.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button } from "@fuel-ui/react";
import { Address, InputType, Provider, bn } from "fuels";
import type { Dispatch, SetStateAction } from "react";
import { useState } from "react";
import { useState, useEffect } from "react";
import { useWalletFunds } from "../../hooks/useWalletFunds";
import { NoFundsMessage } from "./NoFundsMessage";

Expand Down Expand Up @@ -40,6 +40,26 @@ export default function SellItem({
const { hasFunds, showNoFunds, getBalance, showNoFundsMessage } =
useWalletFunds(contract);

useEffect(() => {
const handleGlobalKeyDown = (e: globalThis.KeyboardEvent) => {
if (e.key === "Shift") {
e.preventDefault();

if (status === "error") {
setStatus("none");
updatePageNum();
} else if (status === "none" && !showNoFunds) {
sellItems();
}
}
};

window.addEventListener("keydown", handleGlobalKeyDown);
return () => {
window.removeEventListener("keydown", handleGlobalKeyDown);
};
}, [status, showNoFunds]);

async function sellWithoutGasStation() {
if (!wallet || !contract) throw new Error("Wallet or contract not found");

Expand Down Expand Up @@ -191,13 +211,23 @@ export default function SellItem({
setStatus("none");
updatePageNum();
}}
role="button"
tabIndex={0}
aria-label="Try again (press S or Shift)"
>
Try Again
</Button>
</div>
)}
{status === "none" && !showNoFunds && (
<Button css={buttonStyle} variant="outlined" onPress={sellItems}>
<Button
css={buttonStyle}
variant="outlined"
onPress={sellItems}
role="button"
tabIndex={0}
aria-label="Sell all items (press S or Shift)"
>
Sell All Items
</Button>
)}
Expand Down

0 comments on commit 2162acd

Please sign in to comment.