Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/release-0.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
EchoDex committed Oct 4, 2024
2 parents e7f7240 + 2357fee commit e70b7c7
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 78 deletions.
5 changes: 5 additions & 0 deletions src/assets/icons/tableSettings.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion src/assets/icons/tablesSize.svg

This file was deleted.

1 change: 1 addition & 0 deletions src/assets/icons/tablesSizeSelector.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 4 additions & 73 deletions src/screens/SpotScreen/BottomTables/BaseTable.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import React, { useState } from "react";
import { Config } from "react-popper-tooltip";
import React from "react";
import styled from "@emotion/styled";
import { observer } from "mobx-react-lite";

import { Row } from "@components/Flex";
import SizedBox from "@components/SizedBox";
import { SmartFlex } from "@components/SmartFlex";
import Tab from "@components/Tab";
import Text, { TEXT_TYPES } from "@components/Text";
import Tooltip from "@components/Tooltip";
import { media } from "@themes/breakpoints";

import tableSizeSelector from "@assets/icons/tablesSize.svg";

import { useStores } from "@stores";
import { TRADE_TABLE_SIZE } from "@stores/SettingsStore";

import { MAX_TABLE_HEIGHT, RESIZE_TOOLTIP_CONFIG, TABLE_SIZES_CONFIG } from "./constants";
import { MAX_TABLE_HEIGHT } from "./constants";
import { TableActionButtons } from "./TableActionButtons";

