-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathWalletConnectCreateOfferPreview.tsx
58 lines (50 loc) · 1.97 KB
/
WalletConnectCreateOfferPreview.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { Button, Flex, Loading, useOpenDialog, chiaToMojo } from '@chia-network/core';
import { Trans } from '@lingui/macro';
import React, { useMemo } from 'react';
import OfferBuilderData from '../../@types/OfferBuilderData';
import useAssetIdName from '../../hooks/useAssetIdName';
import createOfferForIdsToOfferBuilderData from '../../util/createOfferForIdsToOfferBuilderData';
import parseFee from '../../util/parseFee';
import OfferBuilderViewerDialog from '../offers2/OfferBuilderViewerDialog';
export type WalletConnectOfferPreviewProps = {
value: Record<string, number>;
values: Record<string, any>;
onChange: (values: Record<string, any>) => void;
};
export default function WalletConnectCreateOfferPreview(props: WalletConnectOfferPreviewProps) {
const { value, values, onChange } = props;
const { lookupByWalletId, isLoading: isLoadingAssetIdNames } = useAssetIdName();
const openDialog = useOpenDialog();
const fee = parseFee(values.fee);
const offerBuilderData: OfferBuilderData | undefined = useMemo(() => {
if (isLoadingAssetIdNames) {
return undefined;
}
return createOfferForIdsToOfferBuilderData(value, lookupByWalletId, fee);
}, [value, lookupByWalletId, isLoadingAssetIdNames, fee]);
async function handleShowPreview() {
const offerBuilderDataResult = await openDialog(<OfferBuilderViewerDialog offerBuilderData={offerBuilderData} />);
if (offerBuilderDataResult) {
// use new fee value
const feeChia = offerBuilderDataResult.offered.fee?.[0]?.amount;
const feeMojos = feeChia ? chiaToMojo(feeChia).toFixed() : undefined;
onChange({
...values,
fee: feeMojos,
});
}
}
return (
<Flex flexDirection="column" gap={2}>
{isLoadingAssetIdNames ? (
<Loading />
) : (
<Flex>
<Button variant="outlined" color="secondary" onClick={handleShowPreview}>
<Trans>Show Offer Details</Trans>
</Button>
</Flex>
)}
</Flex>
);
}