Files
supabase/apps/studio/components/interfaces/QueryInsights/QueryInsights.tsx
Joshen Lim 944c5862f3 Chore/small refactors (#47740)
## Context

Just extracting the fixes which I think are applicable from this
[PR](https://github.com/supabase/supabase/pull/47695)

Main files are
- `apps/studio/hooks/analytics/useLogsQuery.tsx`
- `packages/common/auth.tsx`
- `packages/common/feature-flags.tsx`

## Changes involved
- Adjust `useLogsQuery` to accept an object as prop, rather than 4
individual params
- This one doesn't address any Sentry issues, but is just a improvement
to the function's API imo, more readable
- Adjust how user email is retrieved in `feature-flags`
- Related Sentry issue
[here](https://supabase.sentry.io/issues/7592718607/?project=5459134)
- The error is a bit vague, but Claude's attempt to fix looks alright in
general IMO
  - Minimally verified that feature flags are loading as expected still

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Bug Fixes**
* Improved log-related screens and queries for more reliable loading and
filtering across the app.
* Fixed profile and account data handling so identity details are
retrieved more consistently.
* Improved authentication handling to better recognize missing user data
and keep the app stable.
* Updated feature flag personalization to use more accurate account
information.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-07-08 23:21:48 +08:00

99 lines
3.1 KiB
TypeScript

import { useParams } from 'common'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import { useMemo, useState } from 'react'
import { useSupamonitorIndexAdvisor } from './hooks/useSupamonitorIndexAdvisor'
import { getSupamonitorLogsQuery } from './QueryInsights.constants'
import { QueryInsightsChart } from './QueryInsightsChart/QueryInsightsChart'
import { QueryInsightsHealth } from './QueryInsightsHealth/QueryInsightsHealth'
import { QueryInsightsTable } from './QueryInsightsTable/QueryInsightsTable'
import {
aggregateLogsByQuery,
filterSystemLogs,
parseSupamonitorLogs,
transformLogsToChartData,
} from './utils/supamonitor.utils'
import { useLogsQuery } from '@/hooks/analytics/useLogsQuery'
dayjs.extend(utc)
interface QueryInsightsProps {
dateRange?: {
period_start: { date: string; time_period: string }
period_end: { date: string; time_period: string }
interval: string
}
}
export const QueryInsights = ({ dateRange }: QueryInsightsProps) => {
const { ref } = useParams()
const effectiveDateRange = useMemo(() => {
if (dateRange) {
return {
iso_timestamp_start: dateRange.period_start.date,
iso_timestamp_end: dateRange.period_end.date,
}
}
const end = dayjs.utc()
const start = end.subtract(1, 'hour')
return {
iso_timestamp_start: start.toISOString(),
iso_timestamp_end: end.toISOString(),
}
}, [dateRange])
const sql = useMemo(
() =>
getSupamonitorLogsQuery(
effectiveDateRange.iso_timestamp_start,
effectiveDateRange.iso_timestamp_end
),
[effectiveDateRange]
)
const { logData, isLoading } = useLogsQuery({
projectRef: ref as string,
initialParams: {
sql,
iso_timestamp_start: effectiveDateRange.iso_timestamp_start,
iso_timestamp_end: effectiveDateRange.iso_timestamp_end,
},
})
const [selectedQuery, setSelectedQuery] = useState<string | null>(null)
const parsedLogs = useMemo(() => parseSupamonitorLogs(logData || []), [logData])
const filteredLogs = useMemo(() => filterSystemLogs(parsedLogs), [parsedLogs])
const chartData = useMemo(() => transformLogsToChartData(filteredLogs), [filteredLogs])
const selectedChartData = useMemo(
() =>
selectedQuery
? transformLogsToChartData(
filteredLogs.filter((log) => log.query?.replace(/\s+/g, ' ').trim() === selectedQuery)
)
: undefined,
[filteredLogs, selectedQuery]
)
const aggregatedData = useMemo(() => aggregateLogsByQuery(filteredLogs), [filteredLogs])
const enrichedData = useSupamonitorIndexAdvisor(aggregatedData)
return (
<div className="flex flex-col flex-1 min-h-0">
<QueryInsightsHealth data={enrichedData} isLoading={isLoading} />
<QueryInsightsChart
chartData={chartData}
selectedChartData={selectedChartData}
isLoading={isLoading}
/>
<QueryInsightsTable
data={enrichedData}
isLoading={isLoading}
currentSelectedQuery={selectedQuery}
onCurrentSelectQuery={setSelectedQuery}
/>
</div>
)
}