Skip to content

A modern platform for visual, flexible, and extensible graph-based investigations. For cybersecurity analysts and investigators.

feat(app): fix double-click detection and graphRef initialization

dextmorgn3ef942a2bce5
diff --git a/flowsint-app/src/components/sketches/graph/components/graph-main.tsx b/flowsint-app/src/components/sketches/graph/components/graph-main.tsx
index 2f6014b3..ae9d8d3f 100644
--- a/flowsint-app/src/components/sketches/graph/components/graph-main.tsx
+++ b/flowsint-app/src/components/sketches/graph/components/graph-main.tsx
@@ -28,6 +28,8 @@ type EdgeContextMenuProps = BaseContextMenuProps & {
edge?: GraphEdge
edges?: GraphEdge[]
setMenu: Dispatch<SetStateAction<EdgeContextMenuProps | null>>
+ onSubmitNew?: (edgeId: string, label: string) => void
+ onDismissNew?: (edgeId: string) => void
}
type BackgroundContextMenuProps = BaseContextMenuProps & {
@@ -79,36 +81,24 @@ const GraphMain = () => {
[toggleNodeSelection, setCurrentNodeId, clearSelectedNodes]
)
- const lastClickRef = useRef<{ time: number; x: number; y: number }>({ time: 0, x: 0, y: 0 })
+ const lastBgClickRef = useRef(0)
const handleBackgroundClick = useCallback(
(event?: MouseEvent) => {
- if (quickAdd.active) {
- closeQuickAdd()
- return
- }
-
- // Double-click detection from background clicks
- if (event && graphRef.current && containerRef.current) {
- const now = Date.now()
- const last = lastClickRef.current
- const dx = event.clientX - last.x
- const dy = event.clientY - last.y
- const timeDiff = now - last.time
- const dist = Math.sqrt(dx * dx + dy * dy)
-
- lastClickRef.current = { time: now, x: event.clientX, y: event.clientY }
-
- if (timeDiff < 400 && dist < 10) {
- // Double click detected
+ // Double-click detection from consecutive onBackgroundClick calls
+ const now = Date.now()
+ if (event && now - lastBgClickRef.current < 400) {
+ lastBgClickRef.current = 0
+ if (graphRef.current && containerRef.current) {
const rect = containerRef.current.getBoundingClientRect()
- const screenX = event.clientX - rect.left
- const screenY = event.clientY - rect.top
- const graphCoords = graphRef.current.screen2GraphCoords(screenX, screenY)
- openQuickAdd(screenX, screenY, graphCoords.x, graphCoords.y)
- return
+ const sx = event.clientX - rect.left
+ const sy = event.clientY - rect.top
+ const gc = graphRef.current.screen2GraphCoords(sx, sy)
+ openQuickAdd(sx, sy, gc.x, gc.y)
}
+ return
}
+ lastBgClickRef.current = now
if (linkCreation.mode !== 'idle') {
cancelLinkCreation()
@@ -122,19 +112,16 @@ const GraphMain = () => {
setEdgeMenu(null)
setBackgroundMenu(null)
},
- [
- quickAdd.active,
- closeQuickAdd,
- openQuickAdd,
- linkCreation.mode,
- cancelLinkCreation,
- setCurrentNodeId,
- clearSelectedNodes,
- clearSelectedEdges,
- setCurrentEdgeId
- ]
+ [openQuickAdd, linkCreation.mode, cancelLinkCreation, setCurrentNodeId, clearSelectedNodes, clearSelectedEdges, setCurrentEdgeId]
)
+ // Stable ref so ForceGraph2D always calls the latest version
+ const bgClickRef = useRef(handleBackgroundClick)
+ bgClickRef.current = handleBackgroundClick
+ const stableBackgroundClick = useCallback((event?: MouseEvent) => {
+ bgClickRef.current(event)
+ }, [])
+
const onNodeContextMenu = useCallback(
(node: any, event: MouseEvent) => {
if (!containerRef.current || !node) return
@@ -151,7 +138,7 @@ const GraphMain = () => {
wrapperWidth: pane.width,
wrapperHeight: pane.height,
setMenu: setBackgroundMenu,
- onClick: handleBackgroundClick
+ onClick: stableBackgroundClick
})
setNodeMenu(null)
return
@@ -163,7 +150,7 @@ const GraphMain = () => {
wrapperWidth: pane.width,
wrapperHeight: pane.height,
setMenu: setNodeMenu,
- onClick: handleBackgroundClick
+ onClick: stableBackgroundClick
})
},
[selectedNodes]
@@ -185,7 +172,7 @@ const GraphMain = () => {
wrapperWidth: pane.width,
wrapperHeight: pane.height,
setMenu: setEdgeMenu,
- onClick: handleBackgroundClick
+ onClick: stableBackgroundClick
})
} else {
setEdgeMenu({
@@ -195,7 +182,7 @@ const GraphMain = () => {
wrapperWidth: pane.width,
wrapperHeight: pane.height,
setMenu: setEdgeMenu,
- onClick: handleBackgroundClick
+ onClick: stableBackgroundClick
})
}
setNodeMenu(null)
@@ -217,7 +204,7 @@ const GraphMain = () => {
wrapperWidth: pane.width,
wrapperHeight: pane.height,
setMenu: setBackgroundMenu,
- onClick: handleBackgroundClick
+ onClick: stableBackgroundClick
})
}, [])
@@ -236,7 +223,7 @@ const GraphMain = () => {
wrapperWidth: pane.width,
wrapperHeight: pane.height,
setMenu: setEdgeMenu,
- onClick: handleBackgroundClick,
+ onClick: stableBackgroundClick,
onSubmitNew: submitNewEdge,
onDismissNew: dismissNewEdge
})
@@ -268,7 +255,7 @@ const GraphMain = () => {
onNodeRightClick={onNodeContextMenu}
onEdgeRightClick={onEdgeContextMenu}
onBackgroundRightClick={onBackgroundContextMenu}
- onBackgroundClick={handleBackgroundClick}
+ onBackgroundClick={stableBackgroundClick}
showLabels={true}
showIcons={true}
onGraphRef={handleGraphRef}
diff --git a/flowsint-app/src/components/sketches/graph/components/quick-add-overlay.tsx b/flowsint-app/src/components/sketches/graph/components/quick-add-overlay.tsx
index aeb98942..2df1a626 100644
--- a/flowsint-app/src/components/sketches/graph/components/quick-add-overlay.tsx
+++ b/flowsint-app/src/components/sketches/graph/components/quick-add-overlay.tsx
@@ -37,29 +37,6 @@ export const QuickAddOverlay = ({
}
}, [active])
- // Close on click outside
- useEffect(() => {
- if (!active) return
-
- const handleMouseDown = (e: MouseEvent) => {
- if (
- inputRef.current &&
- !inputRef.current.closest('[data-quick-add]')?.contains(e.target as Node)
- ) {
- onCancel()
- }
- }
-
- const timer = setTimeout(() => {
- document.addEventListener('mousedown', handleMouseDown)
- }, 50)
-
- return () => {
- clearTimeout(timer)
- document.removeEventListener('mousedown', handleMouseDown)
- }
- }, [active, onCancel])
-
const handleKeyDown = useCallback(
(e: React.KeyboardEvent) => {
if (e.key === 'Enter') {
@@ -99,6 +76,7 @@ export const QuickAddOverlay = ({
value={text}
onChange={(e) => onTextChange(e.target.value)}
onKeyDown={handleKeyDown}
+ onBlur={onCancel}
className="h-8 min-w-[200px] max-w-[320px] text-sm shadow-lg border-border/80 bg-background"
placeholder="Type to add..."
/>
diff --git a/flowsint-app/src/components/sketches/graph/hooks/use-graph-events.ts b/flowsint-app/src/components/sketches/graph/hooks/use-graph-events.ts
index 39d88520..652b29c3 100644
--- a/flowsint-app/src/components/sketches/graph/hooks/use-graph-events.ts
+++ b/flowsint-app/src/components/sketches/graph/hooks/use-graph-events.ts
@@ -88,7 +88,7 @@ export const useGraphEvents = ({
)
const handleBackgroundClick = useCallback(
- (event?: MouseEvent) => {
+ (event: MouseEvent) => {
onBackgroundClick?.(event)
},
[onBackgroundClick]
diff --git a/flowsint-app/src/components/sketches/graph/hooks/use-graph-initialization.ts b/flowsint-app/src/components/sketches/graph/hooks/use-graph-initialization.ts
index 2bb71a98..d8907890 100644
--- a/flowsint-app/src/components/sketches/graph/hooks/use-graph-initialization.ts
+++ b/flowsint-app/src/components/sketches/graph/hooks/use-graph-initialization.ts
@@ -1,4 +1,4 @@
-import { useEffect, useRef, useCallback } from 'react'
+import { useEffect, useRef } from 'react'
interface UseGraphInitializationParams {
graphRef: React.RefObject<any>
@@ -94,7 +94,6 @@ export const useGraphInitialization = ({
const graphInstance = graphRef.current
if (!graphInstance || isGraphReadyRef.current) return
- // Wait for graph methods to be available
if (typeof graphInstance.zoom !== 'function' || typeof graphInstance.zoomToFit !== 'function') {
return
}
diff --git a/flowsint-app/src/components/sketches/graph/hooks/use-quick-add.ts b/flowsint-app/src/components/sketches/graph/hooks/use-quick-add.ts
index 6d1d3c7c..280f514f 100644
--- a/flowsint-app/src/components/sketches/graph/hooks/use-quick-add.ts
+++ b/flowsint-app/src/components/sketches/graph/hooks/use-quick-add.ts
@@ -1,4 +1,4 @@
-import { useCallback, useEffect, useRef, useState } from 'react'
+import { useCallback, useEffect, useState } from 'react'
import { useGraphStore } from '@/stores/graph-store'
import { sketchService } from '@/api/sketch-service'
import { type GraphNode, type NodeProperties } from '@/types/graph'
diff --git a/flowsint-app/src/components/sketches/graph/index.tsx b/flowsint-app/src/components/sketches/graph/index.tsx
index d8a58e31..ec012812 100644
--- a/flowsint-app/src/components/sketches/graph/index.tsx
+++ b/flowsint-app/src/components/sketches/graph/index.tsx
@@ -65,6 +65,15 @@ const GraphViewer: React.FC<GraphViewerProps> = ({
const graphRef = useRef<any>(null)
const containerRef = useRef<HTMLDivElement>(null)
+ // Notify parent when graphRef becomes available
+ const graphReadyRef = useRef(false)
+ useEffect(() => {
+ if (graphRef.current && !graphReadyRef.current) {
+ graphReadyRef.current = true
+ onGraphRef?.(graphRef.current)
+ }
+ })
+
const { saveAllNodePositions } = useSaveNodePositions(sketchId)
const isSelectorModeActive = useGraphControls((s) => s.isSelectorModeActive)