Files
supabase/apps/studio/components/ui/ProjectSettings/DisplayApiSettings.utils.ts
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

64 lines
1.8 KiB
TypeScript

import dayjs from 'dayjs'
import { useRef } from 'react'
import { useLogsQuery } from '@/hooks/analytics/useLogsQuery'
export function useLastUsedAPIKeysLogQuery({
projectRef,
enabled,
}: {
projectRef: string
enabled?: boolean
}) {
const now = useRef(new Date()).current
return useLogsQuery({
projectRef,
enabled,
initialParams: {
iso_timestamp_start: new Date(now.getTime() - 24 * 60 * 60 * 1000).toISOString(),
iso_timestamp_end: now.toISOString(),
sql: "-- last-used-anon--service_role-api-keys\nSELECT unix_millis(max(timestamp)) as timestamp, payload.role, payload.signature_prefix FROM edge_logs cross join unnest(metadata) as m cross join unnest(m.request) as request cross join unnest(request.sb) as sb cross join unnest(sb.jwt) as jwt cross join unnest(jwt.apikey) as apikey cross join unnest(apikey.payload) as payload WHERE apikey.invalid is null and payload.issuer = 'supabase' and payload.algorithm = 'HS256' and payload.role in ('anon', 'service_role') GROUP BY payload.role, payload.signature_prefix",
},
})
}
export function getLastUsedAPIKeys(
apiKeys: {
tags: string
api_key: string
}[],
logData:
| {
timestamp: number
role?: 'anon' | 'service_role' | string
signature_prefix?: string
}[]
| null
) {
if (apiKeys.length < 1 || !logData || logData.length < 1) {
return {}
}
const now = dayjs()
return apiKeys.reduce(
(a, i) => {
const entry = logData?.find(
({ role, signature_prefix }) =>
role &&
signature_prefix &&
i.tags.indexOf(role) >= 0 &&
i.api_key.split('.')[2]?.startsWith(signature_prefix)
)?.timestamp
if (entry) {
a[i.api_key] = dayjs.duration(now.diff(dayjs(entry))).humanize(false)
}
return a
},
{} as { [apikey: string]: string }
)
}