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: improve Orderbook UI #556

Merged
merged 193 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
193 commits
Select commit Hold shift + click to select a range
1d5dcb1
feat: move Swap/Limit and Buy/Sell controls to radio group controls
dib542 May 9, 2024
cc01915
feat: remove "HALF" wallet amount shortcut button from token input
dib542 May 10, 2024
1a2780d
feat: allow custom header text on token input
dib542 May 10, 2024
6108a63
feat: allow token change to be optional on token input
dib542 May 10, 2024
828005b
feat: replace token amount with amountIn amountOut input groups
dib542 May 10, 2024
1e6300c
feat: allow maxValue=0 to remove "MAX" shortcut button on token input
dib542 May 10, 2024
56b9324
feat: improve estimated trade price
dib542 May 10, 2024
13e732f
feat: improve estimated (validating) trade price part 2
dib542 May 10, 2024
b0ac12d
feat: enforce order type change with price tab type change
dib542 May 10, 2024
eea0679
feat: refine Expiration control setting in Limit Order card
dib542 May 10, 2024
f9f96f6
feat: reduce recomputation on expiration time changes
dib542 May 10, 2024
d31a947
feat: put Swap/Limit Buy/Sell buttons in card header
dib542 May 10, 2024
3b5529d
Revert "feat: put Swap/Limit Buy/Sell buttons in card header"
dib542 May 10, 2024
56e96a1
feat: focus on input control when click input group
dib542 May 10, 2024
b5b47bb
feat: remove less useful simulation result information
dib542 May 10, 2024
d09be32
fix: initial state then type amountOut fix, price was 0
dib542 May 10, 2024
eb661dd
feat: make easier to focus on text inputs from anywhere in group
dib542 May 10, 2024
1b68d7e
feat: add basic styling of limit price input
dib542 May 10, 2024
a8265e5
feat: refine design of Expiry control to single click to custom
dib542 May 10, 2024
da8c7b3
feat: enable RadioButtonGroup to skip buttons of no description
dib542 May 10, 2024
e4712bb
feat: add LimitPrice input, connected to all other input fields
dib542 May 10, 2024
49fa8cb
feat: remove Swap/Limit buttons to make card header more compact
dib542 May 10, 2024
ba40847
Revert "feat: remove Swap/Limit buttons to make card header more comp…
dib542 May 10, 2024
1abd58c
feat: control LimitPrice visibility with Swap tab only
dib542 May 10, 2024
1bf3321
feat: put Swap/Limit Buy/Sell buttons in card header
dib542 May 10, 2024
0873663
feat: stop LimitOrder card from scrolling
dib542 May 10, 2024
93e850b
refactor: remove grid layout from TokenInputGroup
dib542 May 14, 2024
4fd2e5d
refactor: don't redefine border width and style twice
dib542 May 14, 2024
6ef30e3
fix: align header text and header button text using extra margins
dib542 May 14, 2024
0c8a556
feat: allow extra subheader fields to TokenInputGroup
dib542 May 14, 2024
6423f7e
feat: display error state if user has insufficient funds for order
dib542 May 14, 2024
f9b01ea
feat: display zero balance to explain why there is no trade preview
dib542 May 14, 2024
8427ff9
fix: prevent estimated trades from going above user's input balance:
dib542 May 14, 2024
676ec6b
fix: make order confirmation button state based on simulation result
dib542 May 14, 2024
19b4e07
feat: add inactive state to token input to indicate validating results
dib542 May 14, 2024
d3ff604
feat: improve unconnected wallet state
dib542 May 14, 2024
b541823
fix: improve isLoading state:
dib542 May 14, 2024
abcb189
fix: estimation limit should be in base denom not display denom
dib542 May 14, 2024
5ed9606
feat: make chart a dynamic height
dib542 May 15, 2024
28bf86e
feat: make chart row wider if possible
dib542 May 15, 2024
c709438
feat: create space for a chart-depth connection element
dib542 May 15, 2024
3d03a36
feat: use custom colors on TradingView chart
dib542 May 15, 2024
a44dbe9
feat: decouple chart from depth
dib542 May 15, 2024
f6fd635
feat: add more chart resolutions, default to 5 minute wicks
dib542 May 15, 2024
13a0886
feat: hide the pools tab (but keep the page available)
dib542 May 15, 2024
c1d5a48
Revert "feat: add more chart resolutions, default to 5 minute wicks"
dib542 May 16, 2024
a216658
feat: switch "Order" heading to "Trade"
dib542 May 16, 2024
3fc1f79
refactor: use NumberInput for limit order input:
dib542 May 16, 2024
954985d
feat: add error style for "0" limit price
dib542 May 16, 2024
1d06407
feat: abstract out LimitPriceInput component
dib542 May 16, 2024
d2524dd
fix: tabbing in TokenInputGroup where Tokens cannot be changed
dib542 May 16, 2024
2fde683
feat: allow input focus on LimitPrice group click
dib542 May 16, 2024
b5f1ca4
fix: allow unrounded numbers in Custom time amount input
dib542 May 16, 2024
7998a0b
feat: allow "Custom" PriceLimit shortcut button:
dib542 May 16, 2024
5934992
feat: select all limit price input on "Custom" price limit shortcut
dib542 May 16, 2024
4f0f5a6
feat: refine expiration time shortcuts
dib542 May 16, 2024
b11da0d
feat: allow expiration time shortcut to prefill custom expiration time
dib542 May 16, 2024
7512029
fix: change form values before animation makes the change visible
dib542 May 16, 2024
a309054
feat: refine wording of "My Orders" table
dib542 May 16, 2024
e031c6a
feat: make focusability of limit order inputs more visible:
dib542 May 16, 2024
a616019
Revert "Revert "feat: add more chart resolutions, default to 5 minute…
dib542 May 16, 2024
73daf77
feat: add normal page-card background to OrderbookChart card
dib542 May 17, 2024
a8a9b41
feat: visually connect the chart and depth cards with background color
dib542 May 17, 2024
d808330
feat: add order depth price indication tracking
dib542 May 17, 2024
7409a57
fix: higher prices should be at the top of the depth table
dib542 May 17, 2024
5144c80
feat: show priceIndication on depth table
dib542 May 17, 2024
85eeecc
feat: add depth price indication line on depth table hover
dib542 May 17, 2024
2a9625c
fix: Tab components were causing abrupt re-renders when not needed
dib542 May 17, 2024
24ee5e8
fix: ensure chart style overrides are applied properly
dib542 May 17, 2024
6952382
feat: show Orderbook depth price indication on Orderbook chart
dib542 May 17, 2024
129ae48
fix: switch to useState for chart tracking: to be able to react to
dib542 May 17, 2024
a233b00
feat: connect chart and depth components by price
dib542 May 18, 2024
fdea84a
fix: remove non-zero depth rows
dib542 May 18, 2024
aaa1e7a
feat: add pixel to overlap depth table border
dib542 May 18, 2024
b4566d3
feat: use a bezier curve to join lines
dib542 May 18, 2024
936019e
feat: ease up on connection line sharpness
dib542 May 18, 2024
6ba4283
feat: add connection area drawings
dib542 May 18, 2024
e26e247
feat: combine price indication and offset together
dib542 May 19, 2024
9b8eb8a
feat: improve indexer to add each new update in onAccumulated metadata
dib542 May 19, 2024
05b264c
feat: add useBuckets hook for bucketing to a specific price resolution
dib542 May 19, 2024
eb13c1f
fix: remove unused bucket transition style calculations
dib542 May 20, 2024
108574f
fix: chart-connector height should grow and shrink depending on its c…
dib542 May 20, 2024
2a07cef
fix: allow dynamic row count in Orderbook depth table
dib542 May 20, 2024
4d4742a
fix: align the set price line from the Orderbook depth with connector
dib542 May 20, 2024
05a298d
refactor: make column widths easier to understand
dib542 May 20, 2024
d560f86
feat: use buckets hook for Orderbook depth table
dib542 May 20, 2024
32ceff0
refactor: simplify PriceOffset type
dib542 May 20, 2024
9c6fab3
feat: expand ConnectionArea type
dib542 May 20, 2024
2809187
fix: table row keys were inefficient or not present
dib542 May 20, 2024
ed3741e
feat: add tracking for bucket price position offsets of Depth table
dib542 May 20, 2024
46a5c91
fix: make prices explicit: price was being confused for displayPrice
dib542 May 20, 2024
248bc6b
refactor: store Buckets in inner/outer bounds rather than lower/upper:
dib542 May 20, 2024
36b04b1
feat: draw bucket price connections
dib542 May 20, 2024
5543643
feat: add depthPriceIndication hover state to bucket connections
dib542 May 20, 2024
d4afb38
feat: draw liquidity and cumulative liquidity buckets in connector
dib542 May 21, 2024
8caa79b
feat: add half-pixel indicator of liquidity
dib542 May 21, 2024
9f34089
feat: make conenction areas less distracting
dib542 May 21, 2024
f34dbee
fix: prevent text-wrapping in Orderbook depth current price cell
dib542 May 21, 2024
ec20c0d
perf: don't require sharp points for fill area drawings
dib542 May 21, 2024
d0c0ebb
perf: abstract out connection curve point math
dib542 May 21, 2024
b2cf4e4
feat: add dynamic decimal places for Orderbook depth amount
dib542 May 21, 2024
eae4662
feat: add directional color to Orderbook depth
dib542 May 21, 2024
cea6f98
refactor: move token valuation higher for better efficiency
dib542 May 21, 2024
9259aed
feat: add Orderbook depth colored cell backgrounds
dib542 May 21, 2024
43f367a
feat: limit area connectors to only visible Orderbook depth rows
dib542 May 21, 2024
e3fe527
feat: refine bucket connection opacities
dib542 May 21, 2024
e434538
perf: reduce number of chart liquidity buckets drawn
dib542 May 21, 2024
33d3415
feat: add bucket resolution control
dib542 May 21, 2024
3c0c0aa
feat: set a reasonable first bucket resolution on first pair price
dib542 May 21, 2024
bbb204f
fix: format price values correctly
dib542 May 21, 2024
ccfaa59
fix: allow undefined bucketResolution when liquidity is not yet loaded
dib542 May 21, 2024
58f7f6b
feat: control connection areas to display only with Depth table tab
dib542 May 21, 2024
284b62b
feat: add more room for depth chart
dib542 May 21, 2024
2ac05f5
feat: allow Trades list to display enough rows to scroll
dib542 May 22, 2024
da2d688
feat: balance red/green brightnesses better, align to text colors
dib542 May 22, 2024
f330e3c
fix: flip red/green colors
dib542 May 22, 2024
1b799a0
feat: add brightness corrected colors to connection areas
dib542 May 22, 2024
2b93686
feat: ensure depth numbers are drawn over background colors
dib542 May 22, 2024
d52a5ee
fix: draw depth price indication connection line with existing offsets
dib542 May 22, 2024
5e381c0
refactor: abstract out depth price indication line creation
dib542 May 22, 2024
fa99780
refactor: name ConnectionLine better to represent what it looks like:
dib542 May 22, 2024
d92926c
feat: add active row styling for when current price row is hovered
dib542 May 22, 2024
2215521
feat: remove price indication for current price on chart
dib542 May 22, 2024
bd3fe8e
feat: re-enable chart scrolling
dib542 May 22, 2024
c0e22e9
feat: allow chart axis updates to run faster when chart is focused
dib542 May 22, 2024
019eeb0
feat: enable click to select limit price
dib542 May 22, 2024
ea70adc
fix: track "hovered" style using current price indication:
dib542 May 22, 2024
4480f94
feat: allow setting price indication from LimitOrder form
dib542 May 22, 2024
2676f3e
fix: RadioButtonGroupInput can send strings when numbers were expected
dib542 May 22, 2024
c9047ee
feat: highlight top of current price bucket when trading in it
dib542 May 22, 2024
5ee2474
fix: ensure active buckets are consistently calculated
dib542 May 22, 2024
14d9875
feat: allow toggling the display of the connector depth chart
dib542 May 23, 2024
e2f1d49
feat: add space for connector visualization only when control is active
dib542 May 23, 2024
eb16eae
feat: add more options to connector width
dib542 May 23, 2024
ebba395
feat: refine colors and bucket widths to be less intrusive
dib542 May 23, 2024
71b98d1
fix: draw connection lines over chart bucket areas
dib542 May 23, 2024
26c0e41
feat: make chart connector default width even smaller
dib542 May 23, 2024
02ed15e
feat: make chart connector inidividual buckets width smaller
dib542 May 23, 2024
0339d8f
feat: make cumulative depth indicating more subtle
dib542 May 23, 2024
dc3ddcf
fix: alignment of connector side was off by border-width
dib542 May 23, 2024
00d41ba
perf: only use sharp points for price chart axis connection, dimension
dib542 May 23, 2024
31d5cdf
feat: hide left toolbar of chart by default
dib542 May 24, 2024
c97eda3
fix: remove price indication line when nothing to connect to
dib542 May 24, 2024
ab72d77
feat: move connection button controls into the Depth table toolbar
dib542 May 24, 2024
0f05a9b
refactor: simplify filteredBuckets usage in depth table
dib542 May 24, 2024
72b52f8
feat: add show Buy/Sell only buttons to Depth toolbar
dib542 May 24, 2024
eb6608e
feat: balance red and greens more
dib542 May 24, 2024
00a4e84
fix: prevent thrown exception when changing between chart timescales
dib542 May 24, 2024
132061b
feat: fix possible large width of average price description
dib542 May 24, 2024
1653376
fix: fix value-suffix spacing layout by combining to one inline-block
dib542 May 24, 2024
f2ee8e6
feat: make Trade card even smaller: sized to header buttons
dib542 May 24, 2024
5fdf6fc
fix: ensure that warnings and errors don't resize the card width
dib542 May 24, 2024
1648ee8
feat: add depth reserves % bar indication
dib542 May 24, 2024
a36b8c1
fix: fix accidental mutation of stateful arrays
dib542 May 24, 2024
ff6105c
fix: check chart state before doing some chart functions
dib542 May 24, 2024
b3230b8
feat: add Depth table transition effects
dib542 May 24, 2024
b9bc50f
feat: add price-slippage indication to Orderbook chart
dib542 May 24, 2024
2e1d969
fix: opacity admjustments should be applied just before drawing:
dib542 May 25, 2024
d948121
feat: reduce component logic by asking chart to create own empty bars
dib542 May 30, 2024
307f79f
fix: reduce initial chart renderings on component start
dib542 May 31, 2024
a35b095
feat: make order line color white to match orderbook depth table
dib542 Jun 2, 2024
0ad999a
feat: refine price connection area opacity
dib542 Jun 2, 2024
7a78f9e
feat: allow priceIndication to be 0 but disallow price to be 0
dib542 Jun 2, 2024
a89b6cb
Revert "feat: hide the pools tab (but keep the page available)"
dib542 Jun 2, 2024
3fe8cc6
fix: fix inconsistent calculation leading to max update depth error
dib542 Jun 2, 2024
9792f6c
fix: all txs were showing in "My Orders" with unconnected wallets
dib542 Jun 3, 2024
6a9c9d6
feat: add better empty My Orders table description
dib542 Jun 3, 2024
b17415f
feat: add empty My Orders table description for unconnected state
dib542 Jun 3, 2024
a77346c
fix: buy/sell percentage must take into account reserve prices
dib542 Jun 3, 2024
d9eb10a
Merge branch 'main' into 555-improve-orderbook-order-card
dib542 Jun 3, 2024
b63c6b3
fix: ensure undefined bucketOffsets are handled correctly
dib542 Jun 3, 2024
6feafcf
refactor: make default undefined bucketOffset more visibly consistent
dib542 Jun 3, 2024
f46ec16
fix: incorrect end price could sometimes be calculated
dib542 Jun 3, 2024
daf1de6
refactor: combine two effects for setting price indiciation into one
dib542 Jun 3, 2024
da2067a
fix: limit market order should set limit at current market price
dib542 Jun 3, 2024
b8ac614
fix: prevent slippage tolerance result from looking negative
dib542 Jun 3, 2024
408a2b4
fix: remove connection lines/areas when chart is being removed
dib542 Jun 3, 2024
076a82b
feat: add empty data states for recent orders list
dib542 Jun 3, 2024
4c37a03
feat: make trades list refresh occasionally
dib542 Jun 3, 2024
d9f99ed
feat: abstract useOnDexMsg to add general subscriptions to dex action…
dib542 Jun 3, 2024
46aae64
feat: add click chart to set limit price
dib542 Jun 3, 2024
a9876a5
fix: avoid chart re-rendering with cached chart pairs
dib542 Jun 3, 2024
b612bc8
feat: remove tokenPairs dependency from Orderbook chart
dib542 Jun 3, 2024
20d8198
feat: flip or reset order trade state when changing tokens
dib542 Jun 4, 2024
b5e7868
fix: change all usages of BigNumber .toPrecision method to .precision
dib542 Jun 4, 2024
b8a8f36
fix: account=null and account=undefined react-queries were colliding
dib542 Jun 4, 2024
639d5c3
fix: make zero-result hook behavior more obvious: allow null parameter
dib542 Jun 4, 2024
8e4deac
feat: ensure Expiry shortcuts are open for all limit orders:
dib542 Jun 4, 2024
7693a6a
refactor: allow useTransactionTableData page state to be set by parent
dib542 Jun 4, 2024
1d19005
fix: use consistent end display price of txs calculations
dib542 Jun 4, 2024
4305d0a
fix: use consistent end display price of txs calculations, part 2
dib542 Jun 5, 2024
3aea800
refactor: rename form slippage to include percentage indication
dib542 Jun 5, 2024
85919ac
fix: replace some tickIndex->basePrice with tickIndex->displayPrice
dib542 Jun 5, 2024
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
4 changes: 4 additions & 0 deletions public/tradingview.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:root {
--tv-color-platform-background: var(--page-card, hsl(212deg, 28%, 17%));
--tv-color-pane-background: var(--default, hsl(219deg, 40%, 11%));
}
25 changes: 20 additions & 5 deletions src/components/RadioButtonGroupInput/RadioButtonGroupInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ function useSelectedButtonBackgroundMove<T extends string | number>(
if (movingButton && targetButton) {
movingButton.style.width = `${targetButton.offsetWidth}px`;
movingButton.style.left = `${targetButton.offsetLeft}px`;
if (newValue !== undefined) {
if (newValue !== undefined && movingButton.style.opacity !== '0') {
movingButton.classList.add('transition-ready');
} else {
movingButton?.classList.remove('transition-ready');
}
movingButton.style.opacity = '1';
}
// "remove" button if the target was not found
else if (movingButton) {
movingButton?.classList.remove('transition-ready');
movingButton.style.opacity = '0';
}
},
[value, refsByValue, movingButton]
Expand All @@ -72,7 +78,10 @@ function useSelectedButtonBackgroundMove<T extends string | number>(
interface Props<T extends string | number> {
className?: string;
buttonClassName?: string;
values: { [value in T]: ReactNode } | Map<T, ReactNode> | T[];
values:
| { readonly [value in T]: ReactNode }
| Map<T, ReactNode>
| readonly T[];
value: T;
onChange: (value: T) => void;
}
Expand All @@ -87,21 +96,24 @@ export default function RadioButtonGroupInput<T extends string | number>({
const [movingAssetRef, createRefForValue] =
useSelectedButtonBackgroundMove<T>(value);
const entries = useMemo(() => {
const valueIsNumber = typeof value === 'number';
return Array.isArray(values)
? values.map<[T, string]>((value) => [value, `${value}`])
? values.filter(Boolean).map<[T, string]>((value) => [value, `${value}`])
: values instanceof Map
? Array.from(values.entries())
: (Object.entries(values).map(([value, description]) => [
value,
valueIsNumber ? Number(value) : value,
description,
]) as [T, string][]);
}, [values]);
}, [value, values]);
const selectedIndex = entries.findIndex(
([entryValue]) => entryValue === value
);
const includedIndexes = useMemo(() => {
return (
entries
// do not display falsy description buttons
.filter(([_key, value]) => !!value)
.map((_, index, entries) => {
// cumulate weightings
let result = 0;
Expand Down Expand Up @@ -156,6 +168,9 @@ export default function RadioButtonGroupInput<T extends string | number>({
const currentIndex = includedIndexes.includes(index);
const nextIndex = includedIndexes.includes(index + 1);

// skip those with no description
if (!description) return [];

// include button if required or if excluding it
// will not reduce the number of shown buttons
if (currentIndex || (previousIndex && nextIndex)) {
Expand Down
10 changes: 7 additions & 3 deletions src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function Table<
getRowKey,
context,
rowDescription = 'Data',
messageOnEmptyData,
filtered = false,
}: {
className?: string;
Expand All @@ -27,6 +28,7 @@ export default function Table<
getRowKey?: (row: DataRow) => string | number;
context: Context;
rowDescription?: string;
messageOnEmptyData?: ReactNode;
filtered?: boolean;
}) {
if (columns.length !== headings.length) {
Expand Down Expand Up @@ -85,9 +87,11 @@ export default function Table<
<tr>
<td colSpan={columns.length}>
{data ? (
<>
No {filtered ? 'Matching' : ''} {rowDescription} Found
</>
messageOnEmptyData || (
<>
No {filtered ? 'Matching' : ''} {rowDescription} Found
</>
)
) : (
<>Loading...</>
)}
Expand Down
8 changes: 3 additions & 5 deletions src/components/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import './Tabs.scss';

export interface Tab {
nav: ReactNode;
Tab: React.FunctionComponent;
tab?: ReactNode;
}
export default function Tabs({
tabs,
Expand All @@ -23,7 +23,7 @@ export default function Tabs({
givenTabIndex !== undefined
? [givenTabIndex, givenSetTabIndex]
: [defaultTabIndex, defaultSetTabIndex];
const { Tab } = tabs[tabIndex];
const { tab } = tabs[tabIndex];

return (
<div className={['tabs col gap-4', className].filter(Boolean).join(' ')}>
Expand All @@ -47,9 +47,7 @@ export default function Tabs({
})}
</div>
<div className="tabs__tab flex row">
<div className="flex col">
<Tab />
</div>
<div className="flex col">{tab}</div>
</div>
</div>
);
Expand Down
26 changes: 1 addition & 25 deletions src/components/TokenInputGroup/TokenInputGroup.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
border-radius: 1rem;
border: 1px solid transparent;
background-color: var(--default);
grid-template-columns: auto 1fr;
grid-template-rows: auto 1fr auto;
color: var(--default-alt);
display: grid;
row-gap: 2px;
Expand All @@ -21,7 +19,7 @@
background-color: hsla(0, 73%, 97%, 1);
&,
.token-group-balance button {
border: 1px solid var(--error);
border-color: var(--error);
}
.token-group-title,
.token-picker-toggle,
Expand All @@ -34,41 +32,24 @@
}

.token-picker-toggle {
grid-column: 1;
text-align: start;
grid-row-start: 2;
grid-row-end: 4;
grid-row: 2 / 4;
row-gap: 2px;

.token-chain {
margin-bottom: -2px;
}
}

.token-group-title {
grid-column: 1;
font-size: font-size.$text-m;
text-align: start;
grid-row: 1;
font-weight: normal;
align-self: center;
}

.token-group-balance {
font-size: font-size.$text-m;
grid-column: 2;
grid-row: 1;
text-align: end;
display: flex;
gap: paddings.$p-3;
margin-left: auto;
}

.token-group-value {
font-size: font-size.$text-m;
grid-column: 2;
grid-row: 3;
text-align: end;
}

Expand All @@ -78,12 +59,7 @@
background-color: transparent;
border: 0px none transparent;
font-size: font-size.$input;
color: var(--default-alt);
grid-column: 2;
text-align: end;
grid-row: 2;
outline: none;
margin-top: margins.$m-2;
}

&:disabled {
Expand Down
150 changes: 86 additions & 64 deletions src/components/TokenInputGroup/TokenInputGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useMemo } from 'react';
import React, { ReactNode, useCallback, useMemo, useRef } from 'react';
import BigNumber from 'bignumber.js';

import TokenPicker from '../TokenPicker';
Expand Down Expand Up @@ -29,6 +29,9 @@ interface InputGroupProps {
disabled?: boolean;
disabledInput?: boolean;
disabledToken?: boolean;
inactive?: boolean;
header?: ReactNode;
subHeader?: ReactNode;
maxValue?: number;
}

Expand All @@ -46,9 +49,12 @@ export default function TokenInputGroup({
token,
denom = token?.base,
defaultAssetMode,
header,
subHeader,
disabled = false,
disabledInput = disabled,
disabledToken = disabled,
inactive = false,
maxValue: givenMaxValue,
}: InputGroupProps) {
const onPickerChange = useCallback(
Expand All @@ -72,7 +78,8 @@ export default function TokenInputGroup({
}, [value, price]);

const { data: balance } = useBankBalanceDisplayAmount(denom);
const maxValue = givenMaxValue || balance;
const maxValue = givenMaxValue ?? balance;
const inputRef = useRef<HTMLInputElement>(null);
return (
<div
className={[
Expand All @@ -82,70 +89,85 @@ export default function TokenInputGroup({
]
.filter(Boolean)
.join(' ')}
onClick={() => inputRef.current?.focus()}
onKeyDown={() => undefined}
role="button"
tabIndex={-1}
>
{maxValue && (
<h5 className="token-group-title">
Available{' '}
{formatAmount(maxValue, {
useGrouping: true,
})}
</h5>
)}
{!disabledInput && token && maxValue && Number(maxValue) > 0 && (
<span className="token-group-balance">
<button
type="button"
className="badge badge-light"
onClick={() =>
onValueChanged?.(
// allow max value be as long as it needs to be to perfectly fit user's balance
roundToBaseUnit(token, maxValue) || ''
)
}
<div className="row gap-3">
<div className="col flex my-1">
<h5 className="token-group-title">
{header ||
(maxValue &&
`Available ${formatAmount(maxValue, {
useGrouping: true,
})}`) || <>&nbsp;</>}
</h5>
</div>
{subHeader && (
<div className="col token-group-sub-header my-1">{subHeader}</div>
)}
{!disabledInput && token && !!maxValue && Number(maxValue) > 0 && (
<div className="col token-group-balance gap-3">
<button
type="button"
className="badge badge-light"
onClick={() =>
onValueChanged?.(
// allow max value be as long as it needs to be to perfectly fit user's balance
roundToBaseUnit(token, maxValue) || ''
)
}
>
MAX
</button>
</div>
)}
</div>
<div className="row">
<div className="col flex">
<TokenPicker
className="gutter-l-3"
value={token}
onChange={onTokenChanged ? onPickerChange : undefined}
exclusion={exclusion}
disabled={disabledToken}
defaultAssetMode={defaultAssetMode}
/>
</div>
<div className="col flex mt-sm">
<NumberInput
type="text"
innerRef={inputRef}
className={[
'token-group-input',
!Number(value) && 'input--zero',
inactive && 'text-muted',
]
.filter(Boolean)
.join(' ')}
value={value}
placeholder={placeholder}
onChange={onValueChanged}
onClick={selectAll}
disabled={disabledInput}
style={useMemo(() => {
return {
// set width as minimum amount available
minWidth: '100%',
width: 0,
};
}, [])}
/>
<span
className={['token-group-value', inactive && 'text-muted']
.filter(Boolean)
.join(' ')}
>
MAX
</button>
<button
type="button"
className="badge badge-light"
onClick={() =>
// allow rounding on half of balance because we don't need an exact target
onValueChanged?.(
roundToBaseUnit(token, Number(maxValue) / 2) || ''
)
}
>
HALF
</button>
</span>
)}
<TokenPicker
className="gutter-l-3"
value={token}
onChange={onPickerChange}
exclusion={exclusion}
disabled={disabledToken}
defaultAssetMode={defaultAssetMode}
/>
<NumberInput
type="text"
className={['token-group-input', !Number(value) && 'input--zero']
.filter(Boolean)
.join(' ')}
value={value}
placeholder={placeholder}
onChange={onValueChanged}
onClick={selectAll}
disabled={disabledInput}
style={useMemo(() => {
return {
// set width as minimum amount available
minWidth: '100%',
width: 0,
};
}, [])}
/>
<span className="token-group-value">{secondaryValue}</span>
{secondaryValue}
</span>
</div>
</div>
</div>
);
}
Loading
Loading