@kodiak-finance/orderly-ui-tpsl 2.8.18 → 2.8.19-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,16 +1,3570 @@
1
- import { utils, useSymbolsInfo, usePositionStream, useLocalStorage, useTPSLOrder, useMemoizedFn, useOrderEntry, useOrderStream, findPositionTPSLFromOrders, useMutation, useMarkPrice, useIndexPrice, useLeverageBySymbol, findTPSLFromOrder, findTPSLOrderPriceFromOrder } from '@kodiak-finance/orderly-hooks';
1
+ import { utils, useSymbolsInfo, usePositionStream, useLocalStorage, useEstLiqPriceBySymbol, useTPSLOrder, useTpslPriceChecker, ERROR_MSG_CODES, useMemoizedFn, useOrderEntry, useOrderStream, findPositionTPSLFromOrders, useMutation, useMarkPrice, useIndexPrice, useLeverageBySymbol, useMaxQty, findTPSLFromOrder, findTPSLOrderPriceFromOrder } from '@kodiak-finance/orderly-hooks';
2
2
  import { useTranslation, i18n, Trans } from '@kodiak-finance/orderly-i18n';
3
3
  import { SDKError, AlgoOrderRootType, PositionType, OrderType, OrderSide, OrderStatus, AlgoOrderType } from '@kodiak-finance/orderly-types';
4
- import { Flex, Input, inputFormatter, cn, Slider, Text, registerSimpleSheet, registerSimpleDialog, useScreen, ScrollArea, Grid, Button, ThrottledButton, Divider, Box, toast, TokenIcon, modal, Tooltip, ExclamationFillIcon, Select, Checkbox, ChevronDownIcon, Badge, textVariants, SimpleDropdownMenu, CaretDownIcon, useModal } from '@kodiak-finance/orderly-ui';
4
+ import { Flex, Input, inputFormatter, cn, Slider, Text, registerSimpleSheet, registerSimpleDialog, useScreen, ScrollArea, DotStatus, Grid, Button, ThrottledButton, Divider, Box, toast, TokenIcon, modal, Tooltip, ExclamationFillIcon, Select, ChevronDownIcon, Badge, textVariants, Checkbox, SimpleDropdownMenu, CaretDownIcon, useModal } from '@kodiak-finance/orderly-ui';
5
+ import { memo, useRef, useState, useEffect, createContext, useMemo, useContext, Fragment as Fragment$1 } from 'react';
5
6
  import { useOrderEntryFormErrorMsg } from '@kodiak-finance/orderly-react-app';
6
7
  import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
7
- import { memo, useRef, useState, useEffect, createContext, useMemo, Fragment as Fragment$1, useContext } from 'react';
8
- import { Decimal, checkIsNaN, getTPSLDirection, transSymbolformString, todpIfNeed } from '@kodiak-finance/orderly-utils';
8
+ import { Decimal, checkIsNaN, transSymbolformString, getTPSLDirection, formatNum, todpIfNeed } from '@kodiak-finance/orderly-utils';
9
+ import { order, positions } from '@kodiak-finance/orderly-perp';
9
10
  import { AuthGuardDataTable } from '@kodiak-finance/orderly-ui-connector';
10
- import { positions } from '@kodiak-finance/orderly-perp';
11
11
 
