Files
supabase/apps/studio/routes/project/$ref/observability/realtime.tsx
Maksym Ionutsa 028d329417 Fix/studio observability timezone labels (#54198)
## Problem

Observability date labels ignore the timezone set in the profile. They
always show the browser timezone or UTC. The header range label (for
example "Nov 27, 12:40pm → 12:50pm") uses bare `dayjs()`. The Data API
charts (Total Requests, Error Counts, Response Speed, Network Traffic)
pass `displayDateInUtc`, so the first and last timestamps under each
chart always show UTC.

## Solution

- Format the header range label with `useFormatDateTime()` on the
Database, Auth, Storage, Realtime, PostgREST and Edge Functions
observability pages.
- Remove `displayDateInUtc` from the four Data API charts in
`ApiRenderers.tsx`.
- Use `text-xs` for the date labels in `BarChart`, so they match
`ComposedChart` and `StackedBarChart`. This is a separate commit.

## Review instructions

1. Open Observability, Database, and set a timezone in your profile that
differs from your browser.
2. Check that the range label next to the date picker and the tooltip
show the same timezone.
3. Open Observability, Data API. Check that the labels under each chart
follow the profile timezone and use the same font size as the Database
charts.

<details>
<summary>Screenshots</summary>

<img width="1124" height="844" alt="CleanShot 2026-11-27 at 13 37 14@2x"
src="https://github.com/user-attachments/assets/e3d29c77-317a-4489-aa4c-1fbf4783cb57"
/>


font in Data API
<img width="318" height="72" alt="CleanShot 2026-11-27 at 13 40 19@2x"
src="https://github.com/user-attachments/assets/06e4d12f-245f-4d4e-9dc9-b7f2f7879e52"
/>
font in Database
<img width="342" height="66" alt="CleanShot 2026-11-27 at 13 41 08@2x"
src="https://github.com/user-attachments/assets/de200eb2-4351-471d-a220-bacea142b0d9"
/>


</details>


## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill (not applicable, no docs
changes)
2026-11-27 16:39:47 +01:00

248 lines
8.8 KiB
TypeScript