interface Props {
tabs: { title: string; disabled: boolean; rowCount: number }[];
Expand All @@ -28,19 +24,6 @@ interface Props {
export const BaseTable: React.FC<Props> = observer(({ tabs, activeTab, onTabClick, children }) => {
const { settingsStore } = useStores();

const [isTooltipVisible, setIsTooltipVisible] = useState(false);

const tooltipConfig: Config = {
...RESIZE_TOOLTIP_CONFIG,
visible: isTooltipVisible,
onVisibleChange: setIsTooltipVisible,
};

const handleTableSize = (size: TRADE_TABLE_SIZE) => {
settingsStore.setTradeTableSize(size);
setIsTooltipVisible(false);
};

return (
<Root gap="16px" size={settingsStore.tradeTableSize} column>
<TableRoot>
Expand All @@ -63,35 +46,7 @@ export const BaseTable: React.FC<Props> = observer(({ tabs, activeTab, onTabClic
)}
</Tab>
))}
<TableSizeSelector>
<Tooltip
config={tooltipConfig}
content={
<div>
{TABLE_SIZES_CONFIG.map(({ size, icon, title }) => (
<TableSize
key={title}
active={settingsStore.tradeTableSize === size}
onClick={() => handleTableSize(size)}
>
<img alt={title} src={icon} />
<SizedBox width={4} />
<Text type={TEXT_TYPES.BUTTON} nowrap>
{title.toUpperCase()}
</Text>
</TableSize>
))}
</div>
}
>
<img
alt="Table Size"
src={tableSizeSelector}
style={{ cursor: "pointer" }}
onClick={() => setIsTooltipVisible(true)}
/>
</Tooltip>
</TableSizeSelector>
<TableActionButtons />
</TabContainer>
<TableContainer className="better-scroll">{children}</TableContainer>
</TableRoot>
Expand Down Expand Up @@ -134,36 +89,12 @@ const TabContainer = styled(Row)`
}
`;

const TableSizeSelector = styled.div`
position: absolute;
right: 12px;
top: 4px;
${media.mobile} {
display: none;
}
`;

const TableContainer = styled(SmartFlex)`
width: 100%;
height: 100%;
overflow-y: scroll;
`;

const TableSize = styled.div<{ active?: boolean }>`
display: flex;
align-items: center;
padding: 4px 12px;
width: 100%;
cursor: pointer;
${({ active, theme }) => active && `background: ${theme.colors.borderPrimary}`};
:hover {
background: ${({ theme }) => theme.colors.borderSecondary};
}
`;

const Badge = styled.div`
margin-left: 4px;
background: ${({ theme }) => theme.colors.borderSecondary};
Expand Down
47 changes: 43 additions & 4 deletions src/screens/SpotScreen/BottomTables/SpotTable/SpotTableVM.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React, { PropsWithChildren, useMemo } from "react";
import { makeAutoObservable, reaction } from "mobx";
import { Nullable } from "tsdef";

import { OrderType } from "@compolabs/spark-orderbook-ts-sdk";

import useVM from "@hooks/useVM";
import { RootStore, useStores } from "@stores";

Expand Down Expand Up @@ -42,9 +44,33 @@ class SpotTableVM {

isOpenOrdersLoaded = false;
isHistoryOrdersLoaded = false;

// filters
offset = 0;
limit = 10;

filterIsSellOrderTypeEnabled = true;
filterIsBuyOrderTypeEnabled = true;
toggleFilterOrderType = (orderType: OrderType) => {
if (orderType === OrderType.Sell) {
if (this.filterIsSellOrderTypeEnabled && !this.filterIsBuyOrderTypeEnabled) {
this.filterIsSellOrderTypeEnabled = false;
this.filterIsBuyOrderTypeEnabled = true;
return;
}
this.filterIsSellOrderTypeEnabled = !this.filterIsSellOrderTypeEnabled;
return;
}

if (this.filterIsBuyOrderTypeEnabled && !this.filterIsSellOrderTypeEnabled) {
// Cannot uncheck 'buy' because 'sell' is already unchecked
this.filterIsBuyOrderTypeEnabled = false;
this.filterIsSellOrderTypeEnabled = true;
return;
}
this.filterIsBuyOrderTypeEnabled = !this.filterIsBuyOrderTypeEnabled;
};

constructor(rootStore: RootStore) {
makeAutoObservable(this);
this.rootStore = rootStore;
Expand All @@ -68,6 +94,21 @@ class SpotTableVM {
return this.isOpenOrdersLoaded && this.isHistoryOrdersLoaded;
}

get tableFilters() {
const orderType =
this.filterIsSellOrderTypeEnabled && this.filterIsBuyOrderTypeEnabled
? undefined
: this.filterIsSellOrderTypeEnabled
? OrderType.Sell
: OrderType.Buy;

return {
limit: this.limit,
offset: this.offset,
orderType,
};
}

cancelOrder = async (order: SpotMarketOrder) => {
const { notificationStore } = this.rootStore;
const bcNetwork = FuelNetwork.getInstance();
Expand Down Expand Up @@ -117,8 +158,7 @@ class SpotTableVM {

this.subscriptionToOpenOrders = bcNetwork
.subscribeSpotOrders({
limit: this.limit,
offset: this.offset,
...this.tableFilters,
market: tradeStore.market!.contractAddress,
asset: tradeStore.market!.baseToken.assetId,
user: accountStore.address!,
Expand Down Expand Up @@ -147,8 +187,7 @@ class SpotTableVM {
}
this.subscriptionToHistoryOrders = bcNetwork
.subscribeSpotOrders({
limit: this.limit,
offset: this.offset,
...this.tableFilters,
market: tradeStore.market!.contractAddress,
asset: tradeStore.market!.baseToken.assetId,
user: accountStore.address!,
Expand Down
176 changes: 176 additions & 0 deletions src/screens/SpotScreen/BottomTables/TableActionButtons.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import React, { useState } from "react";
import { Config } from "react-popper-tooltip";
import styled from "@emotion/styled";
import { observer } from "mobx-react-lite";

import { OrderType } from "@compolabs/spark-orderbook-ts-sdk";

import SizedBox from "@components/SizedBox";
import { SmartFlex } from "@components/SmartFlex";
import Text, { TEXT_TYPES } from "@components/Text";
import Tooltip from "@components/Tooltip";
import { media } from "@themes/breakpoints";

import TableSettingsIcon from "@assets/icons/tableSettings.svg?react";
import TableSizeSelectorIcon from "@assets/icons/tablesSizeSelector.svg?react";

import { useStores } from "@stores";
import { TRADE_TABLE_SIZE } from "@stores/SettingsStore";

import { Checkbox } from "@src/components/Checkbox";

import { useSpotTableVMProvider } from "./SpotTable/SpotTableVM";
import { RESIZE_TOOLTIP_CONFIG, TABLE_SIZES_CONFIG } from "./constants";

const useTooltipConfig = (isVisible: boolean, setIsVisible: React.Dispatch<React.SetStateAction<boolean>>): Config => ({
...RESIZE_TOOLTIP_CONFIG,
visible: isVisible,
onVisibleChange: setIsVisible,
});

export const TableActionButtons: React.FC = observer(() => {
const vm = useSpotTableVMProvider();
const { settingsStore } = useStores();

const [isResizeTooltipVisible, setIsResizeTooltipVisible] = useState(false);
const [isSettingsTooltipVisible, setIsSettingsTooltipVisible] = useState(false);

const resizeTooltipConfig = useTooltipConfig(isResizeTooltipVisible, setIsResizeTooltipVisible);
const settingsTooltipConfig = useTooltipConfig(isSettingsTooltipVisible, setIsSettingsTooltipVisible);

const handleTableSize = (size: TRADE_TABLE_SIZE) => {
settingsStore.setTradeTableSize(size);
setIsResizeTooltipVisible(false);
};

const isAnySettingsChanged = false;

const renderResizeTooltipContent = () => {
return (
<div>
{TABLE_SIZES_CONFIG.map(({ size, icon, title }) => (
<TableSize key={title} active={settingsStore.tradeTableSize === size} onClick={() => handleTableSize(size)}>
<img alt={title} src={icon} />
<SizedBox width={4} />
<Text type={TEXT_TYPES.BUTTON} nowrap>
{title.toUpperCase()}
</Text>
</TableSize>
))}
</div>
);
};

const renderSettingsTooltipContent = () => {
return (
<SettingsTooltipContainer>
{/* <SmartFlex alignItems="flex-start" gap="12px" column>
<Text type={TEXT_TYPES.BODY} secondary>
Type
</Text>
<Checkbox checked>
<Text type={TEXT_TYPES.BUTTON_SECONDARY} primary>
MARKET
</Text>
</Checkbox>
<Checkbox checked>
<Text type={TEXT_TYPES.BUTTON_SECONDARY} primary>
LIMIT
</Text>
</Checkbox>
</SmartFlex> */}
<SmartFlex alignItems="flex-start" gap="12px" column>
<Text type={TEXT_TYPES.BODY} secondary>
Side
</Text>
<Checkbox checked={vm.filterIsBuyOrderTypeEnabled} onChange={() => vm.toggleFilterOrderType(OrderType.Buy)}>
<Text type={TEXT_TYPES.BUTTON_SECONDARY} primary>
BUY
</Text>
</Checkbox>
<Checkbox checked={vm.filterIsSellOrderTypeEnabled} onChange={() => vm.toggleFilterOrderType(OrderType.Sell)}>
<Text type={TEXT_TYPES.BUTTON_SECONDARY} primary>
SELL
</Text>
</Checkbox>
</SmartFlex>
</SettingsTooltipContainer>
);
};

const renderResizeButton = () => {
return (
<Tooltip config={resizeTooltipConfig} content={renderResizeTooltipContent()}>
<Icon isActive={isResizeTooltipVisible}>
<TableSizeSelectorIcon onClick={() => setIsResizeTooltipVisible(true)} />
</Icon>
</Tooltip>
);
};

const renderSettingsButton = () => {
return (
<Tooltip config={settingsTooltipConfig} content={renderSettingsTooltipContent()}>
{isAnySettingsChanged && <GreenDot />}
<Icon isActive={isSettingsTooltipVisible}>
<TableSettingsIcon onClick={() => setIsSettingsTooltipVisible(true)} />
</Icon>
</Tooltip>
);
};

return (
<ActionContainer>
{renderResizeButton()}
{renderSettingsButton()}
</ActionContainer>
);
});

const ActionContainer = styled(SmartFlex)`
gap: 12px;
position: absolute;
right: 12px;
top: 4px;
${media.mobile} {
display: none;
}
`;

const TableSize = styled.div<{ active?: boolean }>`
display: flex;
align-items: center;
padding: 4px 12px;
width: 100%;
cursor: pointer;
${({ active, theme }) => active && `background: ${theme.colors.borderPrimary}`};
:hover {
background: ${({ theme }) => theme.colors.borderSecondary};
}
`;

const SettingsTooltipContainer = styled(SmartFlex)`
gap: 16px;
padding: 0 16px 16px;
position: relative;
`;

const GreenDot = styled.div`
position: absolute;
right: -3px;
top: -3px;
width: 8px;
height: 8px;
background-color: ${({ theme }) => theme.colors.greenLight};
border-radius: 8px;
`;

const Icon = styled(SmartFlex)<{ isActive: boolean }>`
cursor: pointer;
transition: color 250ms;
color: ${({ theme, isActive }) => (isActive ? theme.colors.textPrimary : theme.colors.textDisabled)};
`;

0 comments on commit e70b7c7

Please sign in to comment.