import { useQuery } from '@tanstack/react-query' import { IS_PLATFORM, useParams } from 'common' import { parseAsString, useQueryState } from 'nuqs' import { useEffect, useMemo } from 'react' import { toast } from 'sonner' import { Card, Table, TableBody, TableHead, TableHeader, TableRow } from 'ui' import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader' import { APIKeyRow } from './APIKeyRow' import { CreateSecretAPIKeyDialog } from './CreateSecretAPIKeyDialog' import { AlertError } from '@/components/ui/AlertError' import { FormHeader } from '@/components/ui/Forms/FormHeader' import { NoPermission } from '@/components/ui/NoPermission' import { useAPIKeyDeleteMutation } from '@/data/api-keys/api-key-delete-mutation' import { apiKeysQueryOptions, type APIKeysData } from '@/data/api-keys/api-keys-query' import { getWriteGrantToastMessage, useGrant } from '@/lib/grants' import { useTrack } from '@/lib/telemetry/track' export const SecretAPIKeys = () => { const { ref: projectRef } = useParams() const track = useTrack() // Lists keys without their secret values (revealing happens per key), so a plain grant is enough const apiKeysGrant = useGrant('GET /v1/projects/{ref}/api-keys', { projectRef }) const deleteGrant = useGrant('DELETE /v1/projects/{ref}/api-keys/{id}', { projectRef }) const { data: apiKeysData, error, isSuccess: isSuccessApiKeys, isPending: isLoadingApiKeys, isError: isErrorApiKeys, } = useQuery(apiKeysQueryOptions({ projectRef, grant: apiKeysGrant.grant })) const isLoadingGrant = apiKeysGrant.status === 'loading' const secretApiKeys = useMemo( () => apiKeysData?.filter( (key): key is Extract => key.type === 'secret' ) ?? [], [apiKeysData] ) const empty = secretApiKeys?.length === 0 && !isLoadingApiKeys const [deleteId, setDeleteId] = useQueryState('deleteSecretKey', parseAsString) const apiKeyToDelete = secretApiKeys?.find((key) => key.id === deleteId) const { mutate: deleteAPIKey, isPending: isDeletingAPIKey, isSuccess: isDeleteSuccess, } = useAPIKeyDeleteMutation({ onSuccess: (_data, variables) => { // Attribute to the project the key was deleted from, not whichever project the route is on now track('api_key_removed', { keyType: 'secret' }, { project: variables.projectRef }) toast.success('Successfully deleted secret key') setDeleteId(null) }, }) const onDeleteAPIKey = (apiKey: Extract) => { if (!projectRef) return console.error('Project ref is required') if (!apiKey.id) return console.error('API key ID is required') if (!deleteGrant.grant) { toast.error(getWriteGrantToastMessage(deleteGrant.status, 'delete', 'API keys')) return } deleteAPIKey({ projectRef, id: apiKey.id, grant: deleteGrant.grant }) } useEffect(() => { if (isSuccessApiKeys && !!deleteId && !apiKeyToDelete && !isDeleteSuccess) { toast('Unable to find secret key') setDeleteId(null) } }, [apiKeyToDelete, deleteId, isDeleteSuccess, isSuccessApiKeys, setDeleteId]) return (
: null} /> {apiKeysGrant.status === 'denied' ? ( ) : apiKeysGrant.status === 'error' ? ( ) : isLoadingGrant || isLoadingApiKeys ? ( ) : isErrorApiKeys ? ( ) : empty ? (

No secret API keys found

Your project is not accessible via secret keys—there are no active secret keys created.

) : ( Name API Key {IS_PLATFORM && } {secretApiKeys.map((apiKey) => ( onDeleteAPIKey(apiKey)} setKeyToDelete={setDeleteId} isDeleteModalOpen={apiKeyToDelete?.id === apiKey.id} /> ))}
)}
) }