12
- var H=e=>{let{t}=useTranslation(),{order:o,symbolLeverage:r}=e,{symbol:s}=o,i=useMarkPrice(s),n=useIndexPrice(s),a=useLeverageBySymbol(r?void 0:s),l=r||a;return jsxs(Flex,{direction:"column",itemAlign:"start",className:cn("oui-w-full oui-gap-3 oui-font-semibold ",e.classNames?.root),children:[jsxs(Flex,{itemAlign:"center",className:cn("oui-gap-2 ",e.classNames?.symbol),children:[jsxs(Flex,{gap:1,itemAlign:"center",children:[jsx(TokenIcon,{symbol:s,className:"oui-size-5"}),jsx(Text.formatted,{className:"oui-whitespace-nowrap oui-break-normal",rule:"symbol",formatString:"base-type",size:"sm",weight:"semibold",intensity:98,children:s})]}),jsxs(Text,{size:"2xs",className:"oui-h-[18px] oui-rounded oui-bg-base-7 oui-px-2 oui-font-semibold oui-text-base-contrast-36",children:[l,"x"]})]}),jsxs(Grid,{cols:2,gapX:2,gapY:1,className:cn("oui-w-full oui-gap-x-2 ",e.classNames?.container),children:[jsxs(Flex,{justify:"between",className:" oui-text-base-contrast-36",children:[jsx(Text,{size:"2xs",children:t("tpsl.advanced.quantity")}),jsx(Text.numeral,{rule:"price",className:"oui-text-base-contrast-80",size:"2xs",dp:e.baseDP??2,children:Number(o.order_quantity)})]}),jsxs(Flex,{justify:"between",className:"oui-text-base-contrast-36",children:[jsx(Text,{size:"2xs",children:t("tpsl.advanced.lastPrice")}),jsx(Text.numeral,{rule:"price",className:"oui-text-base-contrast-80",size:"2xs",dp:e.quoteDP??2,children:n?.data})]}),jsxs(Flex,{justify:"between",className:"oui-text-base-contrast-36",children:[jsx(Text,{size:"2xs",children:t("tpsl.advanced.orderPrice")}),jsx(Text.numeral,{rule:"price",className:"oui-text-base-contrast-80",size:"2xs",dp:e.quoteDP??2,children:Number(o.order_price)})]}),jsxs(Flex,{justify:"between",className:"oui-text-base-contrast-36",children:[jsx(Text,{size:"2xs",children:t("common.markPrice")}),jsx(Text.numeral,{rule:"price",className:"oui-text-base-contrast-80",size:"2xs",dp:e.quoteDP??2,children:i?.data})]})]})]})};var pe=e=>{let{t}=useTranslation(),{tp_pnl:o,sl_pnl:r}=e,s=useMemo(()=>{let i=jsx(Text,{className:"oui-text-base-contrast-36",children:"-- x"});if(o===void 0||r===void 0||checkIsNaN(o)||checkIsNaN(r))return i;let n=new Decimal(o),a=new Decimal(r);if(a.isZero()||n.isZero())return i;let l=n.div(a).abs().toNumber().toFixed(2);return jsxs(Flex,{gap:1,itemAlign:"center",className:"oui-text-base-contrast-80",children:[jsx(Text,{children:l}),jsx(Text,{className:"oui-text-base-contrast-36",children:"x"})]})},[o,r]);return jsxs(Flex,{direction:"column",itemAlign:"start",className:cn("oui-w-full oui-gap-1 oui-text-2xs oui-text-base-contrast-36",e.className),children:[jsxs(Flex,{justify:"between",className:"oui-w-full",children:[jsx(Text,{size:"2xs",children:t("tpsl.totalEstTpPnl")}),jsx(Text.numeral,{suffix:jsx(Text,{className:"oui-text-base-contrast-36 oui-ml-1",children:"USDC"}),rule:"price",coloring:true,visible:true,size:"2xs",dp:2,children:o?Number(o):"--"})]}),jsxs(Flex,{justify:"between",className:"oui-w-full",children:[jsx(Text,{size:"2xs",children:t("tpsl.totalEstSlPnl")}),r?jsx(Text.numeral,{suffix:jsx(Text,{className:"oui-text-base-contrast-36 oui-ml-1",children:"USDC"}),coloring:true,visible:true,size:"2xs",dp:2,children:Number(r)}):jsx(Text,{size:"2xs",children:"-- USDC"})]}),jsxs(Flex,{justify:"between",className:"oui-w-full",children:[jsx(Text,{size:"2xs",children:t("tpsl.riskRewardRatio")}),jsx(Text,{className:"oui-text-base-contrast-80",size:"2xs",children:s})]})]})};var it=e=>({disableOrderTypeSelector:e.disableOrderTypeSelector,values:e.values,onChange:e.onChange,type:e.type,quote_dp:e.quote_dp,positionType:e.positionType,errors:e.errors,hideOrderPrice:e.hideOrderPrice,rootOrderPrice:e.rootOrderPrice,symbol:e.symbol,disableEnableCheckbox:e.disableEnableCheckbox,symbolLeverage:e.symbolLeverage,side:e.side});var kt=e=>{let{type:t,values:o}=e,[r,s]=useLocalStorage("TP/SL_Mode","Offset%"),[i,n]=useState(true),{t:a}=useTranslation(),l=useMemo(()=>{switch(r){case "Offset":return `${t.toLowerCase()}_offset`;case "Offset%":return `${t.toLowerCase()}_offset_percentage`;default:return `${t.toLowerCase()}_pnl`}},[r]),m=useMemo(()=>o[r],[o,r]),f=useMemo(()=>[{label:a("tpsl.pnl"),value:"PnL",testId:"PnL_menu_item"},{label:a("tpsl.offset"),value:"Offset",testId:"Offset_mneu_item"},{label:`${a("tpsl.offset")}%`,value:"Offset%",testId:"Offset%_menu_item"}],[a]),u=useRef(""),d=p=>{e.onChange(l,p);},g=p=>{let{dp:y=2}=p;return {onRenderBefore:(c,P)=>(c=`${c}`,c===""||c==="-"?"":r==="Offset%"?`${new Decimal(c.replace(new RegExp(u.current.replace(".","\\.")+"$"),"")).mul(100).todp(2,4).toString()}${u.current}`:(r==="Offset"&&(c=todpIfNeed(c,y)),`${c}`)),onSendBefore:c=>{if(/^\-?0{2,}$/.test(c))return "0";if(r==="Offset%"){if(c!==""){c=todpIfNeed(c,2);let P=c.match(/\.0{0,2}$/);P?u.current=P[0]:u.current="",c=new Decimal(c).div(100).toString(),c=`${c}${u.current}`;}}else r==="PnL"&&t==="SL"&&i||(c=todpIfNeed(c,y));return c===""||c==="-"?"":c}}};return {mode:r,modes:f,type:e.type,formatter:g,onModeChange:p=>{s(p);},value:m,pnl:o.PnL,onValueChange:d,quote_dp:e.quote_dp,setFocus:n}};var zt=e=>{let{mode:t,modes:o,onModeChange:r,onValueChange:s,quote:i,quote_dp:n,value:a,pnl:l}=e,{t:m}=useTranslation(),[f,u]=useState(t),[d,g]=useState(t==="Offset%"?"%":i),p=useMemo(()=>{let y=Number(l);if(isNaN(y)||y===0)return "";if(y>0)return "oui-text-trade-profit";if(y<0)return "oui-text-trade-loss"},[l]);return useEffect(()=>{let y=o.find(c=>c.value===t)?.label;u(y),g(t==="Offset%"?"%":i);},[t,o]),jsx(Input,{prefix:f,size:{initial:"lg",lg:"md"},placeholder:d,align:"right",value:a,"data-testid":e.testId,autoComplete:"off",onValueChange:s,formatters:[e.formatter({dp:n,mode:t}),inputFormatter.currencyFormatter,inputFormatter.decimalPointFormatter],classNames:{input:cn("oui-text-2xs",p),prefix:"oui-text-base-contrast-54 oui-text-2xs",root:"oui-outline-line-12 focus-within:oui-outline-primary-light"},onFocus:()=>{g(""),e.setFocus(true);},onBlur:()=>{g(t==="Offset%"?"%":i),e.setFocus(false);},suffix:jsxs(Fragment,{children:[t==="Offset%"&&!!a&&jsx(Text,{size:"2xs",color:"inherit",className:cn("oui-ml-[2px]",p),children:"%"}),jsx(Fo,{mode:t,modes:o,onModeChange:y=>r(y.value)})]})})},Fo=e=>jsx(SimpleDropdownMenu,{currentValue:e.mode,menu:e.modes,align:"end",size:"xs",className:"oui-min-w-[80px]",onSelect:t=>e.onModeChange(t),children:jsx("button",{className:"oui-p-2",children:jsx(CaretDownIcon,{size:12,color:"white"})})});var Qt=e=>{let{testId:t,quote:o,...r}=e,s=kt(r);return jsx(zt,{...s,testId:t,quote:o})};var Kt=e=>{let{t}=useTranslation(),o=[{label:t("orderEntry.orderType.limitOrder"),value:OrderType.LIMIT},{label:t("orderEntry.orderType.marketOrder"),value:OrderType.MARKET}];return jsx(Select.options,{value:e.type,options:o,disabled:e.disabled,onValueChange:e.onChange,size:"xs",classNames:{trigger:"oui-bg-transparent oui-w-auto oui-outline-line-1 oui-input-root oui-bg-base-6 oui-h-10 lg:oui-h-8 oui-outline-line-12 "},valueFormatter:(r,s)=>{let i={[OrderType.LIMIT]:t("orderEntry.orderType.limit"),[OrderType.MARKET]:t("common.marketPrice")}[r];return jsx(Text,{size:"2xs",children:i})}})};var lt=e=>{let[t,o]=useState("USDC"),{t:r}=useTranslation();return jsx(Input.tooltip,{"data-testid":`oui-testid-tpsl-popUp-${e.type.toLowerCase()}-input`,prefix:e.label??r("common.markPrice"),size:{initial:"lg",lg:"md"},tooltip:e.error,placeholder:t,disabled:e.disabled,align:"right",autoComplete:"off",inputMode:"decimal",value:e.value,color:e.error?"danger":void 0,classNames:{input:"oui-text-2xs placeholder:oui-text-2xs",prefix:"oui-text-base-contrast-54 oui-text-2xs",root:"oui-w-full"},onValueChange:e.onValueChange,onFocus:()=>{o("");},onBlur:()=>{o("USDC");},formatters:[inputFormatter.numberFormatter,inputFormatter.dpFormatter(e.quote_dp),inputFormatter.currencyFormatter,inputFormatter.decimalPointFormatter]})};var at=e=>{let{t}=useTranslation(),{getErrorMsg:o}=useOrderEntryFormErrorMsg(e.errors),{values:r,positionType:s}=e,i=useLeverageBySymbol(e.symbolLeverage?void 0:e.symbol),n=e.symbolLeverage||i,a=useMemo(()=>{if(!n||isNaN(Number(n)))return null;let l=null;if(!e.rootOrderPrice||!r.trigger_price&&!r.order_price)return null;let m=new Decimal(0);if(r.order_type===OrderType.MARKET){if(!r.trigger_price)return null;m=new Decimal(r.trigger_price);}else if(r.order_type===OrderType.LIMIT){if(!r.order_price)return null;m=new Decimal(r.order_price);}let f=new Decimal(e.rootOrderPrice),u=getTPSLDirection({side:e.side,type:e.type,closePrice:m.toNumber(),orderPrice:f.toNumber()});return l=m.minus(f).div(f).mul(n).abs().mul(100).mul(u).toNumber(),l},[r,e.rootOrderPrice,i,e.type,e.side]);return jsxs(Flex,{direction:"column",itemAlign:"start",justify:"start",className:"oui-w-full",children:[jsxs(Flex,{className:"oui-w-full",itemAlign:"center",justify:"start",children:[!e.disableEnableCheckbox&&jsx(Checkbox,{"data-testid":`oui-testid-orderEntry-${e.type}-enable-checkBox`,id:`enable_${e.type}`,color:"white",checked:r.enable,onCheckedChange:l=>{e.onChange(`${e.type}_enable`,!!l);}}),jsx("label",{htmlFor:`enable_${e.type}`,className:cn("oui-cursor-pointer oui-text-sm",e.disableEnableCheckbox?"oui-ml-0 oui-text-base-contrast":"oui-ml-1 oui-text-base-contrast-36"),children:e.type==="tp"?t("tpsl.advanced.TP.label"):t("tpsl.advanced.SL.label")})]}),jsxs(Flex,{direction:"column",gap:2,itemAlign:"start",className:cn("oui-w-full oui-pt-2",r.enable?"":"oui-hidden"),children:[jsxs(Flex,{direction:"column",itemAlign:"start",className:"oui-w-full oui-gap-0.5",children:[jsx(Text,{className:"oui-text-2xs oui-text-base-contrast-54",children:t("tpsl.advanced.triggerPrice")}),jsxs(Grid,{cols:2,gap:2,className:"oui-w-full oui-px-0.5",children:[jsx(lt,{type:`${e.type} price`,value:r.trigger_price,error:o(`${e.type}_trigger_price`),onValueChange:l=>{e.onChange(`${e.type}_trigger_price`,l);},quote_dp:e.quote_dp}),jsx(Qt,{type:e.type==="tp"?"TP":"SL",onChange:(l,m)=>{e.onChange(l,m);},quote:"USDC",quote_dp:2,values:r})]})]}),jsxs(Flex,{direction:"column",className:cn("oui-w-full oui-gap-0.5",e.hideOrderPrice?"oui-hidden":""),itemAlign:"start",children:[jsx(Text,{className:"oui-text-2xs oui-text-base-contrast-54",children:t("tpsl.advanced.orderPrice")}),jsxs(Grid,{cols:2,gap:2,className:"oui-w-full oui-px-0.5",children:[jsx(lt,{disabled:s===PositionType.FULL||r.order_type===OrderType.MARKET,type:"order price",label:r.order_type===OrderType.LIMIT?t("tpsl.advanced.limit"):t("tpsl.advanced.market"),value:r.order_price,error:o(`${e.type}_order_price`),onValueChange:l=>{e.onChange(`${e.type}_order_price`,l);},quote_dp:e.quote_dp}),jsx(Kt,{disabled:s===PositionType.FULL||e.disableOrderTypeSelector,type:r.order_type,onChange:l=>{e.onChange(`${e.type}_order_type`,l);}})]})]})]}),jsx(jo,{price:r.order_type===OrderType.MARKET?r.trigger_price:r.order_price,orderType:r.order_type,pnl:r.PnL,roi:a,dp:e.quote_dp,className:"oui-mt-1"})]})},jo=e=>{let{t}=useTranslation(),{price:o,pnl:r,roi:s,dp:i,className:n,orderType:a}=e;return !s||!o||!r?null:jsx(Text,{className:cn("oui-text-2xs oui-text-base-contrast-36",n),children:jsx(Trans,{i18nKey:"tpsl.advanced.ROI",components:[jsx(Fragment$1,{children:jsx(Text.numeral,{className:"oui-px-1 oui-text-base-contrast",dp:i,suffix:jsx(Text,{className:"oui-pl-0.5",children:"USDC"}),children:o})},"price"),jsx(Fragment$1,{children:jsx(Text,{className:"oui-px-1 oui-text-base-contrast",children:a===OrderType.MARKET?t("common.market"):t("common.limit")})},"orderType"),jsx(Fragment$1,{children:jsx(Text.numeral,{coloring:true,className:"oui-whitespace-nowrap oui-px-1",dp:2,suffix:jsx(Text,{className:"oui-pl-0.5",children:"USDC"}),children:r})},"pnl"),jsx(Fragment$1,{children:jsx(Text.numeral,{coloring:true,className:"oui-whitespace-nowrap oui-px-1",dp:2,suffix:"%",children:s})},"roi")]})})};var $=e=>{let t=it(e);return jsx(at,{...t})};var Ht=e=>({value:e.value,onChange:e.onChange,disableSelector:e.disableSelector});var ti="position_type",Zt=e=>{let{t}=useTranslation(),o=[{label:t("tpsl.positionType.partial"),value:PositionType.PARTIAL},{label:t("tpsl.positionType.full"),value:PositionType.FULL}];return jsxs(Flex,{gap:1,itemAlign:"center",justify:"start",children:[jsx(Tooltip,{className:"oui-w-[280px] oui-p-3",content:e.value===PositionType.FULL?t("tpsl.positionType.full.tips"):t("tpsl.positionType.partial.tips"),children:jsx(ExclamationFillIcon,{className:"oui-cursor-pointer oui-text-base-contrast-54",size:12})}),e.disableSelector?e.value===PositionType.FULL?jsx(Text,{className:"oui-text-2xs oui-font-semibold oui-text-base-contrast-54",children:t("tpsl.positionType.full")}):jsx(Text,{className:"oui-text-2xs oui-font-semibold oui-text-base-contrast-54",children:t("tpsl.positionType.partial")}):jsx(Select.options,{value:e.value,options:o,onValueChange:r=>{e.onChange(ti,r);},size:"xs",classNames:{trigger:" oui-bg-transparent oui-border-0 oui-w-auto oui-px-0"},contentProps:{className:" oui-bg-base-8 oui-border-0"}})]})};var Y=e=>{let t=Ht(e);return jsx(Zt,{...t})};var Ue=memo(e=>{let{maxQty:t,base_dp:o,baseTick:r,quantity:s}=e,i=useRef(null),{t:n}=useTranslation(),[a,l]=useState(0),m=d=>{let g=d;Number(d)>t&&(g=t.toString()),r>0&&e.onQuantityChange?.(utils.formatNumber(g,r)??d);},f=d=>{l(d);let g=new Decimal(d).div(100).mul(t).toFixed(o,Decimal.ROUND_DOWN);m(g);};useEffect(()=>{let d=Math.min(Number(s||0),t),g=new Decimal(d).div(t).mul(100).toDecimalPlaces(2,Decimal.ROUND_DOWN).toNumber();l(g);},[s,t]);let u=e.quantity.toString().length>0?e.errorMsg:void 0;return jsxs(Fragment,{children:[jsx(Flex,{gap:2,children:jsx("div",{className:"oui-flex-1",children:jsx(Input.tooltip,{"data-testid":"oui-testid-tpsl-popUp-quantity-input",ref:i,prefix:n("common.quantity"),size:{initial:"md",lg:"sm"},align:"right",value:e.quantity,autoComplete:"off",inputMode:"decimal",classNames:{prefix:"oui-text-base-contrast-54",root:cn("oui-bg-base-5 oui-outline-line-12",u&&"oui-outline-danger")},tooltipProps:{content:{className:"oui-bg-base-6 oui-text-base-contrast-80"},arrow:{className:"oui-fill-base-6"}},tooltip:u,color:u?"danger":void 0,formatters:[inputFormatter.dpFormatter(e.base_dp),inputFormatter.numberFormatter,inputFormatter.currencyFormatter,inputFormatter.decimalPointFormatter],onValueChange:d=>{e.onQuantityChange?.(d);let g=Number(d);if(g&&g>e.maxQty){let p=e.maxQty;e.onQuantityChange?.(p),i.current?.blur();}},onBlur:d=>m(d.target.value),suffix:jsx("span",{className:"oui-px-3 oui-text-2xs oui-text-base-contrast-54",children:e.base})})})}),jsx(Flex,{mt:2,itemAlign:"center",height:"15px",children:jsx(Slider,{min:0,max:100,markCount:5,showTip:true,value:[a],color:"primary",onValueChange:d=>{f(d[0]);}})}),jsxs(Flex,{justify:"between",children:[jsx(Text.numeral,{color:"primary",size:"2xs",suffix:"%",children:a}),jsxs(Flex,{itemAlign:"center",gap:1,children:[jsx("button",{className:"oui-leading-none",style:{lineHeight:0},onClick:()=>{e.onQuantityChange?.(e.maxQty);},children:jsx(Text,{color:"primary",size:"2xs",children:n("common.max")})}),jsx(Text.numeral,{rule:"price",size:"2xs",intensity:54,tick:e.baseTick,children:e.maxQty})]})]})]})});var er=e=>{let{TPSL_OrderEntity:t,symbolInfo:o,onCancel:r,onComplete:s,status:i,position:n,setValues:a,onClose:l,isEditing:m}=e,{errors:f,validated:u}=e.metaState,{t:d}=useTranslation(),{isMobile:g}=useScreen(),{getErrorMsg:p}=useOrderEntryFormErrorMsg(f);if(!n)return null;let y=()=>t.position_type===PositionType.FULL?null:jsx(Box,{className:"oui-px-0.5",children:jsx(Ue,{maxQty:e.maxQty,quantity:e.orderQuantity??e.maxQty,baseTick:o("base_tick"),base_dp:o("base_dp"),onQuantityChange:e.setQuantity,base:o("base"),isEditing:e.isEditing,errorMsg:u?p("quantity"):void 0})});return jsxs("div",{id:"orderly-tp_sl-order-edit-content",children:[jsx(ScrollArea,{className:cn(g&&"oui-h-[calc(100vh-200px)]"),children:jsxs("div",{className:"oui-px-2",children:[jsx(H,{baseDP:o("base_dp"),quoteDP:o("quote_dp"),classNames:{root:"oui-mb-3",container:"oui-gap-x-[30px]"},order:{symbol:n.symbol,order_quantity:n.position_qty.toString(),order_price:n.average_open_price.toString()},symbolLeverage:n.leverage}),jsxs(Flex,{direction:"column",justify:"start",itemAlign:"start",gap:3,className:"oui-mb-3 oui-w-full",children:[!m&&jsx(Y,{disableSelector:true,value:t.position_type??PositionType.PARTIAL,onChange:(c,P)=>{e.setOrderValue(c,P);}}),t.position_type===PositionType.FULL&&jsx(Text,{className:"oui-text-2xs oui-text-warning",children:d("tpsl.positionType.full.tips.market")})]}),y(),jsxs(Flex,{direction:"column",itemAlign:"start",justify:"start",gap:6,className:"oui-mt-3 oui-w-full",children:[jsx($,{symbol:n.symbol,rootOrderPrice:n.average_open_price.toString(),type:"tp",side:n.position_qty>0?OrderSide.BUY:OrderSide.SELL,values:{enable:t.tp_enable??true,trigger_price:t.tp_trigger_price?.toString()??void 0,PnL:t.tp_pnl?.toString()??void 0,Offset:t.tp_offset?.toString()??void 0,"Offset%":t.tp_offset_percentage?.toString()??void 0,order_price:t.tp_order_price?.toString()??void 0,order_type:t.tp_order_type??OrderType.MARKET},hideOrderPrice:t.position_type===PositionType.FULL,errors:u?f:null,disableOrderTypeSelector:m,quote_dp:o("quote_dp"),positionType:t.position_type??PositionType.PARTIAL,onChange:(c,P)=>{e.setOrderValue(c,P);},symbolLeverage:n.leverage}),jsx($,{symbol:n.symbol,rootOrderPrice:n.average_open_price.toString(),type:"sl",side:n.position_qty>0?OrderSide.BUY:OrderSide.SELL,values:{enable:t.sl_enable??true,trigger_price:t.sl_trigger_price?.toString()??void 0,PnL:t.sl_pnl?.toString()??void 0,Offset:t.sl_offset?.toString()??void 0,"Offset%":t.sl_offset_percentage?.toString()??void 0,order_price:t.sl_order_price?.toString()??void 0,order_type:t.sl_order_type??OrderType.MARKET},hideOrderPrice:t.position_type===PositionType.FULL,errors:u?f:null,quote_dp:o("quote_dp"),positionType:t.position_type??PositionType.PARTIAL,disableOrderTypeSelector:m,onChange:(c,P)=>{e.setOrderValue(c,P);},symbolLeverage:n.leverage})]}),jsx(pe,{tp_pnl:t.tp_pnl,sl_pnl:t.sl_pnl,className:"oui-my-3"})]})}),jsxs(Grid,{px:2,cols:2,gap:3,mt:4,children:[jsx(Button,{size:"md",color:"secondary","data-testid":"tpsl-cancel",onClick:()=>{e.close?.();},children:d("common.cancel")}),jsx(ThrottledButton,{size:"md","data-testid":"tpsl-confirm",disabled:i.isCreateMutating,loading:i.isCreateMutating||i.isUpdateMutating,onClick:()=>{e.onSubmit().then(()=>{e.close?.(),s?.();}).catch(c=>{});},children:d("common.confirm")})]})]})};var vi=e=>{let{tpPrice:t,slPrice:o}=e,{t:r}=useTranslation();return t&&o?jsx(Badge,{size:"xs",color:"neutral",children:r("common.tpsl")}):t?jsx(Badge,{size:"xs",color:"neutral",children:r("tpsl.tp")}):o?jsx(Badge,{size:"xs",color:"neutral",children:r("tpsl.sl")}):null},Ne=e=>{let{symbol:t,tpPrice:o,slPrice:r,qty:s,maxQty:i,side:n,quoteDP:a,baseDP:l,isEditing:m,isPositionTPSL:f,orderInfo:u}=e,{t:d}=useTranslation(),[g,p]=useLocalStorage("orderly_order_confirm",true),y=()=>u.position_type===PositionType.FULL?jsx(Text,{children:d("tpsl.positionType.full")}):jsx(Text,{children:d("tpsl.positionType.partial")}),c=({price:N,isOrderPrice:O,isEnable:w,colorType:oe})=>w?!N&&O?jsx(Text,{className:"oui-text-base-contrast-36",children:d("common.market")}):jsx(Text.numeral,{unit:"USDC",rule:"price",className:cn("oui-text-base-contrast",oe==="TP"?"oui-text-trade-profit":"oui-text-trade-loss"),unitClassName:"oui-text-base-contrast-36 oui-ml-1",dp:a,padding:false,children:N}):jsx(Text,{className:"oui-text-base-contrast-36",children:"-- USDC"}),P=f;return jsxs(Fragment,{children:[m&&jsx(Text,{as:"div",size:"2xs",intensity:80,className:"oui-mb-3",children:d("tpsl.agreement",{symbol:transSymbolformString(t)})}),jsxs(Flex,{pb:4,children:[jsx(Box,{grow:true,children:jsx(Text.formatted,{rule:"symbol",formatString:"base-type",size:"base",showIcon:true,as:"div",intensity:80,children:t})}),jsxs(Flex,{gap:1,children:[P&&jsx(Badge,{size:"xs",color:"primary",children:d("common.position")}),jsx(vi,{tpPrice:o,slPrice:r}),n===OrderSide.SELL?jsx(Badge,{size:"xs",color:"success",children:d("common.buy")}):jsx(Badge,{size:"xs",color:"danger",children:d("common.sell")})]})]}),jsx(Divider,{}),u.tp_trigger_price||u.sl_trigger_price?jsxs(Fragment,{children:[jsx(Divider,{className:"oui-my-4"}),jsxs("div",{className:textVariants({size:"sm",intensity:54,className:"oui-space-y-1 oui-w-full oui-flex oui-flex-col oui-gap-3"}),children:[jsx(Text,{className:"oui-text-base-contrast",children:y()}),jsxs(Flex,{justify:"between",children:[jsx(Text,{children:d("common.orderQty")}),jsx(Text.numeral,{rule:"price",dp:l,padding:false,className:"oui-text-base-contrast",children:u.quantity??"-"})]}),jsxs(Flex,{direction:"column",justify:"between",itemAlign:"start",gap:1,className:"oui-w-full",children:[jsxs(Flex,{justify:"between",className:"oui-w-full",children:[jsx(Text,{children:d("tpsl.tpTriggerPrice")})," ",c({price:u.tp_trigger_price??"",isOrderPrice:false,isEnable:!!u.tp_trigger_price,colorType:"TP"})]}),jsxs(Flex,{justify:"between",className:"oui-w-full",children:[jsx(Text,{children:d("tpsl.tpOrderPrice")}),c({price:u.tp_order_price??"",isOrderPrice:true,isEnable:!!u.tp_trigger_price,colorType:"TP"})]})]}),jsxs(Flex,{direction:"column",justify:"between",itemAlign:"start",gap:1,className:"oui-w-full",children:[jsxs(Flex,{justify:"between",className:"oui-w-full",children:[jsx(Text,{children:d("tpsl.slTriggerPrice")}),c({price:u.sl_trigger_price??"",isOrderPrice:false,isEnable:!!u.sl_trigger_price,colorType:"SL"})]}),jsxs(Flex,{justify:"between",className:"oui-w-full",children:[jsx(Text,{children:d("tpsl.slOrderPrice")}),c({price:u.sl_order_price??"",isOrderPrice:true,isEnable:!!u.sl_trigger_price,colorType:"SL"})]})]})]})]}):null,jsx(Box,{pt:2,children:jsxs(Flex,{gap:1,children:[jsx(Checkbox,{id:"disabledConfirm",color:"white",checked:!g,onCheckedChange:N=>{p(!N);}}),jsx("label",{htmlFor:"disabledConfirm",className:textVariants({size:"xs",intensity:54,className:"oui-ml-1"}),children:d("orderEntry.disableOrderConfirm")})]})})]})};var mr=e=>{let{symbol:t,order:o,isEditing:r,positionType:s,triggerPrice:i,type:n,withTriggerPrice:a}=e,{t:l}=useTranslation();if(r&&!o)throw new SDKError("order is required when isEditing is true");let m=useSymbolsInfo();useRef(AlgoOrderRootType.TP_SL);let [{rows:u}]=usePositionStream(),[d]=useLocalStorage("orderly_order_confirm",true),g=u.find(x=>x.symbol===t);useEffect(()=>{g||e.close?.();},[g]);let [p,{submit:y,deleteOrder:c,setValue:P,setValues:N,validate:O,metaState:w,errors:oe,isCreateMutating:be,isUpdateMutating:ie}]=useTPSLOrder({symbol:t,position_qty:g?.position_qty??0,average_open_price:g?.average_open_price??0},{defaultOrder:o,positionType:i?PositionType.PARTIAL:s,tpslEnable:{tp_enable:a?n==="tp":true,sl_enable:a?n==="sl":true},isEditing:r}),tt=x=>{P("quantity",x);},k=(x,L)=>{P(x,L);},h=(x,L)=>{P(x,L);},wt=useMemo(()=>Math.abs(Number(g?.position_qty)),[g?.position_qty]);useMemo(()=>{let x=o?.algo_type===AlgoOrderRootType.POSITIONAL_TP_SL?wt:o?.quantity,L=0;if((Number(p.quantity)!==x||!r&&p.quantity)&&(L=1),o&&r){let{tp_trigger_price:rt,sl_trigger_price:W}=utils.findTPSLFromOrder(o),{tp_order_price:Dt,sl_order_price:Et}=utils.findTPSLOrderPriceFromOrder(o);rt!==Number(p.tp_trigger_price)&&typeof typeof p.tp_trigger_price<"u"&&(L=2),W!==Number(p.sl_trigger_price)&&typeof p.sl_trigger_price<"u"&&(L=3),typeof p.tp_order_price<"u"&&Dt!==OrderType.MARKET&&Dt!==Number(p.tp_order_price)&&(L=4),typeof p.sl_order_price<"u"&&Et!==OrderType.MARKET&&Et!==Number(p.sl_order_price)&&(L=5);}return L===1&&!p.tp_trigger_price&&!p.sl_trigger_price&&(L=-1),L},[p.tp_trigger_price,p.tp_order_price,p.sl_trigger_price,p.sl_order_price,p.quantity,o,r]);useEffect(()=>{a&&i&&(P(n==="tp"?"tp_trigger_price":"sl_trigger_price",i),e.qty&&P("quantity",e.qty));},[n,i,e.qty]);let lo=()=>o?.algo_order_id&&o?.symbol?c(o?.algo_order_id,o?.symbol):Promise.reject("order id or symbol is invalid"),ao=(x,L)=>{if(!d)return Promise.resolve(true);let rt=Math.abs(Number(g?.position_qty));return `${x.tp_trigger_price??""}`.length===0&&`${x.sl_trigger_price??""}`.length===0?modal.confirm({title:l("orders.cancelOrder"),content:l("tpsl.cancelOrder.description"),onOk:()=>L.cancel()}).then(()=>true,()=>Promise.reject(false)):modal.confirm({title:l("tpsl.confirmOrder"),onOk:async()=>{try{let W=await L.submit({accountId:g?.account_id});return W.success?W:(W.message&&toast.error(W.message),!1)}catch(W){return W?.message&&toast.error(W.message),false}},classNames:{body:"!oui-pb-0"},content:jsx(Ne,{isPositionTPSL:s===PositionType.FULL,isEditing:r,symbol:x.symbol,qty:Number(x.quantity),maxQty:rt,tpPrice:Number(x.tp_trigger_price),slPrice:Number(x.sl_trigger_price),side:x.side,orderInfo:x,quoteDP:m[t]("quote_dp")??2,baseDP:m[t]("base_dp")??2})}).then(()=>true,()=>Promise.reject(false))},uo=async()=>{try{if(await O())return d?ao(p,{position:g,submit:y,cancel:lo}):y({accountId:g?.account_id}).then(()=>!0).catch(L=>{throw L?.message&&toast.error(L.message),!1})}catch(x){return Promise.reject(x)}};return {isEditing:r,symbolInfo:m[t],maxQty:wt,setQuantity:useMemoizedFn(tt),orderQuantity:p.quantity,TPSL_OrderEntity:p,setOrderValue:P,setPnL:h,setOrderPrice:k,onSubmit:uo,metaState:w,errors:oe,status:{isCreateMutating:be,isUpdateMutating:ie},position:g,setValues:N}};var he=e=>{let{onCancel:t,onComplete:o,...r}=e,s=mr(r);return jsx(er,{...s,onCancel:t,onComplete:o,close:r.close})},ze="TPSLSheetId",fe="TPSLDialogId";registerSimpleSheet(ze,he);registerSimpleDialog(fe,he,{classNames:{content:"oui-w-[420px]"}});var Hi=e=>{let{position:t,order:o,baseDP:r,quoteDP:s,buttonProps:i,isEditing:n}=e,[a]=useLocalStorage("orderly_order_confirm",true),{t:l}=useTranslation(),m=n?o?.algo_type===AlgoOrderRootType.POSITIONAL_TP_SL:void 0;return jsx(Box,{onClick:()=>{modal.show(fe,{order:o,symbol:t.symbol,baseDP:r,quoteDP:s,positionType:m?PositionType.FULL:PositionType.PARTIAL,isEditing:n});},className:"oui-cursor-pointer",children:e.children||jsx(Button,{variant:"outlined",size:"sm",color:"secondary",...i,children:e.label})})};var Xi=e=>{let{position:t,order:o,symbolInfo:r,isEditing:s}=e,{resolve:i,hide:n,updateArgs:a}=useModal(),l=s?o?.algo_type===AlgoOrderRootType.POSITIONAL_TP_SL:void 0;return jsx(Fragment,{children:jsx(he,{...e,positionType:e.positionType??(l?PositionType.FULL:PositionType.PARTIAL),onCancel:()=>{n();}})})};var br=e=>{let{t}=useTranslation(),{errors:o,validated:r}=e.metaState,{formattedOrder:s,setValue:i,symbolInfo:n,setValues:a}=e,[l,m]=useState({enable:true,order_type:s.tp_order_type??OrderType.MARKET,order_price:s.tp_order_price??"",trigger_price:s.tp_trigger_price??"",PnL:s.tp_pnl??"",Offset:s.tp_offset??"","Offset%":s.tp_offset_percentage??"",ROI:s.tp_ROI??""}),[f,u]=useState({enable:true,order_type:s.sl_order_type??OrderType.MARKET,order_price:s.sl_order_price??"",trigger_price:s.sl_trigger_price??"",PnL:s.sl_pnl??"",Offset:s.sl_offset??"","Offset%":s.sl_offset_percentage??"",ROI:s.sl_ROI??""});return useEffect(()=>{m(d=>({...d,enable:s.tp_enable??true,order_type:s.tp_order_type??OrderType.MARKET,order_price:s.tp_order_price??"",trigger_price:s.tp_trigger_price??"",PnL:s.tp_pnl??"",Offset:s.tp_offset??"","Offset%":s.tp_offset_percentage??"",ROI:s.tp_ROI??""}));},[s]),useEffect(()=>{u(d=>({...d,enable:s.sl_enable??true,order_type:s.sl_order_type??OrderType.MARKET,order_price:s.sl_order_price??"",trigger_price:s.sl_trigger_price??"",PnL:s.sl_pnl??"",Offset:s.sl_offset??"","Offset%":s.sl_offset_percentage??"",ROI:s.sl_ROI??""}));},[s]),jsxs("div",{className:"oui-rounded-[16px] oui-py-3 oui-flex oui-flex-col oui-justify-between oui-h-full",children:[jsx("div",{className:"oui-px-3",children:jsxs(Flex,{className:"oui-mb-5 oui-cursor-pointer oui-text-base oui-text-base-contrast-80",gap:1,itemAlign:"center",onClick:e.onClose,children:[jsx(os,{className:" oui-text-base-contrast-80"}),jsx(Text,{children:t("tpsl.advanced.title")})]})}),jsxs(ScrollArea,{className:"oui-flex-1",children:[jsx("div",{className:"oui-px-3",children:jsx(H,{order:s,baseDP:n.base_dp,quoteDP:n.quote_dp,symbolLeverage:e.symbolLeverage})}),jsx(Divider,{className:"oui-my-3"}),jsxs("div",{className:"oui-px-3",children:[jsxs(Flex,{className:"oui-gap-[6px]",children:[jsx(Button,{onClick:()=>{i("side",OrderSide.BUY);},size:"sm",fullWidth:true,"data-type":OrderSide.BUY,className:cn(s.side===OrderSide.BUY?"oui-bg-success-darken hover:oui-bg-success-darken/80 active:oui-bg-success-darken/80":"oui-bg-base-7 oui-text-base-contrast-36 hover:oui-bg-base-6 active:oui-bg-base-6"),"data-testid":"oui-testid-orderEntry-side-buy-button",children:t("common.buy")}),jsx(Button,{onClick:()=>{i("side",OrderSide.SELL);},"data-type":OrderSide.SELL,fullWidth:true,size:"sm",className:cn(s.side===OrderSide.SELL?"oui-bg-danger-darken hover:oui-bg-danger-darken/80 active:oui-bg-danger-darken/80":"oui-bg-base-7 oui-text-base-contrast-36 hover:oui-bg-base-6 active:oui-bg-base-6"),"data-testid":"oui-testid-orderEntry-side-sell-button",children:t("common.sell")})]}),jsxs("div",{className:"oui-py-3",children:[jsx(Y,{value:s.position_type??PositionType.PARTIAL,onChange:(d,g)=>{if(g===PositionType.FULL){a({position_type:PositionType.FULL,tp_order_type:OrderType.MARKET,tp_order_price:void 0,sl_order_type:OrderType.MARKET,sl_order_price:void 0});return}i("position_type",g);}}),s.position_type===PositionType.FULL&&jsxs(Flex,{justify:"start",itemAlign:"start",gap:2,className:"oui-mt-3 oui-w-full",children:[jsx("div",{className:"oui-relative oui-top-[7px] oui-size-1 oui-rounded-full oui-bg-[#D25f00]"}),jsx(Text,{className:"oui-text-2xs oui-text-[#D25f00]",children:t("tpsl.positionType.full.tips.market")})]})]}),jsxs(Flex,{direction:"column",gap:6,children:[jsx($,{rootOrderPrice:s.order_price,symbol:n.symbol,type:"tp",side:s.side,values:l,errors:r?o:null,quote_dp:n.quote_dp,hideOrderPrice:s.position_type===PositionType.FULL,onChange:(d,g)=>{i(d,g);},positionType:s.position_type??PositionType.PARTIAL,symbolLeverage:e.symbolLeverage}),jsx($,{rootOrderPrice:s.order_price,symbol:n.symbol,type:"sl",side:s.side,values:f,hideOrderPrice:s.position_type===PositionType.FULL,errors:r?o:null,quote_dp:n.quote_dp,positionType:s.position_type??PositionType.PARTIAL,onChange:(d,g)=>{i(d,g);},symbolLeverage:e.symbolLeverage})]}),jsx(pe,{tp_pnl:s.tp_pnl,sl_pnl:s.sl_pnl,className:"oui-mt-6"})]})]}),jsxs(Flex,{className:"oui-mt-6 oui-px-3",gap:2,children:[jsx(Button,{size:"md",fullWidth:true,color:"gray",variant:"outlined",className:"oui-text-base-contrast-36",onClick:e.onClose,children:t("common.cancel")}),jsx(Button,{size:"md",fullWidth:true,color:"success",className:cn(s.side===OrderSide.SELL?"oui-bg-danger-darken hover:oui-bg-danger-darken/80 active:oui-bg-danger-darken/80":"oui-bg-success-darken hover:oui-bg-success-darken/80 active:oui-bg-success-darken/80"),onClick:e.onSubmit,children:t("tpsl.advanced.submit")})]})]})},os=e=>jsx("svg",{xmlns:"http://www.w3.org/2000/svg",width:"14",height:"14",viewBox:"0 0 14 14",fill:"currentColor",...e,children:jsx("path",{d:"M8.03752 2.9294C7.89169 2.95857 7.74527 3.03207 7.65661 3.16624L5.33145 6.66624C5.20137 6.86224 5.20137 7.12648 5.33145 7.32248L7.65661 10.8225C7.83452 11.0902 8.20669 11.1655 8.47385 10.9864C8.74044 10.8079 8.8151 10.434 8.63719 10.1662L6.53019 6.99408L8.63719 3.82249C8.8151 3.55416 8.74044 3.18082 8.47385 3.00232C8.34027 2.91249 8.18335 2.89965 8.03752 2.9294Z"})});var Sr=(e,t)=>{if(e.tp_trigger_price||e.sl_trigger_price){if(t==="tp")return !!e.tp_trigger_price;if(t==="sl")return !!e.sl_trigger_price}return true},Or=e=>{let{order:t,setOrderValue:o,onClose:r}=e,[s,i]=useState(null),{formattedOrder:n,setValue:a,setValues:l,helper:m,symbolInfo:f,metaState:u,...d}=useOrderEntry(t.symbol,{initialOrder:{symbol:t.symbol,order_type:t.order_type,side:t.side,order_price:t.order_price,order_quantity:t.order_quantity,position_type:t.position_type??PositionType.PARTIAL,trigger_price:t.trigger_price,tp_enable:Sr(t,"tp"),sl_enable:Sr(t,"sl"),tp_trigger_price:t.tp_trigger_price,sl_trigger_price:t.sl_trigger_price,tp_order_price:t.tp_order_price,sl_order_price:t.sl_order_price,tp_order_type:t.tp_order_type,sl_order_type:t.sl_order_type,sl_pnl:t.sl_pnl,sl_offset:t.sl_offset,sl_offset_percentage:t.sl_offset_percentage,tp_pnl:t.tp_pnl,tp_offset:t.tp_offset,tp_offset_percentage:t.tp_offset_percentage}});return {order:t,formattedOrder:n,symbolInfo:f,setValue:a,setValues:l,onSubmit:()=>{m.validate().then(()=>{e.onSubmit(n);}).catch(p=>{});},onClose:r,metaState:u,symbolLeverage:e.symbolLeverage}};var Ir=e=>{let t=Or({order:e.order,setOrderValue:e.setOrderValue,onSubmit:e.onSubmit,onClose:e.onClose,symbolLeverage:e.symbolLeverage});return jsx(br,{...t})},ds="TPSLAdvancedSheetId",us="TPSLAdvancedDialogId";registerSimpleSheet(ds,Ir,{title:()=>i18n.t("common.settings")});registerSimpleDialog(us,Ir,{title:()=>i18n.t("common.settings")});var Nr=e=>{let{position:t}=e,o=t.symbol,r=useSymbolsInfo()[o],{isMobile:s}=useScreen(),{t:i}=useTranslation(),[n,a]=useState([]),[l,m]=useState([]),[f,{cancelAlgoOrder:u,cancelPostionOrdersByTypes:d,refresh:g}]=useOrderStream({symbol:t.symbol,status:OrderStatus.INCOMPLETE,includes:[AlgoOrderRootType.POSITIONAL_TP_SL,AlgoOrderRootType.TP_SL],size:500},{keeplive:true}),p=async O=>await u(O.algo_order_id,O.symbol),y=async()=>await d(o,[AlgoOrderRootType.TP_SL]),c=({order:O,positionType:w,isEditing:oe})=>{let be=s?ze:fe;modal.show(be,{order:O,symbol:t.symbol,positionType:w,isEditing:oe});},P=(O,w)=>{c({order:O,positionType:w,isEditing:true});},N=O=>{c({positionType:O,isEditing:false});};return useEffect(()=>{if(f){let{fullPositionOrder:O,partialPositionOrders:w}=findPositionTPSLFromOrders(f,o);a(O?[O]:[]),m(w??[]);}},[f,o]),{symbolInfo:r,position:t,symbol:o,fullPositionOrders:n,partialPositionOrders:l,cancelPostionOrdersByTypes:d,onCancelOrder:p,onCancelAllTPSLOrders:y,editTPSLOrder:P,addTPSLOrder:N}};var hr=createContext({}),Cr=e=>{let t=useSymbolsInfo()[e.symbol];return jsx(hr.Provider,{value:{base_dp:t("base_dp"),quote_dp:t("quote_dp"),position:e.position},children:e.children})},X=()=>useContext(hr);var C=e=>jsx(Flex,{direction:"column",justify:"center",itemAlign:"start",className:"oui-text-2xs oui-h-[36px]",children:e.children});var gt=({order:e})=>{let{position:t,base_dp:o,quote_dp:r}=X(),{tp_trigger_price:s,sl_trigger_price:i}=findTPSLFromOrder(e),n,a,l=new Decimal(e.quantity).eq(0)?t.position_qty:e.quantity,m=t.position_qty>0?OrderSide.BUY:OrderSide.SELL,f=t?.average_open_price;if(s){let u=getTPSLDirection({side:m,type:"tp",closePrice:s,orderPrice:f});n=new Decimal(positions.unrealizedPnL({qty:l,openPrice:f,markPrice:s})).abs().mul(u).toNumber();}if(i){let u=getTPSLDirection({side:m,type:"sl",closePrice:i,orderPrice:f});a=new Decimal(positions.unrealizedPnL({qty:l,openPrice:f,markPrice:i})).abs().mul(u).toNumber();}return jsxs(Flex,{gap:2,direction:"column",justify:"between",itemAlign:"start",className:"oui-text-2xs",children:[n&&jsx(C,{children:jsx(Text.numeral,{dp:2,rm:Decimal.ROUND_DOWN,coloring:true,padding:false,children:n})}),a&&jsx(C,{children:jsx(Text.numeral,{dp:2,rm:Decimal.ROUND_DOWN,coloring:true,padding:false,children:a})})]})};var ft=({order:e})=>{let{quote_dp:t}=X(),{tp_order_price:o,sl_order_price:r}=findTPSLOrderPriceFromOrder(e),{t:s}=useTranslation();return jsxs(Flex,{gap:2,direction:"column",justify:"between",itemAlign:"start",className:"oui-text-2xs",children:[o&&jsx(C,{children:o===OrderType.MARKET?jsx(Text,{children:s("common.market")}):jsx(Text.numeral,{dp:t,rm:Decimal.ROUND_DOWN,padding:false,children:o})}),r&&jsx(C,{children:r===OrderType.MARKET?jsx(Text,{children:s("common.market")}):jsx(Text.numeral,{dp:t,rm:Decimal.ROUND_DOWN,padding:false,children:r})})]})};var yt=({order:e})=>{let{position:t,base_dp:o}=X(),{tp_trigger_price:r,sl_trigger_price:s}=findTPSLFromOrder(e),i=useMemo(()=>e.quantity===0?-t.position_qty:t.position_qty>0?-e.quantity:e.quantity,[e.quantity,t.position_qty]);return jsxs(Flex,{direction:"column",justify:"start",itemAlign:"start",className:"oui-h-full oui-text-2xs",children:[jsx(C,{children:jsx(Text.numeral,{dp:o,rm:Decimal.ROUND_DOWN,padding:false,children:i})}),r&&s&&jsx(C,{children:jsx("div",{})})]})};var Tt=({order:e})=>{let{quote_dp:t}=X(),{tp_trigger_price:o,sl_trigger_price:r}=findTPSLFromOrder(e),{t:s}=useTranslation();return jsxs(Flex,{gap:1,direction:"column",justify:"between",itemAlign:"start",className:"oui-text-2xs",children:[o&&jsx(C,{children:jsxs(Flex,{direction:"column",justify:"start",itemAlign:"start",children:[jsx(Text,{className:"oui-text-base-contrast-36",children:s("common.market")}),jsx(Text.numeral,{dp:t,rm:Decimal.ROUND_DOWN,padding:false,children:o})]})}),r&&jsx(C,{children:jsxs(Flex,{direction:"column",justify:"start",itemAlign:"start",children:[jsx(Text,{className:"oui-text-base-contrast-36",children:s("common.market")}),jsx(Text.numeral,{dp:t,rm:Decimal.ROUND_DOWN,padding:false,children:r})]})})]})};var xt=({order:e})=>{let{tp_trigger_price:t,sl_trigger_price:o}=findTPSLFromOrder(e),{t:r}=useTranslation();return jsxs(Flex,{direction:"column",justify:"between",itemAlign:"start",className:"oui-text-2xs",children:[t&&jsx(C,{children:jsx(Text,{className:"oui-text-trade-profit",children:r("tpsl.tp")})}),o&&jsx(C,{children:jsx(Text,{className:"oui-text-trade-loss",children:r("tpsl.sl")})})]})};var He=e=>{let{t}=useTranslation(),{isMobile:o}=useScreen(),{onCancelOrder:r}=e;return useMemo(()=>{let i=[{title:t("tpsl.tpslDetail.qty"),dataIndex:"quantity",width:70,className:"oui-py-2",render:(a,l)=>jsx(yt,{order:l})},{title:t("tpsl.tpslDetail.type"),dataIndex:"type",width:35,className:"oui-pl-1 oui-py-2",render:(a,l)=>jsx(xt,{order:l})},{title:t("tpsl.tpslDetail.trigger"),dataIndex:"trigger",width:70,className:"oui-pl-1 oui-py-2",render:(a,l)=>jsx(Tt,{order:l})},{title:t("tpsl.tpslDetail.price"),dataIndex:"price",width:70,className:"oui-py-2",render:(a,l)=>jsx(ft,{order:l})},{title:jsx(Tooltip,{className:"oui-max-w-[280px] oui-bg-base-8 oui-p-3 oui-text-2xs oui-text-base-contrast",content:t("tpsl.tpslDetail.estPnl.tooltip"),children:jsx(Text,{className:"oui-underline oui-decoration-dashed oui-underline-offset-2",children:t("tpsl.tpslDetail.estPnl")})}),dataIndex:"estpnl",width:70,className:"!oui-pr-0 oui-py-2",render:(a,l)=>jsx(gt,{order:l})}],n=[{title:t("tpsl.tpslDetail.qty"),dataIndex:"quantity",width:70,className:cn(" oui-py-2 !oui-pl-5"),render:(a,l)=>jsx(yt,{order:l})},{title:t("tpsl.tpslDetail.type"),dataIndex:"type",width:35,className:"oui-pl-1 oui-py-2",render:(a,l)=>jsx(xt,{order:l})},{title:t("tpsl.tpslDetail.trigger"),dataIndex:"trigger",width:70,className:"oui-pl-1 oui-py-2",render:(a,l)=>jsx(Tt,{order:l})},{title:t("tpsl.tpslDetail.price"),dataIndex:"price",width:70,className:"oui-pl-1 oui-py-2",render:(a,l)=>jsx(ft,{order:l})},{title:jsx(Tooltip,{className:"oui-max-w-[280px] oui-bg-base-8 oui-p-3 oui-text-2xs oui-text-base-contrast",content:t("tpsl.tpslDetail.estPnl.tooltip"),children:jsx(Text,{className:"oui-underline oui-decoration-dashed oui-underline-offset-2",children:t("tpsl.tpslDetail.estPnl")})}),dataIndex:"estpnl",width:70,className:"oui-pl-1 oui-py-2",render:(a,l)=>jsx(gt,{order:l})},{title:"",dataIndex:"delete",width:50,className:cn("oui-py-2 !oui-pr-5"),render:(a,l)=>jsx(Xs,{order:l,onCancelOrder:r})}];return o?i:n},[t,o])};var Zs=e=>{let{size:t=18}=e;return jsx("svg",{xmlns:"http://www.w3.org/2000/svg",width:t,height:t,viewBox:"0 0 18 18",fill:"currentColor",...e,children:jsx("path",{d:"M5.48081 15.375C5.10681 15.375 4.78731 15.2426 4.52231 14.9777C4.25744 14.7127 4.125 14.3932 4.125 14.0192V4.50004H3.375V3.37505H6.75V2.71167H11.25V3.37505H14.625V4.50004H13.875V14.0192C13.875 14.3981 13.7438 14.7188 13.4813 14.9813C13.2188 15.2438 12.8981 15.375 12.5192 15.375H5.48081ZM12.75 4.50004H5.25V14.0192C5.25 14.0866 5.27162 14.1419 5.31487 14.1852C5.35812 14.2284 5.41344 14.25 5.48081 14.25H12.5192C12.5769 14.25 12.6298 14.226 12.6778 14.1779C12.7259 14.1299 12.75 14.077 12.75 14.0192V4.50004ZM7.053 12.75H8.17781V6.00004H7.053V12.75ZM9.82219 12.75H10.947V6.00004H9.82219V12.75Z"})})},Xs=e=>{let[t,o]=useState(false);return jsx(ThrottledButton,{size:"sm",loading:t,variant:"text",color:"gray",onClick:r=>{r.stopPropagation(),o(true),e.onCancelOrder?.(e.order).then(()=>{},s=>{toast.error(s.message);}).finally(()=>{o(false);});},children:jsx(Zs,{className:"oui-text-base-contrast-54 hover:oui-text-base-contrast oui-cursor-pointer"})})};var St=e=>{let{orders:t}=e,o=He({onCancelOrder:e.onCancelOrder});return jsx(AuthGuardDataTable,{columns:o,dataSource:t,className:"oui-bg-transparent oui-text-2xs",bordered:true,classNames:{root:cn(e.className),header:"!oui-bg-base-8",scroll:cn(!t||t.length===0?"!oui-min-h-[170px]":"!oui-min-h-[100px]")},onRow:r=>({className:cn("oui-h-[53px] oui-cursor-svg-edit !oui-border-none !oui-p-0"),onClick:()=>{e.editTPSLOrder(r);}})})};var Ot=e=>{let{t}=useTranslation(),{orders:o}=e,r=He({onCancelOrder:()=>Promise.resolve()}),s=useMemo(()=>o.reduce((i,n)=>(i[n.algo_order_id]=true,i),{}),[o]);return jsx(AuthGuardDataTable,{columns:r,dataSource:o,expanded:s,bordered:true,getRowCanExpand:()=>true,expandRowRender:i=>jsxs(Flex,{gap:2,justify:"end",children:[jsx(Button,{variant:"outlined",size:"sm",color:"gray",className:"oui-h-6 oui-text-2xs oui-text-base-contrast-54",onClick:()=>e.editTPSLOrder(i.original),children:t("common.edit")}),jsx(dn,{order:i.original,onCancelOrder:e.canCancelOrder})]}),className:"oui-bg-transparent oui-px-2 oui-text-2xs",classNames:{scroll:cn(!o||o.length===0?"!oui-min-h-[170px]":"!oui-min-h-[100px]")},onRow:()=>({className:cn("oui-h-[53px] oui-cursor-svg-edit !oui-border-none !oui-p-0")}),generatedRowKey:i=>i.algo_order_id})},dn=e=>{let[t,o]=useState(false),{t:r}=useTranslation();return jsx(ThrottledButton,{className:"oui-h-6 oui-text-2xs oui-text-base-contrast-54",size:"sm",loading:t,variant:"outlined",color:"gray",onClick:s=>{s.stopPropagation(),o(true),e.onCancelOrder?.(e.order).then(()=>{},i=>{toast.error(i.message);}).finally(()=>{o(false);});},children:r("common.delete")})};var Kr=e=>{let{isMobile:t}=useScreen(),{position:o,fullPositionOrders:r,partialPositionOrders:s,onCancelOrder:i,onCancelAllTPSLOrders:n,editTPSLOrder:a,addTPSLOrder:l,symbolInfo:m}=e;return jsx(Box,{children:jsxs(ScrollArea,{className:cn(t&&"oui-h-[calc(100vh-100px)]"),children:[jsx(H,{order:{symbol:o.symbol,order_quantity:o.position_qty.toString(),order_price:o.average_open_price.toString()},symbolLeverage:o.leverage,baseDP:m("base_dp"),quoteDP:m("quote_dp"),classNames:{root:cn("oui-mb-6 oui-gap-3 oui-px-5"),container:"oui-gap-x-[30px]"}}),jsx(yn,{position:o,orders:r,onCancelOrder:i,onCancelAllTPSLOrders:n,editTPSLOrder:a,addTPSLOrder:l}),jsx(Pn,{position:o,orders:s,onCancelOrder:i,onCancelAllTPSLOrders:n,editTPSLOrder:a,addTPSLOrder:l})]})})},yn=e=>{let[t,o]=useState(true),{orders:r}=e,{isMobile:s}=useScreen();return jsxs(Box,{className:"oui-mt-6",children:[jsxs(Box,{className:cn("oui-flex oui-items-center oui-justify-between oui-px-5"),children:[jsx(Gr,{positionType:PositionType.FULL,open:t,onOpenChange:o}),r&&r.length===0&&jsx(Flex,{gap:2,children:jsx($r,{positionType:PositionType.FULL,position:e.position,addTPSLOrder:e.addTPSLOrder})})]}),jsx(Box,{className:cn("oui-overflow-hidden oui-transition-[height] oui-duration-150",t?"oui-h-auto":"oui-h-0 oui-pb-4"),children:s?jsx(Ot,{orders:r,editTPSLOrder:i=>e.editTPSLOrder(i,PositionType.FULL),canCancelOrder:e.onCancelOrder}):jsx(St,{orders:r,editTPSLOrder:i=>{e.editTPSLOrder(i,PositionType.FULL);},onCancelOrder:e.onCancelOrder})})]})},Pn=e=>{let[t,o]=useState(true),{orders:r}=e,{isMobile:s}=useScreen();return jsxs(Box,{className:"oui-pt-6",children:[jsxs(Box,{className:cn("oui-flex oui-items-center oui-justify-between","oui-px-5",s?"oui-flex-col oui-items-start oui-justify-start oui-gap-[11px]":"oui-flex-row oui-items-center oui-justify-between"),children:[jsx(Gr,{positionType:PositionType.PARTIAL,open:t,onOpenChange:o}),jsxs(Flex,{gap:2,children:[jsx($r,{positionType:PositionType.PARTIAL,position:e.position,addTPSLOrder:e.addTPSLOrder}),r&&r.length>0&&jsx(_n,{canCancelAll:r&&r.length>0,onCancelAllTPSLOrders:e.onCancelAllTPSLOrders})]})]}),jsx(Box,{className:cn("oui-overflow-hidden oui-transition-[height] oui-duration-150",t?"oui-h-auto":"oui-h-0 oui-pb-4"),children:s?jsx(Ot,{orders:r,editTPSLOrder:i=>e.editTPSLOrder(i,PositionType.PARTIAL),canCancelOrder:e.onCancelOrder}):jsx(St,{className:"oui-h-[300px]",orders:r,editTPSLOrder:i=>{e.editTPSLOrder(i,PositionType.PARTIAL);},onCancelOrder:e.onCancelOrder})})]})},Gr=e=>{let{t}=useTranslation();return jsxs(Flex,{gap:1,itemAlign:"center",justify:"start",className:"oui-text-2xs",children:[jsxs(Flex,{itemAlign:"center",justify:"start",gap:1,className:"oui-cursor-pointer",onClick:()=>e.onOpenChange(!e.open),children:[jsx(ChevronDownIcon,{size:12,color:"white",className:cn("oui-cursor-pointer oui-transition-transform",e.open&&"oui-rotate-180")}),e.positionType===PositionType.FULL?jsx(Text,{children:t("tpsl.positionType.full")}):jsx(Text,{children:t("tpsl.positionType.partial")})]}),jsx(Tooltip,{className:"oui-w-[280px] oui-p-3",content:e.positionType===PositionType.FULL?t("tpsl.positionType.full.tips"):t("tpsl.positionType.partial.tips"),children:jsx(ExclamationFillIcon,{className:"oui-cursor-pointer oui-text-base-contrast-54",size:12})})]})},$r=e=>{let{t}=useTranslation();return jsx(ThrottledButton,{variant:"outlined",size:"sm",color:"gray",className:"oui-h-6 oui-w-[94px] oui-text-2xs",onClick:()=>{e.addTPSLOrder(e.positionType);},children:t("tpsl.add")})},_n=e=>{let[t,o]=useState(false),{t:r}=useTranslation();return jsx(ThrottledButton,{loading:t,variant:"outlined",disabled:!e.canCancelAll,size:"sm",color:"gray",className:"oui-h-6 oui-w-[94px] oui-text-2xs disabled:oui-border-base-contrast-16 disabled:oui-bg-transparent disabled:oui-text-base-contrast-20",onClick:()=>{o(true),e.onCancelAllTPSLOrders().then(()=>{},s=>{toast.error(s.message);}).finally(()=>{o(false);});},children:r("tpsl.cancelAll")})};var Hr=e=>{let t=Nr(e);return jsx(Cr,{symbol:e.position.symbol,position:e.position,children:jsx(Kr,{...t})})},bn="TPSLDetailDialogId ",Sn="TPSLDetailSheetId";registerSimpleDialog(bn,Hr,{classNames:{content:"oui-w-[420px] lg:oui-pt-4 lg:oui-pb-2 !oui-px-0",body:"lg:oui-py-0"}});registerSimpleSheet(Sn,Hr,{classNames:{content:"!oui-p-0"}});var ht=e=>{let {type:t,triggerPrice:o,symbol:r}=e,s=useSymbolsInfo(),[{rows:i},n]=usePositionStream(r),a=i?.[0];useRef(AlgoOrderRootType.TP_SL);let [m]=useLocalStorage("orderly_order_confirm",true),{t:f}=useTranslation(),[u,{submit:d,deleteOrder:g,setValue:p,setValues:y,validate:c,errors:P,isCreateMutating:N,isUpdateMutating:O}]=useTPSLOrder({symbol:r,position_qty:a?.position_qty,average_open_price:a?.average_open_price},{defaultOrder:void 0,positionType:PositionType.PARTIAL,tpslEnable:{tp_enable:t==="tp",sl_enable:t==="sl"},isEditing:false}),w=k=>{p("quantity",k);},oe=(k,h)=>{p(k,h);},be=(k,h)=>{p(k,h);},ie=useMemo(()=>Math.abs(Number(a.position_qty)),[a.position_qty]);useEffect(()=>{ie&&(p("quantity",ie),p(t==="tp"?"tp_trigger_price":"sl_trigger_price",o??""));},[t,o,ie]);let tt=async()=>{try{if(await c())return m?modal.confirm({title:f("tpsl.confirmOrder"),onOk:async()=>{try{let h=await d({accountId:a.account_id});return h.success?h:(h.message&&toast.error(h.message),!1)}catch(h){return h?.message&&toast.error(h.message),!1}},classNames:{body:"!oui-pb-0"},content:jsx(Ne,{isPositionTPSL:!1,isEditing:!1,symbol:r,qty:Number(u.quantity),maxQty:ie,tpPrice:Number(u.tp_trigger_price),slPrice:Number(u.sl_trigger_price),side:u.side,orderInfo:u,quoteDP:s[r]("quote_dp"),baseDP:s[r]("base_dp")})}).then(()=>!0,()=>Promise.reject(!1)):d({accountId:a.account_id}).then(()=>!0).catch(h=>{throw h?.message&&toast.error(h.message),!1})}catch(k){return Promise.reject(k)}};return {symbolInfo:s[r],maxQty:ie,setQuantity:useMemoizedFn(w),orderQuantity:u.quantity,isPosition:false,TPSL_OrderEntity:u,setOrderValue:p,setPnL:be,setOrderPrice:oe,onSubmit:tt,errors:P,status:{isCreateMutating:N,isUpdateMutating:O},position:a,setValues:y,type:t,triggerPrice:o}};var Ct=e=>{let{type:t,triggerPrice:o,errors:r,TPSL_OrderEntity:s}=e,{t:i}=useTranslation(),{getErrorMsg:n}=useOrderEntryFormErrorMsg(r),a=jsxs(Flex,{width:"100%",itemAlign:"center",gap:3,mt:4,children:[jsx(Button,{color:"gray",onClick:()=>{e.onComplete?.(),e.close?.();},fullWidth:true,className:"oui-text-sm",size:"md",children:i("common.cancel")},"secondary"),jsx(Button,{onClick:()=>{e.onSubmit().then(()=>{e.onComplete?.(),e.close?.();});},fullWidth:true,className:"oui-text-sm",size:"md",children:i("common.confirm")},"primary")]}),l=()=>{let f=n("tp_trigger_price"),u=n("sl_trigger_price"),d="";return f&&t==="tp"&&(d=f),u&&t==="sl"&&(d=u),d?jsxs(Flex,{justify:"start",itemAlign:"start",gap:2,className:"oui-mt-2 oui-w-full",children:[jsx("div",{className:"oui-relative oui-top-[7px] oui-size-1 oui-rounded-full oui-bg-danger"}),jsx(Text,{className:"oui-text-danger",children:d})]}):null},m=()=>{let{tp_pnl:f,sl_pnl:u}=s;return t==="sl"?jsxs(Flex,{justify:"between",className:"oui-w-full",children:[jsx(Text,{size:"2xs",children:i("tpsl.totalEstSlPnl")}),u?jsx(Text.numeral,{suffix:jsx(Text,{className:"oui-ml-1 oui-text-base-contrast-36",children:"USDC"}),coloring:true,visible:true,size:"2xs",dp:2,children:Number(u)}):jsx(Text,{size:"2xs",children:"-- USDC"})]}):jsxs(Flex,{justify:"between",className:"oui-w-full",children:[jsx(Text,{size:"2xs",children:i("tpsl.totalEstTpPnl")}),jsx(Text.numeral,{suffix:jsx(Text,{className:"oui-text-base-contrast-36 oui-ml-1",children:"USDC"}),rule:"price",coloring:true,visible:true,size:"2xs",dp:2,children:f?Number(f):"--"})]})};return jsxs(Box,{className:"oui-w-full oui-px-0.5",children:[jsx(Ue,{maxQty:e.maxQty,quantity:Number(e.orderQuantity??e.maxQty),baseTick:e.symbolInfo("base_tick"),base_dp:e.symbolInfo("base_dp"),base:e.symbolInfo("base"),isEditing:false,errorMsg:n("quantity"),onQuantityChange:e.setQuantity}),jsxs(Flex,{direction:"column",itemAlign:"start",className:cn("oui-mt-4 oui-w-full oui-gap-1 oui-text-xs oui-text-base-contrast-36"),children:[jsxs(Flex,{justify:"between",className:"oui-w-full",children:[jsx(Text,{size:"xs",children:i("tpsl.advanced.triggerPrice")}),jsx(Text.numeral,{className:"oui-text-base-contrast",suffix:jsx(Text,{className:"oui-ml-1 oui-text-xs oui-text-base-contrast-36",children:"USDC"}),rule:"price",size:"xs",dp:e.symbolInfo("quote_dp"),children:o?Number(o):"--"})]}),jsxs(Flex,{justify:"between",className:"oui-w-full",children:[jsx(Text,{size:"xs",children:i("tpsl.advanced.orderPrice")}),jsx(Text,{className:"oui-text-base-contrast",size:"xs",children:i("tpsl.advanced.market")})]}),m()]}),l(),jsx(Divider,{className:"oui-my-3 oui-w-full"}),jsx(Flex,{itemAlign:"center",onClick:()=>{e.close?.(),e.showAdvancedTPSLDialog?.({qty:Number(s.quantity)});},className:"oui-pb-4",children:jsx(Text,{color:"primary",className:"oui-cursor-pointer oui-text-sm",children:i("tpsl.advancedSetting")})}),a]})};var vt=e=>{let{close:t,onComplete:o,showAdvancedTPSLDialog:r}=e,s=ht(e);return jsx(Ct,{...s,close:t,onComplete:o,showAdvancedTPSLDialog:r})},Zr="TPSLSimpleSheetId",Xr="TPSLSimpleDialogId";registerSimpleSheet(Zr,vt,{classNames:{}});registerSimpleDialog(Xr,vt,{classNames:{content:"oui-w-[420px]"}});function Yn(e){let t=e.child_orders[0],o=t.algo_type===AlgoOrderRootType.TP_SL?PositionType.PARTIAL:PositionType.FULL,r=t.child_orders.find(n=>n.algo_type===AlgoOrderType.TAKE_PROFIT&&n.trigger_price),s=t.child_orders.find(n=>n.algo_type===AlgoOrderType.STOP_LOSS&&n.trigger_price),i={};return r&&(i.tp_trigger_price=r.trigger_price?.toString(),i.tp_order_type=typeof r.type=="string"?r.type.replace("_ORDER",""):void 0,i.tp_order_type===OrderType.LIMIT&&(i.tp_order_price=r.price?.toString())),s&&(i.sl_trigger_price=s.trigger_price?.toString(),i.sl_order_type=typeof s.type=="string"?s.type.replace("_ORDER",""):void 0,i.sl_order_type===OrderType.LIMIT&&(i.sl_order_price=s.price?.toString())),{baseInfo:{symbol:e.symbol,order_type:e.type,side:e.side,order_price:e.price,order_quantity:e.quantity,position_type:o,tp_enable:!!r?.trigger_price,sl_enable:!!s?.trigger_price},tpslPriceInfo:i,tpInfo:{orderId:r?.algo_order_id},slInfo:{orderId:s?.algo_order_id}}}function Xe(e,t){if(t==null)return true;if(isNaN(Number(t)))return false;let o=new Decimal(Number(e));return !new Decimal(Number(t)).eq(o)}var to=e=>{if(!e.order)throw new SDKError("order is required for editBracketOrder");let{baseInfo:t,tpslPriceInfo:o,tpInfo:r,slInfo:s}=Yn(e.order),[i,{isMutating:n}]=useMutation("/v1/algo/order","PUT"),{formattedOrder:a,setValue:l,setValues:m,metaState:f,symbolInfo:u,helper:d}=useOrderEntry(e.order.symbol,{initialOrder:t}),g=e.order.symbol,p=useMemo(()=>{let c=false,{tp_order_price:P,sl_order_price:N,tp_trigger_price:O,sl_trigger_price:w}=a;return o.tp_trigger_price&&(c=c||Xe(o.tp_trigger_price,O??0)),o.tp_order_price&&(c=c||Xe(o.tp_order_price,P??0)),o.sl_trigger_price&&(c=c||Xe(o.sl_trigger_price,w??0)),o.sl_order_price&&(c=c||Xe(o.sl_order_price,N??0)),c},[o,a.tp_order_price,a.sl_order_price,a.tp_trigger_price,a.sl_trigger_price]);return useEffect(()=>{m({...o});},[e.order,m]),{symbol:g,symbolInfo:u,formattedOrder:a,setValue:l,setValues:m,metaState:f,onSubmit:async()=>d.validate().then(()=>{let c={order_id:r.orderId,algo_type:AlgoOrderType.TAKE_PROFIT,trigger_price:a.tp_trigger_price,reduce_only:true};a.tp_order_type===OrderType.LIMIT&&(c.price=a.tp_order_price);let P={order_id:s.orderId,algo_type:AlgoOrderType.STOP_LOSS,trigger_price:a.sl_trigger_price,reduce_only:true};a.sl_order_type===OrderType.LIMIT&&(P.price=a.sl_order_price);let N=[];return r.orderId&&N.push(c),s.orderId&&N.push(P),i({order_id:e.order.algo_order_id,child_orders:[{order_id:e.order.child_orders[0].algo_order_id,child_orders:N}]})}),isMutating:n,isPriceChanged:p}};var so=e=>{let{t}=useTranslation(),{errors:o,validated:r}=e.metaState,{isMobile:s}=useScreen(),{formattedOrder:i,setValue:n,symbol:a,symbolInfo:l,setValues:m}=e,[f,u]=useState({enable:true,order_type:i.tp_order_type??OrderType.MARKET,order_price:i.tp_order_price??"",trigger_price:i.tp_trigger_price??"",PnL:i.tp_pnl??"",Offset:i.tp_offset??"","Offset%":i.tp_offset_percentage??"",ROI:i.tp_ROI??""}),[d,g]=useState({enable:true,order_type:i.sl_order_type??OrderType.MARKET,order_price:i.sl_order_price??"",trigger_price:i.sl_trigger_price??"",PnL:i.sl_pnl??"",Offset:i.sl_offset??"","Offset%":i.sl_offset_percentage??"",ROI:i.sl_ROI??""});return useEffect(()=>{u(p=>({...p,enable:i.tp_enable??true,order_type:i.tp_order_type??OrderType.MARKET,order_price:i.tp_order_price??"",trigger_price:i.tp_trigger_price??"",PnL:i.tp_pnl??"",Offset:i.tp_offset??"","Offset%":i.tp_offset_percentage??"",ROI:i.tp_ROI??""}));},[i]),useEffect(()=>{g(p=>({...p,enable:i.sl_enable??true,order_type:i.sl_order_type??OrderType.MARKET,order_price:i.sl_order_price??"",trigger_price:i.sl_trigger_price??"",PnL:i.sl_pnl??"",Offset:i.sl_offset??"","Offset%":i.sl_offset_percentage??"",ROI:i.sl_ROI??""}));},[i]),jsxs("div",{children:[jsxs(ScrollArea,{className:cn("oui-flex-1",s&&"oui-h-[calc(100vh-200px)]"),children:[jsx("div",{className:"",children:jsx(H,{order:{symbol:a,order_quantity:i.order_quantity,order_price:i.order_price},baseDP:l.base_dp,quoteDP:l.quote_dp})}),jsx(Divider,{className:"oui-my-3"}),jsxs("div",{className:"",children:[jsxs("div",{className:"oui-py-3",children:[jsx(Y,{value:i.position_type??PositionType.PARTIAL,disableSelector:true,onChange:(p,y)=>{if(y===PositionType.FULL){m({position_type:PositionType.FULL,tp_order_type:OrderType.MARKET,tp_order_price:void 0,sl_order_type:OrderType.MARKET,sl_order_price:void 0});return}n("position_type",y);}}),i.position_type===PositionType.FULL&&jsxs(Flex,{justify:"start",itemAlign:"start",gap:2,className:"oui-mt-3 oui-w-full",children:[jsx("div",{className:"oui-relative oui-top-[7px] oui-size-1 oui-rounded-full oui-bg-[#D25f00]"}),jsx(Text,{className:"oui-text-2xs oui-text-[#D25f00]",children:t("tpsl.positionType.full.tips.market")})]})]}),jsxs(Flex,{direction:"column",gap:6,children:[i.tp_enable&&jsx($,{disableEnableCheckbox:true,disableOrderTypeSelector:true,rootOrderPrice:i.order_price,symbol:l.symbol,type:"tp",side:i.side,values:f,errors:r?o:null,quote_dp:l.quote_dp,hideOrderPrice:i.position_type===PositionType.FULL,onChange:(p,y)=>{n(p,y);},positionType:i.position_type??PositionType.PARTIAL}),i.sl_enable&&i.tp_enable&&jsx(Divider,{className:"oui-w-full"}),i.sl_enable&&jsx($,{disableEnableCheckbox:true,disableOrderTypeSelector:true,rootOrderPrice:i.order_price,symbol:l.symbol,type:"sl",side:i.side,values:d,hideOrderPrice:i.position_type===PositionType.FULL,errors:r?o:null,quote_dp:l.quote_dp,positionType:i.position_type??PositionType.PARTIAL,onChange:(p,y)=>{n(p,y);}})]}),jsx(pe,{tp_pnl:i.tp_pnl,sl_pnl:i.sl_pnl,className:"oui-mt-6"})]})]}),jsx(Flex,{itemAlign:"center",gap:3,mt:5,width:"100%",justify:"center",children:jsx(ThrottledButton,{className:"oui-w-[184px]","data-testid":"tpsl-confirm",disabled:!e.isPriceChanged,loading:e.isMutating,onClick:()=>{e.onSubmit().then(()=>{e.onClose?.();}).catch(p=>{});},children:t("common.confirm")})})]})};var no=e=>{let t=to({order:e.order});return jsx(so,{...t,onClose:e.close})},sl="EditBracketOrderSheetId",nl="EditBracketOrderDialogId";registerSimpleSheet(sl,no);registerSimpleDialog(nl,no,{classNames:{content:"oui-w-[420px]"}});
12
+ // src/editorPopover.tsx
13
+ var OrderInfo = (props) => {
14
+ const { t } = useTranslation();
15
+ const { order: order2, symbolLeverage } = props;
16
+ const { symbol } = order2;
17
+ const markPrice = useMarkPrice(symbol);
18
+ const indexPrice = useIndexPrice(symbol);
19
+ const leverage = useLeverageBySymbol(symbolLeverage ? void 0 : symbol);
20
+ const currentLeverage = symbolLeverage || leverage;
21
+ return /* @__PURE__ */ jsxs(
22
+ Flex,
23
+ {
24
+ direction: "column",
25
+ itemAlign: "start",
26
+ className: cn(
27
+ "oui-w-full oui-gap-3 oui-font-semibold ",
28
+ props.classNames?.root
29
+ ),
30
+ children: [
31
+ /* @__PURE__ */ jsxs(
32
+ Flex,
33
+ {
34
+ itemAlign: "center",
35
+ className: cn("oui-gap-2 ", props.classNames?.symbol),
36
+ children: [
37
+ /* @__PURE__ */ jsxs(Flex, { gap: 1, itemAlign: "center", children: [
38
+ /* @__PURE__ */ jsx(TokenIcon, { symbol, className: "oui-size-5" }),
39
+ /* @__PURE__ */ jsx(
40
+ Text.formatted,
41
+ {
42
+ className: "oui-whitespace-nowrap oui-break-normal",
43
+ rule: "symbol",
44
+ formatString: "base-type",
45
+ size: "sm",
46
+ weight: "semibold",
47
+ intensity: 98,
48
+ children: symbol
49
+ }
50
+ )
51
+ ] }),
52
+ /* @__PURE__ */ jsxs(
53
+ Text,
54
+ {
55
+ size: "2xs",
56
+ className: "oui-h-[18px] oui-rounded oui-bg-base-7 oui-px-2 oui-font-semibold oui-text-base-contrast-36",
57
+ children: [
58
+ currentLeverage,
59
+ "x"
60
+ ]
61
+ }
62
+ )
63
+ ]
64
+ }
65
+ ),
66
+ /* @__PURE__ */ jsxs(
67
+ Grid,
68
+ {
69
+ cols: 2,
70
+ gapX: 2,
71
+ gapY: 1,
72
+ className: cn("oui-w-full oui-gap-x-2 ", props.classNames?.container),
73
+ children: [
74
+ /* @__PURE__ */ jsxs(Flex, { justify: "between", className: " oui-text-base-contrast-36", children: [
75
+ /* @__PURE__ */ jsx(Text, { size: "2xs", children: t("common.quantity") }),
76
+ /* @__PURE__ */ jsx(
77
+ Text.numeral,
78
+ {
79
+ rule: "price",
80
+ className: "oui-text-base-contrast-80",
81
+ size: "2xs",
82
+ dp: props.baseDP ?? 2,
83
+ children: Number(order2.order_quantity)
84
+ }
85
+ )
86
+ ] }),
87
+ /* @__PURE__ */ jsxs(Flex, { justify: "between", className: "oui-text-base-contrast-36", children: [
88
+ /* @__PURE__ */ jsx(Text, { size: "2xs", children: t("common.lastPrice") }),
89
+ /* @__PURE__ */ jsx(
90
+ Text.numeral,
91
+ {
92
+ rule: "price",
93
+ className: "oui-text-base-contrast-80",
94
+ size: "2xs",
95
+ dp: props.quoteDP ?? 2,
96
+ children: indexPrice?.data
97
+ }
98
+ )
99
+ ] }),
100
+ /* @__PURE__ */ jsxs(Flex, { justify: "between", className: "oui-text-base-contrast-36", children: [
101
+ /* @__PURE__ */ jsx(Text, { size: "2xs", children: t("common.orderPrice") }),
102
+ /* @__PURE__ */ jsx(
103
+ Text.numeral,
104
+ {
105
+ rule: "price",
106
+ className: "oui-text-base-contrast-80",
107
+ size: "2xs",
108
+ dp: props.quoteDP ?? 2,
109
+ children: Number(order2.order_price)
110
+ }
111
+ )
112
+ ] }),
113
+ /* @__PURE__ */ jsxs(Flex, { justify: "between", className: "oui-text-base-contrast-36", children: [
114
+ /* @__PURE__ */ jsx(Text, { size: "2xs", children: t("common.markPrice") }),
115
+ /* @__PURE__ */ jsx(
116
+ Text.numeral,
117
+ {
118
+ rule: "price",
119
+ className: "oui-text-base-contrast-80",
120
+ size: "2xs",
121
+ dp: props.quoteDP ?? 2,
122
+ children: markPrice?.data
123
+ }
124
+ )
125
+ ] }),
126
+ /* @__PURE__ */ jsxs(Flex, { justify: "between", className: "oui-text-base-contrast-36", children: [
127
+ /* @__PURE__ */ jsx(Text, { size: "2xs", children: t("positions.column.liqPrice") }),
128
+ /* @__PURE__ */ jsx(
129
+ Text.numeral,
130
+ {
131
+ rule: "price",
132
+ className: "oui-text-warning",
133
+ size: "2xs",
134
+ dp: props.quoteDP ?? 2,
135
+ children: props.estLiqPrice ?? "--"
136
+ }
137
+ )
138
+ ] })
139
+ ]
140
+ }
141
+ )
142
+ ]
143
+ }
144
+ );
145
+ };
146
+ var PnlInfo = (props) => {
147
+ const { t } = useTranslation();
148
+ const { tp_pnl, sl_pnl } = props;
149
+ const riskRatio = useMemo(() => {
150
+ const defaultNode = /* @__PURE__ */ jsx(Text, { className: "oui-text-base-contrast-36", children: "-- x" });
151
+ if (tp_pnl === void 0 || sl_pnl === void 0) {
152
+ return defaultNode;
153
+ }
154
+ if (checkIsNaN(tp_pnl) || checkIsNaN(sl_pnl)) {
155
+ return defaultNode;
156
+ }
157
+ const tpDecimal = new Decimal(tp_pnl);
158
+ const slDecimal = new Decimal(sl_pnl);
159
+ if (slDecimal.isZero() || tpDecimal.isZero()) {
160
+ return defaultNode;
161
+ }
162
+ const ratio = tpDecimal.div(slDecimal).abs().toNumber().toFixed(2);
163
+ return /* @__PURE__ */ jsxs(Flex, { gap: 1, itemAlign: "center", className: "oui-text-base-contrast-80", children: [
164
+ /* @__PURE__ */ jsx(Text, { children: ratio }),
165
+ /* @__PURE__ */ jsx(Text, { className: "oui-text-base-contrast-36", children: "x" })
166
+ ] });
167
+ }, [tp_pnl, sl_pnl]);
168
+ return /* @__PURE__ */ jsxs(
169
+ Flex,
170
+ {
171
+ direction: "column",
172
+ itemAlign: "start",
173
+ className: cn(
174
+ "oui-w-full oui-gap-1 oui-text-2xs oui-text-base-contrast-36",
175
+ props.className
176
+ ),
177
+ children: [
178
+ /* @__PURE__ */ jsxs(Flex, { justify: "between", className: "oui-w-full", children: [
179
+ /* @__PURE__ */ jsx(Text, { size: "2xs", children: t("tpsl.totalEstTpPnl") }),
180
+ /* @__PURE__ */ jsx(
181
+ Text.numeral,
182
+ {
183
+ suffix: /* @__PURE__ */ jsx(Text, { className: "oui-text-base-contrast-36 oui-ml-1", children: "USDC" }),
184
+ rule: "price",
185
+ coloring: true,
186
+ visible: true,
187
+ size: "2xs",
188
+ dp: 2,
189
+ children: tp_pnl ? Number(tp_pnl) : "--"
190
+ }
191
+ )
192
+ ] }),
193
+ /* @__PURE__ */ jsxs(Flex, { justify: "between", className: "oui-w-full", children: [
194
+ /* @__PURE__ */ jsx(Text, { size: "2xs", children: t("tpsl.totalEstSlPnl") }),
195
+ sl_pnl ? /* @__PURE__ */ jsx(
196
+ Text.numeral,
197
+ {
198
+ suffix: /* @__PURE__ */ jsx(Text, { className: "oui-text-base-contrast-36 oui-ml-1", children: "USDC" }),
199
+ coloring: true,
200
+ visible: true,
201
+ size: "2xs",
202
+ dp: 2,
203
+ children: Number(sl_pnl)
204
+ }
205
+ ) : /* @__PURE__ */ jsx(Text, { size: "2xs", children: "-- USDC" })
206
+ ] }),
207
+ /* @__PURE__ */ jsxs(Flex, { justify: "between", className: "oui-w-full", children: [
208
+ /* @__PURE__ */ jsx(Text, { size: "2xs", children: t("tpsl.riskRewardRatio") }),
209
+ /* @__PURE__ */ jsx(Text, { className: "oui-text-base-contrast-80", size: "2xs", children: riskRatio })
210
+ ] })
211
+ ]
212
+ }
213
+ );
214
+ };
215
+ var useTPSLInputRowScript = (props) => {
216
+ const { values, side, type, rootOrderPrice } = props;
217
+ const symbolLeverage = useLeverageBySymbol(
218
+ props.symbolLeverage ? void 0 : props.symbol
219
+ );
220
+ const leverage = props.symbolLeverage || symbolLeverage;
221
+ const roi = useMemo(() => {
222
+ if (!leverage || !rootOrderPrice || Number(rootOrderPrice) === 0) {
223
+ return null;
224
+ }
225
+ let closePrice;
226
+ if (values.order_type === OrderType.MARKET) {
227
+ closePrice = values.trigger_price;
228
+ } else if (values.order_type === OrderType.LIMIT) {
229
+ closePrice = values.order_price;
230
+ }
231
+ if (!closePrice) {
232
+ return null;
233
+ }
234
+ try {
235
+ const roi2 = order.tpslROI({
236
+ side,
237
+ type,
238
+ closePrice: Number(closePrice),
239
+ orderPrice: Number(rootOrderPrice),
240
+ leverage
241
+ });
242
+ return roi2 * 100;
243
+ } catch (error) {
244
+ return null;
245
+ }
246
+ }, [values, leverage, rootOrderPrice, type, side]);
247
+ return {
248
+ ...props,
249
+ roi
250
+ };
251
+ };
252
+ var usePNLInputBuilder = (props) => {
253
+ const { type, values } = props;
254
+ const [mode, setMode] = useLocalStorage(
255
+ "TP/SL_Mode",
256
+ "Offset%" /* PERCENTAGE */
257
+ );
258
+ const [focus, setFocus] = useState(true);
259
+ const { t } = useTranslation();
260
+ const key = useMemo(() => {
261
+ switch (mode) {
262
+ case "Offset" /* OFFSET */:
263
+ return `${type.toLowerCase()}_offset`;
264
+ case "Offset%" /* PERCENTAGE */:
265
+ return `${type.toLowerCase()}_offset_percentage`;
266
+ default:
267
+ return `${type.toLowerCase()}_pnl`;
268
+ }
269
+ }, [mode]);
270
+ const value = useMemo(() => {
271
+ return values[mode];
272
+ }, [values, mode]);
273
+ const modes = useMemo(() => {
274
+ return [
275
+ {
276
+ label: t("tpsl.pnl"),
277
+ value: "PnL" /* PnL */,
278
+ testId: `${"PnL" /* PnL */}_menu_item`
279
+ },
280
+ {
281
+ label: t("tpsl.offset"),
282
+ value: "Offset" /* OFFSET */,
283
+ testId: `${"Offset" /* OFFSET */}_mneu_item`
284
+ },
285
+ {
286
+ label: `${t("tpsl.offset")}%`,
287
+ value: "Offset%" /* PERCENTAGE */,
288
+ testId: `${"Offset%" /* PERCENTAGE */}_menu_item`
289
+ }
290
+ ];
291
+ }, [t]);
292
+ const percentageSuffix = useRef("");
293
+ const onValueChange = (value2) => {
294
+ props.onChange(key, value2);
295
+ };
296
+ const formatter = (options) => {
297
+ const { dp = 2 } = options;
298
+ return {
299
+ onRenderBefore: (value2, options2) => {
300
+ value2 = `${value2}`;
301
+ if (value2 === "" || value2 === "-")
302
+ return "";
303
+ if (mode === "Offset%" /* PERCENTAGE */) {
304
+ return `${new Decimal(
305
+ value2.replace(
306
+ new RegExp(percentageSuffix.current.replace(".", "\\.") + "$"),
307
+ ""
308
+ )
309
+ ).mul(100).todp(2, 4).toString()}${percentageSuffix.current}`;
310
+ } else if (mode === "Offset" /* OFFSET */) {
311
+ value2 = todpIfNeed(value2, dp);
312
+ } else ;
313
+ return `${value2}`;
314
+ },
315
+ onSendBefore: (value2) => {
316
+ if (/^\-?0{2,}$/.test(value2)) {
317
+ return "0";
318
+ }
319
+ if (mode === "Offset%" /* PERCENTAGE */) {
320
+ if (value2 !== "") {
321
+ value2 = todpIfNeed(value2, 2);
322
+ const endStr = value2.match(/\.0{0,2}$/);
323
+ if (!!endStr) {
324
+ percentageSuffix.current = endStr[0];
325
+ } else {
326
+ percentageSuffix.current = "";
327
+ }
328
+ value2 = new Decimal(value2).div(100).toString();
329
+ value2 = `${value2}${percentageSuffix.current}`;
330
+ }
331
+ } else if (mode === "PnL" /* PnL */ && type === "SL" && focus) {
332
+ value2 = value2.startsWith("-") ? value2 : "-" + value2;
333
+ } else {
334
+ value2 = todpIfNeed(value2, dp);
335
+ }
336
+ if (value2 === "" || value2 === "-")
337
+ return "";
338
+ return value2;
339
+ }
340
+ };
341
+ };
342
+ return {
343
+ mode,
344
+ modes,
345
+ type: props.type,
346
+ formatter,
347
+ onModeChange: (mode2) => {
348
+ setMode(mode2);
349
+ },
350
+ value,
351
+ pnl: values["PnL" /* PnL */],
352
+ onValueChange,
353
+ quote_dp: props.quote_dp,
354
+ setFocus
355
+ };
356
+ };
357
+ var PNLInput = (props) => {
358
+ const {
359
+ mode,
360
+ modes,
361
+ onModeChange,
362
+ onValueChange,
363
+ quote,
364
+ quote_dp,
365
+ value,
366
+ pnl
367
+ } = props;
368
+ const { t } = useTranslation();
369
+ const [prefix, setPrefix] = useState(mode);
370
+ const [placeholder, setPlaceholder] = useState(
371
+ mode === "Offset%" /* PERCENTAGE */ ? "%" : quote
372
+ );
373
+ const color = useMemo(() => {
374
+ const num = Number(pnl);
375
+ if (isNaN(num) || num === 0)
376
+ return "";
377
+ if (num > 0)
378
+ return "oui-text-trade-profit";
379
+ if (num < 0)
380
+ return "oui-text-trade-loss";
381
+ }, [pnl]);
382
+ useEffect(() => {
383
+ const label = modes.find((item) => item.value === mode)?.label;
384
+ setPrefix(label);
385
+ setPlaceholder(mode === "Offset%" /* PERCENTAGE */ ? "%" : quote);
386
+ }, [mode, modes]);
387
+ return /* @__PURE__ */ jsx(
388
+ Input,
389
+ {
390
+ prefix,
391
+ size: {
392
+ initial: "lg",
393
+ lg: "md"
394
+ },
395
+ placeholder,
396
+ align: "right",
397
+ value,
398
+ "data-testid": props.testId,
399
+ autoComplete: "off",
400
+ onValueChange,
401
+ formatters: [
402
+ // inputFormatter.numberFormatter,
403
+ props.formatter({ dp: quote_dp, mode }),
404
+ inputFormatter.currencyFormatter,
405
+ inputFormatter.decimalPointFormatter
406
+ ],
407
+ classNames: {
408
+ input: cn("oui-text-2xs", color),
409
+ prefix: "oui-text-base-contrast-54 oui-text-2xs",
410
+ root: "oui-outline-line-12 focus-within:oui-outline-primary-light"
411
+ },
412
+ onFocus: () => {
413
+ setPlaceholder("");
414
+ props.setFocus(true);
415
+ },
416
+ onBlur: () => {
417
+ setPlaceholder(mode === "Offset%" /* PERCENTAGE */ ? "%" : quote);
418
+ props.setFocus(false);
419
+ },
420
+ suffix: /* @__PURE__ */ jsxs(Fragment, { children: [
421
+ mode === "Offset%" /* PERCENTAGE */ && !!value && /* @__PURE__ */ jsx(
422
+ Text,
423
+ {
424
+ size: "2xs",
425
+ color: "inherit",
426
+ className: cn("oui-ml-[2px]", color),
427
+ children: "%"
428
+ }
429
+ ),
430
+ /* @__PURE__ */ jsx(
431
+ PNLMenus,
432
+ {
433
+ mode,
434
+ modes,
435
+ onModeChange: (item) => onModeChange(item.value)
436
+ }
437
+ )
438
+ ] })
439
+ }
440
+ );
441
+ };
442
+ var PNLMenus = (props) => {
443
+ return /* @__PURE__ */ jsx(
444
+ SimpleDropdownMenu,
445
+ {
446
+ currentValue: props.mode,
447
+ menu: props.modes,
448
+ align: "end",
449
+ size: "xs",
450
+ className: "oui-min-w-[80px]",
451
+ onSelect: (item) => props.onModeChange(item),
452
+ children: /* @__PURE__ */ jsx("button", { className: "oui-p-2", children: /* @__PURE__ */ jsx(CaretDownIcon, { size: 12, color: "white" }) })
453
+ }
454
+ );
455
+ };
456
+ var PnlInputWidget = (props) => {
457
+ const { testId, quote, ...rest } = props;
458
+ const state = usePNLInputBuilder(rest);
459
+ return /* @__PURE__ */ jsx(PNLInput, { ...state, testId, quote });
460
+ };
461
+ var OrderPriceType = (props) => {
462
+ const { t } = useTranslation();
463
+ const options = [
464
+ { label: t("orderEntry.orderType.limitOrder"), value: OrderType.LIMIT },
465
+ { label: t("orderEntry.orderType.marketOrder"), value: OrderType.MARKET }
466
+ ];
467
+ return /* @__PURE__ */ jsx(
468
+ Select.options,
469
+ {
470
+ value: props.type,
471
+ options,
472
+ disabled: props.disabled,
473
+ onValueChange: props.onChange,
474
+ size: "xs",
475
+ classNames: {
476
+ trigger: "oui-bg-transparent oui-w-auto oui-outline-line-1 oui-input-root oui-bg-base-6 oui-h-10 lg:oui-h-8 oui-outline-line-12 "
477
+ },
478
+ valueFormatter: (value, option) => {
479
+ const displayLabel = {
480
+ [OrderType.LIMIT]: t("orderEntry.orderType.limit"),
481
+ [OrderType.MARKET]: t("common.marketPrice")
482
+ }[value];
483
+ return /* @__PURE__ */ jsx(Text, { size: "2xs", children: displayLabel });
484
+ }
485
+ }
486
+ );
487
+ };
488
+ var PriceInput = (props) => {
489
+ const [placeholder, setPlaceholder] = useState("USDC");
490
+ const { t } = useTranslation();
491
+ return /* @__PURE__ */ jsx(
492
+ Input.tooltip,
493
+ {
494
+ "data-testid": `oui-testid-tpsl-popUp-${props.type.toLowerCase()}-input`,
495
+ prefix: props.label ?? t("common.markPrice"),
496
+ size: { initial: "lg", lg: "md" },
497
+ tooltip: props.error,
498
+ placeholder,
499
+ disabled: props.disabled,
500
+ align: "right",
501
+ autoComplete: "off",
502
+ inputMode: "decimal",
503
+ value: props.value,
504
+ color: props.error ? "danger" : void 0,
505
+ classNames: {
506
+ input: cn(
507
+ "oui-text-2xs placeholder:oui-text-2xs",
508
+ props.classNames?.input
509
+ ),
510
+ prefix: cn(
511
+ "oui-text-base-contrast-54 oui-text-2xs",
512
+ props.classNames?.prefix
513
+ ),
514
+ root: cn("oui-w-full", props.classNames?.root)
515
+ },
516
+ onValueChange: props.onValueChange,
517
+ onFocus: () => {
518
+ setPlaceholder("");
519
+ },
520
+ onBlur: () => {
521
+ setPlaceholder("USDC");
522
+ },
523
+ formatters: [
524
+ inputFormatter.numberFormatter,
525
+ inputFormatter.dpFormatter(props.quote_dp),
526
+ inputFormatter.currencyFormatter,
527
+ inputFormatter.decimalPointFormatter
528
+ ]
529
+ }
530
+ );
531
+ };
532
+ var TPSLInputRowUI = (props) => {
533
+ const { t } = useTranslation();
534
+ const { getErrorMsg } = useOrderEntryFormErrorMsg(props.errors);
535
+ const { values, positionType } = props;
536
+ return /* @__PURE__ */ jsxs(
537
+ Flex,
538
+ {
539
+ direction: "column",
540
+ itemAlign: "start",
541
+ justify: "start",
542
+ className: "oui-w-full",
543
+ children: [
544
+ /* @__PURE__ */ jsx(Text, { size: "sm", intensity: 98, children: props.type === "tp" ? t("tpsl.takeProfit") : t("tpsl.stopLoss") }),
545
+ /* @__PURE__ */ jsxs(
546
+ Flex,
547
+ {
548
+ direction: "column",
549
+ gap: 2,
550
+ itemAlign: "start",
551
+ className: "oui-w-full oui-pt-2",
552
+ children: [
553
+ /* @__PURE__ */ jsxs(
554
+ Flex,
555
+ {
556
+ direction: "column",
557
+ itemAlign: "start",
558
+ className: "oui-w-full oui-gap-0.5",
559
+ children: [
560
+ /* @__PURE__ */ jsx(Text, { className: "oui-text-2xs oui-text-base-contrast-54", children: t("common.triggerPrice") }),
561
+ /* @__PURE__ */ jsxs(Grid, { cols: 2, gap: 2, className: "oui-w-full oui-px-0.5", children: [
562
+ /* @__PURE__ */ jsx(
563
+ PriceInput,
564
+ {
565
+ type: `${props.type} price`,
566
+ value: values.trigger_price,
567
+ error: getErrorMsg(`${props.type}_trigger_price`),
568
+ onValueChange: (value) => {
569
+ props.onChange(`${props.type}_trigger_price`, value);
570
+ },
571
+ quote_dp: props.quote_dp,
572
+ classNames: {
573
+ root: props.inputWarnNode ? "oui-outline-warning-darken focus-within:oui-outline-warning-darken" : void 0
574
+ }
575
+ }
576
+ ),
577
+ /* @__PURE__ */ jsx(
578
+ PnlInputWidget,
579
+ {
580
+ type: props.type === "tp" ? "TP" : "SL",
581
+ onChange: (key, value) => {
582
+ props.onChange(key, value);
583
+ },
584
+ quote: "USDC",
585
+ quote_dp: 2,
586
+ values
587
+ }
588
+ )
589
+ ] })
590
+ ]
591
+ }
592
+ ),
593
+ props.inputWarnNode,
594
+ /* @__PURE__ */ jsxs(
595
+ Flex,
596
+ {
597
+ direction: "column",
598
+ className: cn(
599
+ "oui-w-full oui-gap-0.5",
600
+ props.hideOrderPrice ? "oui-hidden" : ""
601
+ ),
602
+ itemAlign: "start",
603
+ children: [
604
+ /* @__PURE__ */ jsx(Text, { className: "oui-text-2xs oui-text-base-contrast-54", children: t("common.orderPrice") }),
605
+ /* @__PURE__ */ jsxs(Grid, { cols: 2, gap: 2, className: "oui-w-full oui-px-0.5", children: [
606
+ /* @__PURE__ */ jsx(
607
+ PriceInput,
608
+ {
609
+ disabled: positionType === PositionType.FULL || values.order_type === OrderType.MARKET,
610
+ type: "order price",
611
+ label: values.order_type === OrderType.LIMIT ? t("common.limit") : t("common.market"),
612
+ value: values.order_price,
613
+ error: getErrorMsg(`${props.type}_order_price`),
614
+ onValueChange: (value) => {
615
+ props.onChange(`${props.type}_order_price`, value);
616
+ },
617
+ quote_dp: props.quote_dp
618
+ }
619
+ ),
620
+ /* @__PURE__ */ jsx(
621
+ OrderPriceType,
622
+ {
623
+ disabled: positionType === PositionType.FULL || props.disableOrderTypeSelector,
624
+ type: values.order_type,
625
+ onChange: (value) => {
626
+ props.onChange(`${props.type}_order_type`, value);
627
+ }
628
+ }
629
+ )
630
+ ] })
631
+ ]
632
+ }
633
+ )
634
+ ]
635
+ }
636
+ ),
637
+ /* @__PURE__ */ jsx(
638
+ RenderROI,
639
+ {
640
+ price: values.order_type === OrderType.MARKET ? values.trigger_price : values.order_price,
641
+ orderType: values.order_type,
642
+ pnl: values.PnL,
643
+ roi: props.roi,
644
+ dp: props.quote_dp,
645
+ className: "oui-mt-1"
646
+ }
647
+ )
648
+ ]
649
+ }
650
+ );
651
+ };
652
+ var RenderROI = (props) => {
653
+ const { t } = useTranslation();
654
+ const { price, pnl, roi, dp, className, orderType } = props;
655
+ if (!roi || !price || !pnl) {
656
+ return null;
657
+ }
658
+ return /* @__PURE__ */ jsx(Text, { className: cn("oui-text-2xs oui-text-base-contrast-36", className), children: /* @__PURE__ */ jsx(
659
+ Trans,
660
+ {
661
+ i18nKey: "tpsl.advanced.ROI",
662
+ components: [
663
+ /* @__PURE__ */ jsx(Fragment$1, { children: /* @__PURE__ */ jsx(
664
+ Text.numeral,
665
+ {
666
+ className: "oui-px-1 oui-text-base-contrast",
667
+ dp,
668
+ suffix: /* @__PURE__ */ jsx(Text, { className: "oui-pl-0.5", children: "USDC" }),
669
+ children: price
670
+ }
671
+ ) }, "price"),
672
+ /* @__PURE__ */ jsx(Fragment$1, { children: /* @__PURE__ */ jsx(Text, { className: "oui-px-1 oui-text-base-contrast", children: orderType === OrderType.MARKET ? t("common.market") : t("common.limit") }) }, "orderType"),
673
+ /* @__PURE__ */ jsx(Fragment$1, { children: /* @__PURE__ */ jsx(
674
+ Text.numeral,
675
+ {
676
+ coloring: true,
677
+ className: "oui-whitespace-nowrap oui-px-1",
678
+ dp: 2,
679
+ suffix: /* @__PURE__ */ jsx(Text, { className: "oui-pl-0.5", children: "USDC" }),
680
+ children: pnl
681
+ }
682
+ ) }, "pnl"),
683
+ /* @__PURE__ */ jsx(Fragment$1, { children: /* @__PURE__ */ jsx(
684
+ Text.numeral,
685
+ {
686
+ coloring: true,
687
+ className: "oui-whitespace-nowrap oui-px-1",
688
+ dp: 2,
689
+ suffix: "%",
690
+ children: roi
691
+ }
692
+ ) }, "roi")
693
+ ]
694
+ }
695
+ ) });
696
+ };
697
+ var TPSLInputRowWidget = (props) => {
698
+ const state = useTPSLInputRowScript(props);
699
+ return /* @__PURE__ */ jsx(TPSLInputRowUI, { ...state });
700
+ };
13
701
 
