vega-frontend-monorepo/libs/positions/src/lib/positions-table.tsx

147 lines
4.0 KiB
TypeScript
Raw Normal View History

2022-04-01 13:43:40 +00:00
import { forwardRef } from 'react';
2022-03-25 12:32:14 +00:00
import type { ValueFormatterParams } from 'ag-grid-community';
import {
PriceFlashCell,
addDecimalsFormatNumber,
2022-03-25 12:32:14 +00:00
volumePrefix,
addDecimal,
t,
2022-03-25 12:32:14 +00:00
} from '@vegaprotocol/react-helpers';
import { AgGridDynamic as AgGrid } from '@vegaprotocol/ui-toolkit';
import { AgGridColumn } from 'ag-grid-react';
import type { AgGridReact } from 'ag-grid-react';
import type { Positions_party_positions } from './__generated__/Positions';
2022-03-29 23:03:27 +00:00
import { MarketTradingMode } from '@vegaprotocol/types';
2022-03-25 12:32:14 +00:00
interface PositionsTableProps {
2022-03-29 17:31:00 +00:00
data: Positions_party_positions[] | null;
2022-03-25 12:32:14 +00:00
}
export const getRowId = ({ data }: { data: Positions_party_positions }) =>
2022-03-25 12:32:14 +00:00
data.market.id;
const alphanumericComparator = (a: string, b: string, isInverted: boolean) => {
if (a < b) {
return isInverted ? 1 : -1;
2022-03-25 12:32:14 +00:00
}
if (a > b) {
return isInverted ? -1 : 1;
2022-03-25 12:32:14 +00:00
}
return 0;
};
const comparator = (
valueA: string,
valueB: string,
nodeA: { data: Positions_party_positions },
nodeB: { data: Positions_party_positions },
isInverted: boolean
) =>
alphanumericComparator(
nodeA.data.market.tradableInstrument.instrument.name,
nodeB.data.market.tradableInstrument.instrument.name,
isInverted
) ||
alphanumericComparator(
nodeA.data.market.id,
nodeB.data.market.id,
isInverted
);
interface PositionsTableValueFormatterParams extends ValueFormatterParams {
data: Positions_party_positions;
}
2022-03-25 12:32:14 +00:00
export const PositionsTable = forwardRef<AgGridReact, PositionsTableProps>(
({ data }, ref) => {
2022-03-25 12:32:14 +00:00
return (
<AgGrid
style={{ width: '100%', height: '100%' }}
overlayNoRowsTemplate="No positions"
rowData={data}
getRowId={getRowId}
2022-03-25 12:32:14 +00:00
ref={ref}
defaultColDef={{
flex: 1,
resizable: true,
}}
onGridReady={(event) => {
event.columnApi.applyColumnState({
state: [
{
colId: 'market.tradableInstrument.instrument.code',
sort: 'asc',
},
],
});
}}
components={{ PriceFlashCell }}
2022-03-25 12:32:14 +00:00
>
<AgGridColumn
headerName={t('Market')}
field="market.tradableInstrument.instrument.code"
comparator={comparator}
sortable
2022-03-25 12:32:14 +00:00
/>
<AgGridColumn
headerName={t('Amount')}
2022-03-25 12:32:14 +00:00
field="openVolume"
valueFormatter={({ value }: PositionsTableValueFormatterParams) =>
2022-03-25 12:32:14 +00:00
volumePrefix(value)
}
/>
<AgGridColumn
headerName={t('Average Entry Price')}
2022-03-25 12:32:14 +00:00
field="averageEntryPrice"
cellRenderer="PriceFlashCell"
valueFormatter={({
value,
data,
}: PositionsTableValueFormatterParams) =>
addDecimalsFormatNumber(value, data.market.decimalPlaces)
2022-03-25 12:32:14 +00:00
}
/>
<AgGridColumn
headerName={t('Mark Price')}
2022-03-25 12:32:14 +00:00
field="market.data.markPrice"
type="rightAligned"
cellRenderer="PriceFlashCell"
valueFormatter={({
value,
data,
}: PositionsTableValueFormatterParams) => {
if (
data.market.data?.marketTradingMode ===
MarketTradingMode.OpeningAuction
) {
return '-';
}
return addDecimal(value, data.market.decimalPlaces);
}}
2022-03-25 12:32:14 +00:00
/>
<AgGridColumn
headerName={t('Realised PNL')}
2022-03-25 12:32:14 +00:00
field="realisedPNL"
type="rightAligned"
cellClassRules={{
'color-vega-green': ({ value }: { value: string }) =>
Number(value) > 0,
'color-vega-red': ({ value }: { value: string }) =>
Number(value) < 0,
}}
feat: trading page market summary & select markets modal opening from market title & fix: positions table realised PnL (#505) * feat: [#456] select markets modal opening from market title * feat: add a global zustand store for managing connect dialogs and landing dialog * feat: add tests * feat: [#456] make arrow configurable * feat: [#456] make arrow configurable * feat: [#456] trading tab active only on portfolio * chore: update tranches Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> * fix: [#445] shallow routing from index (#484) * fix: [#445] shallow routing from index * fix: [#445] use link to redirect to market - an attempt to fix reload * fix: [#445] remove stretched link from last link - it makes all the other links unusable * fix: [#445] fix lint on select market list - remove stretched link * fix: [#456] put everything in landing folder to avoid conflicts * fix: remove condition for cypress for auto connecting * feat: [#456] add global store and fix href routing * feat: [#456] add global store and fix href routing * feat: [#456] add one more test * feat: [#154] pull market data summary * feat: [#154] move header above the trade grid child sections * feat: [#154] flex oerflow and styling updates for market summary * feat: [#154] fix styling * fix: [154] fix cyp tests and styling * fix: [#154] fix markets navigation cypress step * fix: [#154] fix for navigate to markets link * fix: failing tests from market change * fix: [#154] set nav items based on market id and show last viewed market on landing * fix: [#412] invalid decimal place on realised PnL field * fix: [#154] remove redundant curly braces * fix: [#154] show hyphen on volume if market data is undefined Co-authored-by: Matthew Russell <mattrussell36@gmail.com> Co-authored-by: dexturr <dexturr@users.noreply.github.com> Co-authored-by: Joe <joe@vega.xyz>
2022-06-06 16:19:56 +00:00
valueFormatter={({ value, data }: ValueFormatterParams) =>
volumePrefix(
addDecimalsFormatNumber(value, data.market.decimalPlaces, 3)
)
}
cellRenderer="PriceFlashCell"
2022-03-25 12:32:14 +00:00
/>
</AgGrid>
);
}
);
export default PositionsTable;