Skip to content

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

1.6 KB
Raw Blame
import { useState, useCallback } from 'react'
import type { GraphNode, GraphViewerRef } from '@/types'
import { TooltipState } from '../utils/types'

export const useTooltip = (graphRef: React.RefObject<GraphViewerRef>) => {
  const [tooltip, setTooltip] = useState<TooltipState>({
    x: 0,
    y: 0,
    data: null,
    visible: false
  })

  const showTooltip = useCallback(
    (node: GraphNode | null) => {
      if (!node || !graphRef.current) {
        setTooltip((prev) => ({ ...prev, visible: false }))
        return
      }

      const weight = node.neighbors?.length || 0
      const label = node.nodeLabel || node.id

      try {
        const screenCoords = graphRef.current.graph2ScreenCoords(node.x, node.y)
        const tooltipWidth = 120
        const tooltipHeight = 60

        let x = screenCoords.x
        let y = screenCoords.y - 30

        if (x + tooltipWidth > window.innerWidth) {
          x = window.innerWidth - tooltipWidth - 10
        }
        if (x < 10) {
          x = 10
        }
        if (y < tooltipHeight + 10) {
          y = screenCoords.y + 100
        }

        setTooltip({
          x,
          y,
          data: {
            label,
            type: node.nodeType || 'unknown',
            connections: weight.toString()
          },
          visible: true
        })
      } catch {
        setTooltip((prev) => ({ ...prev, visible: false }))
      }
    },
    [graphRef]
  )

  const hideTooltip = useCallback(() => {
    setTooltip((prev) => ({ ...prev, visible: false }))
  }, [])

  return {
    tooltip,
    showTooltip,
    hideTooltip
  }
}