Skip to content

Commit

Permalink
Merge pull request #12 from prototyp3-dev/feature/submit-log-feedback
Browse files Browse the repository at this point in the history
Feature/submit log feedback
  • Loading branch information
arthuravianna authored Jan 23, 2024
2 parents 0e905c4 + 2fe2908 commit 5cb9ec8
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 8 deletions.
90 changes: 85 additions & 5 deletions frontend/app/components/CartridgeInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client"

import { ethers } from "ethers";
import React, { Suspense, useContext, useRef } from 'react'
import React, { Suspense, useContext, useRef, useState } from 'react'
import { selectedCartridgeContext } from '../cartridges/selectedCartridgeProvider';
import PowerSettingsNewIcon from '@mui/icons-material/PowerSettingsNew';
import PublishIcon from '@mui/icons-material/Publish';
Expand All @@ -27,8 +27,57 @@ import Link from 'next/link';
import CartridgeScoreboard from './CartridgeScoreboard';
import { envClient } from "../utils/clientEnv";
import { fontPressStart2P } from '../utils/font';
import { delay } from "../utils/util";
import CheckIcon from "./svg/CheckIcon";
import ErrorIcon from "./svg/ErrorIcon";
import CloseIcon from "./svg/CloseIcon";


enum STATUS {
READY,
VALIDATING,
VALID,
INVALID,
}

interface LOG_STATUS {
status:STATUS,
error?:string
}

function logFeedback(logStatus:LOG_STATUS, setLogStatus:Function) {
if (logStatus.status === STATUS.VALID) {
delay(2500).then(() =>{
setLogStatus({status: STATUS.READY} as LOG_STATUS);
})
return (
<div className="fixed flex items-center max-w-xs p-4 space-x-4 text-gray-500 bg-white rounded-lg shadow-lg right-5 bottom-20 dark:text-gray-400 dark:divide-gray-700 space-x dark:bg-gray-800" role="alert">
<CheckIcon/>
<div className="ms-3 text-sm font-bold">Log Validated</div>
</div>
)
} else if (logStatus.status === STATUS.INVALID) {
const click = () => {
setLogStatus({status: STATUS.READY} as LOG_STATUS)
}
return (
<div className="fixed flex-col items-center max-w-xs p-4 space-x-4 text-gray-500 bg-white rounded-lg shadow right-5 bottom-[20%] dark:text-gray-400 dark:divide-gray-700 space-x dark:bg-gray-800" role="alert">
<div className="flex items-center pb-1 border-b">
<ErrorIcon/>
<div className="ms-3 text-sm font-normal">Invalid Log.</div>
<button onClick={click} type="button" className="ms-auto -mx-1.5 -my-1.5 bg-white text-gray-400 hover:text-gray-900 rounded-lg focus:ring-2 focus:ring-gray-300 p-1.5 hover:bg-gray-100 inline-flex items-center justify-center h-8 w-8 dark:text-gray-500 dark:hover:text-white dark:bg-gray-800 dark:hover:bg-gray-700" data-dismiss-target="#toast-danger" aria-label="Close">
<span className="sr-only">Close</span>
<CloseIcon/>
</button>
</div>
<div>
{logStatus.error}
</div>
</div>
)
}
}