import { useQueryClient } from '@tanstack/react-query'
import { createFileRoute } from '@tanstack/react-router'
import { useParams } from 'common'
import { ArrowRightIcon, RefreshCwIcon } from 'lucide-react'
import { useEffect, useMemo, useRef, useState } from 'react'
import { Button } from 'ui'
import { OBSERVABILITY_DOCS_HREFS } from '@/components/interfaces/Observability/Observability.constants'
import ReportFilterBar from '@/components/interfaces/Reports/ReportFilterBar'
import { ReportHeader } from '@/components/interfaces/Reports/ReportHeader'
import { ReportPadding } from '@/components/interfaces/Reports/ReportPadding'
import { REPORT_DATERANGE_HELPER_LABELS } from '@/components/interfaces/Reports/Reports.constants'
import ReportStickyNav from '@/components/interfaces/Reports/ReportStickyNav'
import { SharedAPIReport } from '@/components/interfaces/Reports/SharedAPIReport/SharedAPIReport'
import { useSharedAPIReport } from '@/components/interfaces/Reports/SharedAPIReport/SharedAPIReport.constants'
import { ReportChartV2 } from '@/components/interfaces/Reports/v2/ReportChartV2'
import { LogsDatePicker } from '@/components/interfaces/Settings/Logs/Logs.DatePickers'
import UpgradePrompt from '@/components/interfaces/Settings/Logs/UpgradePrompt'
import { DocsButton } from '@/components/ui/DocsButton'
import { ObservabilityLink } from '@/components/ui/ObservabilityLink'
import { ShortcutTooltip } from '@/components/ui/ShortcutTooltip'
import { realtimeReports } from '@/data/reports/v2/realtime.config'
import { useRefreshHandler, useReportDateRange } from '@/hooks/misc/useReportDateRange'
import { useFormatDateTime } from '@/lib/datetime'
import { useDatabaseSelectorStateSnapshot } from '@/state/database-selector'
import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
import { useShortcut } from '@/state/shortcuts/useShortcut'
export const Route = createFileRoute('/project/$ref/observability/realtime')({
component: RealtimeReport,
staticData: {
observabilityLayoutTitle: 'Realtime',
},
})
function RealtimeReport() {
return (
<ReportPadding>
<RealtimeUsage />
</ReportPadding>
)
}
type UpdateDateRange = (from: string, to: string) => void
const REPORT_TITLE = 'Realtime'
const RealtimeUsage = () => {
const [isRefreshing, setIsRefreshing] = useState(false)
const [showDatePicker, setShowDatePicker] = useState(false)
const { db, chart, ref } = useParams()
const {
selectedDateRange,
updateDateRange: updateDateRangeFromHook,
datePickerValue,
datePickerHelpers,
showUpgradePrompt,
setShowUpgradePrompt,
handleDatePickerChange,
} = useReportDateRange(REPORT_DATERANGE_HELPER_LABELS.LAST_60_MINUTES)
const formatDateTime = useFormatDateTime()
const queryClient = useQueryClient()
const {
data,
error,
isLoading,
refetch,
isRefetching,
filters,
addFilter,
removeFilters,
isLoadingData,
sql,
} = useSharedAPIReport({
filterBy: 'realtime',
start: selectedDateRange?.period_start?.date,
end: selectedDateRange?.period_end?.date,
})
const chartSyncId = `realtime-${ref}`
const state = useDatabaseSelectorStateSnapshot()
const reportConfig = useMemo(() => {
return realtimeReports({
projectRef: ref!,
startDate: selectedDateRange?.period_start?.date ?? '',
endDate: selectedDateRange?.period_end?.date ?? '',
interval: selectedDateRange?.interval ?? 'minute',
databaseIdentifier: state.selectedDatabaseId,
})
}, [ref, selectedDateRange, state.selectedDatabaseId])
const onRefreshReport = useRefreshHandler(
datePickerValue,
datePickerHelpers,
handleDatePickerChange,
async () => {
if (!selectedDateRange) return
setIsRefreshing(true)
queryClient.invalidateQueries({
queryKey: ['projects', ref, 'report-v2', { queryGroup: 'realtime' }],
})
refetch()
setTimeout(() => setIsRefreshing(false), 1000)
}
)
useShortcut(SHORTCUT_IDS.OBSERVABILITY_REFRESH, onRefreshReport, {
enabled: !isRefreshing,
})
useShortcut(SHORTCUT_IDS.OBSERVABILITY_TOGGLE_DATE_PICKER, () => {
setShowDatePicker((open) => !open)
})
const urlStateHasSyncedRef = useRef(false)
useEffect(() => {
if (urlStateHasSyncedRef.current) return
urlStateHasSyncedRef.current = true
if (db !== undefined) {
setTimeout(() => {
// [Joshen] Adding a timeout here to support navigation from settings to reports
// Both are rendering different instances of ProjectLayout which is where the
// DatabaseSelectorContextProvider lies in (unless we reckon shifting the provider up one more level is better)
state.setSelectedDatabaseId(db)
}, 100)
}
if (chart !== undefined) {
setTimeout(() => {
const el = document.getElementById(chart)
if (el) el.scrollIntoView({ behavior: 'smooth', block: 'center' })
}, 200)
}
}, [db, chart, state])
const updateDateRange: UpdateDateRange = (from: string, to: string) => {
updateDateRangeFromHook(from, to)
}
return (
<>
<ReportHeader showDatabaseSelector={false} title={REPORT_TITLE} />
<ReportStickyNav
content={
<div className="flex flex-col gap-3 w-full">
<div className="w-full flex items-center gap-2 flex-wrap">
<ShortcutTooltip
shortcutId={SHORTCUT_IDS.OBSERVABILITY_REFRESH}
label="Refresh report"
side="bottom"
>
<Button
disabled={isRefreshing}
icon={<RefreshCwIcon className={isRefreshing ? 'animate-spin' : ''} />}
className="w-7"
onClick={onRefreshReport}
/>
</ShortcutTooltip>
<LogsDatePicker
onSubmit={handleDatePickerChange}
value={datePickerValue}
helpers={datePickerHelpers}
open={showDatePicker}
onOpenChange={setShowDatePicker}
shortcutId={SHORTCUT_IDS.OBSERVABILITY_TOGGLE_DATE_PICKER}
/>
<UpgradePrompt
show={showUpgradePrompt}
setShowUpgradePrompt={setShowUpgradePrompt}
title="Report date range"
description="Report data can be stored for a maximum of 3 months depending on the plan that your project is on."
source="realtimeReportDateRange"
/>
{selectedDateRange && (
<div className="order-last flex w-full flex-wrap items-center gap-x-2 text-xs xl:order-none xl:w-auto">
<p className="text-foreground-light">
{formatDateTime(selectedDateRange.period_start.date, 'MMM D, h:mma')}
</p>
<p className="text-foreground-light">
<ArrowRightIcon size={12} />
</p>
<p className="text-foreground-light">
{formatDateTime(selectedDateRange.period_end.date, 'MMM D, h:mma')}
</p>
</div>
)}
<DocsButton
href={OBSERVABILITY_DOCS_HREFS.realtime}
topic={REPORT_TITLE}
className="ml-auto"
/>
</div>
</div>
}
>
<div className="mt-8 grid @4xl:grid-cols-2 gap-4">
{selectedDateRange &&
reportConfig
.filter((report) => !report.hide)
.map((report) => (
<ReportChartV2
key={`${report.id}`}
queryGroup="realtime"
report={report}
projectRef={ref!}
interval={selectedDateRange.interval}
startDate={selectedDateRange?.period_start?.date}
endDate={selectedDateRange?.period_end?.date}
updateDateRange={updateDateRange}
syncId={chartSyncId}
/>
))}
</div>
<div className="">
<div className="mb-4">
<h5 className="text-foreground mb-2">Realtime API Gateway</h5>
<ReportFilterBar
filters={filters}
onAddFilter={addFilter}
onRemoveFilters={removeFilters}
isLoading={isLoadingData || isRefetching}
hideDatepicker={true}
datepickerHelpers={datePickerHelpers}
selectedProduct={'realtime'}
showDatabaseSelector={false}
/>
</div>
<SharedAPIReport
data={data}
error={error}
isLoading={isLoading}
isRefetching={isRefetching}
hiddenReports={['networkTraffic']}
sql={sql}
/>
</div>
</ReportStickyNav>
<div className="py-8">
<ObservabilityLink />
</div>
</>
)
}