Files
supabase/apps/studio/components/ui/Charts/useChartHighlight.tsx
T
Francesco SansalvadoreandTheOtherBrian1 939e5c855a feat: reports infra o11y (#33310)
* test client connections charts

* add granularity to database dashboard

* add range selection

* add selection to area charts

* refactor ChartHeader

* fix default

* fix time preset selected label and add tooltip actions

* client connections stacked chart

* polish all new chartts

* change attribute

* fix

* add real queries

* memory stack

* cleanup

* precision 1

* update charts

* add endpoints

* tooltip

* format

* improve charts

* toggle max value

* show limit

* cleanup

* cleanup

* update api schema

* fix ram_usage_max_available

* remove pgbouncer and comments

* show total with percentage in chart header

* pretty

* hide

* update reports database

* remove client_connections

* revert

* fix connectionStrings error

* restore correct queries and conditions

* vertically zoom in to percentage data

* add feature flag logic on charts

* add feature flag logic to date picker

* chore: remove comment

* add tooltip to charts metrics

* chore: comment new pg endpoints

* feat: lazy load ComposedChartHandler queries

* feat: dim non-hovered chart values when hovering chart label item

* feat: improve dim styling

* chore: improve light mode stacked chart colors

* chore: remove 15s interval

* chore: sync types

* chore: remove type

* chore: restore type

* fix hook error

* show chart type toggle

* remove console log

* temp

* keep connections charts as before

* restore DateRangePicker

* show last aggregate value when not hovering

* clean up imports

* disk IOps read and write

* restore flag

* refactor queries

* clean up grafana promo banner

* Update database-charts.ts | updated language

* fix hook error

* fix hook error

* fix hook error

* attempt fix hook error

---------

Co-authored-by: TheOtherBrian1 <91111415+TheOtherBrian1@users.noreply.github.com>
2025-04-14 10:53:52 +02:00

86 lines
2.5 KiB
TypeScript

import { useState } from 'react'
import dayjs from 'dayjs'
export interface ChartHighlight {
left: string | undefined
right: string | undefined
coordinates: { left: any; right: any }
isSelecting: boolean
popoverPosition: { x: number; y: number } | null
handleMouseDown: (e: { activeLabel?: string; coordinates?: any }) => void
handleMouseMove: (e: { activeLabel?: string; coordinates?: any }) => void
handleMouseUp: (e: any) => void
clearHighlight: () => void
}
export function useChartHighlight(): ChartHighlight {
const [left, setLeft] = useState<string | undefined>(undefined)
const [right, setRight] = useState<string | undefined>(undefined)
const [coordinates, setCoordinates] = useState<any>({ left: undefined, right: undefined })
const [isSelecting, setIsSelecting] = useState(false)
const [popoverPosition, setPopoverPosition] = useState<{ x: number; y: number } | null>(null)
const [initialPoint, setInitialPoint] = useState<string | undefined>(undefined)
const handleMouseDown = (e: any) => {
clearHighlight()
if (!e || !e.activeLabel) return
setIsSelecting(true)
setLeft(e.activeLabel)
setRight(e.activeLabel)
setInitialPoint(e.activeLabel)
setCoordinates({ left: e.coordinates, right: e.coordinates })
}
const handleMouseMove = (e: any) => {
if (!isSelecting || !e || !e.activeLabel) return
const currentTimestamp = dayjs(e.activeLabel)
const initialTimestamp = dayjs(initialPoint)
if (currentTimestamp.isBefore(initialTimestamp)) {
// If dragging left, update left and keep right as initial
setLeft(e.activeLabel)
setRight(initialPoint)
setCoordinates({
left: e.coordinates,
right: coordinates.right,
})
} else {
// If dragging right, update right and keep left as initial
setRight(e.activeLabel)
setLeft(initialPoint)
setCoordinates({
left: coordinates.left,
right: e.coordinates,
})
}
}
const handleMouseUp = (e: any) => {
if (!isSelecting) return
setIsSelecting(false)
setPopoverPosition({ x: e.chartX, y: e.chartY })
setInitialPoint(undefined)
}
const clearHighlight = () => {
setLeft(undefined)
setRight(undefined)
setCoordinates({ left: undefined, right: undefined })
setPopoverPosition(null)
setInitialPoint(undefined)
}
return {
left,
right,
coordinates,
isSelecting,
popoverPosition,
handleMouseDown,
handleMouseMove,
handleMouseUp,
clearHighlight,
}
}