function scoreboardFallback() {
const arr = Array.from(Array(3).keys());

Expand Down Expand Up @@ -84,6 +133,7 @@ function CartridgeInfo() {
const fileRef = useRef<HTMLInputElement | null>(null);
const [{ wallet }, connect] = useConnectWallet();
const { download } = useDownloader();
const [submitLogStatus, setSubmitLogStatus] = useState({status: STATUS.READY} as LOG_STATUS);

if (!selectedCartridge) return <></>;

Expand All @@ -106,7 +156,15 @@ function CartridgeInfo() {
in_card: selectedCartridge.inCard ? ethers.utils.hexlify(selectedCartridge.inCard) : "0x",
log: ethers.utils.hexlify(selectedCartridge.gameplayLog)
}
const replayRes = await replay(signer, envClient.DAPP_ADDR, inputData, {decode:true, cartesiNodeUrl: envClient.CARTESI_NODE_URL});

setSubmitLogStatus({status: STATUS.VALIDATING});
try {
const replayRes = await replay(signer, envClient.DAPP_ADDR, inputData, {decode:true, cartesiNodeUrl: envClient.CARTESI_NODE_URL});
console.log(replayRes);
setSubmitLogStatus({status: STATUS.VALID});
} catch (error) {
setSubmitLogStatus({status: STATUS.INVALID, error: (error as Error).message});
}
}

async function uploadLog() {
Expand Down Expand Up @@ -184,9 +242,26 @@ function CartridgeInfo() {
<span>Turn on</span>
</button>

<button className={"button-57"} onClick={() => {submitLog()}} disabled={!selectedCartridge.gameplayLog || !wallet}>
<span><PublishIcon/></span>
<span>Submit Log</span>
<button className={"button-57"} onClick={() => {submitLog()}}
disabled={!selectedCartridge.gameplayLog || !wallet || submitLogStatus.status != STATUS.READY}>
{
submitLogStatus.status === STATUS.READY?
<>
<span><PublishIcon/></span>
<span>Submit Log</span>
</>
:
<>
<span>
<div className='w-5 h-5 border-2 rounded-full border-current animate-spin'>
</div>
</span>

<span>
Validating
</span>
</>
}
</button>

<button className="button-57" onClick={() => {uploadLog()}}>
Expand Down Expand Up @@ -292,6 +367,11 @@ function CartridgeInfo() {
</Tab.Group>
</div>
</div>

{
logFeedback(submitLogStatus, setSubmitLogStatus)
}

</fieldset>
)
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/components/ThemeSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import { useEffect, useState } from "react";


import DarkIcon from "@/app/components/DarkIcon";
import LightIcon from "@/app/components/LightIcon";
import DarkIcon from "@/app/components/svg/DarkIcon";
import LightIcon from "@/app/components/svg/LightIcon";

const LIGHT_TRANSLATE = "-translate-x-2";
const LIGHT_BTN = "bg-yellow-500";
Expand Down
14 changes: 14 additions & 0 deletions frontend/app/components/svg/CheckIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react'

function CheckIcon() {
return (
<div className="inline-flex items-center justify-center flex-shrink-0 w-8 h-8 text-green-500 bg-green-100 rounded-lg dark:bg-green-800 dark:text-green-200">
<svg className="w-5 h-5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20">
<path d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5Zm3.707 8.207-4 4a1 1 0 0 1-1.414 0l-2-2a1 1 0 0 1 1.414-1.414L9 10.586l3.293-3.293a1 1 0 0 1 1.414 1.414Z"/>
</svg>
<span className="sr-only">Check icon</span>
</div>
)
}

export default CheckIcon
11 changes: 11 additions & 0 deletions frontend/app/components/svg/CloseIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react'

function CloseIcon() {
return (
<svg className="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 14">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6"/>
</svg>
)
}

export default CloseIcon
File renamed without changes.
14 changes: 14 additions & 0 deletions frontend/app/components/svg/ErrorIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react'

function ErrorIcon() {
return (
<div className="inline-flex items-center justify-center flex-shrink-0 w-8 h-8 text-red-500 bg-red-100 rounded-lg dark:bg-red-800 dark:text-red-200">
<svg className="w-5 h-5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20">
<path d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5Zm3.707 11.793a1 1 0 1 1-1.414 1.414L10 11.414l-2.293 2.293a1 1 0 0 1-1.414-1.414L8.586 10 6.293 7.707a1 1 0 0 1 1.414-1.414L10 8.586l2.293-2.293a1 1 0 0 1 1.414 1.414L11.414 10l2.293 2.293Z"/>
</svg>
<span className="sr-only">Error icon</span>
</div>
)
}

export default ErrorIcon
File renamed without changes.
1 change: 0 additions & 1 deletion frontend/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ body {
transform: translateY(225%) translateX(-50%);
height: 14px;
line-height: 13px;
font-size: 60%;
}

.button-57:disabled,
Expand Down
3 changes: 3 additions & 0 deletions frontend/app/utils/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function delay(ms: number) {
return new Promise( resolve => setTimeout(resolve, ms) );
}

0 comments on commit 5cb9ec8

Please sign in to comment.