diff --git a/components/LogBox.tsx b/components/LogBox.tsx index f32c8a2..99f411f 100644 --- a/components/LogBox.tsx +++ b/components/LogBox.tsx @@ -1,22 +1,17 @@ -import { - useRef, - useLayoutEffect, - ReactNode, - FC, - useState, - useCallback, -} from "react"; -import { Notepad, Prohibit } from "phosphor-react"; -import useStayScrolled from "react-stay-scrolled"; import NextLink from "next/link"; - -import Container from "./Container"; -import LogText from "./LogText"; -import state, { ILog } from "../state"; -import { Pre, Link, Heading, Button, Text, Flex, Box } from "."; +import { Notepad, Prohibit } from "phosphor-react"; +import { + FC, ReactNode, useCallback, useLayoutEffect, useRef, useState +} from "react"; +import useStayScrolled from "react-stay-scrolled"; import regexifyString from "regexify-string"; import { useSnapshot } from "valtio"; +import { Box, Button, Flex, Heading, Link, Pre, Text } from "."; +import state, { ILog } from "../state"; import { AccountDialog } from "./Accounts"; +import Container from "./Container"; +import LogText from "./LogText"; + interface ILogBox { title: string; @@ -160,7 +155,7 @@ export const Log: FC = ({ const enrichAccounts = useCallback( (str?: string): ReactNode => { - if (!str || !accounts.length) return null; + if (!str) return null; const pattern = `(${accounts.map((acc) => acc.address).join("|")})`; const res = regexifyString({ diff --git a/state/actions/addFaucetAccount.ts b/state/actions/addFaucetAccount.ts index 00d6b02..9f02313 100644 --- a/state/actions/addFaucetAccount.ts +++ b/state/actions/addFaucetAccount.ts @@ -33,8 +33,6 @@ export const addFaucetAccount = async (showToast: boolean = false) => { return toast.error("You can only have maximum 6 accounts"); } if (typeof window !== 'undefined') { - - const toastId = showToast ? toast.loading("Creating account") : ""; const res = await fetch(`${window.location.origin}/api/faucet`, { method: "POST", @@ -92,5 +90,4 @@ export const addFunds = async (address: string) => { currAccount.xrp = (Number(currAccount.xrp) + (json.xrp * 1000000)).toString(); } } - -} \ No newline at end of file +} diff --git a/state/actions/compileCode.ts b/state/actions/compileCode.ts index 92e32c9..e06122a 100644 --- a/state/actions/compileCode.ts +++ b/state/actions/compileCode.ts @@ -1,10 +1,10 @@ -import toast from "react-hot-toast"; import Router from 'next/router'; - +import toast from "react-hot-toast"; +import { ref } from "valtio"; +import { decodeBinary } from "../../utils/decodeBinary"; import state from "../index"; import { saveFile } from "./saveFile"; -import { decodeBinary } from "../../utils/decodeBinary"; -import { ref } from "valtio"; + /* compileCode sends the code of the active file to compile endpoint * If all goes well you will get base64 encoded wasm file back with @@ -15,17 +15,22 @@ import { ref } from "valtio"; export const compileCode = async (activeId: number) => { // Save the file to global state saveFile(false); + if (!process.env.NEXT_PUBLIC_COMPILE_API_ENDPOINT) { throw Error("Missing env!"); } + // Bail out if we're already compiling if (state.compiling) { // if compiling is ongoing return return; } + // Set loading state to true state.compiling = true; + // Reset development log state.logs = [] + try { const res = await fetch(process.env.NEXT_PUBLIC_COMPILE_API_ENDPOINT, { method: "POST", @@ -46,6 +51,7 @@ export const compileCode = async (activeId: number) => { }); const json = await res.json(); state.compiling = false; + if (!json.success) { state.logs.push({ type: "error", message: json.message }); if (json.tasks && json.tasks.length > 0) { @@ -57,16 +63,19 @@ export const compileCode = async (activeId: number) => { } return toast.error(`Couldn't compile!`, { position: "bottom-center" }); } + state.logs.push({ type: "success", message: `File ${state.files?.[activeId]?.name} compiled successfully. Ready to deploy.`, link: Router.asPath.replace("develop", "deploy"), linkText: "Go to deploy", }); + // Decode base64 encoded wasm that is coming back from the endpoint const bufferData = await decodeBinary(json.output); state.files[state.active].compiledContent = ref(bufferData); state.files[state.active].lastCompiled = new Date(); + // Import wabt from and create human readable version of wasm file and // put it into state import("wabt").then((wabt) => { @@ -80,12 +89,22 @@ export const compileCode = async (activeId: number) => { state.files[state.active].compiledWatContent = wast; toast.success("Compiled successfully!", { position: "bottom-center" }); }); - } catch (err) { - console.log(err); - state.logs.push({ - type: "error", - message: "Error occured while compiling!", - }); + } catch (err: any) { + const error = err as Error + + // TODO: Centralized error handling? Just a thought + if(error.message.includes("Failed to fetch")) { + state.logs.push({ + type: "error", + message: "No connection to the compiler server!", + }); + } else { + state.logs.push({ + type: "error", + message: "Error occured while compiling!", + }); + } + state.compiling = false; } }; diff --git a/state/actions/deployHook.ts b/state/actions/deployHook.ts index dd2c77f..fef94ba 100644 --- a/state/actions/deployHook.ts +++ b/state/actions/deployHook.ts @@ -1,9 +1,10 @@ -import { derive, sign } from "xrpl-accountlib"; import toast from "react-hot-toast"; - -import state, { IAccount } from "../index"; -import calculateHookOn, { TTS } from "../../utils/hookOnCalculator"; +import { derive, sign } from "xrpl-accountlib"; +import { AnyJson } from "xrpl-client"; import { SetHookData } from "../../components/SetHookDialog"; +import calculateHookOn, { TTS } from "../../utils/hookOnCalculator"; +import state, { IAccount } from "../index"; + const hash = async (string: string) => { const utf8 = new TextEncoder().encode(string); @@ -51,19 +52,22 @@ function arrayBufferToHex(arrayBuffer?: ArrayBuffer | null) { * hex string, signs the transaction and deploys it to * Hooks testnet. */ -export const deployHook = async (account: IAccount & { name?: string }, data: SetHookData) => { +export const deployHook = async (account: IAccount & { name?: string }, data: SetHookData): Promise => { if ( !state.files || state.files.length === 0 || !state.files?.[state.active]?.compiledContent ) { + console.log("ebin1") return; } if (!state.files?.[state.active]?.compiledContent) { + console.log("ebin2") return; } if (!state.client) { + console.log("ebin3") return; } const HookNamespace = await hash(arrayBufferToHex( @@ -136,6 +140,7 @@ export const deployHook = async (account: IAccount & { name?: string }, data: Se }); } } catch (err) { + console.log("Ebin") console.log(err); state.deployLogs.push({ type: "error", @@ -220,4 +225,4 @@ export const deleteHook = async (account: IAccount & { name?: string }) => { } return submitRes; } -}; \ No newline at end of file +};