Files
supabase/apps/studio/components/ui/ProjectSettings/DisplayApiSettings.utils.ts
Gildas Garcia c6fc456910 chore: cleanup duplicate exports studio (#47387)
## Problem

Knip reports many duplicate exports (both named and default). Besides,
we're moving away from default exports and even have an eslint rule to
enforce it on new code.

## Solution

- Cleanup those exports
- Update imports when necessary

No functional changes. If it builds, it's fine
2026-06-29 15:46:16 +02:00

63 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,
{
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",
},
enabled
)
}
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 }
)
}