|
1 | | -import { ReactNode, useCallback, useMemo } from 'react' |
| 1 | +import { ReactNode, useMemo } from 'react' |
2 | 2 |
|
3 | | -import { shortenOrderId } from '@cowprotocol/common-utils' |
4 | | -import { EnrichedOrder, SupportedChainId, getChainInfo } from '@cowprotocol/cow-sdk' |
5 | | -import { ToastMessageType } from '@cowprotocol/events' |
6 | | -import { useTokensByAddressMap } from '@cowprotocol/tokens' |
7 | | -import { TokenInfo, UiOrderType } from '@cowprotocol/types' |
| 3 | +import { getIsNativeToken } from '@cowprotocol/common-utils' |
| 4 | +import { SupportedChainId, getChainInfo, ChainInfo } from '@cowprotocol/cow-sdk' |
8 | 5 |
|
9 | | -import { Trans } from '@lingui/react/macro' |
10 | 6 | import { useBridgeSupportedNetwork } from 'entities/bridgeProvider' |
11 | 7 |
|
12 | | -import { useOrder } from 'legacy/state/orders/hooks' |
| 8 | +import { useUltimateOrder } from 'common/hooks/useUltimateOrder' |
| 9 | +import { getUltimateOrderTradeAmounts } from 'common/updaters/orders/utils' |
| 10 | +import { getUiOrderType } from 'utils/orderUtils/getUiOrderType' |
13 | 11 |
|
14 | | -import { OrderSummary } from 'common/pure/OrderSummary' |
15 | | -import { SellForAtLeastTemplate } from 'common/pure/OrderSummary/summaryTemplates' |
| 12 | +import { OrderNotificationContent, OrderNotificationContentProps } from '../../pure/OrderNotificationContent' |
| 13 | +import { OrderNotificationInfo } from '../../types' |
16 | 14 |
|
17 | | -import { |
18 | | - getToastMessageCallback, |
19 | | - isEnrichedOrder, |
20 | | - mapEnrichedOrderToInfo, |
21 | | - mapStoreOrderToInfo, |
22 | | - OrderInfo, |
23 | | -} from './utils' |
24 | | - |
25 | | -import { ReceiverInfo } from '../../pure/ReceiverInfo' |
26 | | -import { TransactionContentWithLink } from '../TransactionContentWithLink' |
27 | | - |
28 | | -export interface BaseOrderNotificationProps { |
29 | | - title: ReactNode |
30 | | - messageType: ToastMessageType |
| 15 | +interface BaseOrderNotificationProps extends Omit<OrderNotificationContentProps, 'orderInfo'> { |
31 | 16 | chainId: SupportedChainId |
32 | 17 | orderUid: string |
33 | | - orderType: UiOrderType |
34 | | - actionTitle?: string |
35 | | - orderInfo?: OrderInfo | EnrichedOrder |
36 | | - transactionHash?: string |
37 | | - skipExplorerLink?: boolean |
38 | | - isEthFlow?: boolean |
39 | | - children?: ReactNode |
40 | | - bottomContent?: ReactNode |
41 | | - hideReceiver?: boolean |
42 | | - receiver?: string |
43 | | - customTemplate?: typeof SellForAtLeastTemplate |
| 18 | + orderInfo?: OrderNotificationInfo |
44 | 19 | } |
45 | 20 |
|
46 | 21 | export function OrderNotification(props: BaseOrderNotificationProps): ReactNode { |
47 | | - const { |
48 | | - title, |
49 | | - actionTitle, |
50 | | - orderUid, |
51 | | - orderType, |
52 | | - transactionHash, |
53 | | - chainId, |
54 | | - messageType, |
55 | | - children, |
56 | | - orderInfo, |
57 | | - isEthFlow, |
58 | | - skipExplorerLink, |
59 | | - hideReceiver, |
60 | | - receiver, |
61 | | - } = props |
62 | | - const allTokens = useTokensByAddressMap() |
63 | | - |
64 | | - const orderFromStore = useOrder({ chainId, id: orderInfo ? undefined : orderUid }) |
65 | | - |
66 | | - const order = useMemo(() => { |
67 | | - if (orderInfo) { |
68 | | - return isEnrichedOrder(orderInfo) ? mapEnrichedOrderToInfo(orderInfo, allTokens) : orderInfo |
| 22 | + const { orderUid, chainId, orderInfo: _orderInfo, ...rest } = props |
| 23 | + const ultimateOrder = useUltimateOrder(chainId, orderUid) |
| 24 | + |
| 25 | + const orderInfo: OrderNotificationInfo | undefined = useMemo(() => { |
| 26 | + if (ultimateOrder) { |
| 27 | + const { id, kind, owner, receiver } = ultimateOrder.orderFromStore |
| 28 | + |
| 29 | + return { |
| 30 | + ...getUltimateOrderTradeAmounts(ultimateOrder), |
| 31 | + orderUid: id, |
| 32 | + kind: kind, |
| 33 | + owner: owner, |
| 34 | + orderType: getUiOrderType(ultimateOrder.orderFromStore), |
| 35 | + isEthFlowOrder: getIsNativeToken(ultimateOrder.orderFromStore.inputToken), |
| 36 | + receiver: ultimateOrder.bridgeOrderFromStore?.recipient ?? receiver, |
| 37 | + } |
69 | 38 | } |
70 | 39 |
|
71 | | - return orderFromStore ? mapStoreOrderToInfo(orderFromStore) : undefined |
72 | | - }, [orderFromStore, orderInfo, allTokens]) |
73 | | - |
74 | | - const sourceChainId = order?.inputToken.chainId |
75 | | - const srcChainData = sourceChainId ? getChainInfo(sourceChainId) : undefined |
76 | | - const dstChainData = useBridgeSupportedNetwork(order?.outputToken.chainId) |
| 40 | + if (_orderInfo) return _orderInfo |
77 | 41 |
|
78 | | - const onToastMessage = useMemo( |
79 | | - () => |
80 | | - getToastMessageCallback(messageType, { |
81 | | - orderUid, |
82 | | - orderType, |
83 | | - }), |
84 | | - [messageType, orderType, orderUid], |
85 | | - ) |
| 42 | + return undefined |
| 43 | + }, [ultimateOrder, _orderInfo]) |
86 | 44 |
|
87 | | - const ref = useCallback( |
88 | | - (node: HTMLDivElement) => { |
89 | | - if (node) onToastMessage(node.innerText) |
90 | | - }, |
91 | | - [onToastMessage], |
| 45 | + const { srcChainData, dstChainData } = useTradeChainsInfo( |
| 46 | + orderInfo?.inputAmount.currency.chainId, |
| 47 | + orderInfo?.outputAmount.currency.chainId, |
92 | 48 | ) |
93 | 49 |
|
94 | | - if (!order) return |
| 50 | + if (!orderInfo) return |
95 | 51 |
|
96 | | - const content = ( |
97 | | - <div ref={ref}> |
98 | | - <strong>{title}</strong> |
99 | | - <br /> |
100 | | - <p> |
101 | | - <Trans>Order</Trans> <strong>{shortenOrderId(orderUid)}</strong>: |
102 | | - </p> |
103 | | - {children || |
104 | | - (order.inputToken && order.outputToken ? ( |
105 | | - <OrderSummary |
106 | | - actionTitle={actionTitle} |
107 | | - customTemplate={props.customTemplate} |
108 | | - kind={order.kind} |
109 | | - inputToken={order.inputToken as TokenInfo} |
110 | | - outputToken={order.outputToken as TokenInfo} |
111 | | - sellAmount={order.inputAmount.toString()} |
112 | | - buyAmount={order.outputAmount.toString()} |
113 | | - srcChainData={srcChainData} |
114 | | - dstChainData={dstChainData} |
115 | | - /> |
116 | | - ) : null)} |
117 | | - {!hideReceiver && <ReceiverInfo receiver={receiver ?? order.receiver} owner={order.owner} />} |
118 | | - {props.bottomContent} |
119 | | - </div> |
| 52 | + return ( |
| 53 | + <OrderNotificationContent {...rest} orderInfo={orderInfo} srcChainData={srcChainData} dstChainData={dstChainData} /> |
120 | 54 | ) |
| 55 | +} |
121 | 56 |
|
122 | | - if (skipExplorerLink) { |
123 | | - return content |
124 | | - } |
| 57 | +function useTradeChainsInfo( |
| 58 | + sourceChainId: SupportedChainId | undefined, |
| 59 | + destChainId: SupportedChainId | undefined, |
| 60 | +): { srcChainData: ChainInfo | undefined; dstChainData: ChainInfo | undefined } { |
| 61 | + const srcChainData = sourceChainId ? getChainInfo(sourceChainId) : undefined |
| 62 | + const dstChainData = useBridgeSupportedNetwork(destChainId) |
125 | 63 |
|
126 | | - return ( |
127 | | - <TransactionContentWithLink isEthFlow={isEthFlow} transactionHash={transactionHash} orderUid={orderUid}> |
128 | | - {content} |
129 | | - </TransactionContentWithLink> |
130 | | - ) |
| 64 | + return { srcChainData, dstChainData } |
131 | 65 | } |
0 commit comments