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