Skip to content

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

feat(app): add quick-add node on double-click

dextmorgn5292a3c5412b
diff --git a/flowsint-app/src/api/sketch-service.ts b/flowsint-app/src/api/sketch-service.ts
index 0196d0ed..7026fb40 100644
--- a/flowsint-app/src/api/sketch-service.ts
+++ b/flowsint-app/src/api/sketch-service.ts
@@ -79,6 +79,12 @@ export const sketchService = {
method: 'GET'
})
},
+ detectType: async (text: string): Promise<any> => {
+ return fetchWithAuth(`/api/types/detect`, {
+ method: 'POST',
+ body: JSON.stringify({ text })
+ })
+ },
update: async (sketchId: string, body: BodyInit): Promise<any> => {
return fetchWithAuth(`/api/sketches/${sketchId}`, {
method: 'PUT',
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 9061e7e2..2f6014b3 100644
--- a/flowsint-app/src/components/sketches/graph/components/graph-main.tsx
+++ b/flowsint-app/src/components/sketches/graph/components/graph-main.tsx
@@ -8,6 +8,8 @@ import { PathPanel } from '../actions/path-finder'
import { useParams } from '@tanstack/react-router'
import { type GraphNode, GraphEdge } from '@/types'
import { useLinkCreation } from '../hooks/use-link-creation'
+import { useQuickAdd } from '../hooks/use-quick-add'
+import { QuickAddOverlay } from './quick-add-overlay'
type BaseContextMenuProps = {
rawTop: number
@@ -61,6 +63,9 @@ const GraphMain = () => {
dismissNewEdge
} = useLinkCreation(sketchId)
+ const { quickAdd, openQuickAdd, closeQuickAdd, setQuickAddText, submitQuickAdd } =
+ useQuickAdd(sketchId)
+
const handleNodeClick = useCallback(
(node: any, event: MouseEvent) => {
const isMultiSelect = event.ctrlKey || event.shiftKey
@@ -74,26 +79,61 @@ const GraphMain = () => {
[toggleNodeSelection, setCurrentNodeId, clearSelectedNodes]
)
- const handleBackgroundClick = useCallback(() => {
- if (linkCreation.mode !== 'idle') {
- cancelLinkCreation()
- return
- }
- setCurrentNodeId(null)
- clearSelectedNodes()
- clearSelectedEdges()
- setCurrentEdgeId(null)
- setNodeMenu(null)
- setEdgeMenu(null)
- setBackgroundMenu(null)
- }, [
- linkCreation.mode,
- cancelLinkCreation,
- setCurrentNodeId,
- clearSelectedNodes,
- clearSelectedEdges,
- setCurrentEdgeId
- ])
+ const lastClickRef = useRef<{ time: number; x: number; y: number }>({ time: 0, x: 0, y: 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
+ 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
+ }
+ }
+
+ if (linkCreation.mode !== 'idle') {
+ cancelLinkCreation()
+ return
+ }
+ setCurrentNodeId(null)
+ clearSelectedNodes()
+ clearSelectedEdges()
+ setCurrentEdgeId(null)
+ setNodeMenu(null)
+ setEdgeMenu(null)
+ setBackgroundMenu(null)
+ },
+ [
+ quickAdd.active,
+ closeQuickAdd,
+ openQuickAdd,
+ linkCreation.mode,
+ cancelLinkCreation,
+ setCurrentNodeId,
+ clearSelectedNodes,
+ clearSelectedEdges,
+ setCurrentEdgeId
+ ]
+ )
const onNodeContextMenu = useCallback(
(node: any, event: MouseEvent) => {
@@ -237,6 +277,16 @@ const GraphMain = () => {
sketchId={sketchId}
linkCreation={linkCreationProp}
/>
+ <QuickAddOverlay
+ active={quickAdd.active}
+ position={quickAdd.position}
+ text={quickAdd.text}
+ detection={quickAdd.detection}
+ loading={quickAdd.loading}
+ onTextChange={setQuickAddText}
+ onSubmit={submitQuickAdd}
+ onCancel={closeQuickAdd}
+ />
<PathPanel />
{nodeMenu && selectedNodes.length === 0 && <NodeContextMenu {...nodeMenu} />}
{edgeMenu && <EdgeContextMenu {...edgeMenu} />}
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
new file mode 100644
index 00000000..aeb98942
--- /dev/null
+++ b/flowsint-app/src/components/sketches/graph/components/quick-add-overlay.tsx
@@ -0,0 +1,113 @@
+import { useCallback, useEffect, useRef } from 'react'
+import { Input } from '@/components/ui/input'
+import { Loader2 } from 'lucide-react'
+
+interface QuickAddOverlayProps {
+ active: boolean
+ position: { x: number; y: number } | null
+ text: string
+ detection: {
+ type: string
+ key: string
+ fields: Array<{ name: string; primary: boolean }>
+ } | null
+ loading: boolean
+ onTextChange: (text: string) => void
+ onSubmit: () => void
+ onCancel: () => void
+}
+
+export const QuickAddOverlay = ({
+ active,
+ position,
+ text,
+ detection,
+ loading,
+ onTextChange,
+ onSubmit,
+ onCancel
+}: QuickAddOverlayProps) => {
+ const inputRef = useRef<HTMLInputElement>(null)
+
+ useEffect(() => {
+ if (active) {
+ requestAnimationFrame(() => {
+ inputRef.current?.focus()
+ })
+ }
+ }, [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') {
+ e.preventDefault()
+ onSubmit()
+ } else if (e.key === 'Escape') {
+ e.preventDefault()
+ onCancel()
+ }
+ },
+ [onSubmit, onCancel]
+ )
+
+ if (!active || !position) return null
+
+ const detectedType = detection?.type
+ const showBadge = detectedType && text.trim().length > 0
+
+ return (
+ <div
+ data-quick-add
+ className="absolute z-50 pointer-events-auto"
+ style={{
+ left: position.x,
+ top: position.y,
+ transform: 'translate(-50%, -50%)'
+ }}
+ >
+ <div className="flex flex-col items-center gap-1.5">
+ {showBadge && !loading && (
+ <span className="shrink-0 text-[10px] px-1.5 py-0.5 rounded bg-primary/10 text-primary border border-primary/20 font-medium">
+ {detectedType}
+ </span>
+ )}
+ <Input
+ ref={inputRef}
+ value={text}
+ onChange={(e) => onTextChange(e.target.value)}
+ onKeyDown={handleKeyDown}
+ className="h-8 min-w-[200px] max-w-[320px] text-sm shadow-lg border-border/80 bg-background"
+ placeholder="Type to add..."
+ />
+ {loading && (
+ <div className="shrink-0">
+ <Loader2 className="h-3.5 w-3.5 animate-spin text-muted-foreground" />
+ </div>
+ )}
+ </div>
+ </div>
+ )
+}
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 c9d0d7bc..39d88520 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
@@ -5,7 +5,7 @@ interface UseGraphEventsParams {
onNodeClick?: (node: GraphNode, event: MouseEvent) => void
onNodeRightClick?: (node: GraphNode, event: MouseEvent) => void
onEdgeRightClick?: (edge: GraphEdge, event: MouseEvent) => void
- onBackgroundClick?: () => void
+ onBackgroundClick?: (event?: MouseEvent) => void
onBackgroundRightClick?: (event: MouseEvent) => void
autoZoomOnCurrentNode: boolean
autoZoomOnNode: boolean
@@ -87,9 +87,12 @@ export const useGraphEvents = ({
[toggleEdgeSelection, setCurrentEdgeId, clearSelectedEdges, edgeMap]
)
- const handleBackgroundClick = useCallback(() => {
- onBackgroundClick?.()
- }, [onBackgroundClick])
+ const handleBackgroundClick = useCallback(
+ (event?: MouseEvent) => {
+ onBackgroundClick?.(event)
+ },
+ [onBackgroundClick]
+ )
const handleBackgroundRightClick = useCallback(
(event: MouseEvent) => {
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
new file mode 100644
index 00000000..6d1d3c7c
--- /dev/null
+++ b/flowsint-app/src/components/sketches/graph/hooks/use-quick-add.ts
@@ -0,0 +1,157 @@
+import { useCallback, useEffect, useRef, useState } from 'react'
+import { useGraphStore } from '@/stores/graph-store'
+import { sketchService } from '@/api/sketch-service'
+import { type GraphNode, type NodeProperties } from '@/types/graph'
+import { useDebounce } from '@/hooks/use-debounce'
+import { toast } from 'sonner'
+import { v4 as uuidv4 } from 'uuid'
+
+interface DetectionResult {
+ type: string
+ key: string
+ fields: Array<{
+ name: string
+ label: string
+ description: string
+ required: boolean
+ primary: boolean
+ value: string | null
+ }>
+}
+
+interface QuickAddState {
+ active: boolean
+ position: { x: number; y: number } | null // screen coords
+ graphPosition: { x: number; y: number } | null // graph coords
+ text: string
+ detection: DetectionResult | null
+ loading: boolean
+}
+
+const INITIAL_STATE: QuickAddState = {
+ active: false,
+ position: null,
+ graphPosition: null,
+ text: '',
+ detection: null,
+ loading: false
+}
+
+export const useQuickAdd = (sketchId?: string) => {
+ const [state, setState] = useState<QuickAddState>(INITIAL_STATE)
+ const debouncedText = useDebounce(state.text, 300)
+
+ const addNode = useGraphStore((s) => s.addNode)
+ const replaceNode = useGraphStore((s) => s.replaceNode)
+
+ // Detect type when debounced text changes
+ useEffect(() => {
+ if (!debouncedText.trim() || !state.active) {
+ setState((prev) => ({ ...prev, detection: null, loading: false }))
+ return
+ }
+
+ let cancelled = false
+ setState((prev) => ({ ...prev, loading: true }))
+
+ sketchService
+ .detectType(debouncedText)
+ .then((result: DetectionResult) => {
+ if (!cancelled) {
+ setState((prev) => ({ ...prev, detection: result, loading: false }))
+ }
+ })
+ .catch(() => {
+ if (!cancelled) {
+ setState((prev) => ({ ...prev, detection: null, loading: false }))
+ }
+ })
+
+ return () => {
+ cancelled = true
+ }
+ }, [debouncedText, state.active])
+
+ const open = useCallback((screenX: number, screenY: number, graphX: number, graphY: number) => {
+ setState({
+ active: true,
+ position: { x: screenX, y: screenY },
+ graphPosition: { x: graphX, y: graphY },
+ text: '',
+ detection: null,
+ loading: false
+ })
+ }, [])
+
+ const close = useCallback(() => {
+ setState(INITIAL_STATE)
+ }, [])
+
+ const setText = useCallback((text: string) => {
+ setState((prev) => ({ ...prev, text }))
+ }, [])
+
+ const submit = useCallback(async () => {
+ if (!state.text.trim() || !sketchId || !state.graphPosition) return
+
+ const detection = state.detection
+ const nodeType = detection?.type?.toLowerCase() || 'phrase'
+ const label = state.text.trim()
+
+ // Build nodeProperties from detection fields
+ const nodeProperties: NodeProperties = {}
+ if (detection?.fields) {
+ for (const field of detection.fields) {
+ if (field.primary && field.value) {
+ nodeProperties[field.name] = field.value
+ }
+ }
+ }
+ // Fallback: if no primary field was set, use 'text' for phrase
+ if (Object.keys(nodeProperties).length === 0) {
+ nodeProperties['text'] = label
+ }
+
+ const tempId = uuidv4()
+ const node: GraphNode = {
+ id: tempId,
+ nodeType,
+ nodeLabel: label,
+ x: state.graphPosition.x,
+ y: state.graphPosition.y,
+ nodeSize: 4,
+ nodeColor: null,
+ nodeShape: 'circle',
+ nodeFlag: null,
+ nodeIcon: null,
+ nodeImage: null,
+ nodeProperties,
+ nodeMetadata: {}
+ }
+
+ // Close overlay immediately
+ setState(INITIAL_STATE)
+
+ // Optimistic add to store
+ addNode(node)
+
+ // Persist to API
+ try {
+ const response = await sketchService.addNode(sketchId, JSON.stringify(node))
+ const newNode: GraphNode = response.node
+ if (newNode) {
+ replaceNode(tempId, newNode.id, newNode.nodeProperties)
+ }
+ } catch {
+ toast.error('Could not create node.')
+ }
+ }, [state.text, state.detection, state.graphPosition, sketchId, addNode, replaceNode])
+
+ return {
+ quickAdd: state,
+ openQuickAdd: open,
+ closeQuickAdd: close,
+ setQuickAddText: setText,
+ submitQuickAdd: submit
+ }
+}
diff --git a/flowsint-app/src/components/sketches/graph/utils/types.ts b/flowsint-app/src/components/sketches/graph/utils/types.ts
index 934be142..c4e190e5 100644
--- a/flowsint-app/src/components/sketches/graph/utils/types.ts
+++ b/flowsint-app/src/components/sketches/graph/utils/types.ts
@@ -6,7 +6,7 @@ export interface GraphViewerProps {
onNodeClick?: (node: GraphNode, event: MouseEvent) => void
onNodeRightClick?: (node: GraphNode, event: MouseEvent) => void
onEdgeRightClick?: (edge: GraphEdge, event: MouseEvent) => void
- onBackgroundClick?: () => void
+ onBackgroundClick?: (event?: MouseEvent) => void
onBackgroundRightClick?: (event: MouseEvent) => void
showLabels?: boolean
showIcons?: boolean