d0976bbd46
* feat: use close position hook and dialog setup * chore: update wallet tx interface for batch market instruction * feat: add usage of data provider to show relevant order information * feat: render correctly formatted values in close position dialog * feat: make vega tx dialog more flexibly by allowing custom ui for every state of the tx * feat: adjust text alignment and spacing between active orders and order to close * feat: add unit tests * chore: remove stray log * chore: fix lint * chore: ignore ts error for formatter function of vesting chart * feat: split components up, memozie variables * feat: add shared loading state to prevent content popping in * feat: add time in force label * feat: move transaction result hook to wallet lib * feat: prevent being able to close vega tx dialog, must reject tx * chore: add test for useTransactionResult hook * chore: fix positiosn test after hook relocation * Revert "feat: prevent being able to close vega tx dialog, must reject tx" This reverts commit d1ecda69c3c55822bb042320f82b2e1c3833b99a. * chore: add check for order edge to be defined * chore: remove close callback * feat: use tx result state to determine dialog state * chore: update close position hook to check for transaction result * fix: readd types tif selection persistance * feat: convert order event func to be async, use it in close position for more result context * fix: rename utils * chore: adjust error language Co-authored-by: Madalina Raicu <madalina@raygroup.uk>
125 lines
3.0 KiB
TypeScript
125 lines
3.0 KiB
TypeScript
import type { ReactNode } from 'react';
|
|
import { useCallback, useMemo, useState } from 'react';
|
|
import { useVegaWallet } from './use-vega-wallet';
|
|
import type { VegaTransactionContentMap } from './vega-transaction-dialog';
|
|
import { VegaTransactionDialog } from './vega-transaction-dialog';
|
|
import type { Intent } from '@vegaprotocol/ui-toolkit';
|
|
import type { Transaction } from './connectors';
|
|
import { ClientErrors } from './connectors';
|
|
import { WalletError } from './connectors';
|
|
|
|
export interface DialogProps {
|
|
intent?: Intent;
|
|
title?: string;
|
|
icon?: ReactNode;
|
|
content?: VegaTransactionContentMap;
|
|
}
|
|
|
|
export enum VegaTxStatus {
|
|
Default = 'Default',
|
|
Requested = 'Requested',
|
|
Pending = 'Pending',
|
|
Error = 'Error',
|
|
Complete = 'Complete',
|
|
}
|
|
|
|
export interface VegaTxState {
|
|
status: VegaTxStatus;
|
|
error: WalletError | null;
|
|
txHash: string | null;
|
|
signature: string | null;
|
|
dialogOpen: boolean;
|
|
}
|
|
|
|
export const initialState = {
|
|
status: VegaTxStatus.Default,
|
|
error: null,
|
|
txHash: null,
|
|
signature: null,
|
|
dialogOpen: false,
|
|
};
|
|
|
|
export const useVegaTransaction = () => {
|
|
const { sendTx } = useVegaWallet();
|
|
const [transaction, _setTransaction] = useState<VegaTxState>(initialState);
|
|
|
|
const setTransaction = useCallback((update: Partial<VegaTxState>) => {
|
|
_setTransaction((curr) => ({
|
|
...curr,
|
|
...update,
|
|
}));
|
|
}, []);
|
|
|
|
const reset = useCallback(() => {
|
|
setTransaction(initialState);
|
|
}, [setTransaction]);
|
|
|
|
const setComplete = useCallback(() => {
|
|
setTransaction({ status: VegaTxStatus.Complete });
|
|
}, [setTransaction]);
|
|
|
|
const send = useCallback(
|
|
async (pubKey: string, tx: Transaction) => {
|
|
try {
|
|
setTransaction({
|
|
error: null,
|
|
txHash: null,
|
|
signature: null,
|
|
status: VegaTxStatus.Requested,
|
|
dialogOpen: true,
|
|
});
|
|
|
|
const res = await sendTx(pubKey, tx);
|
|
|
|
if (res === null) {
|
|
// User rejected
|
|
reset();
|
|
return null;
|
|
}
|
|
|
|
if (res.signature && res.transactionHash) {
|
|
setTransaction({
|
|
status: VegaTxStatus.Pending,
|
|
txHash: res.transactionHash,
|
|
signature: res.signature,
|
|
});
|
|
|
|
return res;
|
|
}
|
|
|
|
return null;
|
|
} catch (err) {
|
|
setTransaction({
|
|
error: err instanceof WalletError ? err : ClientErrors.UNKNOWN,
|
|
status: VegaTxStatus.Error,
|
|
});
|
|
return null;
|
|
}
|
|
},
|
|
[sendTx, setTransaction, reset]
|
|
);
|
|
|
|
const Dialog = useMemo(() => {
|
|
return (props: DialogProps) => (
|
|
<VegaTransactionDialog
|
|
{...props}
|
|
isOpen={transaction.dialogOpen}
|
|
onChange={(isOpen) => {
|
|
if (!isOpen) reset();
|
|
setTransaction({ dialogOpen: isOpen });
|
|
}}
|
|
transaction={transaction}
|
|
/>
|
|
);
|
|
}, [transaction, setTransaction, reset]);
|
|
|
|
return {
|
|
send,
|
|
transaction,
|
|
reset,
|
|
setComplete,
|
|
setTransaction,
|
|
Dialog,
|
|
};
|
|
};
|