14
- export { os as ArrowRightIcon, nl as EditBracketOrderDialogId, sl as EditBracketOrderSheetId, no as EditBracketOrderWidget, Ne as PositionTPSLConfirm, Hi as PositionTPSLPopover, Xi as PositionTPSLSheet, er as TPSL, us as TPSLAdvancedDialogId, ds as TPSLAdvancedSheetId, br as TPSLAdvancedUI, Ir as TPSLAdvancedWidget, bn as TPSLDetailDialogId, Sn as TPSLDetailSheetId, Hr as TPSLDetailWidget, fe as TPSLDialogId, Y as TPSLPositionTypeWidget, ze as TPSLSheetId, Xr as TPSLSimpleDialogId, Ct as TPSLSimpleDialogUI, vt as TPSLSimpleDialogWidget, Zr as TPSLSimpleSheetId, he as TPSLWidget, Or as useTPSLAdvanced, mr as useTPSLBuilder, ht as useTPSLSimpleDialog };
702
+ // src/components/tpslPostionType/tpslPositionType.script.tsx
703
+ var useTPSLPositionTypeScript = (props) => {
704
+ return {
705
+ value: props.value,
706
+ onChange: props.onChange,
707
+ disableSelector: props.disableSelector
708
+ };
709
+ };
710
+ var positionTypeKey = "position_type";
711
+ var TPSLPositionTypeUI = (props) => {
712
+ const { t } = useTranslation();
713
+ const options = [
714
+ {
715
+ label: t("tpsl.partialPosition"),
716
+ value: PositionType.PARTIAL
717
+ },
718
+ {
719
+ label: t("tpsl.fullPosition"),
720
+ value: PositionType.FULL
721
+ }
722
+ ];
723
+ return /* @__PURE__ */ jsxs(Flex, { gap: 1, itemAlign: "center", justify: "start", children: [
724
+ /* @__PURE__ */ jsx(
725
+ Tooltip,
726
+ {
727
+ className: "oui-w-[280px] oui-p-3",
728
+ content: props.value === PositionType.FULL ? t("tpsl.positionType.full.tips") : t("tpsl.positionType.partial.tips"),
729
+ children: /* @__PURE__ */ jsx(
730
+ ExclamationFillIcon,
731
+ {
732
+ className: "oui-cursor-pointer oui-text-base-contrast-54",
733
+ size: 12
734
+ }
735
+ )
736
+ }
737
+ ),
738
+ props.disableSelector ? props.value === PositionType.FULL ? /* @__PURE__ */ jsx(Text, { className: "oui-text-2xs oui-font-normal oui-text-base-contrast-54", children: t("tpsl.positionType.full") }) : /* @__PURE__ */ jsx(Text, { className: "oui-text-2xs oui-font-normal oui-text-base-contrast-54", children: t("tpsl.positionType.partial") }) : /* @__PURE__ */ jsx(
739
+ Select.options,
740
+ {
741
+ value: props.value,
742
+ options,
743
+ onValueChange: (event) => {
744
+ props.onChange(positionTypeKey, event);
745
+ },
746
+ size: "xs",
747
+ classNames: {
748
+ trigger: " oui-bg-transparent oui-border-0 oui-w-auto oui-px-0 oui-font-normal"
749
+ },
750
+ contentProps: {
751
+ className: " oui-bg-base-8 oui-border-0"
752
+ }
753
+ }
754
+ )
755
+ ] });
756
+ };
757
+ var TPSLPositionTypeWidget = (props) => {
758
+ const state = useTPSLPositionTypeScript(props);
759
+ return /* @__PURE__ */ jsx(TPSLPositionTypeUI, { ...state });
760
+ };
761
+ var TPSLQuantity = memo((props) => {
762
+ const { maxQty, base_dp, baseTick, quantity } = props;
763
+ const inputRef = useRef(null);
764
+ const { t } = useTranslation();
765
+ const [sliderValue, setSliderValue] = useState(0);
766
+ const formatQuantity = (value) => {
767
+ let _qty = value;
768
+ if (Number(value) > maxQty) {
769
+ _qty = maxQty.toString();
770
+ }
771
+ if (baseTick > 0) {
772
+ props.onQuantityChange?.(utils.formatNumber(_qty, baseTick) ?? value);
773
+ }
774
+ };
775
+ const onSliderValueChange = (value) => {
776
+ setSliderValue(value);
777
+ const qty = new Decimal(value).div(100).mul(maxQty).toFixed(base_dp, Decimal.ROUND_DOWN);
778
+ formatQuantity(qty);
779
+ };
780
+ useEffect(() => {
781
+ const qty = Math.min(Number(quantity || 0), maxQty);
782
+ const slider = new Decimal(qty).div(maxQty).mul(100).toDecimalPlaces(2, Decimal.ROUND_DOWN).toNumber();
783
+ setSliderValue(slider);
784
+ }, [quantity, maxQty]);
785
+ const errorMsg = props.quantity.toString().length > 0 ? props.errorMsg : void 0;
786
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
787
+ /* @__PURE__ */ jsx(Flex, { gap: 2, children: /* @__PURE__ */ jsx("div", { className: "oui-flex-1", children: /* @__PURE__ */ jsx(
788
+ Input.tooltip,
789
+ {
790
+ "data-testid": "oui-testid-tpsl-popUp-quantity-input",
791
+ ref: inputRef,
792
+ prefix: t("common.quantity"),
793
+ size: {
794
+ initial: "md",
795
+ lg: "sm"
796
+ },
797
+ align: "right",
798
+ value: props.quantity,
799
+ autoComplete: "off",
800
+ inputMode: "decimal",
801
+ classNames: {
802
+ prefix: "oui-text-base-contrast-54",
803
+ root: cn(
804
+ "oui-bg-base-5 oui-outline-line-12",
805
+ errorMsg && "oui-outline-danger"
806
+ )
807
+ },
808
+ tooltipProps: {
809
+ content: {
810
+ className: "oui-bg-base-6 oui-text-base-contrast-80"
811
+ },
812
+ arrow: {
813
+ className: "oui-fill-base-6"
814
+ }
815
+ },
816
+ tooltip: errorMsg,
817
+ color: errorMsg ? "danger" : void 0,
818
+ formatters: [
819
+ inputFormatter.dpFormatter(props.base_dp),
820
+ inputFormatter.numberFormatter,
821
+ inputFormatter.currencyFormatter,
822
+ inputFormatter.decimalPointFormatter
823
+ ],
824
+ onValueChange: (value) => {
825
+ props.onQuantityChange?.(value);
826
+ const qty = Number(value);
827
+ if (qty && qty > props.maxQty) {
828
+ const qty2 = props.maxQty;
829
+ props.onQuantityChange?.(qty2);
830
+ inputRef.current?.blur();
831
+ }
832
+ },
833
+ onBlur: (e) => formatQuantity(e.target.value),
834
+ suffix: /* @__PURE__ */ jsx("span", { className: "oui-px-3 oui-text-2xs oui-text-base-contrast-54", children: props.base })
835
+ }
836
+ ) }) }),
837
+ /* @__PURE__ */ jsx(Flex, { mt: 2, itemAlign: "center", height: "15px", children: /* @__PURE__ */ jsx(
838
+ Slider,
839
+ {
840
+ min: 0,
841
+ max: 100,
842
+ markCount: 5,
843
+ showTip: true,
844
+ value: [sliderValue],
845
+ color: "primary",
846
+ onValueChange: (value) => {
847
+ onSliderValueChange(value[0]);
848
+ }
849
+ }
850
+ ) }),
851
+ /* @__PURE__ */ jsxs(Flex, { justify: "between", children: [
852
+ /* @__PURE__ */ jsx(Text.numeral, { color: "primary", size: "2xs", suffix: "%", children: sliderValue }),
853
+ /* @__PURE__ */ jsxs(Flex, { itemAlign: "center", gap: 1, children: [
854
+ /* @__PURE__ */ jsx(
855
+ "button",
856
+ {
857
+ className: "oui-leading-none",
858
+ style: { lineHeight: 0 },
859
+ onClick: () => {
860
+ props.onQuantityChange?.(props.maxQty);
861
+ },
862
+ children: /* @__PURE__ */ jsx(Text, { color: "primary", size: "2xs", children: t("common.max") })
863
+ }
864
+ ),
865
+ /* @__PURE__ */ jsx(
866
+ Text.numeral,
867
+ {
868
+ rule: "price",
869
+ size: "2xs",
870
+ intensity: 54,
871
+ tick: props.baseTick,
872
+ children: props.maxQty
873
+ }
874
+ )
875
+ ] })
876
+ ] })
877
+ ] });
878
+ });
879
+ var TPSL = (props) => {
880
+ const {
881
+ TPSL_OrderEntity,
882
+ symbolInfo,
883
+ onCancel,
884
+ onComplete,
885
+ status,
886
+ position,
887
+ setValues,
888
+ isEditing
889
+ } = props;
890
+ const { errors, validated } = props.metaState;
891
+ const { t } = useTranslation();
892
+ const { isMobile } = useScreen();
893
+ const tpErrors = useMemo(() => {
894
+ if (!errors)
895
+ return null;
896
+ const { sl_trigger_price, ...rest } = errors;
897
+ return rest;
898
+ }, [errors]);
899
+ const slErrors = useMemo(() => {
900
+ if (!errors)
901
+ return null;
902
+ const { tp_trigger_price, ...rest } = errors;
903
+ return rest;
904
+ }, [errors]);
905
+ const { getErrorMsg } = useOrderEntryFormErrorMsg(errors);
906
+ const { getErrorMsg: getSlPriceErrorMsg } = useOrderEntryFormErrorMsg(
907
+ props.slPriceError
908
+ );
909
+ const isSlPriceWarning = props.slPriceError?.sl_trigger_price?.type === ERROR_MSG_CODES.SL_PRICE_WARNING;
910
+ if (!position) {
911
+ return null;
912
+ }
913
+ const tpslEnable = TPSL_OrderEntity.tp_trigger_price || TPSL_OrderEntity.sl_trigger_price;
914
+ const renderQtyInput = () => {
915
+ if (TPSL_OrderEntity.position_type === PositionType.FULL) {
916
+ return null;
917
+ }
918
+ return /* @__PURE__ */ jsx(Box, { className: "oui-px-0.5", children: /* @__PURE__ */ jsx(
919
+ TPSLQuantity,
920
+ {
921
+ maxQty: props.maxQty,
922
+ quantity: props.orderQuantity ?? props.maxQty,
923
+ baseTick: symbolInfo("base_tick"),
924
+ base_dp: symbolInfo("base_dp"),
925
+ onQuantityChange: props.setQuantity,
926
+ base: symbolInfo("base"),
927
+ isEditing: props.isEditing,
928
+ errorMsg: validated ? getErrorMsg("quantity") : void 0
929
+ }
930
+ ) });
931
+ };
932
+ return /* @__PURE__ */ jsxs("div", { id: "orderly-tp_sl-order-edit-content", children: [
933
+ /* @__PURE__ */ jsx(ScrollArea, { className: cn(isMobile && "oui-h-[calc(100vh-200px)]"), children: /* @__PURE__ */ jsxs("div", { className: "oui-px-2", children: [
934
+ /* @__PURE__ */ jsx(
935
+ OrderInfo,
936
+ {
937
+ baseDP: symbolInfo("base_dp"),
938
+ quoteDP: symbolInfo("quote_dp"),
939
+ estLiqPrice: props.estLiqPrice,
940
+ classNames: {
941
+ root: "oui-mb-3",
942
+ container: "oui-gap-x-[30px]"
943
+ },
944
+ order: {
945
+ symbol: position.symbol,
946
+ order_quantity: position.position_qty.toString(),
947
+ order_price: position.average_open_price.toString()
948
+ },
949
+ symbolLeverage: position.leverage
950
+ }
951
+ ),
952
+ /* @__PURE__ */ jsxs(
953
+ Flex,
954
+ {
955
+ direction: "column",
956
+ justify: "start",
957
+ itemAlign: "start",
958
+ gap: 3,
959
+ className: "oui-mb-3 oui-w-full",
960
+ children: [
961
+ !isEditing && /* @__PURE__ */ jsx(
962
+ TPSLPositionTypeWidget,
963
+ {
964
+ disableSelector: true,
965
+ value: TPSL_OrderEntity.position_type ?? PositionType.PARTIAL,
966
+ onChange: (key, value) => {
967
+ props.setOrderValue(key, value);
968
+ }
969
+ }
970
+ ),
971
+ TPSL_OrderEntity.position_type === PositionType.FULL && /* @__PURE__ */ jsx(
972
+ DotStatus,
973
+ {
974
+ color: "warning",
975
+ size: "xs",
976
+ label: t("tpsl.positionType.full.tips.market")
977
+ }
978
+ )
979
+ ]
980
+ }
981
+ ),
982
+ renderQtyInput(),
983
+ /* @__PURE__ */ jsxs(
984
+ Flex,
985
+ {
986
+ direction: "column",
987
+ itemAlign: "start",
988
+ justify: "start",
989
+ gap: 6,
990
+ className: "oui-mt-3 oui-w-full",
991
+ children: [
992
+ /* @__PURE__ */ jsx(
993
+ TPSLInputRowWidget,
994
+ {
995
+ symbol: position.symbol,
996
+ rootOrderPrice: position.average_open_price.toString(),
997
+ type: "tp",
998
+ side: position.position_qty > 0 ? OrderSide.BUY : OrderSide.SELL,
999
+ values: {
1000
+ // enable: TPSL_OrderEntity.tp_enable ?? false,
1001
+ trigger_price: TPSL_OrderEntity.tp_trigger_price?.toString() ?? void 0,
1002
+ PnL: TPSL_OrderEntity.tp_pnl?.toString() ?? void 0,
1003
+ Offset: TPSL_OrderEntity.tp_offset?.toString() ?? void 0,
1004
+ "Offset%": TPSL_OrderEntity.tp_offset_percentage?.toString() ?? void 0,
1005
+ order_price: TPSL_OrderEntity.tp_order_price?.toString() ?? void 0,
1006
+ order_type: TPSL_OrderEntity.tp_order_type ?? OrderType.MARKET
1007
+ },
1008
+ hideOrderPrice: TPSL_OrderEntity.position_type === PositionType.FULL,
1009
+ errors: validated ? tpErrors : null,
1010
+ disableOrderTypeSelector: isEditing,
1011
+ quote_dp: symbolInfo("quote_dp"),
1012
+ positionType: TPSL_OrderEntity.position_type ?? PositionType.PARTIAL,
1013
+ onChange: (key, value) => {
1014
+ props.setOrderValue(key, value);
1015
+ },
1016
+ symbolLeverage: position.leverage
1017
+ }
1018
+ ),
1019
+ /* @__PURE__ */ jsx(
1020
+ TPSLInputRowWidget,
1021
+ {
1022
+ inputWarnNode: isSlPriceWarning && /* @__PURE__ */ jsx(
1023
+ DotStatus,
1024
+ {
1025
+ color: "warning",
1026
+ label: getSlPriceErrorMsg("sl_trigger_price"),
1027
+ classNames: {
1028
+ root: "oui-mt-1"
1029
+ }
1030
+ }
1031
+ ),
1032
+ symbol: position.symbol,
1033
+ rootOrderPrice: position.average_open_price.toString(),
1034
+ type: "sl",
1035
+ side: position.position_qty > 0 ? OrderSide.BUY : OrderSide.SELL,
1036
+ values: {
1037
+ // enable: TPSL_OrderEntity.sl_enable ?? false,
1038
+ trigger_price: TPSL_OrderEntity.sl_trigger_price?.toString() ?? void 0,
1039
+ PnL: TPSL_OrderEntity.sl_pnl?.toString() ?? void 0,
1040
+ Offset: TPSL_OrderEntity.sl_offset?.toString() ?? void 0,
1041
+ "Offset%": TPSL_OrderEntity.sl_offset_percentage?.toString() ?? void 0,
1042
+ order_price: TPSL_OrderEntity.sl_order_price?.toString() ?? void 0,
1043
+ order_type: TPSL_OrderEntity.sl_order_type ?? OrderType.MARKET
1044
+ },
1045
+ hideOrderPrice: TPSL_OrderEntity.position_type === PositionType.FULL,
1046
+ errors: validated ? slErrors : null,
1047
+ quote_dp: symbolInfo("quote_dp"),
1048
+ positionType: TPSL_OrderEntity.position_type ?? PositionType.PARTIAL,
1049
+ disableOrderTypeSelector: isEditing,
1050
+ onChange: (key, value) => {
1051
+ props.setOrderValue(key, value);
1052
+ },
1053
+ symbolLeverage: position.leverage
1054
+ }
1055
+ )
1056
+ ]
1057
+ }
1058
+ ),
1059
+ /* @__PURE__ */ jsx(
1060
+ PnlInfo,
1061
+ {
1062
+ tp_pnl: TPSL_OrderEntity.tp_pnl,
1063
+ sl_pnl: TPSL_OrderEntity.sl_pnl,
1064
+ className: "oui-my-3"
1065
+ }
1066
+ )
1067
+ ] }) }),
1068
+ /* @__PURE__ */ jsxs(Grid, { px: 2, cols: 2, gap: 3, mt: 4, children: [
1069
+ /* @__PURE__ */ jsx(
1070
+ Button,
1071
+ {
1072
+ size: "md",
1073
+ color: "secondary",
1074
+ "data-testid": "tpsl-cancel",
1075
+ onClick: () => {
1076
+ props.close?.();
1077
+ onCancel?.();
1078
+ },
1079
+ children: t("common.cancel")
1080
+ }
1081
+ ),
1082
+ /* @__PURE__ */ jsx(
1083
+ ThrottledButton,
1084
+ {
1085
+ size: "md",
1086
+ "data-testid": "tpsl-confirm",
1087
+ disabled: status.isCreateMutating || !tpslEnable,
1088
+ loading: status.isCreateMutating || status.isUpdateMutating,
1089
+ onClick: () => {
1090
+ props.onSubmit().then(() => {
1091
+ props.close?.();
1092
+ onComplete?.();
1093
+ }).catch((err) => {
1094
+ });
1095
+ },
1096
+ children: t("common.confirm")
1097
+ }
1098
+ )
1099
+ ] })
1100
+ ] });
1101
+ };
1102
+ var TPSLOrderType = (props) => {
1103
+ const { tpPrice, slPrice } = props;
1104
+ const { t } = useTranslation();
1105
+ if (!!tpPrice && !!slPrice) {
1106
+ return /* @__PURE__ */ jsx(Badge, { size: "xs", color: "neutral", children: t("common.tpsl") });
1107
+ }
1108
+ if (!!tpPrice) {
1109
+ return /* @__PURE__ */ jsx(Badge, { size: "xs", color: "neutral", children: t("tpsl.tp") });
1110
+ }
1111
+ if (!!slPrice) {
1112
+ return /* @__PURE__ */ jsx(Badge, { size: "xs", color: "neutral", children: t("tpsl.sl") });
1113
+ }
1114
+ return null;
1115
+ };
1116
+ var PositionTPSLConfirm = (props) => {
1117
+ const {
1118
+ symbol,
1119
+ tpPrice,
1120
+ slPrice,
1121
+ qty,
1122
+ maxQty,
1123
+ side,
1124
+ quoteDP,
1125
+ baseDP,
1126
+ isEditing,
1127
+ isPositionTPSL: _isPositionTPSL,
1128
+ orderInfo: order2
1129
+ } = props;
1130
+ const { t } = useTranslation();
1131
+ const [needConfirm, setNeedConfirm] = useLocalStorage(
1132
+ "orderly_order_confirm",
1133
+ true
1134
+ );
1135
+ const renderPositionType = () => {
1136
+ if (order2.position_type === PositionType.FULL) {
1137
+ return /* @__PURE__ */ jsx(Text, { children: t("tpsl.positionType.full") });
1138
+ }
1139
+ return /* @__PURE__ */ jsx(Text, { children: t("tpsl.positionType.partial") });
1140
+ };
1141
+ const renderTPSLPrice = ({
1142
+ price,
1143
+ isOrderPrice,
1144
+ isEnable,
1145
+ colorType
1146
+ }) => {
1147
+ if (!isEnable) {
1148
+ return /* @__PURE__ */ jsx(Text, { className: "oui-text-base-contrast-36", children: "-- USDC" });
1149
+ }
1150
+ if (!price) {
1151
+ if (isOrderPrice) {
1152
+ return /* @__PURE__ */ jsx(Text, { className: "oui-text-base-contrast-36", children: t("common.market") });
1153
+ }
1154
+ }
1155
+ return /* @__PURE__ */ jsx(
1156
+ Text.numeral,
1157
+ {
1158
+ unit: "USDC",
1159
+ rule: "price",
1160
+ className: cn(
1161
+ "oui-text-base-contrast",
1162
+ colorType === "TP" ? "oui-text-trade-profit" : "oui-text-trade-loss"
1163
+ ),
1164
+ unitClassName: "oui-text-base-contrast-36 oui-ml-1",
1165
+ dp: quoteDP,
1166
+ padding: false,
1167
+ children: price
1168
+ }
1169
+ );
1170
+ };
1171
+ const isPositionTPSL = _isPositionTPSL;
1172
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
1173
+ isEditing && /* @__PURE__ */ jsx(Text, { as: "div", size: "2xs", intensity: 80, className: "oui-mb-3", children: t("tpsl.agreement", { symbol: transSymbolformString(symbol) }) }),
1174
+ /* @__PURE__ */ jsxs(Flex, { pb: 4, children: [
1175
+ /* @__PURE__ */ jsx(Box, { grow: true, children: /* @__PURE__ */ jsx(
1176
+ Text.formatted,
1177
+ {
1178
+ rule: "symbol",
1179
+ formatString: "base-type",
1180
+ size: "base",
1181
+ showIcon: true,
1182
+ as: "div",
1183
+ intensity: 80,
1184
+ children: symbol
1185
+ }
1186
+ ) }),
1187
+ /* @__PURE__ */ jsxs(Flex, { gap: 1, children: [
1188
+ isPositionTPSL && /* @__PURE__ */ jsx(Badge, { size: "xs", color: "primary", children: t("common.position") }),
1189
+ /* @__PURE__ */ jsx(TPSLOrderType, { tpPrice, slPrice }),
1190
+ side === OrderSide.SELL ? /* @__PURE__ */ jsx(Badge, { size: "xs", color: "success", children: t("common.buy") }) : /* @__PURE__ */ jsx(Badge, { size: "xs", color: "danger", children: t("common.sell") })
1191
+ ] })
1192
+ ] }),
1193
+ /* @__PURE__ */ jsx(Divider, {}),
1194
+ order2.tp_trigger_price || order2.sl_trigger_price ? /* @__PURE__ */ jsxs(Fragment, { children: [
1195
+ /* @__PURE__ */ jsx(Divider, { className: "oui-my-4" }),
1196
+ /* @__PURE__ */ jsxs(
1197
+ "div",
1198
+ {
1199
+ className: textVariants({
1200
+ size: "sm",
1201
+ intensity: 54,
1202
+ className: "oui-space-y-1 oui-w-full oui-flex oui-flex-col oui-gap-3"
1203
+ }),
1204
+ children: [
1205
+ /* @__PURE__ */ jsx(Text, { className: "oui-text-base-contrast", children: renderPositionType() }),
1206
+ /* @__PURE__ */ jsxs(Flex, { justify: "between", children: [
1207
+ /* @__PURE__ */ jsx(Text, { children: t("common.orderQty") }),
1208
+ /* @__PURE__ */ jsx(
1209
+ Text.numeral,
1210
+ {
1211
+ rule: "price",
1212
+ dp: baseDP,
1213
+ padding: false,
1214
+ className: "oui-text-base-contrast",
1215
+ children: order2.quantity ?? "-"
1216
+ }
1217
+ )
1218
+ ] }),
1219
+ /* @__PURE__ */ jsxs(
1220
+ Flex,
1221
+ {
1222
+ direction: "column",
1223
+ justify: "between",
1224
+ itemAlign: "start",
1225
+ gap: 1,
1226
+ className: "oui-w-full",
1227
+ children: [
1228
+ /* @__PURE__ */ jsxs(Flex, { justify: "between", className: "oui-w-full", children: [
1229
+ /* @__PURE__ */ jsx(Text, { children: t("tpsl.tpTriggerPrice") }),
1230
+ " ",
1231
+ renderTPSLPrice({
1232
+ price: order2.tp_trigger_price ?? "",
1233
+ isOrderPrice: false,
1234
+ isEnable: !!order2.tp_trigger_price,
1235
+ colorType: "TP"
1236
+ })
1237
+ ] }),
1238
+ /* @__PURE__ */ jsxs(Flex, { justify: "between", className: "oui-w-full", children: [
1239
+ /* @__PURE__ */ jsx(Text, { children: t("tpsl.tpOrderPrice") }),
1240
+ renderTPSLPrice({
1241
+ price: order2.tp_order_price ?? "",
1242
+ isOrderPrice: true,
1243
+ isEnable: !!order2.tp_trigger_price,
1244
+ colorType: "TP"
1245
+ })
1246
+ ] })
1247
+ ]
1248
+ }
1249
+ ),
1250
+ /* @__PURE__ */ jsxs(
1251
+ Flex,
1252
+ {
1253
+ direction: "column",
1254
+ justify: "between",
1255
+ itemAlign: "start",
1256
+ gap: 1,
1257
+ className: "oui-w-full",
1258
+ children: [
1259
+ /* @__PURE__ */ jsxs(Flex, { justify: "between", className: "oui-w-full", children: [
1260
+ /* @__PURE__ */ jsx(Text, { children: t("tpsl.slTriggerPrice") }),
1261
+ renderTPSLPrice({
1262
+ price: order2.sl_trigger_price ?? "",
1263
+ isOrderPrice: false,
1264
+ isEnable: !!order2.sl_trigger_price,
1265
+ colorType: "SL"
1266
+ })
1267
+ ] }),
1268
+ /* @__PURE__ */ jsxs(Flex, { justify: "between", className: "oui-w-full", children: [
1269
+ /* @__PURE__ */ jsx(Text, { children: t("tpsl.slOrderPrice") }),
1270
+ renderTPSLPrice({
1271
+ price: order2.sl_order_price ?? "",
1272
+ isOrderPrice: true,
1273
+ isEnable: !!order2.sl_trigger_price,
1274
+ colorType: "SL"
1275
+ })
1276
+ ] })
1277
+ ]
1278
+ }
1279
+ )
1280
+ ]
1281
+ }
1282
+ )
1283
+ ] }) : null,
1284
+ /* @__PURE__ */ jsx(Box, { pt: 2, children: /* @__PURE__ */ jsxs(Flex, { gap: 1, children: [
1285
+ /* @__PURE__ */ jsx(
1286
+ Checkbox,
1287
+ {
1288
+ id: "disabledConfirm",
1289
+ color: "white",
1290
+ checked: !needConfirm,
1291
+ onCheckedChange: (check) => {
1292
+ setNeedConfirm(!check);
1293
+ }
1294
+ }
1295
+ ),
1296
+ /* @__PURE__ */ jsx(
1297
+ "label",
1298
+ {
1299
+ htmlFor: "disabledConfirm",
1300
+ className: textVariants({
1301
+ size: "xs",
1302
+ intensity: 54,
1303
+ className: "oui-ml-1"
1304
+ }),
1305
+ children: t("orderEntry.disableOrderConfirm")
1306
+ }
1307
+ )
1308
+ ] }) })
1309
+ ] });
1310
+ };
1311
+ var useTPSLBuilder = (options) => {
1312
+ const {
1313
+ symbol,
1314
+ order: order2,
1315
+ isEditing,
1316
+ positionType,
1317
+ triggerPrice,
1318
+ type,
1319
+ withTriggerPrice
1320
+ } = options;
1321
+ const { t } = useTranslation();
1322
+ if (isEditing && !order2) {
1323
+ throw new SDKError("order is required when isEditing is true");
1324
+ }
1325
+ const symbolInfo = useSymbolsInfo();
1326
+ useRef(AlgoOrderRootType.TP_SL);
1327
+ const [{ rows: positions }] = usePositionStream();
1328
+ const [needConfirm] = useLocalStorage("orderly_order_confirm", true);
1329
+ const position = positions.find((item) => item.symbol === symbol);
1330
+ const estLiqPrice = useEstLiqPriceBySymbol(symbol);
1331
+ useEffect(() => {
1332
+ if (!position) {
1333
+ options.close?.();
1334
+ }
1335
+ }, [position]);
1336
+ const [
1337
+ tpslOrder,
1338
+ {
1339
+ submit,
1340
+ deleteOrder,
1341
+ setValue,
1342
+ setValues,
1343
+ validate,
1344
+ metaState,
1345
+ errors,
1346
+ isCreateMutating,
1347
+ isUpdateMutating
1348
+ }
1349
+ ] = useTPSLOrder(
1350
+ {
1351
+ symbol,
1352
+ position_qty: position?.position_qty ?? 0,
1353
+ average_open_price: position?.average_open_price ?? 0
1354
+ },
1355
+ {
1356
+ defaultOrder: order2,
1357
+ positionType: triggerPrice ? PositionType.PARTIAL : positionType,
1358
+ // tpslEnable: {
1359
+ // tp_enable: !withTriggerPrice ? true : type === "tp",
1360
+ // sl_enable: !withTriggerPrice ? true : type === "sl",
1361
+ // },
1362
+ isEditing
1363
+ }
1364
+ );
1365
+ const slPriceError = useTpslPriceChecker({
1366
+ slPrice: tpslOrder.sl_trigger_price?.toString() ?? void 0,
1367
+ liqPrice: estLiqPrice ?? null,
1368
+ side: tpslOrder.side
1369
+ });
1370
+ const isSlPriceWarning = slPriceError?.sl_trigger_price?.type === ERROR_MSG_CODES.SL_PRICE_WARNING;
1371
+ const setQuantity = (value) => {
1372
+ setValue("quantity", value);
1373
+ };
1374
+ const setOrderPrice = (name, value) => {
1375
+ setValue(name, value);
1376
+ };
1377
+ const setPnL = (type2, value) => {
1378
+ setValue(type2, value);
1379
+ };
1380
+ const maxQty = useMemo(
1381
+ () => Math.abs(Number(position?.position_qty)),
1382
+ [position?.position_qty]
1383
+ );
1384
+ useMemo(() => {
1385
+ const quantity = order2?.algo_type === AlgoOrderRootType.POSITIONAL_TP_SL ? maxQty : order2?.quantity;
1386
+ let diff = 0;
1387
+ if (Number(tpslOrder.quantity) !== quantity) {
1388
+ diff = 1;
1389
+ } else if (!isEditing && !!tpslOrder.quantity) {
1390
+ diff = 1;
1391
+ }
1392
+ if (order2 && isEditing) {
1393
+ const { tp_trigger_price, sl_trigger_price } = utils.findTPSLFromOrder(order2);
1394
+ const { tp_order_price, sl_order_price } = utils.findTPSLOrderPriceFromOrder(order2);
1395
+ if (tp_trigger_price !== Number(tpslOrder.tp_trigger_price) && typeof typeof tpslOrder.tp_trigger_price !== "undefined") {
1396
+ diff = 2;
1397
+ }
1398
+ if (sl_trigger_price !== Number(tpslOrder.sl_trigger_price) && typeof tpslOrder.sl_trigger_price !== "undefined") {
1399
+ diff = 3;
1400
+ }
1401
+ if (typeof tpslOrder.tp_order_price !== "undefined" && tp_order_price !== OrderType.MARKET && tp_order_price !== Number(tpslOrder.tp_order_price)) {
1402
+ diff = 4;
1403
+ }
1404
+ if (typeof tpslOrder.sl_order_price !== "undefined" && sl_order_price !== OrderType.MARKET && sl_order_price !== Number(tpslOrder.sl_order_price)) {
1405
+ diff = 5;
1406
+ }
1407
+ }
1408
+ if (diff === 1 && !tpslOrder.tp_trigger_price && !tpslOrder.sl_trigger_price) {
1409
+ diff = -1;
1410
+ }
1411
+ return diff;
1412
+ }, [
1413
+ tpslOrder.tp_trigger_price,
1414
+ tpslOrder.tp_order_price,
1415
+ tpslOrder.sl_trigger_price,
1416
+ tpslOrder.sl_order_price,
1417
+ tpslOrder.quantity,
1418
+ order2,
1419
+ isEditing
1420
+ ]);
1421
+ useEffect(() => {
1422
+ if (!withTriggerPrice) {
1423
+ return;
1424
+ }
1425
+ if (!triggerPrice) {
1426
+ return;
1427
+ }
1428
+ if (type === "tp") {
1429
+ setValue("tp_trigger_price", triggerPrice);
1430
+ } else {
1431
+ setValue("sl_trigger_price", triggerPrice);
1432
+ }
1433
+ if (options.qty) {
1434
+ setValue("quantity", options.qty);
1435
+ }
1436
+ }, [type, triggerPrice, options.qty]);
1437
+ const cancel = () => {
1438
+ if (order2?.algo_order_id && order2?.symbol) {
1439
+ return deleteOrder(order2?.algo_order_id, order2?.symbol);
1440
+ }
1441
+ return Promise.reject("order id or symbol is invalid");
1442
+ };
1443
+ const onConfirm = (order3, options2) => {
1444
+ if (!needConfirm) {
1445
+ return Promise.resolve(true);
1446
+ }
1447
+ const maxQty2 = Math.abs(Number(position?.position_qty));
1448
+ if (`${order3.tp_trigger_price ?? ""}`.length === 0 && `${order3.sl_trigger_price ?? ""}`.length === 0) {
1449
+ return modal.confirm({
1450
+ title: t("orders.cancelOrder"),
1451
+ content: t("tpsl.cancelOrder.description"),
1452
+ onOk: () => {
1453
+ return options2.cancel();
1454
+ }
1455
+ }).then(
1456
+ () => {
1457
+ return true;
1458
+ },
1459
+ () => {
1460
+ return Promise.reject(false);
1461
+ }
1462
+ );
1463
+ }
1464
+ return modal.confirm({
1465
+ title: t("tpsl.confirmOrder"),
1466
+ // bodyClassName: "lg:oui-py-0",
1467
+ onOk: async () => {
1468
+ try {
1469
+ const res = await options2.submit({
1470
+ accountId: position?.account_id
1471
+ });
1472
+ if (res.success) {
1473
+ return res;
1474
+ }
1475
+ if (res.message) {
1476
+ toast.error(res.message);
1477
+ }
1478
+ return false;
1479
+ } catch (err) {
1480
+ if (err?.message) {
1481
+ toast.error(err.message);
1482
+ }
1483
+ return false;
1484
+ }
1485
+ },
1486
+ classNames: {
1487
+ body: "!oui-pb-0"
1488
+ },
1489
+ content: /* @__PURE__ */ jsx(
1490
+ PositionTPSLConfirm,
1491
+ {
1492
+ isPositionTPSL: positionType === PositionType.FULL,
1493
+ isEditing,
1494
+ symbol: order3.symbol,
1495
+ qty: Number(order3.quantity),
1496
+ maxQty: maxQty2,
1497
+ tpPrice: Number(order3.tp_trigger_price),
1498
+ slPrice: Number(order3.sl_trigger_price),
1499
+ side: order3.side,
1500
+ orderInfo: order3,
1501
+ quoteDP: symbolInfo[symbol]("quote_dp") ?? 2,
1502
+ baseDP: symbolInfo[symbol]("base_dp") ?? 2
1503
+ }
1504
+ )
1505
+ }).then(
1506
+ () => {
1507
+ return true;
1508
+ },
1509
+ () => {
1510
+ return Promise.reject(false);
1511
+ }
1512
+ );
1513
+ };
1514
+ const onSubmit = async () => {
1515
+ try {
1516
+ const validOrder = await validate(
1517
+ isSlPriceWarning ? void 0 : slPriceError
1518
+ );
1519
+ if (validOrder) {
1520
+ if (!needConfirm) {
1521
+ return submit({ accountId: position?.account_id }).then(() => true).catch((err) => {
1522
+ if (err?.message) {
1523
+ toast.error(err.message);
1524
+ }
1525
+ throw false;
1526
+ });
1527
+ }
1528
+ return onConfirm(tpslOrder, {
1529
+ position,
1530
+ submit,
1531
+ cancel
1532
+ });
1533
+ }
1534
+ } catch (error) {
1535
+ return Promise.reject(error);
1536
+ }
1537
+ };
1538
+ return {
1539
+ isEditing,
1540
+ symbolInfo: symbolInfo[symbol],
1541
+ maxQty,
1542
+ setQuantity: useMemoizedFn(setQuantity),
1543
+ orderQuantity: tpslOrder.quantity,
1544
+ // isPosition: isPositionTPSL,
1545
+ TPSL_OrderEntity: tpslOrder,
1546
+ setOrderValue: setValue,
1547
+ setPnL,
1548
+ setOrderPrice,
1549
+ // needConfirm,
1550
+ onSubmit,
1551
+ slPriceError,
1552
+ estLiqPrice,
1553
+ metaState,
1554
+ errors,
1555
+ status: {
1556
+ isCreateMutating,
1557
+ isUpdateMutating
1558
+ },
1559
+ position,
1560
+ setValues
1561
+ };
1562
+ };
1563
+ var TPSLWidget = (props) => {
1564
+ const { onCancel, onComplete, ...rest } = props;
1565
+ const state = useTPSLBuilder(rest);
1566
+ return /* @__PURE__ */ jsx(
1567
+ TPSL,
1568
+ {
1569
+ ...state,
1570
+ onCancel,
1571
+ onComplete,
1572
+ close: rest.close
1573
+ }
1574
+ );
1575
+ };
1576
+ var TPSLSheetId = "TPSLSheetId";
1577
+ var TPSLDialogId = "TPSLDialogId";
1578
+ registerSimpleSheet(TPSLSheetId, TPSLWidget);
1579
+ registerSimpleDialog(TPSLDialogId, TPSLWidget, {
1580
+ classNames: {
1581
+ content: "oui-w-[420px]"
1582
+ }
1583
+ });
1584
+ var PositionTPSLPopover = (props) => {
1585
+ const { position, order: order2, baseDP, quoteDP, buttonProps, isEditing } = props;
1586
+ const [needConfirm] = useLocalStorage("orderly_order_confirm", true);
1587
+ const { t } = useTranslation();
1588
+ const isPositionTPSL = isEditing ? order2?.algo_type === AlgoOrderRootType.POSITIONAL_TP_SL : void 0;
1589
+ const onEdit = () => {
1590
+ modal.show(TPSLDialogId, {
1591
+ order: order2,
1592
+ symbol: position.symbol,
1593
+ baseDP,
1594
+ quoteDP,
1595
+ positionType: isPositionTPSL ? PositionType.FULL : PositionType.PARTIAL,
1596
+ isEditing
1597
+ });
1598
+ };
1599
+ return /* @__PURE__ */ jsx(Box, { onClick: onEdit, className: "oui-cursor-pointer", children: props.children || /* @__PURE__ */ jsx(
1600
+ Button,
1601
+ {
1602
+ variant: "outlined",
1603
+ size: "sm",
1604
+ color: "secondary",
1605
+ ...buttonProps,
1606
+ children: props.label
1607
+ }
1608
+ ) });
1609
+ };
1610
+ var PositionTPSLSheet = (props) => {
1611
+ const { position, order: order2, symbolInfo, isEditing } = props;
1612
+ const { resolve, hide, updateArgs } = useModal();
1613
+ const isPositionTPSL = isEditing ? order2?.algo_type === AlgoOrderRootType.POSITIONAL_TP_SL : void 0;
1614
+ return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(
1615
+ TPSLWidget,
1616
+ {
1617
+ ...props,
1618
+ positionType: props.positionType ?? (isPositionTPSL ? PositionType.FULL : PositionType.PARTIAL),
1619
+ onCancel: () => {
1620
+ hide();
1621
+ }
1622
+ }
1623
+ ) });
1624
+ };
1625
+ var TPSLAdvancedUI = (props) => {
1626
+ const { t } = useTranslation();
1627
+ const { errors, validated } = props.metaState;
1628
+ const { getErrorMsg } = useOrderEntryFormErrorMsg(props.slPriceError);
1629
+ const isSlPriceWarning = props.slPriceError?.sl_trigger_price?.type === ERROR_MSG_CODES.SL_PRICE_WARNING;
1630
+ const {
1631
+ formattedOrder,
1632
+ setValue: setOrderValue,
1633
+ symbolInfo,
1634
+ setValues
1635
+ } = props;
1636
+ const [tpValues, setTpValuse] = useState({
1637
+ enable: false,
1638
+ order_type: formattedOrder.tp_order_type ?? OrderType.MARKET,
1639
+ order_price: formattedOrder.tp_order_price ?? "",
1640
+ trigger_price: formattedOrder.tp_trigger_price ?? "",
1641
+ PnL: formattedOrder.tp_pnl ?? "",
1642
+ Offset: formattedOrder.tp_offset ?? "",
1643
+ "Offset%": formattedOrder.tp_offset_percentage ?? "",
1644
+ ROI: formattedOrder.tp_ROI ?? ""
1645
+ });
1646
+ const [slValues, setSlValues] = useState({
1647
+ enable: false,
1648
+ order_type: formattedOrder.sl_order_type ?? OrderType.MARKET,
1649
+ order_price: formattedOrder.sl_order_price ?? "",
1650
+ trigger_price: formattedOrder.sl_trigger_price ?? "",
1651
+ PnL: formattedOrder.sl_pnl ?? "",
1652
+ Offset: formattedOrder.sl_offset ?? "",
1653
+ "Offset%": formattedOrder.sl_offset_percentage ?? "",
1654
+ ROI: formattedOrder.sl_ROI ?? ""
1655
+ });
1656
+ useEffect(() => {
1657
+ setTpValuse((prev) => ({
1658
+ ...prev,
1659
+ // enable: formattedOrder.tp_enable ?? false,
1660
+ order_type: formattedOrder.tp_order_type ?? OrderType.MARKET,
1661
+ order_price: formattedOrder.tp_order_price ?? "",
1662
+ trigger_price: formattedOrder.tp_trigger_price ?? "",
1663
+ PnL: formattedOrder.tp_pnl ?? "",
1664
+ Offset: formattedOrder.tp_offset ?? "",
1665
+ "Offset%": formattedOrder.tp_offset_percentage ?? "",
1666
+ ROI: formattedOrder.tp_ROI ?? ""
1667
+ }));
1668
+ }, [formattedOrder]);
1669
+ useEffect(() => {
1670
+ setSlValues((prev) => ({
1671
+ ...prev,
1672
+ // enable: formattedOrder.sl_enable ?? false,
1673
+ order_type: formattedOrder.sl_order_type ?? OrderType.MARKET,
1674
+ order_price: formattedOrder.sl_order_price ?? "",
1675
+ trigger_price: formattedOrder.sl_trigger_price ?? "",
1676
+ PnL: formattedOrder.sl_pnl ?? "",
1677
+ Offset: formattedOrder.sl_offset ?? "",
1678
+ "Offset%": formattedOrder.sl_offset_percentage ?? "",
1679
+ ROI: formattedOrder.sl_ROI ?? ""
1680
+ }));
1681
+ }, [formattedOrder]);
1682
+ const tpslEnable = formattedOrder.tp_trigger_price || formattedOrder.sl_trigger_price;
1683
+ return /* @__PURE__ */ jsxs("div", { className: "oui-flex oui-h-full oui-flex-col oui-justify-between oui-rounded-[16px] oui-py-3", children: [
1684
+ /* @__PURE__ */ jsx("div", { className: "oui-px-3", children: /* @__PURE__ */ jsxs(
1685
+ Flex,
1686
+ {
1687
+ className: "oui-mb-5 oui-cursor-pointer oui-text-base oui-text-base-contrast-80",
1688
+ gap: 1,
1689
+ itemAlign: "center",
1690
+ onClick: props.onClose,
1691
+ children: [
1692
+ /* @__PURE__ */ jsx(ArrowRightIcon, { className: " oui-text-base-contrast-80" }),
1693
+ /* @__PURE__ */ jsx(Text, { children: t("common.tpsl") })
1694
+ ]
1695
+ }
1696
+ ) }),
1697
+ /* @__PURE__ */ jsxs(ScrollArea, { className: "oui-flex-1", children: [
1698
+ /* @__PURE__ */ jsx("div", { className: "oui-px-3", children: /* @__PURE__ */ jsx(
1699
+ OrderInfo,
1700
+ {
1701
+ order: formattedOrder,
1702
+ baseDP: symbolInfo.base_dp,
1703
+ quoteDP: symbolInfo.quote_dp,
1704
+ estLiqPrice: props.estLiqPrice ?? void 0,
1705
+ symbolLeverage: props.symbolLeverage
1706
+ }
1707
+ ) }),
1708
+ /* @__PURE__ */ jsx(Divider, { className: "oui-my-3" }),
1709
+ /* @__PURE__ */ jsxs("div", { className: "oui-px-3", children: [
1710
+ /* @__PURE__ */ jsxs(Flex, { className: "oui-gap-[6px]", children: [
1711
+ /* @__PURE__ */ jsx(
1712
+ Button,
1713
+ {
1714
+ onClick: () => {
1715
+ setOrderValue("side", OrderSide.BUY);
1716
+ },
1717
+ size: "sm",
1718
+ fullWidth: true,
1719
+ "data-type": OrderSide.BUY,
1720
+ className: cn(
1721
+ formattedOrder.side === OrderSide.BUY ? "oui-bg-success-darken hover:oui-bg-success-darken/80 active:oui-bg-success-darken/80" : "oui-bg-base-7 oui-text-base-contrast-36 hover:oui-bg-base-6 active:oui-bg-base-6"
1722
+ ),
1723
+ "data-testid": "oui-testid-orderEntry-side-buy-button",
1724
+ children: t("common.buy")
1725
+ }
1726
+ ),
1727
+ /* @__PURE__ */ jsx(
1728
+ Button,
1729
+ {
1730
+ onClick: () => {
1731
+ setOrderValue("side", OrderSide.SELL);
1732
+ },
1733
+ "data-type": OrderSide.SELL,
1734
+ fullWidth: true,
1735
+ size: "sm",
1736
+ className: cn(
1737
+ formattedOrder.side === OrderSide.SELL ? "oui-bg-danger-darken hover:oui-bg-danger-darken/80 active:oui-bg-danger-darken/80" : "oui-bg-base-7 oui-text-base-contrast-36 hover:oui-bg-base-6 active:oui-bg-base-6"
1738
+ ),
1739
+ "data-testid": "oui-testid-orderEntry-side-sell-button",
1740
+ children: t("common.sell")
1741
+ }
1742
+ )
1743
+ ] }),
1744
+ /* @__PURE__ */ jsxs("div", { className: "oui-py-3", children: [
1745
+ /* @__PURE__ */ jsx(
1746
+ TPSLPositionTypeWidget,
1747
+ {
1748
+ value: formattedOrder.position_type ?? PositionType.PARTIAL,
1749
+ onChange: (key, value) => {
1750
+ if (value === PositionType.FULL) {
1751
+ setValues({
1752
+ position_type: PositionType.FULL,
1753
+ tp_order_type: OrderType.MARKET,
1754
+ tp_order_price: void 0,
1755
+ sl_order_type: OrderType.MARKET,
1756
+ sl_order_price: void 0
1757
+ });
1758
+ return;
1759
+ }
1760
+ setOrderValue("position_type", value);
1761
+ }
1762
+ }
1763
+ ),
1764
+ formattedOrder.position_type === PositionType.FULL && /* @__PURE__ */ jsxs(
1765
+ Flex,
1766
+ {
1767
+ justify: "start",
1768
+ itemAlign: "start",
1769
+ gap: 2,
1770
+ className: "oui-mt-3 oui-w-full",
1771
+ children: [
1772
+ /* @__PURE__ */ jsx("div", { className: "oui-relative oui-top-[7px] oui-size-1 oui-rounded-full oui-bg-[#D25f00]" }),
1773
+ /* @__PURE__ */ jsx(Text, { className: "oui-text-2xs oui-text-[#D25f00]", children: t("tpsl.positionType.full.tips.market") })
1774
+ ]
1775
+ }
1776
+ )
1777
+ ] }),
1778
+ /* @__PURE__ */ jsxs(Flex, { direction: "column", gap: 6, children: [
1779
+ /* @__PURE__ */ jsx(
1780
+ TPSLInputRowWidget,
1781
+ {
1782
+ rootOrderPrice: formattedOrder.order_price,
1783
+ symbol: symbolInfo.symbol,
1784
+ type: "tp",
1785
+ side: formattedOrder.side,
1786
+ values: tpValues,
1787
+ errors: validated ? errors : null,
1788
+ quote_dp: symbolInfo.quote_dp,
1789
+ hideOrderPrice: formattedOrder.position_type === PositionType.FULL,
1790
+ onChange: (key, value) => {
1791
+ setOrderValue(key, value);
1792
+ },
1793
+ positionType: formattedOrder.position_type ?? PositionType.PARTIAL,
1794
+ symbolLeverage: props.symbolLeverage
1795
+ }
1796
+ ),
1797
+ /* @__PURE__ */ jsx(
1798
+ TPSLInputRowWidget,
1799
+ {
1800
+ rootOrderPrice: formattedOrder.order_price,
1801
+ symbol: symbolInfo.symbol,
1802
+ type: "sl",
1803
+ side: formattedOrder.side,
1804
+ values: slValues,
1805
+ hideOrderPrice: formattedOrder.position_type === PositionType.FULL,
1806
+ errors: validated ? errors : null,
1807
+ inputWarnNode: isSlPriceWarning && /* @__PURE__ */ jsx(
1808
+ DotStatus,
1809
+ {
1810
+ color: "warning",
1811
+ size: "xs",
1812
+ label: getErrorMsg("sl_trigger_price")
1813
+ }
1814
+ ),
1815
+ quote_dp: symbolInfo.quote_dp,
1816
+ positionType: formattedOrder.position_type ?? PositionType.PARTIAL,
1817
+ onChange: (key, value) => {
1818
+ setOrderValue(key, value);
1819
+ },
1820
+ symbolLeverage: props.symbolLeverage
1821
+ }
1822
+ )
1823
+ ] }),
1824
+ /* @__PURE__ */ jsx(
1825
+ PnlInfo,
1826
+ {
1827
+ tp_pnl: formattedOrder.tp_pnl,
1828
+ sl_pnl: formattedOrder.sl_pnl,
1829
+ className: "oui-mt-6"
1830
+ }
1831
+ )
1832
+ ] })
1833
+ ] }),
1834
+ /* @__PURE__ */ jsxs(Flex, { className: "oui-mt-6 oui-px-3", gap: 2, children: [
1835
+ /* @__PURE__ */ jsx(
1836
+ Button,
1837
+ {
1838
+ size: "md",
1839
+ fullWidth: true,
1840
+ color: "gray",
1841
+ variant: "outlined",
1842
+ className: "oui-text-base-contrast-36",
1843
+ onClick: props.onClose,
1844
+ children: t("common.cancel")
1845
+ }
1846
+ ),
1847
+ /* @__PURE__ */ jsx(
1848
+ Button,
1849
+ {
1850
+ size: "md",
1851
+ fullWidth: true,
1852
+ color: "success",
1853
+ className: cn(
1854
+ formattedOrder.side === OrderSide.SELL ? "oui-bg-danger-darken hover:oui-bg-danger-darken/80 active:oui-bg-danger-darken/80" : "oui-bg-success-darken hover:oui-bg-success-darken/80 active:oui-bg-success-darken/80"
1855
+ ),
1856
+ onClick: props.onSubmit,
1857
+ disabled: !tpslEnable,
1858
+ children: t("tpsl.advanced.submit")
1859
+ }
1860
+ )
1861
+ ] })
1862
+ ] });
1863
+ };
1864
+ var ArrowRightIcon = (props) => {
1865
+ return /* @__PURE__ */ jsx(
1866
+ "svg",
1867
+ {
1868
+ xmlns: "http://www.w3.org/2000/svg",
1869
+ width: "14",
1870
+ height: "14",
1871
+ viewBox: "0 0 14 14",
1872
+ fill: "currentColor",
1873
+ ...props,
1874
+ children: /* @__PURE__ */ jsx("path", { d: "M8.03752 2.9294C7.89169 2.95857 7.74527 3.03207 7.65661 3.16624L5.33145 6.66624C5.20137 6.86224 5.20137 7.12648 5.33145 7.32248L7.65661 10.8225C7.83452 11.0902 8.20669 11.1655 8.47385 10.9864C8.74044 10.8079 8.8151 10.434 8.63719 10.1662L6.53019 6.99408L8.63719 3.82249C8.8151 3.55416 8.74044 3.18082 8.47385 3.00232C8.34027 2.91249 8.18335 2.89965 8.03752 2.9294Z" })
1875
+ }
1876
+ );
1877
+ };
1878
+ var useTPSLAdvanced = (props) => {
1879
+ const { order: order2, setOrderValue, onClose } = props;
1880
+ const [innerMetaState, setInnerMetaState] = useState(null);
1881
+ const {
1882
+ formattedOrder,
1883
+ setValue,
1884
+ setValues,
1885
+ helper,
1886
+ symbolInfo,
1887
+ metaState,
1888
+ ...state
1889
+ } = useOrderEntry(order2.symbol, {
1890
+ initialOrder: {
1891
+ symbol: order2.symbol,
1892
+ order_type: order2.order_type,
1893
+ side: order2.side,
1894
+ order_price: order2.order_price,
1895
+ order_quantity: order2.order_quantity,
1896
+ position_type: order2.position_type ?? PositionType.PARTIAL,
1897
+ trigger_price: order2.trigger_price,
1898
+ // tp_enable: isTPSLEnable(order, "tp"),
1899
+ // sl_enable: isTPSLEnable(order, "sl"),
1900
+ tp_trigger_price: order2.tp_trigger_price,
1901
+ sl_trigger_price: order2.sl_trigger_price,
1902
+ tp_order_price: order2.tp_order_price,
1903
+ sl_order_price: order2.sl_order_price,
1904
+ tp_order_type: order2.tp_order_type,
1905
+ sl_order_type: order2.sl_order_type,
1906
+ sl_pnl: order2.sl_pnl,
1907
+ sl_offset: order2.sl_offset,
1908
+ sl_offset_percentage: order2.sl_offset_percentage,
1909
+ tp_pnl: order2.tp_pnl,
1910
+ tp_offset: order2.tp_offset,
1911
+ tp_offset_percentage: order2.tp_offset_percentage
1912
+ }
1913
+ });
1914
+ const slPriceError = useTpslPriceChecker({
1915
+ slPrice: formattedOrder.sl_trigger_price,
1916
+ liqPrice: state.estLiqPrice,
1917
+ side: formattedOrder.side
1918
+ });
1919
+ const onSubmit = () => {
1920
+ const isSlPriceError = slPriceError?.sl_trigger_price?.type === ERROR_MSG_CODES.SL_PRICE_ERROR;
1921
+ helper.validate(isSlPriceError ? slPriceError : void 0).then(() => {
1922
+ props.onSubmit(formattedOrder);
1923
+ }).catch((err) => {
1924
+ });
1925
+ };
1926
+ return {
1927
+ order: order2,
1928
+ formattedOrder,
1929
+ symbolInfo,
1930
+ slPriceError,
1931
+ estLiqPrice: state.estLiqPrice,
1932
+ setValue,
1933
+ setValues,
1934
+ onSubmit,
1935
+ onClose,
1936
+ metaState,
1937
+ symbolLeverage: props.symbolLeverage
1938
+ };
1939
+ };
1940
+ var TPSLAdvancedWidget = (props) => {
1941
+ const state = useTPSLAdvanced({
1942
+ order: props.order,
1943
+ setOrderValue: props.setOrderValue,
1944
+ onSubmit: props.onSubmit,
1945
+ onClose: props.onClose,
1946
+ symbolLeverage: props.symbolLeverage
1947
+ });
1948
+ return /* @__PURE__ */ jsx(TPSLAdvancedUI, { ...state });
1949
+ };
1950
+ var TPSLAdvancedSheetId = "TPSLAdvancedSheetId";
1951
+ var TPSLAdvancedDialogId = "TPSLAdvancedDialogId";
1952
+ registerSimpleSheet(TPSLAdvancedSheetId, TPSLAdvancedWidget, {
1953
+ title: () => i18n.t("common.settings")
1954
+ });
1955
+ registerSimpleDialog(TPSLAdvancedDialogId, TPSLAdvancedWidget, {
1956
+ title: () => i18n.t("common.settings")
1957
+ });
1958
+ var useTPSLDetail = (props) => {
1959
+ const { position } = props;
1960
+ const symbol = position.symbol;
1961
+ const symbolInfo = useSymbolsInfo()[symbol];
1962
+ const { isMobile } = useScreen();
1963
+ const { t } = useTranslation();
1964
+ const [fullPositionOrders, setFullPositionOrders] = useState(
1965
+ []
1966
+ );
1967
+ const [partialPositionOrders, setPartialPositionOrders] = useState([]);
1968
+ const [tpslOrders, { cancelAlgoOrder, cancelPostionOrdersByTypes, refresh }] = useOrderStream(
1969
+ {
1970
+ symbol: position.symbol,
1971
+ status: OrderStatus.INCOMPLETE,
1972
+ includes: [AlgoOrderRootType.POSITIONAL_TP_SL, AlgoOrderRootType.TP_SL],
1973
+ size: 500
1974
+ },
1975
+ {
1976
+ keeplive: true
1977
+ }
1978
+ );
1979
+ const onCancelOrder = async (order2) => {
1980
+ return await cancelAlgoOrder(order2.algo_order_id, order2.symbol);
1981
+ };
1982
+ const onCancelAllTPSLOrders = async () => {
1983
+ return await cancelPostionOrdersByTypes(symbol, [AlgoOrderRootType.TP_SL]);
1984
+ };
1985
+ const showTPSLDialog = ({
1986
+ order: order2,
1987
+ positionType,
1988
+ isEditing
1989
+ }) => {
1990
+ const dialogId = isMobile ? TPSLSheetId : TPSLDialogId;
1991
+ modal.show(dialogId, {
1992
+ order: order2,
1993
+ symbol: position.symbol,
1994
+ positionType,
1995
+ isEditing
1996
+ });
1997
+ };
1998
+ const editTPSLOrder = (order2, positionType) => {
1999
+ showTPSLDialog({ order: order2, positionType, isEditing: true });
2000
+ };
2001
+ const addTPSLOrder = (positionType) => {
2002
+ showTPSLDialog({ positionType, isEditing: false });
2003
+ };
2004
+ useEffect(() => {
2005
+ if (tpslOrders) {
2006
+ const { fullPositionOrder, partialPositionOrders: partialPositionOrders2 } = findPositionTPSLFromOrders(tpslOrders, symbol);
2007
+ setFullPositionOrders(fullPositionOrder ? [fullPositionOrder] : []);
2008
+ setPartialPositionOrders(partialPositionOrders2 ?? []);
2009
+ }
2010
+ }, [tpslOrders, symbol]);
2011
+ return {
2012
+ symbolInfo,
2013
+ position,
2014
+ symbol,
2015
+ fullPositionOrders,
2016
+ partialPositionOrders,
2017
+ cancelPostionOrdersByTypes,
2018
+ onCancelOrder,
2019
+ onCancelAllTPSLOrders,
2020
+ editTPSLOrder,
2021
+ addTPSLOrder
2022
+ };
2023
+ };
2024
+ var TPSLDetailContext = createContext({});
2025
+ var TPSLDetailProvider = (props) => {
2026
+ const symbolInfo = useSymbolsInfo()[props.symbol];
2027
+ const estLiqPrice = useEstLiqPriceBySymbol(props.symbol);
2028
+ return /* @__PURE__ */ jsx(
2029
+ TPSLDetailContext.Provider,
2030
+ {
2031
+ value: {
2032
+ base_dp: symbolInfo("base_dp"),
2033
+ quote_dp: symbolInfo("quote_dp"),
2034
+ side: props.position.position_qty > 0 ? OrderSide.BUY : OrderSide.SELL,
2035
+ position: props.position,
2036
+ estLiqPrice
2037
+ },
2038
+ children: props.children
2039
+ }
2040
+ );
2041
+ };
2042
+ var useTPSLDetailContext = () => {
2043
+ return useContext(TPSLDetailContext);
2044
+ };
2045
+ var FlexCell = (props) => {
2046
+ return /* @__PURE__ */ jsx(
2047
+ Flex,
2048
+ {
2049
+ direction: "column",
2050
+ justify: "center",
2051
+ itemAlign: "start",
2052
+ className: cn("oui-text-2xs oui-h-[36px]", props.className),
2053
+ children: props.children
2054
+ }
2055
+ );
2056
+ };
2057
+ var EstPnlRender = ({ order: order2 }) => {
2058
+ const { position, base_dp, quote_dp } = useTPSLDetailContext();
2059
+ const { tp_trigger_price, sl_trigger_price } = findTPSLFromOrder(order2);
2060
+ let tp_unrealPnl = void 0;
2061
+ let sl_unrealPnl = void 0;
2062
+ const qty = new Decimal(order2.quantity).eq(0) ? position.position_qty : order2.quantity;
2063
+ const side = position.position_qty > 0 ? OrderSide.BUY : OrderSide.SELL;
2064
+ const openPrice = position?.average_open_price;
2065
+ if (tp_trigger_price) {
2066
+ const direction = getTPSLDirection({
2067
+ side,
2068
+ type: "tp",
2069
+ closePrice: tp_trigger_price,
2070
+ orderPrice: openPrice
2071
+ });
2072
+ tp_unrealPnl = formatNum.pnl(
2073
+ positions.unrealizedPnL({
2074
+ qty,
2075
+ openPrice,
2076
+ // markPrice: unRealizedPrice,
2077
+ markPrice: tp_trigger_price
2078
+ })
2079
+ )?.abs().mul(direction).toNumber();
2080
+ }
2081
+ if (sl_trigger_price) {
2082
+ const direction = getTPSLDirection({
2083
+ side,
2084
+ type: "sl",
2085
+ closePrice: sl_trigger_price,
2086
+ orderPrice: openPrice
2087
+ });
2088
+ sl_unrealPnl = formatNum.pnl(
2089
+ positions.unrealizedPnL({
2090
+ qty,
2091
+ openPrice,
2092
+ // markPrice: unRealizedPrice,
2093
+ markPrice: sl_trigger_price
2094
+ })
2095
+ )?.abs().mul(direction).toNumber();
2096
+ }
2097
+ return /* @__PURE__ */ jsxs(
2098
+ Flex,
2099
+ {
2100
+ gap: 2,
2101
+ direction: "column",
2102
+ justify: "between",
2103
+ itemAlign: "start",
2104
+ className: "oui-text-2xs",
2105
+ children: [
2106
+ tp_unrealPnl && /* @__PURE__ */ jsx(FlexCell, { children: /* @__PURE__ */ jsx(Text.numeral, { dp: 2, rm: Decimal.ROUND_DOWN, coloring: true, padding: false, children: tp_unrealPnl }) }),
2107
+ sl_unrealPnl && /* @__PURE__ */ jsx(FlexCell, { children: /* @__PURE__ */ jsx(Text.numeral, { dp: 2, rm: Decimal.ROUND_DOWN, coloring: true, padding: false, children: sl_unrealPnl }) })
2108
+ ]
2109
+ }
2110
+ );
2111
+ };
2112
+ var OrderPriceRender = ({ order: order2 }) => {
2113
+ const { quote_dp } = useTPSLDetailContext();
2114
+ const { tp_order_price, sl_order_price } = findTPSLOrderPriceFromOrder(order2);
2115
+ const { t } = useTranslation();
2116
+ return /* @__PURE__ */ jsxs(
2117
+ Flex,
2118
+ {
2119
+ gap: 2,
2120
+ direction: "column",
2121
+ justify: "between",
2122
+ itemAlign: "start",
2123
+ className: "oui-text-2xs",
2124
+ children: [
2125
+ tp_order_price && /* @__PURE__ */ jsx(FlexCell, { children: tp_order_price === OrderType.MARKET ? /* @__PURE__ */ jsx(Text, { children: t("common.market") }) : /* @__PURE__ */ jsx(Text.numeral, { dp: quote_dp, rm: Decimal.ROUND_DOWN, padding: false, children: tp_order_price }) }),
2126
+ sl_order_price && /* @__PURE__ */ jsx(FlexCell, { children: sl_order_price === OrderType.MARKET ? /* @__PURE__ */ jsx(Text, { children: t("common.market") }) : /* @__PURE__ */ jsx(Text.numeral, { dp: quote_dp, rm: Decimal.ROUND_DOWN, padding: false, children: sl_order_price }) })
2127
+ ]
2128
+ }
2129
+ );
2130
+ };
2131
+ var QtyRender = ({ order: order2 }) => {
2132
+ const { position, base_dp } = useTPSLDetailContext();
2133
+ const { tp_trigger_price, sl_trigger_price } = findTPSLFromOrder(order2);
2134
+ const quantity = useMemo(() => {
2135
+ if (order2.quantity === 0) {
2136
+ return -position.position_qty;
2137
+ }
2138
+ return position.position_qty > 0 ? -order2.quantity : order2.quantity;
2139
+ }, [order2.quantity, position.position_qty]);
2140
+ return /* @__PURE__ */ jsxs(
2141
+ Flex,
2142
+ {
2143
+ direction: "column",
2144
+ justify: "start",
2145
+ itemAlign: "start",
2146
+ className: "oui-h-full oui-text-2xs",
2147
+ children: [
2148
+ /* @__PURE__ */ jsx(FlexCell, { children: /* @__PURE__ */ jsx(Text.numeral, { dp: base_dp, rm: Decimal.ROUND_DOWN, padding: false, children: quantity }) }),
2149
+ tp_trigger_price && sl_trigger_price && /* @__PURE__ */ jsx(FlexCell, { children: /* @__PURE__ */ jsx("div", {}) })
2150
+ ]
2151
+ }
2152
+ );
2153
+ };
2154
+ var TriggerPrice = ({ order: order2 }) => {
2155
+ const { quote_dp } = useTPSLDetailContext();
2156
+ const { tp_trigger_price, sl_trigger_price } = findTPSLFromOrder(order2);
2157
+ const { t } = useTranslation();
2158
+ return /* @__PURE__ */ jsxs(
2159
+ Flex,
2160
+ {
2161
+ gap: 1,
2162
+ direction: "column",
2163
+ justify: "between",
2164
+ itemAlign: "start",
2165
+ className: "oui-text-2xs",
2166
+ children: [
2167
+ tp_trigger_price && /* @__PURE__ */ jsx(FlexCell, { children: /* @__PURE__ */ jsxs(Flex, { direction: "column", justify: "start", itemAlign: "start", children: [
2168
+ /* @__PURE__ */ jsx(Text, { className: "oui-text-base-contrast-36", children: t("common.market") }),
2169
+ /* @__PURE__ */ jsx(Text.numeral, { dp: quote_dp, rm: Decimal.ROUND_DOWN, padding: false, children: tp_trigger_price })
2170
+ ] }) }),
2171
+ sl_trigger_price && /* @__PURE__ */ jsx(FlexCell, { children: /* @__PURE__ */ jsxs(Flex, { direction: "column", justify: "start", itemAlign: "start", children: [
2172
+ /* @__PURE__ */ jsx(Text, { className: "oui-text-base-contrast-36", children: t("common.market") }),
2173
+ /* @__PURE__ */ jsx(Text.numeral, { dp: quote_dp, rm: Decimal.ROUND_DOWN, padding: false, children: sl_trigger_price })
2174
+ ] }) })
2175
+ ]
2176
+ }
2177
+ );
2178
+ };
2179
+ var CloseToLiqPriceIcon = ({
2180
+ slPriceError,
2181
+ className
2182
+ }) => {
2183
+ const { t } = useTranslation();
2184
+ const { isMobile } = useScreen();
2185
+ const { getErrorMsg } = useOrderEntryFormErrorMsg(slPriceError);
2186
+ const isSlPriceWarning = slPriceError?.sl_trigger_price?.type === ERROR_MSG_CODES.SL_PRICE_WARNING;
2187
+ const icon = /* @__PURE__ */ jsx(
2188
+ ExclamationFillIcon,
2189
+ {
2190
+ onMouseEnter: (e) => e.stopPropagation(),
2191
+ onMouseLeave: (e) => e.stopPropagation(),
2192
+ onPointerEnter: (e) => e.stopPropagation(),
2193
+ onPointerLeave: (e) => e.stopPropagation(),
2194
+ size: 14,
2195
+ className: cn(
2196
+ "oui-text-warning-darken hover:oui-cursor-pointer",
2197
+ className
2198
+ )
2199
+ }
2200
+ );
2201
+ if (!isSlPriceWarning || !slPriceError)
2202
+ return null;
2203
+ if (isMobile) {
2204
+ return /* @__PURE__ */ jsx(
2205
+ "button",
2206
+ {
2207
+ onClick: (e) => {
2208
+ modal.alert({
2209
+ title: t("common.tips"),
2210
+ message: getErrorMsg("sl_trigger_price")
2211
+ });
2212
+ },
2213
+ className: "oui-px-1",
2214
+ children: icon
2215
+ }
2216
+ );
2217
+ }
2218
+ return /* @__PURE__ */ jsx(
2219
+ Tooltip,
2220
+ {
2221
+ content: getErrorMsg("sl_trigger_price"),
2222
+ className: "oui-max-w-[240px] oui-text-base-contrast-80",
2223
+ children: icon
2224
+ }
2225
+ );
2226
+ };
2227
+ CloseToLiqPriceIcon.displayName = "CloseToLiqPriceIcon";
2228
+ var TypeRender = ({ order: order2 }) => {
2229
+ const { side, estLiqPrice } = useTPSLDetailContext();
2230
+ const { tp_trigger_price, sl_trigger_price } = findTPSLFromOrder(order2);
2231
+ const { t } = useTranslation();
2232
+ const slPriceError = useTpslPriceChecker({
2233
+ slPrice: sl_trigger_price?.toString() ?? void 0,
2234
+ liqPrice: estLiqPrice ?? null,
2235
+ side
2236
+ });
2237
+ const isSlPriceWarning = slPriceError?.sl_trigger_price?.type === ERROR_MSG_CODES.SL_PRICE_WARNING;
2238
+ return /* @__PURE__ */ jsxs(
2239
+ Flex,
2240
+ {
2241
+ direction: "column",
2242
+ justify: "between",
2243
+ itemAlign: "start",
2244
+ className: "oui-text-2xs",
2245
+ children: [
2246
+ tp_trigger_price && /* @__PURE__ */ jsx(FlexCell, { children: /* @__PURE__ */ jsx(Text, { className: "oui-text-trade-profit", children: t("tpsl.tp") }) }),
2247
+ sl_trigger_price && /* @__PURE__ */ jsxs(FlexCell, { className: "oui-flex-row oui-items-center oui-gap-1", children: [
2248
+ /* @__PURE__ */ jsx(Text, { className: "oui-text-trade-loss", children: t("tpsl.sl") }),
2249
+ isSlPriceWarning && /* @__PURE__ */ jsx(CloseToLiqPriceIcon, { slPriceError })
2250
+ ] })
2251
+ ]
2252
+ }
2253
+ );
2254
+ };
2255
+ var useColumn = (props) => {
2256
+ const { t } = useTranslation();
2257
+ const { isMobile } = useScreen();
2258
+ const { onCancelOrder } = props;
2259
+ const columns = useMemo(() => {
2260
+ const moblieColumns = [
2261
+ {
2262
+ title: t("tpsl.tpslDetail.qty"),
2263
+ dataIndex: "quantity",
2264
+ width: 70,
2265
+ className: "oui-py-2",
2266
+ render: (_, record) => /* @__PURE__ */ jsx(QtyRender, { order: record })
2267
+ },
2268
+ {
2269
+ title: t("common.type"),
2270
+ dataIndex: "type",
2271
+ width: 35,
2272
+ className: "oui-pl-1 oui-py-2",
2273
+ render: (_, record) => /* @__PURE__ */ jsx(TypeRender, { order: record })
2274
+ },
2275
+ {
2276
+ title: t("common.trigger"),
2277
+ dataIndex: "trigger",
2278
+ width: 70,
2279
+ className: "oui-pl-1 oui-py-2",
2280
+ render: (_, record) => {
2281
+ return /* @__PURE__ */ jsx(TriggerPrice, { order: record });
2282
+ }
2283
+ },
2284
+ {
2285
+ title: t("common.price"),
2286
+ dataIndex: "price",
2287
+ width: 70,
2288
+ className: "oui-py-2",
2289
+ render: (_, record) => /* @__PURE__ */ jsx(OrderPriceRender, { order: record })
2290
+ },
2291
+ {
2292
+ title: /* @__PURE__ */ jsx(
2293
+ Tooltip,
2294
+ {
2295
+ className: "oui-max-w-[280px] oui-bg-base-8 oui-p-3 oui-text-2xs oui-text-base-contrast",
2296
+ content: t("tpsl.tpslDetail.estPnl.tooltip"),
2297
+ children: /* @__PURE__ */ jsx(Text, { className: "oui-underline oui-decoration-dashed oui-underline-offset-2", children: t("tpsl.tpslDetail.estPnl") })
2298
+ }
2299
+ ),
2300
+ dataIndex: "estpnl",
2301
+ width: 70,
2302
+ className: "!oui-pr-0 oui-py-2",
2303
+ render: (_, record) => /* @__PURE__ */ jsx(EstPnlRender, { order: record })
2304
+ }
2305
+ ];
2306
+ const desktopColums = [
2307
+ {
2308
+ title: t("tpsl.tpslDetail.qty"),
2309
+ dataIndex: "quantity",
2310
+ width: 70,
2311
+ className: cn(" oui-py-2 !oui-pl-5"),
2312
+ render: (_, record) => /* @__PURE__ */ jsx(QtyRender, { order: record })
2313
+ },
2314
+ {
2315
+ title: t("common.type"),
2316
+ dataIndex: "type",
2317
+ width: 35,
2318
+ className: "oui-pl-1 oui-py-2",
2319
+ render: (_, record) => /* @__PURE__ */ jsx(TypeRender, { order: record })
2320
+ },
2321
+ {
2322
+ title: t("common.trigger"),
2323
+ dataIndex: "trigger",
2324
+ width: 70,
2325
+ className: "oui-pl-1 oui-py-2",
2326
+ render: (_, record) => {
2327
+ return /* @__PURE__ */ jsx(TriggerPrice, { order: record });
2328
+ }
2329
+ },
2330
+ {
2331
+ title: t("common.price"),
2332
+ dataIndex: "price",
2333
+ width: 70,
2334
+ className: "oui-pl-1 oui-py-2",
2335
+ render: (_, record) => /* @__PURE__ */ jsx(OrderPriceRender, { order: record })
2336
+ },
2337
+ {
2338
+ title: /* @__PURE__ */ jsx(
2339
+ Tooltip,
2340
+ {
2341
+ className: "oui-max-w-[280px] oui-bg-base-8 oui-p-3 oui-text-2xs oui-text-base-contrast",
2342
+ content: t("tpsl.tpslDetail.estPnl.tooltip"),
2343
+ children: /* @__PURE__ */ jsx(Text, { className: "oui-underline oui-decoration-dashed oui-underline-offset-2", children: t("tpsl.tpslDetail.estPnl") })
2344
+ }
2345
+ ),
2346
+ dataIndex: "estpnl",
2347
+ width: 70,
2348
+ className: "oui-pl-1 oui-py-2",
2349
+ render: (_, record) => /* @__PURE__ */ jsx(EstPnlRender, { order: record })
2350
+ },
2351
+ {
2352
+ title: "",
2353
+ dataIndex: "delete",
2354
+ width: 50,
2355
+ className: cn("oui-py-2 !oui-pr-5"),
2356
+ render: (_, record) => {
2357
+ return /* @__PURE__ */ jsx(CancelAllBtn, { order: record, onCancelOrder });
2358
+ }
2359
+ }
2360
+ ];
2361
+ if (isMobile) {
2362
+ return moblieColumns;
2363
+ }
2364
+ return desktopColums;
2365
+ }, [t, isMobile]);
2366
+ return columns;
2367
+ };
2368
+ var DeleteIcon = (props) => {
2369
+ const { size = 18 } = props;
2370
+ return /* @__PURE__ */ jsx(
2371
+ "svg",
2372
+ {
2373
+ xmlns: "http://www.w3.org/2000/svg",
2374
+ width: size,
2375
+ height: size,
2376
+ viewBox: "0 0 18 18",
2377
+ fill: "currentColor",
2378
+ ...props,
2379
+ children: /* @__PURE__ */ jsx("path", { d: "M5.48081 15.375C5.10681 15.375 4.78731 15.2426 4.52231 14.9777C4.25744 14.7127 4.125 14.3932 4.125 14.0192V4.50004H3.375V3.37505H6.75V2.71167H11.25V3.37505H14.625V4.50004H13.875V14.0192C13.875 14.3981 13.7438 14.7188 13.4813 14.9813C13.2188 15.2438 12.8981 15.375 12.5192 15.375H5.48081ZM12.75 4.50004H5.25V14.0192C5.25 14.0866 5.27162 14.1419 5.31487 14.1852C5.35812 14.2284 5.41344 14.25 5.48081 14.25H12.5192C12.5769 14.25 12.6298 14.226 12.6778 14.1779C12.7259 14.1299 12.75 14.077 12.75 14.0192V4.50004ZM7.053 12.75H8.17781V6.00004H7.053V12.75ZM9.82219 12.75H10.947V6.00004H9.82219V12.75Z" })
2380
+ }
2381
+ );
2382
+ };
2383
+ var CancelAllBtn = (props) => {
2384
+ const [loading, setLoading] = useState(false);
2385
+ return /* @__PURE__ */ jsx(
2386
+ ThrottledButton,
2387
+ {
2388
+ size: "sm",
2389
+ loading,
2390
+ variant: "text",
2391
+ color: "gray",
2392
+ onClick: (e) => {
2393
+ e.stopPropagation();
2394
+ setLoading(true);
2395
+ props.onCancelOrder?.(props.order).then(
2396
+ () => {
2397
+ },
2398
+ (error) => {
2399
+ toast.error(error.message);
2400
+ }
2401
+ ).finally(() => {
2402
+ setLoading(false);
2403
+ });
2404
+ },
2405
+ children: /* @__PURE__ */ jsx(DeleteIcon, { className: "oui-text-base-contrast-54 hover:oui-text-base-contrast oui-cursor-pointer" })
2406
+ }
2407
+ );
2408
+ };
2409
+ var OrdersTable = (props) => {
2410
+ const { orders } = props;
2411
+ const columns = useColumn({ onCancelOrder: props.onCancelOrder });
2412
+ return (
2413
+ // <ScrollArea className={cn( props.className)}>
2414
+ /* @__PURE__ */ jsx(
2415
+ AuthGuardDataTable,
2416
+ {
2417
+ columns,
2418
+ dataSource: orders,
2419
+ className: "oui-bg-transparent oui-text-2xs",
2420
+ bordered: true,
2421
+ classNames: {
2422
+ root: cn(props.className),
2423
+ header: "!oui-bg-base-8",
2424
+ scroll: cn(
2425
+ !orders || orders.length === 0 ? "!oui-min-h-[170px]" : "!oui-min-h-[100px]"
2426
+ )
2427
+ },
2428
+ onRow: (record) => {
2429
+ return {
2430
+ className: cn(
2431
+ "oui-h-[53px] oui-cursor-svg-edit !oui-border-none !oui-p-0"
2432
+ ),
2433
+ onClick: () => {
2434
+ props.editTPSLOrder(record);
2435
+ }
2436
+ };
2437
+ }
2438
+ }
2439
+ )
2440
+ );
2441
+ };
2442
+ var OrdersTableMobile = (props) => {
2443
+ const { t } = useTranslation();
2444
+ const { orders } = props;
2445
+ const columns = useColumn({ onCancelOrder: () => Promise.resolve() });
2446
+ const orderIds = useMemo(() => {
2447
+ return orders.reduce(
2448
+ (acc, curr) => {
2449
+ acc[curr.algo_order_id] = true;
2450
+ return acc;
2451
+ },
2452
+ {}
2453
+ );
2454
+ }, [orders]);
2455
+ return /* @__PURE__ */ jsx(
2456
+ AuthGuardDataTable,
2457
+ {
2458
+ columns,
2459
+ dataSource: orders,
2460
+ expanded: orderIds,
2461
+ bordered: true,
2462
+ getRowCanExpand: () => true,
2463
+ expandRowRender: (row) => {
2464
+ return /* @__PURE__ */ jsxs(Flex, { gap: 2, justify: "end", children: [
2465
+ /* @__PURE__ */ jsx(
2466
+ Button,
2467
+ {
2468
+ variant: "outlined",
2469
+ size: "sm",
2470
+ color: "gray",
2471
+ className: "oui-h-6 oui-text-2xs oui-text-base-contrast-54",
2472
+ onClick: () => props.editTPSLOrder(row.original),
2473
+ children: t("common.edit")
2474
+ }
2475
+ ),
2476
+ /* @__PURE__ */ jsx(
2477
+ DeleteBtn,
2478
+ {
2479
+ order: row.original,
2480
+ onCancelOrder: props.canCancelOrder
2481
+ }
2482
+ )
2483
+ ] });
2484
+ },
2485
+ className: "oui-bg-transparent oui-px-2 oui-text-2xs",
2486
+ classNames: {
2487
+ scroll: cn(
2488
+ !orders || orders.length === 0 ? "!oui-min-h-[170px]" : "!oui-min-h-[100px]"
2489
+ )
2490
+ },
2491
+ onRow: () => {
2492
+ return {
2493
+ className: cn(
2494
+ "oui-h-[53px] oui-cursor-svg-edit !oui-border-none !oui-p-0"
2495
+ )
2496
+ };
2497
+ },
2498
+ generatedRowKey: (record) => record.algo_order_id
2499
+ }
2500
+ );
2501
+ };
2502
+ var DeleteBtn = (props) => {
2503
+ const [loading, setLoading] = useState(false);
2504
+ const { t } = useTranslation();
2505
+ return /* @__PURE__ */ jsx(
2506
+ ThrottledButton,
2507
+ {
2508
+ className: "oui-h-6 oui-text-2xs oui-text-base-contrast-54",
2509
+ size: "sm",
2510
+ loading,
2511
+ variant: "outlined",
2512
+ color: "gray",
2513
+ onClick: (e) => {
2514
+ e.stopPropagation();
2515
+ setLoading(true);
2516
+ props.onCancelOrder?.(props.order).then(
2517
+ () => {
2518
+ },
2519
+ (error) => {
2520
+ toast.error(error.message);
2521
+ }
2522
+ ).finally(() => {
2523
+ setLoading(false);
2524
+ });
2525
+ },
2526
+ children: t("common.delete")
2527
+ }
2528
+ );
2529
+ };
2530
+ var TPSLDetailUI = (props) => {
2531
+ const { isMobile } = useScreen();
2532
+ const {
2533
+ position,
2534
+ fullPositionOrders,
2535
+ partialPositionOrders,
2536
+ onCancelOrder,
2537
+ onCancelAllTPSLOrders,
2538
+ editTPSLOrder,
2539
+ addTPSLOrder,
2540
+ symbolInfo
2541
+ } = props;
2542
+ const { estLiqPrice } = useTPSLDetailContext();
2543
+ return /* @__PURE__ */ jsx(Box, { children: /* @__PURE__ */ jsxs(ScrollArea, { className: cn(isMobile && "oui-h-[calc(100vh-100px)]"), children: [
2544
+ /* @__PURE__ */ jsx(
2545
+ OrderInfo,
2546
+ {
2547
+ order: {
2548
+ symbol: position.symbol,
2549
+ order_quantity: position.position_qty.toString(),
2550
+ order_price: position.average_open_price.toString()
2551
+ },
2552
+ estLiqPrice,
2553
+ symbolLeverage: position.leverage,
2554
+ baseDP: symbolInfo("base_dp"),
2555
+ quoteDP: symbolInfo("quote_dp"),
2556
+ classNames: {
2557
+ root: cn("oui-mb-6 oui-gap-3 oui-px-5"),
2558
+ container: "oui-gap-x-[30px]"
2559
+ }
2560
+ }
2561
+ ),
2562
+ /* @__PURE__ */ jsx(
2563
+ FullPositionPart,
2564
+ {
2565
+ position,
2566
+ orders: fullPositionOrders,
2567
+ onCancelOrder,
2568
+ onCancelAllTPSLOrders,
2569
+ editTPSLOrder,
2570
+ addTPSLOrder
2571
+ }
2572
+ ),
2573
+ /* @__PURE__ */ jsx(
2574
+ PartialPositionPart,
2575
+ {
2576
+ position,
2577
+ orders: partialPositionOrders,
2578
+ onCancelOrder,
2579
+ onCancelAllTPSLOrders,
2580
+ editTPSLOrder,
2581
+ addTPSLOrder
2582
+ }
2583
+ )
2584
+ ] }) });
2585
+ };
2586
+ var FullPositionPart = (props) => {
2587
+ const [open, setOpen] = useState(true);
2588
+ const { orders } = props;
2589
+ const { isMobile } = useScreen();
2590
+ return /* @__PURE__ */ jsxs(Box, { className: "oui-mt-6", children: [
2591
+ /* @__PURE__ */ jsxs(
2592
+ Box,
2593
+ {
2594
+ className: cn("oui-flex oui-items-center oui-justify-between oui-px-5"),
2595
+ children: [
2596
+ /* @__PURE__ */ jsx(
2597
+ PositionTypeDescription,
2598
+ {
2599
+ positionType: PositionType.FULL,
2600
+ open,
2601
+ onOpenChange: setOpen
2602
+ }
2603
+ ),
2604
+ orders && orders.length === 0 && /* @__PURE__ */ jsx(Flex, { gap: 2, children: /* @__PURE__ */ jsx(
2605
+ AddButton,
2606
+ {
2607
+ positionType: PositionType.FULL,
2608
+ position: props.position,
2609
+ addTPSLOrder: props.addTPSLOrder
2610
+ }
2611
+ ) })
2612
+ ]
2613
+ }
2614
+ ),
2615
+ /* @__PURE__ */ jsx(
2616
+ Box,
2617
+ {
2618
+ className: cn(
2619
+ "oui-overflow-hidden oui-transition-[height] oui-duration-150",
2620
+ open ? "oui-h-auto" : "oui-h-0 oui-pb-4"
2621
+ ),
2622
+ children: isMobile ? /* @__PURE__ */ jsx(
2623
+ OrdersTableMobile,
2624
+ {
2625
+ orders,
2626
+ editTPSLOrder: (order2) => props.editTPSLOrder(order2, PositionType.FULL),
2627
+ canCancelOrder: props.onCancelOrder
2628
+ }
2629
+ ) : /* @__PURE__ */ jsx(
2630
+ OrdersTable,
2631
+ {
2632
+ orders,
2633
+ editTPSLOrder: (order2) => {
2634
+ props.editTPSLOrder(order2, PositionType.FULL);
2635
+ },
2636
+ onCancelOrder: props.onCancelOrder
2637
+ }
2638
+ )
2639
+ }
2640
+ )
2641
+ ] });
2642
+ };
2643
+ var PartialPositionPart = (props) => {
2644
+ const [open, setOpen] = useState(true);
2645
+ const { orders } = props;
2646
+ const { isMobile } = useScreen();
2647
+ return /* @__PURE__ */ jsxs(Box, { className: "oui-pt-6", children: [
2648
+ /* @__PURE__ */ jsxs(
2649
+ Box,
2650
+ {
2651
+ className: cn(
2652
+ "oui-flex oui-items-center oui-justify-between",
2653
+ "oui-px-5",
2654
+ isMobile ? "oui-flex-col oui-items-start oui-justify-start oui-gap-[11px]" : "oui-flex-row oui-items-center oui-justify-between"
2655
+ ),
2656
+ children: [
2657
+ /* @__PURE__ */ jsx(
2658
+ PositionTypeDescription,
2659
+ {
2660
+ positionType: PositionType.PARTIAL,
2661
+ open,
2662
+ onOpenChange: setOpen
2663
+ }
2664
+ ),
2665
+ /* @__PURE__ */ jsxs(Flex, { gap: 2, children: [
2666
+ /* @__PURE__ */ jsx(
2667
+ AddButton,
2668
+ {
2669
+ positionType: PositionType.PARTIAL,
2670
+ position: props.position,
2671
+ addTPSLOrder: props.addTPSLOrder
2672
+ }
2673
+ ),
2674
+ orders && orders.length > 0 && /* @__PURE__ */ jsx(
2675
+ CancelAllBtn2,
2676
+ {
2677
+ canCancelAll: orders && orders.length > 0,
2678
+ onCancelAllTPSLOrders: props.onCancelAllTPSLOrders
2679
+ }
2680
+ )
2681
+ ] })
2682
+ ]
2683
+ }
2684
+ ),
2685
+ /* @__PURE__ */ jsx(
2686
+ Box,
2687
+ {
2688
+ className: cn(
2689
+ "oui-overflow-hidden oui-transition-[height] oui-duration-150",
2690
+ open ? "oui-h-auto" : "oui-h-0 oui-pb-4"
2691
+ ),
2692
+ children: isMobile ? /* @__PURE__ */ jsx(
2693
+ OrdersTableMobile,
2694
+ {
2695
+ orders,
2696
+ editTPSLOrder: (order2) => props.editTPSLOrder(order2, PositionType.PARTIAL),
2697
+ canCancelOrder: props.onCancelOrder
2698
+ }
2699
+ ) : /* @__PURE__ */ jsx(
2700
+ OrdersTable,
2701
+ {
2702
+ className: "oui-h-[300px]",
2703
+ orders,
2704
+ editTPSLOrder: (order2) => {
2705
+ props.editTPSLOrder(order2, PositionType.PARTIAL);
2706
+ },
2707
+ onCancelOrder: props.onCancelOrder
2708
+ }
2709
+ )
2710
+ }
2711
+ )
2712
+ ] });
2713
+ };
2714
+ var PositionTypeDescription = (props) => {
2715
+ const { t } = useTranslation();
2716
+ return /* @__PURE__ */ jsxs(
2717
+ Flex,
2718
+ {
2719
+ gap: 1,
2720
+ itemAlign: "center",
2721
+ justify: "start",
2722
+ className: "oui-text-2xs",
2723
+ children: [
2724
+ /* @__PURE__ */ jsxs(
2725
+ Flex,
2726
+ {
2727
+ itemAlign: "center",
2728
+ justify: "start",
2729
+ gap: 1,
2730
+ className: "oui-cursor-pointer",
2731
+ onClick: () => props.onOpenChange(!props.open),
2732
+ children: [
2733
+ /* @__PURE__ */ jsx(
2734
+ ChevronDownIcon,
2735
+ {
2736
+ size: 12,
2737
+ color: "white",
2738
+ className: cn(
2739
+ "oui-cursor-pointer oui-transition-transform",
2740
+ props.open && "oui-rotate-180"
2741
+ )
2742
+ }
2743
+ ),
2744
+ props.positionType === PositionType.FULL ? /* @__PURE__ */ jsx(Text, { children: t("tpsl.positionType.full") }) : /* @__PURE__ */ jsx(Text, { children: t("tpsl.positionType.partial") })
2745
+ ]
2746
+ }
2747
+ ),
2748
+ /* @__PURE__ */ jsx(
2749
+ Tooltip,
2750
+ {
2751
+ className: "oui-w-[280px] oui-p-3",
2752
+ content: props.positionType === PositionType.FULL ? t("tpsl.positionType.full.tips") : t("tpsl.positionType.partial.tips"),
2753
+ children: /* @__PURE__ */ jsx(
2754
+ ExclamationFillIcon,
2755
+ {
2756
+ className: "oui-cursor-pointer oui-text-base-contrast-54",
2757
+ size: 12
2758
+ }
2759
+ )
2760
+ }
2761
+ )
2762
+ ]
2763
+ }
2764
+ );
2765
+ };
2766
+ var AddButton = (props) => {
2767
+ const { t } = useTranslation();
2768
+ const onAdd = () => {
2769
+ props.addTPSLOrder(props.positionType);
2770
+ };
2771
+ return /* @__PURE__ */ jsx(
2772
+ ThrottledButton,
2773
+ {
2774
+ variant: "outlined",
2775
+ size: "sm",
2776
+ color: "gray",
2777
+ className: "oui-h-6 oui-w-[94px] oui-text-2xs",
2778
+ onClick: onAdd,
2779
+ children: t("tpsl.add")
2780
+ }
2781
+ );
2782
+ };
2783
+ var CancelAllBtn2 = (props) => {
2784
+ const [loading, setLoading] = useState(false);
2785
+ const { t } = useTranslation();
2786
+ return /* @__PURE__ */ jsx(
2787
+ ThrottledButton,
2788
+ {
2789
+ loading,
2790
+ variant: "outlined",
2791
+ disabled: !props.canCancelAll,
2792
+ size: "sm",
2793
+ color: "gray",
2794
+ className: "oui-h-6 oui-w-[94px] oui-text-2xs disabled:oui-border-base-contrast-16 disabled:oui-bg-transparent disabled:oui-text-base-contrast-20",
2795
+ onClick: () => {
2796
+ setLoading(true);
2797
+ props.onCancelAllTPSLOrders().then(
2798
+ () => {
2799
+ },
2800
+ (error) => {
2801
+ toast.error(error.message);
2802
+ }
2803
+ ).finally(() => {
2804
+ setLoading(false);
2805
+ });
2806
+ },
2807
+ children: t("tpsl.cancelAll")
2808
+ }
2809
+ );
2810
+ };
2811
+ var TPSLDetailWidget = (props) => {
2812
+ const state = useTPSLDetail(props);
2813
+ return /* @__PURE__ */ jsx(
2814
+ TPSLDetailProvider,
2815
+ {
2816
+ symbol: props.position.symbol,
2817
+ position: props.position,
2818
+ children: /* @__PURE__ */ jsx(TPSLDetailUI, { ...state })
2819
+ }
2820
+ );
2821
+ };
2822
+ var TPSLDetailDialogId = "TPSLDetailDialogId ";
2823
+ var TPSLDetailSheetId = "TPSLDetailSheetId";
2824
+ registerSimpleDialog(TPSLDetailDialogId, TPSLDetailWidget, {
2825
+ classNames: {
2826
+ content: "oui-w-[420px] lg:oui-pt-4 lg:oui-pb-2 !oui-px-0",
2827
+ body: "lg:oui-py-0"
2828
+ }
2829
+ });
2830
+ registerSimpleSheet(TPSLDetailSheetId, TPSLDetailWidget, {
2831
+ classNames: {
2832
+ content: "!oui-p-0"
2833
+ }
2834
+ });
2835
+ var useTPSLSimpleDialog = (options) => {
2836
+ const { type, triggerPrice, symbol } = options;
2837
+ const symbolInfo = useSymbolsInfo();
2838
+ const [{ rows: positions }, positionsInfo] = usePositionStream(symbol);
2839
+ const position = positions?.[0];
2840
+ useRef(AlgoOrderRootType.TP_SL);
2841
+ const [needConfirm] = useLocalStorage("orderly_order_confirm", true);
2842
+ const { t } = useTranslation();
2843
+ const [
2844
+ tpslOrder,
2845
+ {
2846
+ submit,
2847
+ deleteOrder,
2848
+ setValue,
2849
+ setValues,
2850
+ validate,
2851
+ errors,
2852
+ isCreateMutating,
2853
+ isUpdateMutating
2854
+ }
2855
+ ] = useTPSLOrder(
2856
+ {
2857
+ symbol,
2858
+ position_qty: position?.position_qty,
2859
+ average_open_price: position?.average_open_price
2860
+ },
2861
+ {
2862
+ defaultOrder: void 0,
2863
+ positionType: PositionType.PARTIAL,
2864
+ // tpslEnable: { tp_enable: type === "tp", sl_enable: type === "sl" },
2865
+ isEditing: false
2866
+ }
2867
+ );
2868
+ const setQuantity = (value) => {
2869
+ setValue("quantity", value);
2870
+ };
2871
+ const setOrderPrice = (name, value) => {
2872
+ setValue(name, value);
2873
+ };
2874
+ const setPnL = (type2, value) => {
2875
+ setValue(type2, value);
2876
+ };
2877
+ const maxQty = useMemo(
2878
+ () => Math.abs(Number(position.position_qty)),
2879
+ [position.position_qty]
2880
+ );
2881
+ useEffect(() => {
2882
+ if (!maxQty) {
2883
+ return;
2884
+ }
2885
+ setValue("quantity", maxQty);
2886
+ if (type === "tp") {
2887
+ setValue("tp_trigger_price", triggerPrice ?? "");
2888
+ } else {
2889
+ setValue("sl_trigger_price", triggerPrice ?? "");
2890
+ }
2891
+ }, [type, triggerPrice, maxQty]);
2892
+ const onSubmit = async () => {
2893
+ try {
2894
+ const validOrder = await validate();
2895
+ if (validOrder) {
2896
+ if (!needConfirm) {
2897
+ return submit({ accountId: position.account_id }).then(() => true).catch((err) => {
2898
+ if (err?.message) {
2899
+ toast.error(err.message);
2900
+ }
2901
+ throw false;
2902
+ });
2903
+ }
2904
+ return modal.confirm({
2905
+ title: t("tpsl.confirmOrder"),
2906
+ // bodyClassName: "lg:oui-py-0",
2907
+ onOk: async () => {
2908
+ try {
2909
+ const res = await submit({
2910
+ accountId: position.account_id
2911
+ });
2912
+ if (res.success) {
2913
+ return res;
2914
+ }
2915
+ if (res.message) {
2916
+ toast.error(res.message);
2917
+ }
2918
+ return false;
2919
+ } catch (err) {
2920
+ if (err?.message) {
2921
+ toast.error(err.message);
2922
+ }
2923
+ return false;
2924
+ }
2925
+ },
2926
+ classNames: {
2927
+ body: "!oui-pb-0"
2928
+ },
2929
+ content: /* @__PURE__ */ jsx(
2930
+ PositionTPSLConfirm,
2931
+ {
2932
+ isPositionTPSL: false,
2933
+ isEditing: false,
2934
+ symbol,
2935
+ qty: Number(tpslOrder.quantity),
2936
+ maxQty,
2937
+ tpPrice: Number(tpslOrder.tp_trigger_price),
2938
+ slPrice: Number(tpslOrder.sl_trigger_price),
2939
+ side: tpslOrder.side,
2940
+ orderInfo: tpslOrder,
2941
+ quoteDP: symbolInfo[symbol]("quote_dp"),
2942
+ baseDP: symbolInfo[symbol]("base_dp")
2943
+ }
2944
+ )
2945
+ }).then(
2946
+ () => {
2947
+ return true;
2948
+ },
2949
+ () => {
2950
+ return Promise.reject(false);
2951
+ }
2952
+ );
2953
+ }
2954
+ } catch (error) {
2955
+ return Promise.reject(error);
2956
+ }
2957
+ };
2958
+ return {
2959
+ symbolInfo: symbolInfo[symbol],
2960
+ maxQty,
2961
+ setQuantity: useMemoizedFn(setQuantity),
2962
+ orderQuantity: tpslOrder.quantity,
2963
+ isPosition: false,
2964
+ TPSL_OrderEntity: tpslOrder,
2965
+ setOrderValue: setValue,
2966
+ setPnL,
2967
+ setOrderPrice,
2968
+ onSubmit,
2969
+ errors,
2970
+ status: {
2971
+ isCreateMutating,
2972
+ isUpdateMutating
2973
+ },
2974
+ position,
2975
+ setValues,
2976
+ type,
2977
+ triggerPrice
2978
+ };
2979
+ };
2980
+ var TPSLSimpleDialogUI = (props) => {
2981
+ const { type, triggerPrice, errors, TPSL_OrderEntity } = props;
2982
+ const { t } = useTranslation();
2983
+ const { getErrorMsg } = useOrderEntryFormErrorMsg(errors);
2984
+ const footer = /* @__PURE__ */ jsxs(Flex, { width: "100%", itemAlign: "center", gap: 3, mt: 4, children: [
2985
+ /* @__PURE__ */ jsx(
2986
+ Button,
2987
+ {
2988
+ color: "gray",
2989
+ onClick: () => {
2990
+ props.onComplete?.();
2991
+ props.close?.();
2992
+ },
2993
+ fullWidth: true,
2994
+ className: "oui-text-sm",
2995
+ size: "md",
2996
+ children: t("common.cancel")
2997
+ },
2998
+ "secondary"
2999
+ ),
3000
+ /* @__PURE__ */ jsx(
3001
+ Button,
3002
+ {
3003
+ onClick: () => {
3004
+ props.onSubmit().then(() => {
3005
+ props.onComplete?.();
3006
+ props.close?.();
3007
+ });
3008
+ },
3009
+ fullWidth: true,
3010
+ className: "oui-text-sm",
3011
+ size: "md",
3012
+ children: t("common.confirm")
3013
+ },
3014
+ "primary"
3015
+ )
3016
+ ] });
3017
+ const errorMessage = () => {
3018
+ const tpError = getErrorMsg("tp_trigger_price");
3019
+ const slError = getErrorMsg("sl_trigger_price");
3020
+ let text = "";
3021
+ if (tpError && type === "tp") {
3022
+ text = tpError;
3023
+ }
3024
+ if (slError && type === "sl") {
3025
+ text = slError;
3026
+ }
3027
+ if (!text) {
3028
+ return null;
3029
+ }
3030
+ return /* @__PURE__ */ jsxs(
3031
+ Flex,
3032
+ {
3033
+ justify: "start",
3034
+ itemAlign: "start",
3035
+ gap: 2,
3036
+ className: "oui-mt-2 oui-w-full",
3037
+ children: [
3038
+ /* @__PURE__ */ jsx("div", { className: "oui-relative oui-top-[7px] oui-size-1 oui-rounded-full oui-bg-danger" }),
3039
+ /* @__PURE__ */ jsx(Text, { className: "oui-text-danger", children: text })
3040
+ ]
3041
+ }
3042
+ );
3043
+ };
3044
+ const renderPnl = () => {
3045
+ const { tp_pnl, sl_pnl } = TPSL_OrderEntity;
3046
+ if (type === "sl") {
3047
+ return /* @__PURE__ */ jsxs(Flex, { justify: "between", className: "oui-w-full", children: [
3048
+ /* @__PURE__ */ jsx(Text, { size: "2xs", children: t("tpsl.totalEstSlPnl") }),
3049
+ sl_pnl ? /* @__PURE__ */ jsx(
3050
+ Text.numeral,
3051
+ {
3052
+ suffix: /* @__PURE__ */ jsx(Text, { className: "oui-ml-1 oui-text-base-contrast-36", children: "USDC" }),
3053
+ coloring: true,
3054
+ visible: true,
3055
+ size: "2xs",
3056
+ dp: 2,
3057
+ children: Number(sl_pnl)
3058
+ }
3059
+ ) : /* @__PURE__ */ jsx(Text, { size: "2xs", children: "-- USDC" })
3060
+ ] });
3061
+ }
3062
+ return /* @__PURE__ */ jsxs(Flex, { justify: "between", className: "oui-w-full", children: [
3063
+ /* @__PURE__ */ jsx(Text, { size: "2xs", children: t("tpsl.totalEstTpPnl") }),
3064
+ /* @__PURE__ */ jsx(
3065
+ Text.numeral,
3066
+ {
3067
+ suffix: /* @__PURE__ */ jsx(Text, { className: "oui-text-base-contrast-36 oui-ml-1", children: "USDC" }),
3068
+ rule: "price",
3069
+ coloring: true,
3070
+ visible: true,
3071
+ size: "2xs",
3072
+ dp: 2,
3073
+ children: tp_pnl ? Number(tp_pnl) : "--"
3074
+ }
3075
+ )
3076
+ ] });
3077
+ };
3078
+ return /* @__PURE__ */ jsxs(Box, { className: "oui-w-full oui-px-0.5", children: [
3079
+ /* @__PURE__ */ jsx(
3080
+ TPSLQuantity,
3081
+ {
3082
+ maxQty: props.maxQty,
3083
+ quantity: Number(props.orderQuantity ?? props.maxQty),
3084
+ baseTick: props.symbolInfo("base_tick"),
3085
+ base_dp: props.symbolInfo("base_dp"),
3086
+ base: props.symbolInfo("base"),
3087
+ isEditing: false,
3088
+ errorMsg: getErrorMsg("quantity"),
3089
+ onQuantityChange: props.setQuantity
3090
+ }
3091
+ ),
3092
+ /* @__PURE__ */ jsxs(
3093
+ Flex,
3094
+ {
3095
+ direction: "column",
3096
+ itemAlign: "start",
3097
+ className: cn(
3098
+ "oui-mt-4 oui-w-full oui-gap-1 oui-text-xs oui-text-base-contrast-36"
3099
+ ),
3100
+ children: [
3101
+ /* @__PURE__ */ jsxs(Flex, { justify: "between", className: "oui-w-full", children: [
3102
+ /* @__PURE__ */ jsx(Text, { size: "xs", children: t("common.triggerPrice") }),
3103
+ /* @__PURE__ */ jsx(
3104
+ Text.numeral,
3105
+ {
3106
+ className: "oui-text-base-contrast",
3107
+ suffix: /* @__PURE__ */ jsx(Text, { className: "oui-ml-1 oui-text-xs oui-text-base-contrast-36", children: "USDC" }),
3108
+ rule: "price",
3109
+ size: "xs",
3110
+ dp: props.symbolInfo("quote_dp"),
3111
+ children: triggerPrice ? Number(triggerPrice) : "--"
3112
+ }
3113
+ )
3114
+ ] }),
3115
+ /* @__PURE__ */ jsxs(Flex, { justify: "between", className: "oui-w-full", children: [
3116
+ /* @__PURE__ */ jsx(Text, { size: "xs", children: t("common.orderPrice") }),
3117
+ /* @__PURE__ */ jsx(Text, { className: "oui-text-base-contrast", size: "xs", children: t("common.market") })
3118
+ ] }),
3119
+ renderPnl()
3120
+ ]
3121
+ }
3122
+ ),
3123
+ errorMessage(),
3124
+ /* @__PURE__ */ jsx(Divider, { className: "oui-my-3 oui-w-full" }),
3125
+ /* @__PURE__ */ jsx(
3126
+ Flex,
3127
+ {
3128
+ itemAlign: "center",
3129
+ onClick: () => {
3130
+ props.close?.();
3131
+ props.showAdvancedTPSLDialog?.({
3132
+ qty: Number(TPSL_OrderEntity.quantity)
3133
+ });
3134
+ },
3135
+ className: "oui-pb-4",
3136
+ children: /* @__PURE__ */ jsx(Text, { color: "primary", className: "oui-cursor-pointer oui-text-sm", children: t("tpsl.advancedSetting") })
3137
+ }
3138
+ ),
3139
+ footer
3140
+ ] });
3141
+ };
3142
+ var TPSLSimpleDialogWidget = (props) => {
3143
+ const { close, onComplete, showAdvancedTPSLDialog } = props;
3144
+ const state = useTPSLSimpleDialog(props);
3145
+ return /* @__PURE__ */ jsx(
3146
+ TPSLSimpleDialogUI,
3147
+ {
3148
+ ...state,
3149
+ close,
3150
+ onComplete,
3151
+ showAdvancedTPSLDialog
3152
+ }
3153
+ );
3154
+ };
3155
+ var TPSLSimpleSheetId = "TPSLSimpleSheetId";
3156
+ var TPSLSimpleDialogId = "TPSLSimpleDialogId";
3157
+ registerSimpleSheet(TPSLSimpleSheetId, TPSLSimpleDialogWidget, {
3158
+ classNames: {}
3159
+ });
3160
+ registerSimpleDialog(TPSLSimpleDialogId, TPSLSimpleDialogWidget, {
3161
+ classNames: {
3162
+ content: "oui-w-[420px]"
3163
+ }
3164
+ });
3165
+ function getInitialOrder(order2) {
3166
+ const childOrder = order2.child_orders[0];
3167
+ const positionType = childOrder.algo_type === AlgoOrderRootType.TP_SL ? PositionType.PARTIAL : PositionType.FULL;
3168
+ const tpOrder = childOrder.child_orders.find(
3169
+ (item) => item.algo_type === AlgoOrderType.TAKE_PROFIT && item.trigger_price
3170
+ );
3171
+ const slOrder = childOrder.child_orders.find(
3172
+ (item) => item.algo_type === AlgoOrderType.STOP_LOSS && item.trigger_price
3173
+ );
3174
+ const tpslPriceInfo = {};
3175
+ if (tpOrder) {
3176
+ tpslPriceInfo.tp_trigger_price = tpOrder.trigger_price?.toString();
3177
+ tpslPriceInfo.tp_order_type = typeof tpOrder.type === "string" ? tpOrder.type.replace("_ORDER", "") : void 0;
3178
+ if (tpslPriceInfo.tp_order_type === OrderType.LIMIT) {
3179
+ tpslPriceInfo.tp_order_price = tpOrder.price?.toString();
3180
+ }
3181
+ }
3182
+ if (slOrder) {
3183
+ tpslPriceInfo.sl_trigger_price = slOrder.trigger_price?.toString();
3184
+ tpslPriceInfo.sl_order_type = typeof slOrder.type === "string" ? slOrder.type.replace("_ORDER", "") : void 0;
3185
+ if (tpslPriceInfo.sl_order_type === OrderType.LIMIT) {
3186
+ tpslPriceInfo.sl_order_price = slOrder.price?.toString();
3187
+ }
3188
+ }
3189
+ return {
3190
+ baseInfo: {
3191
+ symbol: order2.symbol,
3192
+ order_type: order2.type,
3193
+ side: order2.side,
3194
+ order_price: order2.price,
3195
+ order_quantity: order2.quantity,
3196
+ position_type: positionType
3197
+ // tp_enable: !!tpOrder?.trigger_price,
3198
+ // sl_enable: !!slOrder?.trigger_price,
3199
+ },
3200
+ tpslPriceInfo,
3201
+ tpInfo: {
3202
+ orderId: tpOrder?.algo_order_id
3203
+ },
3204
+ slInfo: {
3205
+ orderId: slOrder?.algo_order_id
3206
+ }
3207
+ };
3208
+ }
3209
+ function isTPSLPriceChanged(originPrice, newPrice) {
3210
+ if (newPrice === void 0 || newPrice === null) {
3211
+ return true;
3212
+ }
3213
+ if (isNaN(Number(newPrice))) {
3214
+ return false;
3215
+ }
3216
+ const originDeci = new Decimal(Number(originPrice));
3217
+ const newDeci = new Decimal(Number(newPrice));
3218
+ return !newDeci.eq(originDeci);
3219
+ }
3220
+ var useEditBracketOrder = (props) => {
3221
+ if (!props.order) {
3222
+ throw new SDKError("order is required for editBracketOrder");
3223
+ }
3224
+ const { baseInfo, tpslPriceInfo, tpInfo, slInfo } = getInitialOrder(
3225
+ props.order
3226
+ );
3227
+ const [doUpdateOrder, { isMutating }] = useMutation("/v1/algo/order", "PUT");
3228
+ const maxQty = useEditOrderMaxQty(props.order, props.order.quantity);
3229
+ const {
3230
+ formattedOrder,
3231
+ setValue,
3232
+ setValues,
3233
+ estLiqPrice,
3234
+ metaState,
3235
+ symbolInfo,
3236
+ helper,
3237
+ ...state
3238
+ } = useOrderEntry(props.order.symbol, {
3239
+ initialOrder: baseInfo,
3240
+ maxQty
3241
+ });
3242
+ const symbol = props.order.symbol;
3243
+ const isPriceChanged = useMemo(() => {
3244
+ let dirty = false;
3245
+ const {
3246
+ tp_order_price,
3247
+ sl_order_price,
3248
+ tp_trigger_price,
3249
+ sl_trigger_price
3250
+ } = formattedOrder;
3251
+ if (tpslPriceInfo.tp_trigger_price) {
3252
+ dirty = dirty || isTPSLPriceChanged(
3253
+ tpslPriceInfo.tp_trigger_price,
3254
+ tp_trigger_price ?? 0
3255
+ );
3256
+ }
3257
+ if (tpslPriceInfo.tp_order_price) {
3258
+ dirty = dirty || isTPSLPriceChanged(tpslPriceInfo.tp_order_price, tp_order_price ?? 0);
3259
+ }
3260
+ if (tpslPriceInfo.sl_trigger_price) {
3261
+ dirty = dirty || isTPSLPriceChanged(
3262
+ tpslPriceInfo.sl_trigger_price,
3263
+ sl_trigger_price ?? 0
3264
+ );
3265
+ }
3266
+ if (tpslPriceInfo.sl_order_price) {
3267
+ dirty = dirty || isTPSLPriceChanged(tpslPriceInfo.sl_order_price, sl_order_price ?? 0);
3268
+ }
3269
+ return dirty;
3270
+ }, [
3271
+ tpslPriceInfo,
3272
+ formattedOrder.tp_order_price,
3273
+ formattedOrder.sl_order_price,
3274
+ formattedOrder.tp_trigger_price,
3275
+ formattedOrder.sl_trigger_price
3276
+ ]);
3277
+ useEffect(() => {
3278
+ setValues({
3279
+ ...tpslPriceInfo
3280
+ });
3281
+ }, [props.order, setValues]);
3282
+ const slPriceError = useTpslPriceChecker({
3283
+ slPrice: formattedOrder.sl_trigger_price,
3284
+ liqPrice: estLiqPrice,
3285
+ side: formattedOrder.side
3286
+ });
3287
+ const isSlPriceError = slPriceError?.sl_trigger_price?.type === ERROR_MSG_CODES.SL_PRICE_ERROR;
3288
+ const onSubmit = async () => {
3289
+ return helper.validate(isSlPriceError ? slPriceError : void 0).then(() => {
3290
+ const tpOrder = {
3291
+ order_id: tpInfo.orderId,
3292
+ algo_type: AlgoOrderType.TAKE_PROFIT,
3293
+ trigger_price: formattedOrder.tp_trigger_price,
3294
+ reduce_only: true
3295
+ };
3296
+ if (formattedOrder.tp_order_type === OrderType.LIMIT) {
3297
+ tpOrder.price = formattedOrder.tp_order_price;
3298
+ }
3299
+ const slOrder = {
3300
+ order_id: slInfo.orderId,
3301
+ algo_type: AlgoOrderType.STOP_LOSS,
3302
+ trigger_price: formattedOrder.sl_trigger_price,
3303
+ reduce_only: true
3304
+ };
3305
+ if (formattedOrder.sl_order_type === OrderType.LIMIT) {
3306
+ slOrder.price = formattedOrder.sl_order_price;
3307
+ }
3308
+ const childOrders = [];
3309
+ if (tpInfo.orderId) {
3310
+ childOrders.push(tpOrder);
3311
+ }
3312
+ if (slInfo.orderId) {
3313
+ childOrders.push(slOrder);
3314
+ }
3315
+ return doUpdateOrder({
3316
+ order_id: props.order.algo_order_id,
3317
+ child_orders: [
3318
+ {
3319
+ order_id: props.order.child_orders[0].algo_order_id,
3320
+ child_orders: childOrders
3321
+ }
3322
+ ]
3323
+ });
3324
+ });
3325
+ };
3326
+ return {
3327
+ symbol,
3328
+ symbolInfo,
3329
+ slPriceError,
3330
+ estLiqPrice,
3331
+ side: formattedOrder.side,
3332
+ formattedOrder,
3333
+ setValue,
3334
+ setValues,
3335
+ metaState,
3336
+ onSubmit,
3337
+ isMutating,
3338
+ isPriceChanged
3339
+ };
3340
+ };
3341
+ function useEditOrderMaxQty(order2, positionQty) {
3342
+ const { reduce_only } = order2;
3343
+ const maxQty = useMaxQty(order2.symbol, order2.side, reduce_only);
3344
+ return useMemo(() => {
3345
+ if (reduce_only) {
3346
+ return Math.abs(positionQty ?? 0);
3347
+ }
3348
+ return order2.quantity + Math.abs(maxQty);
3349
+ }, [order2.quantity, maxQty, reduce_only, positionQty]);
3350
+ }
3351
+ var EditBracketOrderUI = (props) => {
3352
+ const { t } = useTranslation();
3353
+ const { errors, validated } = props.metaState;
3354
+ const { isMobile } = useScreen();
3355
+ const isSlPriceWarning = props.slPriceError?.sl_trigger_price?.type === ERROR_MSG_CODES.SL_PRICE_WARNING;
3356
+ const { getErrorMsg } = useOrderEntryFormErrorMsg(props.slPriceError);
3357
+ const {
3358
+ formattedOrder,
3359
+ setValue: setOrderValue,
3360
+ symbol,
3361
+ symbolInfo,
3362
+ setValues
3363
+ } = props;
3364
+ const [tpValues, setTpValuse] = useState({
3365
+ // enable: false,
3366
+ order_type: formattedOrder.tp_order_type ?? OrderType.MARKET,
3367
+ order_price: formattedOrder.tp_order_price ?? "",
3368
+ trigger_price: formattedOrder.tp_trigger_price ?? "",
3369
+ PnL: formattedOrder.tp_pnl ?? "",
3370
+ Offset: formattedOrder.tp_offset ?? "",
3371
+ "Offset%": formattedOrder.tp_offset_percentage ?? "",
3372
+ ROI: formattedOrder.tp_ROI ?? ""
3373
+ });
3374
+ const [slValues, setSlValues] = useState({
3375
+ // enable: false,
3376
+ order_type: formattedOrder.sl_order_type ?? OrderType.MARKET,
3377
+ order_price: formattedOrder.sl_order_price ?? "",
3378
+ trigger_price: formattedOrder.sl_trigger_price ?? "",
3379
+ PnL: formattedOrder.sl_pnl ?? "",
3380
+ Offset: formattedOrder.sl_offset ?? "",
3381
+ "Offset%": formattedOrder.sl_offset_percentage ?? "",
3382
+ ROI: formattedOrder.sl_ROI ?? ""
3383
+ });
3384
+ useEffect(() => {
3385
+ setTpValuse((prev) => ({
3386
+ ...prev,
3387
+ // enable: formattedOrder.tp_enable ?? false,
3388
+ order_type: formattedOrder.tp_order_type ?? OrderType.MARKET,
3389
+ order_price: formattedOrder.tp_order_price ?? "",
3390
+ trigger_price: formattedOrder.tp_trigger_price ?? "",
3391
+ PnL: formattedOrder.tp_pnl ?? "",
3392
+ Offset: formattedOrder.tp_offset ?? "",
3393
+ "Offset%": formattedOrder.tp_offset_percentage ?? "",
3394
+ ROI: formattedOrder.tp_ROI ?? ""
3395
+ }));
3396
+ }, [formattedOrder]);
3397
+ useEffect(() => {
3398
+ setSlValues((prev) => ({
3399
+ ...prev,
3400
+ // enable: formattedOrder.sl_enable ?? false,
3401
+ order_type: formattedOrder.sl_order_type ?? OrderType.MARKET,
3402
+ order_price: formattedOrder.sl_order_price ?? "",
3403
+ trigger_price: formattedOrder.sl_trigger_price ?? "",
3404
+ PnL: formattedOrder.sl_pnl ?? "",
3405
+ Offset: formattedOrder.sl_offset ?? "",
3406
+ "Offset%": formattedOrder.sl_offset_percentage ?? "",
3407
+ ROI: formattedOrder.sl_ROI ?? ""
3408
+ }));
3409
+ }, [formattedOrder]);
3410
+ return /* @__PURE__ */ jsxs("div", { children: [
3411
+ /* @__PURE__ */ jsxs(
3412
+ ScrollArea,
3413
+ {
3414
+ className: cn("oui-flex-1", isMobile && "oui-h-[calc(100vh-200px)]"),
3415
+ children: [
3416
+ /* @__PURE__ */ jsx("div", { className: "", children: /* @__PURE__ */ jsx(
3417
+ OrderInfo,
3418
+ {
3419
+ order: {
3420
+ symbol,
3421
+ order_quantity: formattedOrder.order_quantity,
3422
+ order_price: formattedOrder.order_price
3423
+ },
3424
+ estLiqPrice: props.estLiqPrice ?? void 0,
3425
+ baseDP: symbolInfo.base_dp,
3426
+ quoteDP: symbolInfo.quote_dp
3427
+ }
3428
+ ) }),
3429
+ /* @__PURE__ */ jsx(Divider, { className: "oui-my-3" }),
3430
+ /* @__PURE__ */ jsxs("div", { className: "", children: [
3431
+ /* @__PURE__ */ jsxs("div", { className: "oui-py-3", children: [
3432
+ /* @__PURE__ */ jsx(
3433
+ TPSLPositionTypeWidget,
3434
+ {
3435
+ value: formattedOrder.position_type ?? PositionType.PARTIAL,
3436
+ disableSelector: true,
3437
+ onChange: (key, value) => {
3438
+ if (value === PositionType.FULL) {
3439
+ setValues({
3440
+ position_type: PositionType.FULL,
3441
+ tp_order_type: OrderType.MARKET,
3442
+ tp_order_price: void 0,
3443
+ sl_order_type: OrderType.MARKET,
3444
+ sl_order_price: void 0
3445
+ });
3446
+ return;
3447
+ }
3448
+ setOrderValue("position_type", value);
3449
+ }
3450
+ }
3451
+ ),
3452
+ formattedOrder.position_type === PositionType.FULL && /* @__PURE__ */ jsxs(
3453
+ Flex,
3454
+ {
3455
+ justify: "start",
3456
+ itemAlign: "start",
3457
+ gap: 2,
3458
+ className: "oui-mt-3 oui-w-full",
3459
+ children: [
3460
+ /* @__PURE__ */ jsx("div", { className: "oui-relative oui-top-[7px] oui-size-1 oui-rounded-full oui-bg-[#D25f00]" }),
3461
+ /* @__PURE__ */ jsx(Text, { className: "oui-text-2xs oui-text-[#D25f00]", children: t("tpsl.positionType.full.tips.market") })
3462
+ ]
3463
+ }
3464
+ )
3465
+ ] }),
3466
+ /* @__PURE__ */ jsxs(Flex, { direction: "column", gap: 6, children: [
3467
+ /* @__PURE__ */ jsx(
3468
+ TPSLInputRowWidget,
3469
+ {
3470
+ disableEnableCheckbox: true,
3471
+ disableOrderTypeSelector: true,
3472
+ rootOrderPrice: formattedOrder.order_price,
3473
+ symbol: symbolInfo.symbol,
3474
+ type: "tp",
3475
+ side: formattedOrder.side,
3476
+ values: tpValues,
3477
+ errors: validated ? errors : null,
3478
+ quote_dp: symbolInfo.quote_dp,
3479
+ hideOrderPrice: formattedOrder.position_type === PositionType.FULL,
3480
+ onChange: (key, value) => {
3481
+ setOrderValue(key, value);
3482
+ },
3483
+ positionType: formattedOrder.position_type ?? PositionType.PARTIAL
3484
+ }
3485
+ ),
3486
+ /* @__PURE__ */ jsx(Divider, { className: "oui-w-full" }),
3487
+ /* @__PURE__ */ jsx(
3488
+ TPSLInputRowWidget,
3489
+ {
3490
+ disableEnableCheckbox: true,
3491
+ disableOrderTypeSelector: true,
3492
+ rootOrderPrice: formattedOrder.order_price,
3493
+ symbol: symbolInfo.symbol,
3494
+ type: "sl",
3495
+ side: formattedOrder.side,
3496
+ values: slValues,
3497
+ hideOrderPrice: formattedOrder.position_type === PositionType.FULL,
3498
+ inputWarnNode: isSlPriceWarning && /* @__PURE__ */ jsx(
3499
+ DotStatus,
3500
+ {
3501
+ color: "warning",
3502
+ size: "xs",
3503
+ label: getErrorMsg("sl_trigger_price")
3504
+ }
3505
+ ),
3506
+ errors: validated ? errors : null,
3507
+ quote_dp: symbolInfo.quote_dp,
3508
+ positionType: formattedOrder.position_type ?? PositionType.PARTIAL,
3509
+ onChange: (key, value) => {
3510
+ setOrderValue(key, value);
3511
+ }
3512
+ }
3513
+ )
3514
+ ] }),
3515
+ /* @__PURE__ */ jsx(
3516
+ PnlInfo,
3517
+ {
3518
+ tp_pnl: formattedOrder.tp_pnl,
3519
+ sl_pnl: formattedOrder.sl_pnl,
3520
+ className: "oui-mt-6"
3521
+ }
3522
+ )
3523
+ ] })
3524
+ ]
3525
+ }
3526
+ ),
3527
+ /* @__PURE__ */ jsx(
3528
+ Flex,
3529
+ {
3530
+ itemAlign: "center",
3531
+ gap: 3,
3532
+ mt: 5,
3533
+ width: "100%",
3534
+ justify: "center",
3535
+ children: /* @__PURE__ */ jsx(
3536
+ ThrottledButton,
3537
+ {
3538
+ className: "oui-w-[184px]",
3539
+ "data-testid": "tpsl-confirm",
3540
+ disabled: !props.isPriceChanged,
3541
+ loading: props.isMutating,
3542
+ onClick: () => {
3543
+ props.onSubmit().then(() => {
3544
+ props.onClose?.();
3545
+ }).catch((err) => {
3546
+ });
3547
+ },
3548
+ children: t("common.confirm")
3549
+ }
3550
+ )
3551
+ }
3552
+ )
3553
+ ] });
3554
+ };
3555
+ var EditBracketOrderWidget = (props) => {
3556
+ const state = useEditBracketOrder({ order: props.order });
3557
+ return /* @__PURE__ */ jsx(EditBracketOrderUI, { ...state, onClose: props.close });
3558
+ };
3559
+ var EditBracketOrderSheetId = "EditBracketOrderSheetId";
3560
+ var EditBracketOrderDialogId = "EditBracketOrderDialogId";
3561
+ registerSimpleSheet(EditBracketOrderSheetId, EditBracketOrderWidget);
3562
+ registerSimpleDialog(EditBracketOrderDialogId, EditBracketOrderWidget, {
3563
+ classNames: {
3564
+ content: "oui-w-[420px]"
3565
+ }
3566
+ });
3567
+
3568
+ export { ArrowRightIcon, CloseToLiqPriceIcon, EditBracketOrderDialogId, EditBracketOrderSheetId, EditBracketOrderWidget, PositionTPSLConfirm, PositionTPSLPopover, PositionTPSLSheet, TPSL, TPSLAdvancedDialogId, TPSLAdvancedSheetId, TPSLAdvancedUI, TPSLAdvancedWidget, TPSLDetailDialogId, TPSLDetailSheetId, TPSLDetailWidget, TPSLDialogId, TPSLPositionTypeWidget, TPSLSheetId, TPSLSimpleDialogId, TPSLSimpleDialogUI, TPSLSimpleDialogWidget, TPSLSimpleSheetId, TPSLWidget, useTPSLAdvanced, useTPSLBuilder, useTPSLSimpleDialog };
15
3569
  //# sourceMappingURL=out.js.map
16
3570
  //# sourceMappingURL=index.mjs.map