fix: resolve SVG dimension observer loading mount lifecycle issue using callback refs
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState, useRef } from "react"
|
||||
import { useEffect, useState, useRef, useCallback } from "react"
|
||||
import { Download, Search, Trash2, Edit3, Star, Layers, Activity, HardDrive, X, Check, Copy, Bot, Eye, Code, Brain, Compass, ChevronDown, ChevronUp } from "lucide-react"
|
||||
import { api, type DiscoverResp, type Fit, type Job, type ModelInfo, type RoutingResp, type UpdatesResp, type ConnectResp } from "@/lib/api"
|
||||
import { CapsChips } from "@/components/CapsChips"
|
||||
@@ -142,17 +142,22 @@ function Cockpit() {
|
||||
|
||||
// Canvas pixel tracking for pixel-perfect connection graph without non-uniform scaling
|
||||
const [dimensions, setDimensions] = useState({ width: 800, height: 360 })
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
const observerRef = useRef<ResizeObserver | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (!containerRef.current) return
|
||||
const observer = new ResizeObserver((entries) => {
|
||||
if (!entries || entries.length === 0) return
|
||||
const rect = entries[0].contentRect
|
||||
setDimensions({ width: rect.width, height: rect.height })
|
||||
})
|
||||
observer.observe(containerRef.current)
|
||||
return () => observer.disconnect()
|
||||
const containerRef = useCallback((node: HTMLDivElement | null) => {
|
||||
if (observerRef.current) {
|
||||
observerRef.current.disconnect()
|
||||
observerRef.current = null
|
||||
}
|
||||
if (node) {
|
||||
const observer = new ResizeObserver((entries) => {
|
||||
if (!entries || entries.length === 0) return
|
||||
const rect = entries[0].contentRect
|
||||
setDimensions({ width: rect.width, height: rect.height })
|
||||
})
|
||||
observer.observe(node)
|
||||
observerRef.current = observer
|
||||
}
|
||||
}, [])
|
||||
|
||||
const w = dimensions.width
|
||||
|
||||
Reference in New Issue
Block a user