Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: clear order selection on change tabs #5286

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ export function OrdersTableWidget({

useValidatePageUrlParams(orders.length, currentTabId, currentPageNumber)

// Clear selection when changing tabs
useEffect(() => {
updateOrdersToCancel([])
}, [currentTabId, updateOrdersToCancel])

const filteredOrders = useMemo(() => {
if (!searchTerm) return orders

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ export interface OrderRowProps {
isRowSelectable: boolean
isRowSelected: boolean
isChild?: boolean
isExpanded?: boolean
orderParams: OrderParams
onClick: Command
orderActions: OrderActions
children?: React.ReactNode
isTwapTable?: boolean
}

export function OrderRow({
Expand All @@ -96,12 +98,14 @@ export function OrderRow({
isRowSelectable,
isRowSelected,
isChild,
isExpanded,
orderActions,
orderParams,
onClick,
prices,
spotPrice,
children,
isTwapTable,
}: OrderRowProps) {
const { buyAmount, rateInfoParams, hasEnoughAllowance, hasEnoughBalance, chainId } = orderParams
const { creationTime, expirationTime, status } = order
Expand Down Expand Up @@ -399,7 +403,14 @@ export function OrderRow({
)

return (
<TableRow data-id={order.id} isChildOrder={isChild} isHistoryTab={isHistoryTab} isRowSelectable={isRowSelectable}>
<TableRow
data-id={order.id}
isChildOrder={isChild}
isHistoryTab={isHistoryTab}
isRowSelectable={isRowSelectable}
isTwapTable={isTwapTable}
isExpanded={isExpanded}
>
{/*Checkbox for multiple cancellation*/}
{isRowSelectable && !isHistoryTab && (
<TableRowCheckboxWrapper>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { BalancesAndAllowances } from 'modules/tokens'

import { CancellableOrder } from 'common/utils/isOrderCancellable'
import { isOrderOffChainCancellable } from 'common/utils/isOrderOffChainCancellable'
import { getIsComposableCowParentOrder } from 'utils/orderUtils/getIsComposableCowParentOrder'

import { OrderRow } from './OrderRow'
import { CheckboxCheckmark, TableHeader, TableRowCheckbox, TableRowCheckboxWrapper } from './styled'
Expand All @@ -33,17 +34,13 @@ import { OrdersTablePagination } from '../OrdersTablePagination'
// TODO: move elements to styled.jsx

const TableBox = styled.div`
display: block;
display: flex;
flex-flow: column wrap;
border: none;
padding: 0;
position: relative;
background: var(${UI.COLOR_PAPER});

${Media.upToLargeAlt()} {
width: 100%;
display: flex;
flex-flow: column wrap;
}
width: 100%;
`

const TableInner = styled.div`
Expand Down Expand Up @@ -181,11 +178,23 @@ export function OrdersTable({
})
}, [tableHeaders, currentTab])

// Determine if this is a TWAP table by checking if any order has composableCowInfo with a parentId
const isTwapTable = useMemo(() => {
return orders.some((item) => {
const order = getParsedOrderFromTableItem(item)
return getIsComposableCowParentOrder(order)
})
}, [orders])

return (
<>
<TableBox>
<TableInner onScroll={onScroll}>
<TableHeader isHistoryTab={currentTab === HISTORY_TAB.id} isRowSelectable={isRowSelectable}>
<TableHeader
isHistoryTab={currentTab === HISTORY_TAB.id}
isRowSelectable={isRowSelectable}
isTwapTable={isTwapTable}
>
{visibleHeaders.map((header) => {
if (header.id === 'checkbox' && (!isRowSelectable || currentTab === HISTORY_TAB.id)) {
return null
Expand Down Expand Up @@ -245,6 +254,7 @@ export function OrdersTable({
orderParams={getOrderParams(chainId, balancesAndAllowances, order)}
onClick={() => orderActions.selectReceiptOrder(order)}
orderActions={orderActions}
isTwapTable={isTwapTable}
/>
)
} else {
Expand All @@ -261,6 +271,7 @@ export function OrdersTable({
prices={pendingOrdersPrices[item.parent.id]}
isRateInverted={false}
orderActions={orderActions}
isTwapTable={isTwapTable}
/>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import { PendingOrderPrices } from 'modules/orders/state/pendingOrdersPricesAtom
import { BalancesAndAllowances } from 'modules/tokens'

import { OrderRow } from './OrderRow'
import { WarningTooltip } from './OrderRow/OrderWarning'
import * as styledEl from './OrderRow/styled'
import { OrderActions } from './types'

import { ORDERS_TABLE_PAGE_SIZE } from '../../const/tabs'
import { getOrderParams } from '../../utils/getOrderParams'
import { OrderTableGroup } from '../../utils/orderTableGroupUtils'
import { OrdersTablePagination } from '../OrdersTablePagination'
import { OrderStatusBox } from '../OrderStatusBox'

const GroupBox = styled.div``

Expand All @@ -28,6 +30,78 @@ const Pagination = styled(OrdersTablePagination)`
padding: 10px 0;
`

const TwapStatusAndToggleWrapper = styled.div`
display: flex;
align-items: center;
gap: 8px;
width: 100%;
`

function TwapStatusAndToggle({
parent,
childrenLength,
isCollapsed,
onToggle,
onClick,
children,
}: {
parent: any
childrenLength: number
isCollapsed: boolean
onToggle: () => void
onClick: () => void
children: any[]
}) {
// Check if any child has insufficient balance or allowance
const hasChildWithWarning = children.some(
(child) =>
(child.orderParams?.hasEnoughBalance === false || child.orderParams?.hasEnoughAllowance === false) &&
(child.order.status === OrderStatus.PENDING || child.order.status === OrderStatus.SCHEDULED),
)

// Get the first child with a warning to use its parameters
const childWithWarning = hasChildWithWarning
? children.find(
(child) =>
(child.orderParams?.hasEnoughBalance === false || child.orderParams?.hasEnoughAllowance === false) &&
(child.order.status === OrderStatus.PENDING || child.order.status === OrderStatus.SCHEDULED),
)
: null
Copy link
Contributor

@anxolin anxolin Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like there's repetition here. Isn't this the same as hasChildWithWarning? so:

const hasChildWithWarning = !!childWithWarning


return (
<TwapStatusAndToggleWrapper>
<OrderStatusBox
order={parent}
onClick={onClick}
withWarning={hasChildWithWarning}
WarningTooltip={
hasChildWithWarning && childWithWarning
? ({ children }) => (
<WarningTooltip
children={children}
hasEnoughBalance={childWithWarning.orderParams.hasEnoughBalance ?? false}
hasEnoughAllowance={childWithWarning.orderParams.hasEnoughAllowance ?? false}
inputTokenSymbol={childWithWarning.order.inputToken.symbol || ''}
isOrderScheduled={childWithWarning.order.status === OrderStatus.SCHEDULED}
onApprove={() => childWithWarning.orderActions.approveOrderToken(childWithWarning.order.inputToken)}
showIcon={true}
/>
)
: undefined
}
/>
<styledEl.ToggleExpandButton onClick={onToggle} isCollapsed={isCollapsed}>
{childrenLength && (
<i>
{childrenLength} part{childrenLength > 1 && 's'}
</i>
)}
<button />
</styledEl.ToggleExpandButton>
</TwapStatusAndToggleWrapper>
)
}

export interface TableGroupProps {
item: OrderTableGroup
prices: PendingOrderPrices | undefined | null
Expand All @@ -39,6 +113,7 @@ export interface TableGroupProps {
orderActions: OrderActions
chainId: SupportedChainId
balancesAndAllowances: BalancesAndAllowances
isTwapTable?: boolean
}

export function TableGroup(props: TableGroupProps) {
Expand All @@ -53,6 +128,7 @@ export function TableGroup(props: TableGroupProps) {
orderActions,
chainId,
balancesAndAllowances,
isTwapTable,
} = props

const { parent, children } = item
Expand All @@ -73,8 +149,16 @@ export function TableGroup(props: TableGroupProps) {
prices,
isRateInverted,
orderActions,
isTwapTable,
}

// Create an array of child order data with their orderParams
const childrenWithParams = children.map((child) => ({
order: child,
orderParams: getOrderParams(chainId, balancesAndAllowances, child),
orderActions,
}))

return (
<GroupBox>
<OrderRow
Expand All @@ -84,16 +168,17 @@ export function TableGroup(props: TableGroupProps) {
order={parent}
orderParams={getOrderParams(chainId, balancesAndAllowances, parent)}
onClick={() => orderActions.selectReceiptOrder(parent)}
isExpanded={!isCollapsed}
>
{isParentSigning ? undefined : (
<styledEl.ToggleExpandButton onClick={() => setIsCollapsed((state) => !state)} isCollapsed={isCollapsed}>
{childrenLength && (
<i>
{childrenLength} part{childrenLength > 1 && 's'}
</i>
)}
<button />
</styledEl.ToggleExpandButton>
<TwapStatusAndToggle
parent={parent}
childrenLength={childrenLength}
isCollapsed={isCollapsed}
onToggle={() => setIsCollapsed((state) => !state)}
onClick={() => orderActions.selectReceiptOrder(parent)}
children={childrenWithParams}
/>
)}
</OrderRow>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ export const ScrollContainer = styled.div`
scrollbar-width: none;
`

export const TableHeader = styled.div<{ isHistoryTab: boolean; isRowSelectable: boolean }>`
export const TableHeader = styled.div<{ isHistoryTab: boolean; isRowSelectable: boolean; isTwapTable?: boolean }>`
--header-height: 26px;
--row-height: 41px;
--checkboxSize: 16px;
--checkBoxBorderRadius: 3px;
display: grid;
gap: 14px;
grid-template-columns: ${({ isHistoryTab, isRowSelectable }) => {
grid-template-columns: ${({ isHistoryTab, isRowSelectable, isTwapTable }) => {
if (isHistoryTab) {
return `minmax(200px, 2.5fr)
repeat(4, minmax(110px, 1fr))
Expand All @@ -70,6 +70,13 @@ export const TableHeader = styled.div<{ isHistoryTab: boolean; isRowSelectable:
}

const checkboxColumn = isRowSelectable ? 'var(--checkboxSize)' : ''

// TWAP table layout
if (isTwapTable) {
return `${checkboxColumn} minmax(160px,2fr) minmax(120px,1fr) minmax(140px,1fr) minmax(120px,1fr) minmax(120px,1fr) minmax(100px,110px) minmax(190px,1.6fr) 24px`
}

// Default/Limit orders layout
return `${checkboxColumn} minmax(160px,2fr) minmax(120px,1fr) minmax(140px,1fr) minmax(120px,1fr) minmax(120px,1fr) minmax(100px,110px) minmax(80px,0.8fr) 24px`
}};
grid-template-rows: minmax(var(--header-height), 1fr);
Expand All @@ -94,9 +101,12 @@ export const TableRow = styled(TableHeader)<{
isChildOrder?: boolean
isHistoryTab: boolean
isRowSelectable: boolean
isTwapTable?: boolean
isExpanded?: boolean
}>`
grid-template-rows: minmax(var(--row-height), 1fr);
background: ${({ isChildOrder }) => (isChildOrder ? `var(${UI.COLOR_PAPER_DARKER})` : 'transparent')};
background: ${({ isChildOrder, isExpanded }) =>
isExpanded || isChildOrder ? `var(${UI.COLOR_PAPER_DARKER})` : 'transparent'};
transition: background var(${UI.ANIMATION_DURATION}) ease-in-out;
display: grid;

Expand All @@ -115,10 +125,6 @@ export const TableRow = styled(TableHeader)<{
opacity: 0.6;
}
}

&:last-child {
border-bottom: 0;
}
`

export const CheckboxCheckmark = styled.span`
Expand Down
Loading
Loading