Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Refactor from pointsRef to points state (#6313)

Artur Pata04933836f163
diff --git a/assets/js/dashboard/components/graph.tsx b/assets/js/dashboard/components/graph.tsx
index 85431805e7..f72eea8de5 100644
--- a/assets/js/dashboard/components/graph.tsx
+++ b/assets/js/dashboard/components/graph.tsx
@@ -101,7 +101,7 @@ function InnerGraph<T extends GraphYValues>({
highlightedIndex
}: GraphProps<T>) {
const [extraMarginLeft, setExtraMarginLeft] = useState(0)
-
+ const [points, setPoints] = useState<Point<T>[]>([])
const marginLeft = defaultMarginLeft + extraMarginLeft
const xLeftEdge = marginLeft
const xRightEdge = width - marginRight
@@ -109,9 +109,8 @@ function InnerGraph<T extends GraphYValues>({
const yBottomEdge = height - marginBottom
const svgRef = useRef<SVGSVGElement | null>(null)
- const pointsRef = useRef<Point<T>[] | null>(null)
- // Effect to fully redraw chart from scratch
+ // Effect to redraw the chart from scratch
useEffect(() => {
if (!svgRef.current) {
return
@@ -128,7 +127,7 @@ function InnerGraph<T extends GraphYValues>({
const svg = d3.select(svgRef.current)
const cleanup = () => {
- pointsRef.current = null
+ setPoints([])
svg.selectAll('*').remove()
}
@@ -291,7 +290,7 @@ function InnerGraph<T extends GraphYValues>({
}
}
- pointsRef.current = points
+ setPoints(points)
// Unhide chart
svg.attr('opacity', 1)
@@ -331,8 +330,7 @@ function InnerGraph<T extends GraphYValues>({
useEffect(() => {
const currentSvg = svgRef.current
- if (currentSvg && pointsRef.current) {
- const points = pointsRef.current
+ if (currentSvg && points.length) {
const svg = d3.select(currentSvg)
svg.on(
@@ -367,11 +365,11 @@ function InnerGraph<T extends GraphYValues>({
}
}
}
- }, [onPointerMove, isInHoverableArea, data])
+ }, [onPointerMove, isInHoverableArea, points])
useEffect(() => {
const currentSvg = svgRef.current
- if (currentSvg && pointsRef.current) {
+ if (currentSvg && points.length) {
const svg = d3.select(currentSvg)
svg.on(
'gotpointercapture',
@@ -387,11 +385,11 @@ function InnerGraph<T extends GraphYValues>({
svg.on('gotpointercapture', null)
}
}
- }, [onGotPointerCapture, isInHoverableArea, data])
+ }, [onGotPointerCapture, points])
useEffect(() => {
const currentSvg = svgRef.current
- if (currentSvg && pointsRef.current) {
+ if (currentSvg && points.length) {
const svg = d3.select(currentSvg)
svg.on(
'pointerenter',
@@ -407,11 +405,11 @@ function InnerGraph<T extends GraphYValues>({
svg.on('pointerenter', null)
}
}
- }, [onPointerEnter, isInHoverableArea, data])
+ }, [onPointerEnter, points])
useEffect(() => {
const currentSvg = svgRef.current
- if (currentSvg && pointsRef.current) {
+ if (currentSvg && points.length) {
const svg = d3.select(currentSvg)
svg.on(
'lostpointercapture pointerleave',
@@ -428,13 +426,12 @@ function InnerGraph<T extends GraphYValues>({
svg.on('lostpointercapture pointerleave', null)
}
}
- }, [onPointerLeave, isInHoverableArea, data])
+ }, [onPointerLeave, points])
useEffect(() => {
const currentSvg = svgRef.current
- if (currentSvg && pointsRef.current) {
+ if (currentSvg && points.length) {
const svg = d3.select(currentSvg)
- const points = pointsRef.current
if (typeof onClick !== 'function') {
svg.on('click', null)
} else {
@@ -467,48 +464,42 @@ function InnerGraph<T extends GraphYValues>({
svg.on('click', null)
}
}
- }, [onClick, isInHoverableArea, data])
+ }, [onClick, isInHoverableArea, points])
useEffect(() => {
- if (pointsRef.current) {
- const currentPoints = pointsRef.current
- currentPoints.forEach(({ dots }, index) =>
- dots.forEach((g) =>
- g.attr(
- 'data-active',
- highlightedIndex !== null && index === highlightedIndex ? '' : null
- )
+ points.forEach(({ dots }, index) =>
+ dots.forEach((g) =>
+ g.attr(
+ 'data-active',
+ highlightedIndex !== null && index === highlightedIndex ? '' : null
)
)
+ )
- if (svgRef.current) {
- const svg = d3.select(svgRef.current)
- let line = svg.select<SVGLineElement>(
- `#${highlightIndicatorGroupId} line`
- )
- const shouldShowLine = typeof highlightedIndex === 'number'
- if (shouldShowLine) {
- const { x } = currentPoints[highlightedIndex]
- if (line.empty()) {
- line = svg.select(`#${highlightIndicatorGroupId}`).append('line')
- }
- line
- .attr('x1', 0)
- .attr('x2', 0)
- .attr('y1', marginTop - HIGHLIGHT_LINE_VERTICAL_SPILL_PX)
- .attr(
- 'y2',
- height - marginBottom + HIGHLIGHT_LINE_VERTICAL_SPILL_PX
- )
- .attr('class', currentlySelectedLineClass)
- .attr('transform', `translate(${x}, 0)`)
- }
- if (!shouldShowLine && !line.empty()) {
- line.remove()
+ if (svgRef.current) {
+ const svg = d3.select(svgRef.current)
+ let line = svg.select<SVGLineElement>(
+ `#${highlightIndicatorGroupId} line`
+ )
+ const shouldShowLine = typeof highlightedIndex === 'number'
+ if (shouldShowLine) {
+ const { x } = points[highlightedIndex]
+ if (line.empty()) {
+ line = svg.select(`#${highlightIndicatorGroupId}`).append('line')
}
+ line
+ .attr('x1', 0)
+ .attr('x2', 0)
+ .attr('y1', marginTop - HIGHLIGHT_LINE_VERTICAL_SPILL_PX)
+ .attr('y2', height - marginBottom + HIGHLIGHT_LINE_VERTICAL_SPILL_PX)
+ .attr('class', currentlySelectedLineClass)
+ .attr('transform', `translate(${x}, 0)`)
+ }
+ if (!shouldShowLine && !line.empty()) {
+ line.remove()
}
}
- }, [highlightedIndex, data, height, marginBottom, marginTop])
+ }, [height, highlightedIndex, marginBottom, marginTop, points])
return (
<svg