The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
fix(reports): chart click selects bucket range, fix logs datepicker crash (#48009)
Jordi Enric9069e8d21e26
diff --git a/apps/studio/components/interfaces/Settings/Logs/Logs.DatePickers.tsx b/apps/studio/components/interfaces/Settings/Logs/Logs.DatePickers.tsxindex 4c4a321ed19..7da4fab326e 100644--- a/apps/studio/components/interfaces/Settings/Logs/Logs.DatePickers.tsx+++ b/apps/studio/components/interfaces/Settings/Logs/Logs.DatePickers.tsx@@ -90,6 +90,12 @@ export type DatePickerValue = {text?: string}+const toValidDate = (value?: string): Date | null => {+ if (!value) return null+ const date = new Date(value)+ return isNaN(date.getTime()) ? null : date+}+interface LogsDatePickerProps {value: DatePickerValuehelpers: DatetimeHelper[]@@ -139,13 +145,13 @@ export const LogsDatePicker = ({useEffect(() => {if (!open) {setCustomValue('')- setStartDate(value.from ? new Date(value.from) : null)- const defaultEndDate = value.to ? new Date(value.to) : new Date()+ setStartDate(toValidDate(value.from))+ const defaultEndDate = toValidDate(value.to) ?? new Date()setEndDate(defaultEndDate)setCurrentMonth(new Date(defaultEndDate))- const fromDate = value.from ? new Date(value.from) : null- const toDate = value.to ? new Date(value.to) : null+ const fromDate = toValidDate(value.from)+ const toDate = toValidDate(value.to)setStartTime({HH: fromDate?.getHours().toString().padStart(2, '0') || '00',@@ -180,11 +186,9 @@ export const LogsDatePicker = ({setOpen(false)}- const [startDate, setStartDate] = useState<Date | null>(value.from ? new Date(value.from) : null)- const [endDate, setEndDate] = useState<Date | null>(value.to ? new Date(value.to) : new Date())- const [currentMonth, setCurrentMonth] = useState<Date>(() =>- value.to ? new Date(value.to) : new Date()- )+ const [startDate, setStartDate] = useState<Date | null>(toValidDate(value.from))+ const [endDate, setEndDate] = useState<Date | null>(toValidDate(value.to) ?? new Date())+ const [currentMonth, setCurrentMonth] = useState<Date>(() => toValidDate(value.to) ?? new Date())const [startTime, setStartTime] = useState({HH: startDate?.getHours().toString() || '00',diff --git a/apps/studio/components/ui/Charts/ChartHighlightActions.tsx b/apps/studio/components/ui/Charts/ChartHighlightActions.tsxindex 2184b6ff766..418b7c5c8be 100644--- a/apps/studio/components/ui/Charts/ChartHighlightActions.tsx+++ b/apps/studio/components/ui/Charts/ChartHighlightActions.tsx@@ -14,6 +14,11 @@ import {import { ChartHighlight } from './useChartHighlight'import { useFormatDateTime } from '@/lib/datetime'+const toFormattableDate = (value: string): string | number => {+ const asNumber = Number(value)+ return value !== '' && Number.isFinite(asNumber) ? asNumber : value+}+export type UpdateDateRange = (from: string, to: string) => voidexport type ChartHighlightActionContext = {@@ -106,9 +111,9 @@ export const ChartHighlightActions = ({}}><DropdownMenuLabel className="flex items-center justify-center text-foreground-light font-mono gap-x-2 text-xs">- <span>{formatChartDate(selectedRangeStart!, 'MMM D, H:mm')}</span>+ <span>{formatChartDate(toFormattableDate(selectedRangeStart!), 'MMM D, H:mm')}</span><ArrowRight size={10} />- <span>{formatChartDate(selectedRangeEnd!, 'MMM D, H:mm')}</span>+ <span>{formatChartDate(toFormattableDate(selectedRangeEnd!), 'MMM D, H:mm')}</span></DropdownMenuLabel><DropdownMenuSeparator className="my-0" />{allActions.map((action) => {diff --git a/apps/studio/components/ui/Charts/ComposedChart.tsx b/apps/studio/components/ui/Charts/ComposedChart.tsxindex a539204af78..c8410e63a48 100644--- a/apps/studio/components/ui/Charts/ComposedChart.tsx+++ b/apps/studio/components/ui/Charts/ComposedChart.tsx@@ -490,9 +490,22 @@ export function ComposedChart({const activeTimestamp =data[activeTooltipIndex]?.[xAxisKey] ?? data[activeTooltipIndex]?.timestamp++ const next = data[activeTooltipIndex + 1]+ const prev = data[activeTooltipIndex - 1]+ const prevTimestamp = prev?.[xAxisKey] ?? prev?.timestamp+ const nextTimestamp =+ next?.[xAxisKey] ??+ next?.timestamp ??+ (prevTimestamp != null && activeTimestamp != null+ ? Number(activeTimestamp) + (Number(activeTimestamp) - Number(prevTimestamp))+ : undefined)+chartHighlight?.handleMouseDown({activeLabel: activeTimestamp?.toString(),coordinates: activeLabel,+ nextLabel: nextTimestamp?.toString(),+ nextCoordinate: nextTimestamp,})}}onMouseUp={chartHighlight?.handleMouseUp}diff --git a/apps/studio/components/ui/Charts/ComposedChartHandler.tsx b/apps/studio/components/ui/Charts/ComposedChartHandler.tsxindex 387edfd5701..fbb7b3ff373 100644--- a/apps/studio/components/ui/Charts/ComposedChartHandler.tsx+++ b/apps/studio/components/ui/Charts/ComposedChartHandler.tsx@@ -18,6 +18,11 @@ import { ProjectDailyStatsAttribute } from '@/data/analytics/project-daily-statsimport type { UpdateDateRange } from '@/pages/project/[ref]/observability/database'import { useDatabaseSelectorStateSnapshot } from '@/state/database-selector'+const parseChartTimestamp = (value: string) => {+ const asNumber = Number(value)+ return value !== '' && Number.isFinite(asNumber) ? dayjs(asNumber) : dayjs(value)+}+export interface ComposedChartHandlerProps {id?: stringlabel: string@@ -250,7 +255,9 @@ const ComposedChartHandler = ({onSelect: ({ start, end }) => {const projectRef = ref as stringif (!projectRef) return- const url = `/project/${projectRef}/logs/postgres-logs?its=${start}&ite=${end}`+ const its = parseChartTimestamp(start).toISOString()+ const ite = parseChartTimestamp(end).toISOString()+ const url = `/project/${projectRef}/logs/postgres-logs?its=${its}&ite=${ite}`router.push(url)},},diff --git a/apps/studio/components/ui/Charts/useChartHighlight.test.tsx b/apps/studio/components/ui/Charts/useChartHighlight.test.tsxnew file mode 100644index 00000000000..1be2c97e0eb--- /dev/null+++ b/apps/studio/components/ui/Charts/useChartHighlight.test.tsx@@ -0,0 +1,70 @@+import { act, renderHook } from '@testing-library/react'+import { describe, expect, it } from 'vitest'++import { useChartHighlight } from './useChartHighlight'++const BAR = '1784211420000'+const NEXT_BAR = '1784211480000'+const LATER_BAR = '1784211540000'++describe('useChartHighlight', () => {+ it('expands a single click to cover the clicked bucket', () => {+ const { result } = renderHook(() => useChartHighlight())++ act(() => {+ result.current.handleMouseDown({+ activeLabel: BAR,+ coordinates: Number(BAR),+ nextLabel: NEXT_BAR,+ nextCoordinate: Number(NEXT_BAR),+ })+ })+ act(() => {+ result.current.handleMouseUp({ chartX: 10, chartY: 20 })+ })++ expect(result.current.left).toBe(BAR)+ expect(result.current.right).toBe(NEXT_BAR)+ expect(result.current.coordinates.left).not.toBe(result.current.coordinates.right)+ })++ it('keeps the dragged range without expanding it', () => {+ const { result } = renderHook(() => useChartHighlight())++ act(() => {+ result.current.handleMouseDown({+ activeLabel: BAR,+ coordinates: Number(BAR),+ nextLabel: NEXT_BAR,+ nextCoordinate: Number(NEXT_BAR),+ })+ })+ act(() => {+ result.current.handleMouseMove({ activeLabel: LATER_BAR, coordinates: Number(LATER_BAR) })+ })+ act(() => {+ result.current.handleMouseUp({ chartX: 10, chartY: 20 })+ })++ expect(result.current.left).toBe(BAR)+ expect(result.current.right).toBe(LATER_BAR)+ })++ it('keeps left earlier than right when dragging leftward across epoch-ms labels', () => {+ const { result } = renderHook(() => useChartHighlight())++ act(() => {+ result.current.handleMouseDown({ activeLabel: LATER_BAR, coordinates: Number(LATER_BAR) })+ })+ act(() => {+ result.current.handleMouseMove({ activeLabel: BAR, coordinates: Number(BAR) })+ })+ act(() => {+ result.current.handleMouseUp({ chartX: 10, chartY: 20 })+ })++ expect(Number(result.current.left)).toBeLessThan(Number(result.current.right))+ expect(result.current.left).toBe(BAR)+ expect(result.current.right).toBe(LATER_BAR)+ })+})diff --git a/apps/studio/components/ui/Charts/useChartHighlight.tsx b/apps/studio/components/ui/Charts/useChartHighlight.tsxindex 1fa248b8b80..daea2835060 100644--- a/apps/studio/components/ui/Charts/useChartHighlight.tsx+++ b/apps/studio/components/ui/Charts/useChartHighlight.tsx@@ -1,11 +1,22 @@import dayjs from 'dayjs'import { useState } from 'react'+type Coordinate = string | number++const toDayjs = (value?: string) => {+ const asNumber = Number(value)+ return value !== undefined && value !== '' && Number.isFinite(asNumber)+ ? dayjs(asNumber)+ : dayjs(value)+}+type ChartHighlightMouseEvent = {activeLabel?: string- coordinates?: string+ coordinates?: CoordinatechartX?: numberchartY?: number+ nextLabel?: string+ nextCoordinate?: Coordinate}type Pixel = { x: number; y: number }@@ -13,7 +24,7 @@ type Pixel = { x: number; y: number }export interface ChartHighlight {left: string | undefinedright: string | undefined- coordinates: { left?: string; right?: string }+ coordinates: { left?: Coordinate; right?: Coordinate }isSelecting: booleanpopoverPosition: { x: number; y: number } | nullhandleMouseDown: (e: ChartHighlightMouseEvent) => void@@ -25,7 +36,7 @@ export interface ChartHighlight {export function useChartHighlight(): ChartHighlight {const [left, setLeft] = useState<string | undefined>(undefined)const [right, setRight] = useState<string | undefined>(undefined)- const [coordinates, setCoordinates] = useState<{ left?: string; right?: string }>({+ const [coordinates, setCoordinates] = useState<{ left?: Coordinate; right?: Coordinate }>({left: undefined,right: undefined,})@@ -33,14 +44,18 @@ export function useChartHighlight(): ChartHighlight {const [popoverPosition, setPopoverPosition] = useState<{ x: number; y: number } | null>(null)const [initialPoint, setInitialPoint] = useState<string | undefined>(undefined)const [anchorPixel, setAnchorPixel] = useState<Pixel | undefined>(undefined)+ const [hasDragged, setHasDragged] = useState(false)+ const [nextPoint, setNextPoint] = useState<{ label?: string; coordinate?: Coordinate }>({})const handleMouseDown = (e: ChartHighlightMouseEvent) => {clearHighlight()if (!e || !e.activeLabel) returnsetIsSelecting(true)+ setHasDragged(false)setLeft(e.activeLabel)setRight(e.activeLabel)setInitialPoint(e.activeLabel)+ setNextPoint({ label: e.nextLabel, coordinate: e.nextCoordinate })setCoordinates({ left: e.coordinates, right: e.coordinates })if (typeof e.chartX === 'number' && typeof e.chartY === 'number') {setAnchorPixel({ x: e.chartX, y: e.chartY })@@ -50,8 +65,10 @@ export function useChartHighlight(): ChartHighlight {const handleMouseMove = (e: ChartHighlightMouseEvent) => {if (!isSelecting || !e || !e.activeLabel) return- const currentTimestamp = dayjs(e.activeLabel)- const initialTimestamp = dayjs(initialPoint)+ if (e.activeLabel !== initialPoint) setHasDragged(true)++ const currentTimestamp = toDayjs(e.activeLabel)+ const initialTimestamp = toDayjs(initialPoint)if (currentTimestamp.isBefore(initialTimestamp)) {// If dragging left, update left and keep right as initial@@ -77,6 +94,12 @@ export function useChartHighlight(): ChartHighlight {setIsSelecting(false)setInitialPoint(undefined)+ const isClick = !hasDragged+ if (isClick && nextPoint.label) {+ setRight(nextPoint.label)+ setCoordinates((prev) => ({ ...prev, right: nextPoint.coordinate }))+ }+// Anchor the popover to where the selection started rather than wherever// the mouse happened to be released.if (anchorPixel) {@@ -100,6 +123,8 @@ export function useChartHighlight(): ChartHighlight {setPopoverPosition(null)setInitialPoint(undefined)setAnchorPixel(undefined)+ setHasDragged(false)+ setNextPoint({})}return {