@gearbox-protocol/ui-kit 4.5.0 → 4.5.1
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const C=require("react/jsx-runtime"),H=require("lightweight-charts"),g=require("react"),Y=require("./colors.cjs"),at=require("../../hooks/use-media-query.cjs"),Q=require("../../utils/cn.cjs"),I=require("./default-config.cjs"),oe=require("./formatters.cjs"),ct=require("./graph-current-value.cjs"),ut=require("./graph-tooltip.cjs"),ft=require("./plugins/vertical-line.cjs"),ht=require("./series-config.cjs"),We=require("./series-palette.cjs"),Ge=require("./y-axis-scale.cjs");function dt(e){const r=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const l in e)if(l!=="default"){const u=Object.getOwnPropertyDescriptor(e,l);Object.defineProperty(r,l,u.get?u:{enumerable:!0,get:()=>e[l]})}}return r.default=e,Object.freeze(r)}const ve=dt(g);function te(e){return e.id??e.label}function Ke({lastIndex:e,width:r,padLeft:l,padRight:u,paddingPx:h=I.AXIS_TIME_EDGE_PADDING_PX}){if(e<0)return null;const f=(l||u)&&r>0?h*Math.max(e,1)/r:0;return{from:l&&f!==0?-f:0,to:u?e+f:e}}function qe(e,r,l){if(!l)return e.values().next().value;const u=e.get(l);if(u)return u;const h=r.find(f=>f.id===l||f.label===l);return h?e.get(te(h)):void 0}class pt{_yTop;_yBottom;_color;constructor(r,l,u){this._yTop=r,this._yBottom=l,this._color=u}draw(r){r.useBitmapCoordinateSpace(l=>{const u=l.context,h=l.verticalPixelRatio,f=this._yTop===null?0:this._yTop*h,x=this._yBottom===null?l.bitmapSize.height:this._yBottom*h,R=Math.min(f,x),D=Math.max(f,x);u.fillStyle=this._color,u.fillRect(0,R,l.bitmapSize.width,D-R)})}}class gt{_source;_yTop=null;_yBottom=null;constructor(r){this._source=r}update(){const r=this._source.series;this._yTop=this._source.priceTop===void 0?null:r.priceToCoordinate(this._source.priceTop),this._yBottom=this._source.priceBottom===void 0?null:r.priceToCoordinate(this._source.priceBottom)}zOrder(){return"bottom"}renderer(){return new pt(this._yTop,this._yBottom,this._source.color)}}class mt{series;priceTop;priceBottom;color;_paneViews;constructor(r,l,u,h){this.series=r,this.priceTop=l,this.priceBottom=u,this.color=h,this._paneViews=[new gt(this)]}updateAllViews(){for(const r of this._paneViews)r.update()}paneViews(){return this._paneViews}}function bt(e,r){switch(e){case H.LineStyle.Dotted:return[r,r];case H.LineStyle.Dashed:return[2*r,2*r];case H.LineStyle.LargeDashed:return[6*r,6*r];case H.LineStyle.SparseDotted:return[r,4*r];default:return[]}}class St{_y;_color;_lineWidth;_lineStyle;constructor(r,l,u,h){this._y=r,this._color=l,this._lineWidth=u,this._lineStyle=h}draw(r){r.useBitmapCoordinateSpace(l=>{if(this._y===null)return;const u=l.context,h=l.verticalPixelRatio,f=Math.max(1,Math.round(this._lineWidth*h)),x=Math.round(this._y*h);u.save(),u.strokeStyle=this._color,u.lineWidth=f,u.setLineDash(bt(this._lineStyle,f)),u.beginPath(),u.moveTo(0,x),u.lineTo(l.bitmapSize.width,x),u.stroke(),u.restore()})}}class Ct{_source;_y=null;constructor(r){this._source=r}update(){this._y=this._source.series.priceToCoordinate(this._source.price)}zOrder(){return"top"}renderer(){return new St(this._y,this._source.color,this._source.lineWidth,this._source.lineStyle)}}class xt{series;price;color;lineWidth;lineStyle;_paneViews;constructor(r,l,u,h,f){this.series=r,this.price=l,this.color=u,this.lineWidth=h,this.lineStyle=f,this._paneViews=[new Ct(this)]}updateAllViews(){for(const r of this._paneViews)r.update()}paneViews(){return this._paneViews}}function $e(e,r,l){const u=e.labelText??"",h=e.value,f=h===void 0?"":typeof h=="number"?r==="$"?`$${oe.formatExactValue(h,void 0,l)}`:r==="%"?`${oe.formatExactValue(h,void 0,l)}%`:oe.formatExactValue(h,r!=="none"&&r!=="%"&&r!=="$"&&!oe.isRatioUnit(r)?r:void 0,l):String(h);return{labelText:u,valueStr:f}}const ye=4;function He(e,r,l){const u={left:e,transform:"translateX(-50%)"};if(r<=0||!l||l<=0)return u;const h=l/2,f=ye+h,x=r-ye-h;return x<f?{left:r/2,transform:"translateX(-50%)"}:{left:Math.min(Math.max(e,f),x),transform:"translateX(-50%)"}}function Oe(e,r){for(let l=e.length-1;l>=0;l--)if(Number(e[l].time)<=r)return e[l].value;return 0}function vt({series:e,className:r,showLegend:l=!1,onUnselectSeries:u,xMeasureUnit:h="time",yMeasureUnit:f="token",optionsOverrides:x,verticalLineOptions:R,priceLineOptions:D,zoneOptions:F,currentValueDecimals:se,useSharedPriceScale:O=!1,dualYAxis:z=!1,defaultVisiblePriceScaleId:_e="right",showCurrentValue:Ye=!0,graphTitle:de,yScaleMin:we,yScaleMinMultiple:Pe,visibleTimeFrom:ie,disableZoom:Re=!1,hideDefaultTooltip:pe=!1}){const A=g.useRef(null),N=g.useRef(new Map),ge=g.useRef(!1),Te=g.useRef(!0),Le=g.useRef([]),le=g.useRef(0),j=g.useRef(null),me=g.useRef(null),Ne=g.useRef(null),T=at.useIsMobile(),ae=ve.useMemo(()=>e.flatMap(t=>t.data),[e]),Qe=ve.useMemo(()=>{if(e.length>0){const t=e[e.length-1];if(t.data.length>0)return t.data[t.data.length-1]}},[e]),[Ve,be]=g.useState(()=>{if(!Qe)return;const t=new Map;for(const a of e)a.data.length>0&&t.set(a.label,a.data[a.data.length-1]);return t}),[ke,J]=g.useState(null),[Ee,Ie]=g.useState([]),[Je,Me]=g.useState(0),De=g.useRef(new Map),[Ue,et]=g.useState({}),[U,tt]=g.useState(0);g.useEffect(()=>{const t=()=>{try{if(j.current&&Me(j.current.clientWidth),A.current&&j.current){const i=j.current.clientWidth,c=j.current.clientHeight;A.current.resize(i,c);try{le.current=A.current.paneSize().height}catch{}}}catch{}},a=j.current,n=a?new ResizeObserver(t):null;return Ne.current=n,a&&n?.observe(a),()=>{a&&n?.unobserve(a),n?.disconnect(),Ne.current=null}},[]),g.useEffect(()=>{if(!me.current||ge.current)return;const t=I.getDefaultOptions(),a=Y.resolveCSSColor("hsl(var(--muted-foreground))","rgb(163, 163, 163)"),n=H.createChart(me.current,{...t,leftPriceScale:{...t.leftPriceScale||{},visible:!1,textColor:a},rightPriceScale:{...t.rightPriceScale||{},visible:!T,textColor:a},...T&&{crosshair:{...t.crosshair,mode:H.CrosshairMode.Magnet}}});A.current=n,ge.current=!0;try{le.current=n.paneSize().height}catch{le.current=0}return tt(i=>i+1),()=>{n.remove(),A.current=null,ge.current=!1,N.current.clear()}},[T]),g.useEffect(()=>{const t=A.current;t&&t.applyOptions(Re?{handleScale:{mouseWheel:!1,pinch:!1,axisPressedMouseMove:!1,axisDoubleClickReset:!1},handleScroll:{mouseWheel:!1,pressedMouseMove:!1,horzTouchDrag:!1,vertTouchDrag:!1}}:{handleScale:{mouseWheel:!0,pinch:!0,axisPressedMouseMove:!0,axisDoubleClickReset:!0},handleScroll:{mouseWheel:!0,pressedMouseMove:!0,horzTouchDrag:!0,vertTouchDrag:!0}})},[U,Re]),g.useEffect(()=>{const t=A.current;!t||!x||t.applyOptions(x)},[U,x]),g.useEffect(()=>{const t=A.current;if(!t||e.length===0)return;const n=e.filter(s=>s.data.length>0).length<=1?we:Pe,i=h==="time"?oe.getXFormatters(h,ae):oe.getXFormatters(h),c=new Map(ht.resolvePriceScaleSides(e).map(({series:s,side:o})=>[s.label,o])),p=(s,o,d)=>{const{formatter:E,tickmarksFormatter:k,updateVisibleRange:B}=oe.createAdaptiveYAxisFormatter(s,o,{yScaleMin:n});return{formatter:E,tickmarksFormatter:k,updateVisibleRange:B,labels:d}},m=[];if(z)for(const s of["left","right"]){const o=new Set,d=[];let E=f;for(const k of e)if(k.data.length!==0&&c.get(k.label)===s){o.size===0&&k.yMeasureUnit&&(E=k.yMeasureUnit),o.add(k.label);for(const B of k.data)d.push(B.value)}o.size>0&&m.push(p(d,E,o))}else{const s=ae.map(d=>d.value),o=new Set(e.filter(d=>d.data.length>0).map(d=>d.label));m.push(p(s,f,o))}Le.current=m;const _=new Map;for(const s of m)for(const o of s.labels)_.set(o,s);const w=I.getDefaultOptions();t.applyOptions({localization:{...w.localization||{},timeFormatter:s=>i.timeFormatter(parseFloat(s),void 0)},timeScale:{...w.timeScale||{},...x?.timeScale,tickMarkFormatter:i.tickMarkFormatter,minBarSpacing:0}});const v=!z&&e.length>1&&!O,G=z?T:e.length>1&&!O||T,V=Y.resolveCSSColor("hsl(var(--muted-foreground))","rgb(163, 163, 163)");t.applyOptions({...z&&{defaultVisiblePriceScaleId:_e},leftPriceScale:{...w.leftPriceScale||{},...x?.leftPriceScale,visible:z&&!T,textColor:V},rightPriceScale:{...w.rightPriceScale||{},...x?.rightPriceScale,visible:z?!T:!G,textColor:V}});const ee=new Set(N.current.keys()),q=new Set(e.map(te));for(const s of ee)if(!q.has(s)){const o=N.current.get(s);o&&(t.removeSeries(o),N.current.delete(s))}const b=I.getDefaultSeries(),M=[];let L=0;const W=e.find(s=>s.data.length>0),y=new Map;if(D&&D.length>0)for(const s of D){if(!Number.isFinite(s.price))continue;const o=s.seriesLabel??W?.label;if(!o)continue;const d=y.get(o)??[];d.push(s.price),y.set(o,d)}if(F&&F.length>0)for(const s of F){const o=s.seriesLabel??W?.label;if(!o)continue;const d=y.get(o)??[];Number.isFinite(s.priceTop)&&d.push(s.priceTop),Number.isFinite(s.priceBottom)&&d.push(s.priceBottom),y.set(o,d)}for(let s=0;s<e.length;s++){const o=e[s],d=te(o),E=We.getSeriesColorPalette(s),k=o.lineColor??o.color??E.line;let B,Z;if(o.lineColor??o.color)if(o.topColor&&o.bottomColor)B=o.topColor,Z=o.bottomColor;else if(o.topColor){const S=Y.generateColorsFromBase(k);B=o.topColor,Z=S.bottomColor}else if(o.bottomColor)B=Y.generateColorsFromBase(k).topColor,Z=o.bottomColor;else{const S=Y.generateColorsFromBase(k);B=S.topColor,Z=S.bottomColor}else B=o.topColor||E.top,Z=o.bottomColor||E.bottom;if(o.data.length===0){const S=N.current.get(d);S&&(t.removeSeries(S),N.current.delete(d));continue}const ce=o.data.length>0?Math.min(...o.data.map(S=>S.value)):0,fe=o.data.length>0?Math.max(...o.data.map(S=>S.value)):0,ot=ce<0,rt=o.data.some(S=>S.value===0),nt=!ot&&rt,je=Ge.pickMinMove(fe-ce),Be=z?o.priceScaleId??(L===0?"left":"right"):O||!v||L===0?"right":`scale-${L}`;v&&M.push({priceScaleId:Be||"right",seriesIndex:L});const Se=(O||!v)&&n!==void 0&&typeof n=="number",st=y.get(o.label)??[],it=S=>{const P=S();if(P?.priceRange){Se&&(P.priceRange.minValue=n);for(const $ of st)$<P.priceRange.minValue&&(P.priceRange.minValue=$),$>P.priceRange.maxValue&&(P.priceRange.maxValue=$);const ne=Se?n:nt&&P.priceRange.minValue>=0?0:void 0,he=le.current>0?le.current:j.current?.clientHeight??0;if(he>0){const $=Ge.niceAxisRange({min:P.priceRange.minValue,max:P.priceRange.maxValue,floor:ne,scaleHeightPx:he,fontSizePx:I.AXIS_FONT_SIZE,tickMarkDensity:I.AXIS_TICK_MARK_DENSITY,minMove:je,edgePaddingPx:I.AXIS_EDGE_LABEL_PADDING_PX,edgePaddingTopPx:I.AXIS_TOP_LABEL_PADDING_PX});P.priceRange.minValue=$.min,P.priceRange.maxValue=$.max}P.margins={above:I.AXIS_TOP_LABEL_PADDING_PX,below:I.AXIS_EDGE_LABEL_PADDING_PX}}return P},Fe=_.get(o.label)??m[0],ze=Fe.formatter,lt=Fe.tickmarksFormatter,Ce={lineColor:k,topColor:B,bottomColor:Z,priceScaleId:Be,...o.seriesType==="line"&&{lineStyle:o.lineStyle??H.LineStyle.Solid},priceFormat:{type:"custom",minMove:je,formatter:Se&&typeof n=="number"?S=>ze(S<n?n:S):ze,tickmarksFormatter:lt},autoscaleInfoProvider:it},Xe=o.seriesType==="line";let X=N.current.get(d);const re=o.data.map(S=>({...S,time:S.time}));if(ie!==void 0&&re.length>0&&Number(re[0].time)>ie){const S=Number(re[0].time),P=S-ie,ne=86400,he=P>180*ne?7*ne:P>30*ne?ne:6*3600,$=[];for(let xe=ie;xe<S;xe+=he)$.push({time:xe});re.unshift(...$)}X&&X.seriesType()!==(Xe?"Line":"Area")&&(t.removeSeries(X),N.current.delete(d),X=void 0),X?(X.applyOptions(Ce),X.setData(re)):(Xe?X=t.addSeries(H.LineSeries,{...b,...Ce}):X=t.addSeries(H.AreaSeries,{...b,...Ce}),X.setData(re),N.current.set(d,X)),L++}if(z)try{const s=t.priceScale("left"),o=t.priceScale("right"),d={visible:!T,autoScale:!0,scaleMargins:{top:0,bottom:0},alignLabels:!0,entireTextOnly:!1,tickMarkDensity:I.AXIS_TICK_MARK_DENSITY,textColor:V,...x?.rightPriceScale};s?.applyOptions(d),o?.applyOptions(d)}catch{}else if(v&&M.length>0)for(const s of M)try{const o=t.priceScale(s.priceScaleId);o&&o.applyOptions({visible:!1,autoScale:!0,scaleMargins:{top:0,bottom:0},alignLabels:!0,entireTextOnly:!1,tickMarkDensity:I.AXIS_TICK_MARK_DENSITY,textColor:V})}catch(o){console.warn(`Failed to configure price scale ${s.priceScaleId} for series ${s.seriesIndex}:`,o)}else if(!v)try{const s=t.priceScale("right");s&&s.applyOptions({visible:!T,autoScale:!0,scaleMargins:{top:0,bottom:0},alignLabels:!0,entireTextOnly:!1,tickMarkDensity:I.AXIS_TICK_MARK_DENSITY,textColor:V,...x?.rightPriceScale})}catch{}if(n!==void 0&&!T&&(O||!v)&&t.applyOptions({handleScroll:{vertTouchDrag:!1}}),ae.length>0){const s=ae.map(d=>Number(d.time)).filter(d=>Number.isFinite(d)),o=new Set(s).size-1;if(o>=0){const d=()=>{const E=t.timeScale();if(h!=="time"){E.applyOptions({fixLeftEdge:!0,fixRightEdge:!0}),E.setVisibleLogicalRange({from:0,to:o});return}const k=z&&!T,B=z?!T:!G,Z=!k,ce=!B;E.applyOptions({fixLeftEdge:!Z,fixRightEdge:!ce});const fe=Ke({lastIndex:o,width:E.width(),padLeft:Z,padRight:ce});fe&&E.setVisibleLogicalRange(fe)};try{d()}catch{try{t.timeScale().fitContent(),d()}catch{}}}}Te.current&&(Te.current=!1),x&&t.applyOptions(x);const K=new Map;for(const s of e)s.data.length>0&&K.set(s.label,s.data[s.data.length-1]);be(K.size>0?K:void 0)},[U,e,ae,h,f,T,O,z,_e,we,Pe,ie,D,F,x]),g.useEffect(()=>{const t=A.current;if(!t||e.length===0)return;const a=e.length===1;let n;for(const c of e)if(c.data.length>0){n=c.data[c.data.length-1].time;break}if(!n)return;const i=c=>{const{time:p=n,seriesData:m,point:_}=c||{},w=e[0],v=w?N.current.get(te(w)):void 0;if(!_||!p){const q=new Map;for(const b of e)b.data.length>0&&q.set(b.label,b.data[b.data.length-1]);if(be(q.size>0?q:void 0),a||O||pe)J(null);else if(v&&n){const b=e[0];if(b&&b.data.length>0){const L=t.timeScale().timeToCoordinate(n),W=b.data[b.data.length-1].value,y=v.priceToCoordinate(W);L!==null&&y!==null&&J({x:L,y})}}return}const G=new Map;let V,ee=v;for(let q=0;q<e.length;q++){const b=e[q],M=N.current.get(te(b));if(!M)continue;const L=m?.get(M),W=L&&"value"in L?L.value:void 0;let y;if(W!==void 0)y=W;else if(a){const K=Number(p),s=b.data.length>0?Number(b.data[0].time):Number.POSITIVE_INFINITY;K<s?y=Number.NaN:y=b.data[b.data.length-1]?.value??0}else{const K=b.data.length>0?Number(b.data[0].time):Number.POSITIVE_INFINITY;if(Number(p)<K)continue;y=Oe(b.data,Number(p))}Number.isNaN(y)||(V===void 0&&(V=y,ee=M),G.set(b.label,{value:y,time:p}))}if(be(G),V!==void 0&&ee){const b=t.timeScale().timeToCoordinate(p),M=ee.priceToCoordinate(V);if(b!==null&&M!==null){const L=j.current;if(L){const W=L.querySelector(".LightweightChart");if(W){const y=W.getBoundingClientRect(),K=L.getBoundingClientRect(),s=b+(y.left-K.left),o=M+(y.top-K.top);J({x:s,y:o})}else J({x:b,y:M})}else J({x:b,y:M})}else J(null)}else J(null)};return t.subscribeCrosshairMove(i),T&&t.subscribeClick(i),!a&&!O&&!pe&&requestAnimationFrame(()=>{const c=e[0],p=c?N.current.get(te(c)):void 0;if(p&&n&&c.data.length>0){const _=t.timeScale().timeToCoordinate(n),w=c.data[c.data.length-1].value,v=p.priceToCoordinate(w);_!==null&&v!==null&&J({x:_,y:v})}}),()=>{t.unsubscribeCrosshairMove(i),T&&t.unsubscribeClick(i)}},[e,O,T,pe]),g.useEffect(()=>{const t=A.current;if(!t||e.length===0)return;const a=t.timeScale(),n=()=>{const i=Le.current;if(i.length===0)return;const c=a.getVisibleRange();if(!c)return;const p=Number(c.from),m=Number(c.to);for(const _ of i){const w=[];for(const v of e)if(_.labels.has(v.label))for(const G of v.data){const V=Number(G.time);V>=p&&V<=m&&w.push(G.value)}w.length>0&&_.updateVisibleRange(w)}};return a.subscribeVisibleLogicalRangeChange(n),()=>{a.unsubscribeVisibleLogicalRangeChange(n)}},[e]),g.useEffect(()=>{const t=A.current,a=Array.from(N.current.values()),n=[];if(t&&a.length>0&&R){const i=I.getDefaultVerticalLine(),c=a[0];for(const p of R){const m={...i,...p.options||{}};m.color&&(m.color=Y.resolveCSSColor(m.color,m.color));const _=new ft.VertLine(t,c,p.xCoordinate,m);c.attachPrimitive(_),n.push(_)}}return()=>{if(a.length>0){const i=a[0];for(const c of n)try{i.detachPrimitive(c)}catch{}}}},[R,e,U]),g.useEffect(()=>{const t=[],a=[];if(D&&D.length>0){const n=Y.resolveCSSColor("hsl(var(--destructive))","rgb(239, 68, 68)");for(const i of D){if(!Number.isFinite(i.price))continue;const c=qe(N.current,e,i.seriesLabel);if(!c)continue;const p=i.color?Y.resolveCSSColor(i.color,i.color):n,m=i.lineWidth??1,_=i.lineStyle??H.LineStyle.Dashed,w=c.createPriceLine({price:i.price,color:p,lineWidth:m,lineStyle:_,axisLabelVisible:i.axisLabelVisible??!0,title:i.title??""});t.push({series:c,line:w});const v=new xt(c,i.price,p,m,_);c.attachPrimitive(v),a.push({series:c,primitive:v})}}return()=>{for(const{series:n,primitive:i}of a)try{n.detachPrimitive(i)}catch{}for(const{series:n,line:i}of t)try{n.removePriceLine(i)}catch{}}},[D,e,U]),g.useEffect(()=>{const t=[];if(F&&F.length>0){const a="rgba(239, 68, 68, 0.12)";for(const n of F){const i=qe(N.current,e,n.seriesLabel);if(!i)continue;const c=n.color?Y.resolveCSSColor(n.color,a):a,p=new mt(i,n.priceTop,n.priceBottom,c);i.attachPrimitive(p),t.push({series:i,zone:p})}}return()=>{for(const{series:a,zone:n}of t)try{a.detachPrimitive(n)}catch{}}},[F,e,U]),g.useEffect(()=>{const t=A.current;if(!t||!R?.length){Ie([]);return}const a=()=>{const c=t.timeScale(),p=R.map(m=>c.timeToCoordinate(m.xCoordinate)??null);Ie(p),j.current&&Me(j.current.clientWidth)};a();const n=t.timeScale(),i=()=>{a()};return n.subscribeVisibleLogicalRangeChange(i),()=>{n.unsubscribeVisibleLogicalRangeChange(i)}},[R,e,U]),g.useLayoutEffect(()=>{et(t=>{const a={};let n=!1;for(const[i,c]of De.current){if(!c)continue;const p=c.offsetWidth;a[i]=p,t[i]!==p&&(n=!0)}return Object.keys(t).length!==Object.keys(a).length&&(n=!0),n?a:t})});const ue=e.length===1,Ae=ve.useMemo(()=>{if(ue&&e.length>0){const t=e[0];if(t.data.length>0)return t.data[t.data.length-1]}},[ue,e]);return C.jsxs("div",{ref:j,className:Q.cn("relative z-[1] w-full h-full overflow-hidden p-0",r),children:[de!=null&&de!==!1&&ue?C.jsx("div",{className:"absolute top-2 left-2 right-2 z-[100] pointer-events-none",children:C.jsx("div",{className:"text-xl sm:text-2xl font-semibold text-foreground whitespace-nowrap overflow-hidden text-ellipsis",children:de})}):Ye&&ue&&Ae&&C.jsx(ct.GraphCurrentValue,{point:Ae,xMeasureUnit:h,yMeasureUnit:f,decimals:se}),Ve&&ke&&C.jsx(ut.GraphTooltip,{points:Ve,xMeasureUnit:h,yMeasureUnit:f,series:e,position:ke,containerRef:j,decimals:se}),l&&C.jsx(Ze,{series:e,onUnselect:u}),R&&R.length>0&&Ee.length===R.length&&C.jsx("div",{className:"absolute inset-0 z-[5] pointer-events-none","aria-hidden":!0,children:R.map((t,a)=>{const n=Ee[a],i=t.options??{};if(n===null)return null;const{labelText:c,valueStr:p}=$e(i,f,se),m=i.tooltipText??"";if(!(c||p||m))return null;const w=i.labelBackgroundColor??i.color??"hsl(var(--background))",v=i.labelTextColor??"hsl(var(--foreground))",G=`${String(t.xCoordinate)}-${c}-${m}`,V=He(n,Je,Ue[a]);return C.jsxs("div",{ref:ee=>{De.current.set(a,ee)},className:Q.cn("group absolute top-2 px-2 py-1 rounded text-xs font-medium whitespace-nowrap shadow-sm border border-border",m&&"pointer-events-auto cursor-default"),style:{...V,backgroundColor:w,color:v},children:[c&&C.jsx("span",{className:"block",children:c}),p&&C.jsx("span",{className:Q.cn("block",c&&"mt-0.5"),children:p}),m&&C.jsx("span",{className:"pointer-events-none absolute right-0 top-full z-10 mt-1 hidden w-[280px] whitespace-normal rounded border border-border bg-popover px-2.5 py-1.5 text-left leading-snug text-popover-foreground shadow-md group-hover:block",role:"tooltip",children:m})]},G)})}),C.jsx("div",{ref:me,className:"LightweightChart w-full h-full overflow-hidden"})]})}function Ze({series:e,markers:r,onUnselect:l,inline:u=!1}){const h=e.every(f=>f.data.length===0);return C.jsxs("div",{className:Q.cn("flex flex-wrap",u?"items-center justify-center gap-6":"absolute top-0 left-2 z-[10] gap-2"),children:[e.map((f,x)=>{const R=We.getSeriesColorPalette(x),D=(f.lineColor??f.color)||R.line,F=!h&&f.data.length===0;return C.jsxs("div",{className:Q.cn("flex shrink-0 items-center gap-1.5",!u&&"px-2 py-1 bg-background/80 backdrop-blur-sm rounded border border-border",F&&"opacity-50"),children:[C.jsx("div",{className:Q.cn("shrink-0",u?"size-2 rounded-full":"h-3 w-3 rounded-sm"),style:{backgroundColor:D}}),C.jsxs("span",{className:Q.cn("font-medium text-foreground",u?"text-sm":"text-[10px] sm:text-xs"),children:[f.label,F&&" (no data)"]}),l&&C.jsx("button",{type:"button",onClick:se=>{se.stopPropagation(),l(f.label)},className:"ml-1 flex items-center justify-center w-4 h-4 rounded-sm hover:bg-muted transition-colors cursor-pointer","aria-label":`Remove ${f.label}`,children:C.jsxs("svg",{xmlns:"http://www.w3.org/2000/svg",width:"12",height:"12",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",className:"text-muted-foreground hover:text-foreground","aria-hidden":"true",children:[C.jsx("title",{children:"Close"}),C.jsx("line",{x1:"18",y1:"6",x2:"6",y2:"18"}),C.jsx("line",{x1:"6",y1:"6",x2:"18",y2:"18"})]})})]},f.id??f.label)}),r?.map(f=>C.jsxs("div",{className:Q.cn("flex shrink-0 items-center gap-1.5",!u&&"px-2 py-1 bg-background/80 backdrop-blur-sm rounded border border-border"),children:[C.jsx("div",{className:"h-3 w-0.5 shrink-0",style:{backgroundColor:f.color}}),C.jsx("span",{className:Q.cn("font-medium text-foreground",u?"text-sm":"text-[10px] sm:text-xs"),children:f.label})]},f.id))]})}exports.Graph=vt;exports.GraphLegend=Ze;exports.VERTICAL_LINE_LABEL_EDGE_PADDING=ye;exports.getStepForwardValue=Oe;exports.getVerticalLineLabelStyle=He;exports.getVerticalLineTooltipContent=$e;exports.seriesIdentity=te;exports.visibleLogicalRangeFor=Ke;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const C=require("react/jsx-runtime"),$=require("lightweight-charts"),g=require("react"),Y=require("./colors.cjs"),ct=require("../../hooks/use-media-query.cjs"),Q=require("../../utils/cn.cjs"),I=require("./default-config.cjs"),re=require("./formatters.cjs"),ut=require("./graph-current-value.cjs"),ft=require("./graph-tooltip.cjs"),ht=require("./plugins/vertical-line.cjs"),dt=require("./series-config.cjs"),Ke=require("./series-palette.cjs"),qe=require("./y-axis-scale.cjs");function pt(e){const r=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const l in e)if(l!=="default"){const u=Object.getOwnPropertyDescriptor(e,l);Object.defineProperty(r,l,u.get?u:{enumerable:!0,get:()=>e[l]})}}return r.default=e,Object.freeze(r)}const ye=pt(g);function oe(e){return e.id??e.label}function $e({lastIndex:e,width:r,padLeft:l,padRight:u,paddingPx:h=I.AXIS_TIME_EDGE_PADDING_PX}){if(e<0)return null;const f=(l||u)&&r>0?h*Math.max(e,1)/r:0;return{from:l&&f!==0?-f:0,to:u?e+f:e}}function We(e,r,l){if(!l)return e.values().next().value;const u=e.get(l);if(u)return u;const h=r.find(f=>f.id===l||f.label===l);return h?e.get(oe(h)):void 0}class gt{_yTop;_yBottom;_color;constructor(r,l,u){this._yTop=r,this._yBottom=l,this._color=u}draw(r){r.useBitmapCoordinateSpace(l=>{const u=l.context,h=l.verticalPixelRatio,f=this._yTop===null?0:this._yTop*h,x=this._yBottom===null?l.bitmapSize.height:this._yBottom*h,R=Math.min(f,x),D=Math.max(f,x);u.fillStyle=this._color,u.fillRect(0,R,l.bitmapSize.width,D-R)})}}class mt{_source;_yTop=null;_yBottom=null;constructor(r){this._source=r}update(){const r=this._source.series;this._yTop=this._source.priceTop===void 0?null:r.priceToCoordinate(this._source.priceTop),this._yBottom=this._source.priceBottom===void 0?null:r.priceToCoordinate(this._source.priceBottom)}zOrder(){return"bottom"}renderer(){return new gt(this._yTop,this._yBottom,this._source.color)}}class bt{series;priceTop;priceBottom;color;_paneViews;constructor(r,l,u,h){this.series=r,this.priceTop=l,this.priceBottom=u,this.color=h,this._paneViews=[new mt(this)]}updateAllViews(){for(const r of this._paneViews)r.update()}paneViews(){return this._paneViews}}function St(e,r){switch(e){case $.LineStyle.Dotted:return[r,r];case $.LineStyle.Dashed:return[2*r,2*r];case $.LineStyle.LargeDashed:return[6*r,6*r];case $.LineStyle.SparseDotted:return[r,4*r];default:return[]}}class Ct{_y;_color;_lineWidth;_lineStyle;constructor(r,l,u,h){this._y=r,this._color=l,this._lineWidth=u,this._lineStyle=h}draw(r){r.useBitmapCoordinateSpace(l=>{if(this._y===null)return;const u=l.context,h=l.verticalPixelRatio,f=Math.max(1,Math.round(this._lineWidth*h)),x=Math.round(this._y*h);u.save(),u.strokeStyle=this._color,u.lineWidth=f,u.setLineDash(St(this._lineStyle,f)),u.beginPath(),u.moveTo(0,x),u.lineTo(l.bitmapSize.width,x),u.stroke(),u.restore()})}}class xt{_source;_y=null;constructor(r){this._source=r}update(){this._y=this._source.series.priceToCoordinate(this._source.price)}zOrder(){return"top"}renderer(){return new Ct(this._y,this._source.color,this._source.lineWidth,this._source.lineStyle)}}class vt{series;price;color;lineWidth;lineStyle;_paneViews;constructor(r,l,u,h,f){this.series=r,this.price=l,this.color=u,this.lineWidth=h,this.lineStyle=f,this._paneViews=[new xt(this)]}updateAllViews(){for(const r of this._paneViews)r.update()}paneViews(){return this._paneViews}}function He(e,r,l){const u=e.labelText??"",h=e.value,f=h===void 0?"":typeof h=="number"?r==="$"?`$${re.formatExactValue(h,void 0,l)}`:r==="%"?`${re.formatExactValue(h,void 0,l)}%`:re.formatExactValue(h,r!=="none"&&r!=="%"&&r!=="$"&&!re.isRatioUnit(r)?r:void 0,l):String(h);return{labelText:u,valueStr:f}}const _e=4;function Oe(e,r,l){const u={left:e,transform:"translateX(-50%)"};if(r<=0||!l||l<=0)return u;const h=l/2,f=_e+h,x=r-_e-h;return x<f?{left:r/2,transform:"translateX(-50%)"}:{left:Math.min(Math.max(e,f),x),transform:"translateX(-50%)"}}function Ze(e,r){for(let l=e.length-1;l>=0;l--)if(Number(e[l].time)<=r)return e[l].value;return 0}function yt({series:e,className:r,showLegend:l=!1,onUnselectSeries:u,xMeasureUnit:h="time",yMeasureUnit:f="token",optionsOverrides:x,verticalLineOptions:R,priceLineOptions:D,zoneOptions:F,currentValueDecimals:se,useSharedPriceScale:H=!1,dualYAxis:z=!1,defaultVisiblePriceScaleId:we="right",showCurrentValue:Qe=!0,graphTitle:pe,yScaleMin:Pe,yScaleMinMultiple:Re,visibleTimeFrom:le,disableZoom:Te=!1,hideDefaultTooltip:ge=!1}){const A=g.useRef(null),N=g.useRef(new Map),me=g.useRef(!1),Le=g.useRef(!0),Ne=g.useRef([]),ae=g.useRef(0),j=g.useRef(null),be=g.useRef(null),Ve=g.useRef(null),T=ct.useIsMobile(),ce=ye.useMemo(()=>e.flatMap(t=>t.data),[e]),Je=ye.useMemo(()=>{if(e.length>0){const t=e[e.length-1];if(t.data.length>0)return t.data[t.data.length-1]}},[e]),[ke,Se]=g.useState(()=>{if(!Je)return;const t=new Map;for(const a of e)a.data.length>0&&t.set(a.label,a.data[a.data.length-1]);return t}),[Ee,J]=g.useState(null),[Ie,Me]=g.useState([]),[Ue,De]=g.useState(0),Ae=g.useRef(new Map),[et,tt]=g.useState({}),[U,ot]=g.useState(0);g.useEffect(()=>{const t=()=>{try{if(j.current&&De(j.current.clientWidth),A.current&&j.current){const s=j.current.clientWidth,c=j.current.clientHeight;A.current.resize(s,c);try{ae.current=A.current.paneSize().height}catch{}}}catch{}},a=j.current,n=a?new ResizeObserver(t):null;return Ve.current=n,a&&n?.observe(a),()=>{a&&n?.unobserve(a),n?.disconnect(),Ve.current=null}},[]),g.useEffect(()=>{if(!be.current||me.current)return;const t=I.getDefaultOptions(),a=Y.resolveCSSColor("hsl(var(--muted-foreground))","rgb(163, 163, 163)"),n=$.createChart(be.current,{...t,leftPriceScale:{...t.leftPriceScale||{},visible:!1,textColor:a},rightPriceScale:{...t.rightPriceScale||{},visible:!T,textColor:a},...T&&{crosshair:{...t.crosshair,mode:$.CrosshairMode.Magnet}}});A.current=n,me.current=!0;try{ae.current=n.paneSize().height}catch{ae.current=0}return ot(s=>s+1),()=>{n.remove(),A.current=null,me.current=!1,N.current.clear()}},[T]),g.useEffect(()=>{const t=A.current;t&&t.applyOptions(Te?{handleScale:{mouseWheel:!1,pinch:!1,axisPressedMouseMove:!1,axisDoubleClickReset:!1},handleScroll:{mouseWheel:!1,pressedMouseMove:!1,horzTouchDrag:!1,vertTouchDrag:!1}}:{handleScale:{mouseWheel:!0,pinch:!0,axisPressedMouseMove:!0,axisDoubleClickReset:!0},handleScroll:{mouseWheel:!0,pressedMouseMove:!0,horzTouchDrag:!0,vertTouchDrag:!0}})},[U,Te]),g.useEffect(()=>{const t=A.current;!t||!x||t.applyOptions(x)},[U,x]),g.useEffect(()=>{const t=A.current;if(!t||e.length===0)return;const n=e.filter(i=>i.data.length>0).length<=1?Pe:Re,s=h==="time"?re.getXFormatters(h,ce):re.getXFormatters(h),c=new Map(dt.resolvePriceScaleSides(e).map(({series:i,side:o})=>[i.label,o])),p=(i,o,d)=>{const{formatter:k,tickmarksFormatter:E,updateVisibleRange:B}=re.createAdaptiveYAxisFormatter(i,o,{yScaleMin:n});return{formatter:k,tickmarksFormatter:E,updateVisibleRange:B,labels:d}},m=[];if(z)for(const i of["left","right"]){const o=new Set,d=[];let k=f;for(const E of e)if(E.data.length!==0&&c.get(E.label)===i){o.size===0&&E.yMeasureUnit&&(k=E.yMeasureUnit),o.add(E.label);for(const B of E.data)d.push(B.value)}o.size>0&&m.push(p(d,k,o))}else{const i=ce.map(d=>d.value),o=new Set(e.filter(d=>d.data.length>0).map(d=>d.label));m.push(p(i,f,o))}Ne.current=m;const _=new Map;for(const i of m)for(const o of i.labels)_.set(o,i);const w=I.getDefaultOptions();t.applyOptions({localization:{...w.localization||{},timeFormatter:i=>s.timeFormatter(parseFloat(i),void 0)},timeScale:{...w.timeScale||{},...x?.timeScale,tickMarkFormatter:s.tickMarkFormatter,minBarSpacing:0}});const v=!z&&e.length>1&&!H,G=z?T:e.length>1&&!H||T,V=Y.resolveCSSColor("hsl(var(--muted-foreground))","rgb(163, 163, 163)");t.applyOptions({...z&&{defaultVisiblePriceScaleId:we},leftPriceScale:{...w.leftPriceScale||{},...x?.leftPriceScale,visible:z&&!T,textColor:V},rightPriceScale:{...w.rightPriceScale||{},...x?.rightPriceScale,visible:z?!T:!G,textColor:V}});const ee=new Set(N.current.keys()),q=new Set(e.map(oe));for(const i of ee)if(!q.has(i)){const o=N.current.get(i);o&&(t.removeSeries(o),N.current.delete(i))}const b=I.getDefaultSeries(),M=[];let L=0;const W=e.find(i=>i.data.length>0),y=new Map;if(D&&D.length>0)for(const i of D){if(!Number.isFinite(i.price))continue;const o=i.seriesLabel??W?.label;if(!o)continue;const d=y.get(o)??[];d.push(i.price),y.set(o,d)}if(F&&F.length>0)for(const i of F){const o=i.seriesLabel??W?.label;if(!o)continue;const d=y.get(o)??[];Number.isFinite(i.priceTop)&&d.push(i.priceTop),Number.isFinite(i.priceBottom)&&d.push(i.priceBottom),y.set(o,d)}for(let i=0;i<e.length;i++){const o=e[i],d=oe(o),k=Ke.getSeriesColorPalette(i),E=o.lineColor??o.color??k.line;let B,Z;if(o.lineColor??o.color)if(o.topColor&&o.bottomColor)B=o.topColor,Z=o.bottomColor;else if(o.topColor){const S=Y.generateColorsFromBase(E);B=o.topColor,Z=S.bottomColor}else if(o.bottomColor)B=Y.generateColorsFromBase(E).topColor,Z=o.bottomColor;else{const S=Y.generateColorsFromBase(E);B=S.topColor,Z=S.bottomColor}else B=o.topColor||k.top,Z=o.bottomColor||k.bottom;if(o.data.length===0){const S=N.current.get(d);S&&(t.removeSeries(S),N.current.delete(d));continue}const ue=o.data.length>0?Math.min(...o.data.map(S=>S.value)):0,he=o.data.length>0?Math.max(...o.data.map(S=>S.value)):0,rt=ue<0,nt=o.data.some(S=>S.value===0),it=!rt&&nt,Be=qe.pickMinMove(he-ue),Fe=z?o.priceScaleId??(L===0?"left":"right"):H||!v||L===0?"right":`scale-${L}`;v&&M.push({priceScaleId:Fe||"right",seriesIndex:L});const Ce=(H||!v)&&n!==void 0&&typeof n=="number",st=y.get(o.label)??[],lt=S=>{const P=S();if(P?.priceRange){Ce&&(P.priceRange.minValue=n);for(const K of st)K<P.priceRange.minValue&&(P.priceRange.minValue=K),K>P.priceRange.maxValue&&(P.priceRange.maxValue=K);const ie=Ce?n:it&&P.priceRange.minValue>=0?0:void 0,de=ae.current>0?ae.current:j.current?.clientHeight??0;if(de>0){const K=qe.niceAxisRange({min:P.priceRange.minValue,max:P.priceRange.maxValue,floor:ie,scaleHeightPx:de,fontSizePx:I.AXIS_FONT_SIZE,tickMarkDensity:I.AXIS_TICK_MARK_DENSITY,minMove:Be,edgePaddingPx:I.AXIS_EDGE_LABEL_PADDING_PX,edgePaddingTopPx:I.AXIS_TOP_LABEL_PADDING_PX});P.priceRange.minValue=K.min,P.priceRange.maxValue=K.max}P.margins={above:I.AXIS_TOP_LABEL_PADDING_PX,below:I.AXIS_EDGE_LABEL_PADDING_PX}}return P},ze=_.get(o.label)??m[0],Xe=ze.formatter,at=ze.tickmarksFormatter,xe={lineColor:E,topColor:B,bottomColor:Z,priceScaleId:Fe,...o.seriesType==="line"&&{lineStyle:o.lineStyle??$.LineStyle.Solid},priceFormat:{type:"custom",minMove:Be,formatter:Ce&&typeof n=="number"?S=>Xe(S<n?n:S):Xe,tickmarksFormatter:at},autoscaleInfoProvider:lt},Ge=o.seriesType==="line";let X=N.current.get(d);const ne=o.data.map(S=>({...S,time:S.time}));if(le!==void 0&&ne.length>0&&Number(ne[0].time)>le){const S=Number(ne[0].time),P=S-le,ie=86400,de=P>180*ie?7*ie:P>30*ie?ie:6*3600,K=[];for(let ve=le;ve<S;ve+=de)K.push({time:ve});ne.unshift(...K)}X&&X.seriesType()!==(Ge?"Line":"Area")&&(t.removeSeries(X),N.current.delete(d),X=void 0),X?(X.applyOptions(xe),X.setData(ne)):(Ge?X=t.addSeries($.LineSeries,{...b,...xe}):X=t.addSeries($.AreaSeries,{...b,...xe}),X.setData(ne),N.current.set(d,X)),L++}if(z)try{const i=t.priceScale("left"),o=t.priceScale("right"),d={visible:!T,autoScale:!0,scaleMargins:{top:0,bottom:0},alignLabels:!0,entireTextOnly:!1,tickMarkDensity:I.AXIS_TICK_MARK_DENSITY,textColor:V,...x?.rightPriceScale};i?.applyOptions(d),o?.applyOptions(d)}catch{}else if(v&&M.length>0)for(const i of M)try{const o=t.priceScale(i.priceScaleId);o&&o.applyOptions({visible:!1,autoScale:!0,scaleMargins:{top:0,bottom:0},alignLabels:!0,entireTextOnly:!1,tickMarkDensity:I.AXIS_TICK_MARK_DENSITY,textColor:V})}catch(o){console.warn(`Failed to configure price scale ${i.priceScaleId} for series ${i.seriesIndex}:`,o)}else if(!v)try{const i=t.priceScale("right");i&&i.applyOptions({visible:!T,autoScale:!0,scaleMargins:{top:0,bottom:0},alignLabels:!0,entireTextOnly:!1,tickMarkDensity:I.AXIS_TICK_MARK_DENSITY,textColor:V,...x?.rightPriceScale})}catch{}n!==void 0&&!T&&(H||!v)&&t.applyOptions({handleScroll:{vertTouchDrag:!1}});let O=0;if(ce.length>0){const i=ce.map(d=>Number(d.time)).filter(d=>Number.isFinite(d)),o=new Set(i).size-1;if(o>=0){const d=()=>{const k=t.timeScale();if(h!=="time"){k.applyOptions({fixLeftEdge:!0,fixRightEdge:!0}),k.setVisibleLogicalRange({from:0,to:o});return}const E=z&&!T,B=z?!T:!G,Z=!E,ue=!B;k.applyOptions({fixLeftEdge:!Z,fixRightEdge:!ue}),O=requestAnimationFrame(()=>{const he=$e({lastIndex:o,width:k.width(),padLeft:Z,padRight:ue});try{he&&k.setVisibleLogicalRange(he)}catch{k.fitContent()}})};try{d()}catch{try{t.timeScale().fitContent(),d()}catch{}}}}Le.current&&(Le.current=!1),x&&t.applyOptions(x);const te=new Map;for(const i of e)i.data.length>0&&te.set(i.label,i.data[i.data.length-1]);return Se(te.size>0?te:void 0),()=>cancelAnimationFrame(O)},[U,e,ce,h,f,T,H,z,we,Pe,Re,le,D,F,x]),g.useEffect(()=>{const t=A.current;if(!t||e.length===0)return;const a=e.length===1;let n;for(const c of e)if(c.data.length>0){n=c.data[c.data.length-1].time;break}if(!n)return;const s=c=>{const{time:p=n,seriesData:m,point:_}=c||{},w=e[0],v=w?N.current.get(oe(w)):void 0;if(!_||!p){const q=new Map;for(const b of e)b.data.length>0&&q.set(b.label,b.data[b.data.length-1]);if(Se(q.size>0?q:void 0),a||H||ge)J(null);else if(v&&n){const b=e[0];if(b&&b.data.length>0){const L=t.timeScale().timeToCoordinate(n),W=b.data[b.data.length-1].value,y=v.priceToCoordinate(W);L!==null&&y!==null&&J({x:L,y})}}return}const G=new Map;let V,ee=v;for(let q=0;q<e.length;q++){const b=e[q],M=N.current.get(oe(b));if(!M)continue;const L=m?.get(M),W=L&&"value"in L?L.value:void 0;let y;if(W!==void 0)y=W;else if(a){const O=Number(p),te=b.data.length>0?Number(b.data[0].time):Number.POSITIVE_INFINITY;O<te?y=Number.NaN:y=b.data[b.data.length-1]?.value??0}else{const O=b.data.length>0?Number(b.data[0].time):Number.POSITIVE_INFINITY;if(Number(p)<O)continue;y=Ze(b.data,Number(p))}Number.isNaN(y)||(V===void 0&&(V=y,ee=M),G.set(b.label,{value:y,time:p}))}if(Se(G),V!==void 0&&ee){const b=t.timeScale().timeToCoordinate(p),M=ee.priceToCoordinate(V);if(b!==null&&M!==null){const L=j.current;if(L){const W=L.querySelector(".LightweightChart");if(W){const y=W.getBoundingClientRect(),O=L.getBoundingClientRect(),te=b+(y.left-O.left),i=M+(y.top-O.top);J({x:te,y:i})}else J({x:b,y:M})}else J({x:b,y:M})}else J(null)}else J(null)};return t.subscribeCrosshairMove(s),T&&t.subscribeClick(s),!a&&!H&&!ge&&requestAnimationFrame(()=>{const c=e[0],p=c?N.current.get(oe(c)):void 0;if(p&&n&&c.data.length>0){const _=t.timeScale().timeToCoordinate(n),w=c.data[c.data.length-1].value,v=p.priceToCoordinate(w);_!==null&&v!==null&&J({x:_,y:v})}}),()=>{t.unsubscribeCrosshairMove(s),T&&t.unsubscribeClick(s)}},[e,H,T,ge]),g.useEffect(()=>{const t=A.current;if(!t||e.length===0)return;const a=t.timeScale(),n=()=>{const s=Ne.current;if(s.length===0)return;const c=a.getVisibleRange();if(!c)return;const p=Number(c.from),m=Number(c.to);for(const _ of s){const w=[];for(const v of e)if(_.labels.has(v.label))for(const G of v.data){const V=Number(G.time);V>=p&&V<=m&&w.push(G.value)}w.length>0&&_.updateVisibleRange(w)}};return a.subscribeVisibleLogicalRangeChange(n),()=>{a.unsubscribeVisibleLogicalRangeChange(n)}},[e]),g.useEffect(()=>{const t=A.current,a=Array.from(N.current.values()),n=[];if(t&&a.length>0&&R){const s=I.getDefaultVerticalLine(),c=a[0];for(const p of R){const m={...s,...p.options||{}};m.color&&(m.color=Y.resolveCSSColor(m.color,m.color));const _=new ht.VertLine(t,c,p.xCoordinate,m);c.attachPrimitive(_),n.push(_)}}return()=>{if(a.length>0){const s=a[0];for(const c of n)try{s.detachPrimitive(c)}catch{}}}},[R,e,U]),g.useEffect(()=>{const t=[],a=[];if(D&&D.length>0){const n=Y.resolveCSSColor("hsl(var(--destructive))","rgb(239, 68, 68)");for(const s of D){if(!Number.isFinite(s.price))continue;const c=We(N.current,e,s.seriesLabel);if(!c)continue;const p=s.color?Y.resolveCSSColor(s.color,s.color):n,m=s.lineWidth??1,_=s.lineStyle??$.LineStyle.Dashed,w=c.createPriceLine({price:s.price,color:p,lineWidth:m,lineStyle:_,axisLabelVisible:s.axisLabelVisible??!0,title:s.title??""});t.push({series:c,line:w});const v=new vt(c,s.price,p,m,_);c.attachPrimitive(v),a.push({series:c,primitive:v})}}return()=>{for(const{series:n,primitive:s}of a)try{n.detachPrimitive(s)}catch{}for(const{series:n,line:s}of t)try{n.removePriceLine(s)}catch{}}},[D,e,U]),g.useEffect(()=>{const t=[];if(F&&F.length>0){const a="rgba(239, 68, 68, 0.12)";for(const n of F){const s=We(N.current,e,n.seriesLabel);if(!s)continue;const c=n.color?Y.resolveCSSColor(n.color,a):a,p=new bt(s,n.priceTop,n.priceBottom,c);s.attachPrimitive(p),t.push({series:s,zone:p})}}return()=>{for(const{series:a,zone:n}of t)try{a.detachPrimitive(n)}catch{}}},[F,e,U]),g.useEffect(()=>{const t=A.current;if(!t||!R?.length){Me([]);return}const a=()=>{const c=t.timeScale(),p=R.map(m=>c.timeToCoordinate(m.xCoordinate)??null);Me(p),j.current&&De(j.current.clientWidth)};a();const n=t.timeScale(),s=()=>{a()};return n.subscribeVisibleLogicalRangeChange(s),()=>{n.unsubscribeVisibleLogicalRangeChange(s)}},[R,e,U]),g.useLayoutEffect(()=>{tt(t=>{const a={};let n=!1;for(const[s,c]of Ae.current){if(!c)continue;const p=c.offsetWidth;a[s]=p,t[s]!==p&&(n=!0)}return Object.keys(t).length!==Object.keys(a).length&&(n=!0),n?a:t})});const fe=e.length===1,je=ye.useMemo(()=>{if(fe&&e.length>0){const t=e[0];if(t.data.length>0)return t.data[t.data.length-1]}},[fe,e]);return C.jsxs("div",{ref:j,className:Q.cn("relative z-[1] w-full h-full overflow-hidden p-0",r),children:[pe!=null&&pe!==!1&&fe?C.jsx("div",{className:"absolute top-2 left-2 right-2 z-[100] pointer-events-none",children:C.jsx("div",{className:"text-xl sm:text-2xl font-semibold text-foreground whitespace-nowrap overflow-hidden text-ellipsis",children:pe})}):Qe&&fe&&je&&C.jsx(ut.GraphCurrentValue,{point:je,xMeasureUnit:h,yMeasureUnit:f,decimals:se}),ke&&Ee&&C.jsx(ft.GraphTooltip,{points:ke,xMeasureUnit:h,yMeasureUnit:f,series:e,position:Ee,containerRef:j,decimals:se}),l&&C.jsx(Ye,{series:e,onUnselect:u}),R&&R.length>0&&Ie.length===R.length&&C.jsx("div",{className:"absolute inset-0 z-[5] pointer-events-none","aria-hidden":!0,children:R.map((t,a)=>{const n=Ie[a],s=t.options??{};if(n===null)return null;const{labelText:c,valueStr:p}=He(s,f,se),m=s.tooltipText??"";if(!(c||p||m))return null;const w=s.labelBackgroundColor??s.color??"hsl(var(--background))",v=s.labelTextColor??"hsl(var(--foreground))",G=`${String(t.xCoordinate)}-${c}-${m}`,V=Oe(n,Ue,et[a]);return C.jsxs("div",{ref:ee=>{Ae.current.set(a,ee)},className:Q.cn("group absolute top-2 px-2 py-1 rounded text-xs font-medium whitespace-nowrap shadow-sm border border-border",m&&"pointer-events-auto cursor-default"),style:{...V,backgroundColor:w,color:v},children:[c&&C.jsx("span",{className:"block",children:c}),p&&C.jsx("span",{className:Q.cn("block",c&&"mt-0.5"),children:p}),m&&C.jsx("span",{className:"pointer-events-none absolute right-0 top-full z-10 mt-1 hidden w-[280px] whitespace-normal rounded border border-border bg-popover px-2.5 py-1.5 text-left leading-snug text-popover-foreground shadow-md group-hover:block",role:"tooltip",children:m})]},G)})}),C.jsx("div",{ref:be,className:"LightweightChart w-full h-full overflow-hidden"})]})}function Ye({series:e,markers:r,onUnselect:l,inline:u=!1}){const h=e.every(f=>f.data.length===0);return C.jsxs("div",{className:Q.cn("flex flex-wrap",u?"items-center justify-center gap-6":"absolute top-0 left-2 z-[10] gap-2"),children:[e.map((f,x)=>{const R=Ke.getSeriesColorPalette(x),D=(f.lineColor??f.color)||R.line,F=!h&&f.data.length===0;return C.jsxs("div",{className:Q.cn("flex shrink-0 items-center gap-1.5",!u&&"px-2 py-1 bg-background/80 backdrop-blur-sm rounded border border-border",F&&"opacity-50"),children:[C.jsx("div",{className:Q.cn("shrink-0",u?"size-2 rounded-full":"h-3 w-3 rounded-sm"),style:{backgroundColor:D}}),C.jsxs("span",{className:Q.cn("font-medium text-foreground",u?"text-sm":"text-[10px] sm:text-xs"),children:[f.label,F&&" (no data)"]}),l&&C.jsx("button",{type:"button",onClick:se=>{se.stopPropagation(),l(f.label)},className:"ml-1 flex items-center justify-center w-4 h-4 rounded-sm hover:bg-muted transition-colors cursor-pointer","aria-label":`Remove ${f.label}`,children:C.jsxs("svg",{xmlns:"http://www.w3.org/2000/svg",width:"12",height:"12",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",className:"text-muted-foreground hover:text-foreground","aria-hidden":"true",children:[C.jsx("title",{children:"Close"}),C.jsx("line",{x1:"18",y1:"6",x2:"6",y2:"18"}),C.jsx("line",{x1:"6",y1:"6",x2:"18",y2:"18"})]})})]},f.id??f.label)}),r?.map(f=>C.jsxs("div",{className:Q.cn("flex shrink-0 items-center gap-1.5",!u&&"px-2 py-1 bg-background/80 backdrop-blur-sm rounded border border-border"),children:[C.jsx("div",{className:"h-3 w-0.5 shrink-0",style:{backgroundColor:f.color}}),C.jsx("span",{className:Q.cn("font-medium text-foreground",u?"text-sm":"text-[10px] sm:text-xs"),children:f.label})]},f.id))]})}exports.Graph=yt;exports.GraphLegend=Ye;exports.VERTICAL_LINE_LABEL_EDGE_PADDING=_e;exports.getStepForwardValue=Ze;exports.getVerticalLineLabelStyle=Oe;exports.getVerticalLineTooltipContent=He;exports.seriesIdentity=oe;exports.visibleLogicalRangeFor=$e;
|