import { Box, Button, Container, Divider, HStack, Tab, TabList, TabPanel, TabPanels, Tabs, Text, useToast, VStack, } from "@chakra-ui/react"; import React, { useState } from "react"; import { useAsync } from "react-async"; import { useEventSource } from "react-sse-hooks"; import { CfdTable } from "./components/cfdtables/CfdTable"; import { CfdTableMaker } from "./components/cfdtables/CfdTableMaker"; import CurrencyInputField from "./components/CurrencyInputField"; import useLatestEvent from "./components/Hooks"; import OrderTile from "./components/OrderTile"; import { Cfd, Order, WalletInfo } from "./components/Types"; import Wallet from "./components/Wallet"; import { CfdSellOrderPayload, postCfdSellOrderRequest } from "./MakerClient"; export default function App() { let source = useEventSource({ source: "/api/feed", options: { withCredentials: true } }); const cfdsOrUndefined = useLatestEvent(source, "cfds"); let cfds = cfdsOrUndefined ? cfdsOrUndefined! : []; const order = useLatestEvent(source, "order"); console.log(cfds); const walletInfo = useLatestEvent(source, "wallet"); const toast = useToast(); let [minQuantity, setMinQuantity] = useState("100"); let [maxQuantity, setMaxQuantity] = useState("1000"); let [orderPrice, setOrderPrice] = useState("10000"); const format = (val: any) => `$` + val; const parse = (val: any) => val.replace(/^\$/, ""); let { run: makeNewCfdSellOrder, isLoading: isCreatingNewCfdOrder } = useAsync({ deferFn: async ([payload]: any[]) => { try { await postCfdSellOrderRequest(payload as CfdSellOrderPayload); } catch (e) { const description = typeof e === "string" ? e : JSON.stringify(e); toast({ title: "Error", description, status: "error", duration: 9000, isClosable: true, }); } }, }); const runningStates = ["Accepted", "Contract Setup", "Pending Open"]; const running = cfds.filter((value) => runningStates.includes(value.state)); const openStates = ["Requested"]; const open = cfds.filter((value) => openStates.includes(value.state)); const closedStates = ["Rejected", "Closed"]; const closed = cfds.filter((value) => closedStates.includes(value.state)); // TODO: remove this. It just helps to detect immediately if we missed a state. const unsorted = cfds.filter((value) => !runningStates.includes(value.state) && !closedStates.includes(value.state) && !openStates.includes(value.state) ); const labelWidth = 110; return ( Current Price: {49000} Min Quantity: setMinQuantity(parse(valueString))} value={format(minQuantity)} /> Min Quantity: setMaxQuantity(parse(valueString))} value={format(maxQuantity)} /> Order Price: setOrderPrice(parse(valueString))} value={format(orderPrice)} /> Leverage: {order && } Running [{running.length}] Open [{open.length}] Closed [{closed.length}] Unsorted [{unsorted.length}] (should be empty) ); }