vega-frontend-monorepo/libs/accounts/src/lib/accounts-manager.tsx
botond 7613a9fa9f
Feat/migrate libs (#1266)
* feat: add new generator with config

* feat: split off gql queries to separate files for the new generator

* fix: delete dummy schema

* feat: add generated queries with new codegen

* fix: regenerate from scratch and remove duplicates

* fix: libs and app gen folders

* fix: remove more duplicate queries

* fix: add generated files to be ignored by the formatter

* fix: format

* feat: migrate assets to use new code generator

* fix: generated imports

* fix: lint

* fix: accounts export

* fix: more imports

* fix: add type alias for new type system and regenerate

* fix: format

* fix: export

* fix: format

* fix: delete leftover file from merge

* chore: migrate candles-chart lib

* fix: remove redundant asset files

* chore: migrate accounts

* chore: update glob

* chore: add cleanup to fix-imports to support type migrations for now

* fix: imports

* fix: update market types

* fix: type import paths

* fix: format

* fix: import

* fix: types in e2e tests

* fix: format

* fix: lint
2022-09-09 16:14:48 +01:00

73 lines
2.1 KiB
TypeScript

import { useRef, useCallback, useMemo } from 'react';
import { produce } from 'immer';
import merge from 'lodash/merge';
import { AsyncRenderer } from '@vegaprotocol/ui-toolkit';
import { useDataProvider, addSummaryRows } from '@vegaprotocol/react-helpers';
import type { AccountFieldsFragment } from './__generated__/Accounts';
import type { AgGridReact } from 'ag-grid-react';
import {
AccountsTable,
getGroupId,
getGroupSummaryRow,
} from './accounts-table';
import { accountsDataProvider, getId } from './accounts-data-provider';
interface AccountsManagerProps {
partyId: string;
}
export const AccountsManager = ({ partyId }: AccountsManagerProps) => {
const gridRef = useRef<AgGridReact | null>(null);
const variables = useMemo(() => ({ partyId }), [partyId]);
const update = useCallback(
({ delta }: { delta: AccountFieldsFragment }) => {
const update: AccountFieldsFragment[] = [];
const add: AccountFieldsFragment[] = [];
if (!gridRef.current?.api) {
return false;
}
const rowNode = gridRef.current.api.getRowNode(getId(delta));
if (rowNode) {
const updatedData = produce<AccountFieldsFragment>(
rowNode.data,
(draft: AccountFieldsFragment) => {
merge(draft, delta);
}
);
if (updatedData !== rowNode.data) {
update.push(updatedData);
}
} else {
add.push(delta);
}
if (update.length || add.length) {
gridRef.current.api.applyTransactionAsync({
update,
add,
addIndex: 0,
});
}
if (add.length) {
addSummaryRows(
gridRef.current.api,
gridRef.current.columnApi,
getGroupId,
getGroupSummaryRow
);
}
return true;
},
[gridRef]
);
const { data, error, loading } = useDataProvider<
AccountFieldsFragment[],
AccountFieldsFragment
>({ dataProvider: accountsDataProvider, update, variables });
return (
<AsyncRenderer loading={loading} error={error} data={data}>
<AccountsTable ref={gridRef} data={data} />
</AsyncRenderer>
);
};