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, TableCell, TableFooter, TableHead, TableHeader, TableRow, } from 'ui' import { Admonition } from 'ui-patterns/Admonition' import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader' import { APIKeyRow } from './APIKeyRow' import { CreatePublishableAPIKeyDialog } from './CreatePublishableAPIKeyDialog' 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 PublishableAPIKeys = () => { const { ref: projectRef } = useParams() const track = useTrack() 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 newApiKeys = useMemo( () => apiKeysData.filter(({ type }) => type === 'publishable' || type === 'secret') ?? [], [apiKeysData] ) const hasApiKeys = newApiKeys.length > 0 const publishableApiKeys = useMemo( () => apiKeysData?.filter( (key): key is Extract => key.type === 'publishable' ) ?? [], [apiKeysData] ) const showSelfHostedEmptyState = !IS_PLATFORM && publishableApiKeys.length === 0 && !isLoadingApiKeys const [deleteId, setDeleteId] = useQueryState('deletePublishableKey', parseAsString) const apiKeyToDelete = publishableApiKeys?.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: 'publishable' }, { project: variables.projectRef }) toast.success('Successfully deleted publishable 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 publishable key') setDeleteId(null) } }, [apiKeyToDelete, deleteId, isDeleteSuccess, isSuccessApiKeys, setDeleteId]) return (
: null} /> {apiKeysGrant.status === 'denied' ? ( ) : apiKeysGrant.status === 'error' ? ( ) : isLoadingGrant || isLoadingApiKeys ? ( ) : isErrorApiKeys ? ( ) : showSelfHostedEmptyState ? (

No publishable API keys found

This may be a configuration issue. Ensure your API keys are available to Studio.

) : ( Name API Key {IS_PLATFORM && } {hasApiKeys && publishableApiKeys.length === 0 && (

No publishable keys created yet

)} {publishableApiKeys.map((apiKey) => ( onDeleteAPIKey(apiKey)} setKeyToDelete={setDeleteId} /> ))}

Publishable keys can be safely shared publicly

)}
) }