mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## Summary Follow-up to #54070. Removes the `data-attr` attributes added there, as raised in review: PostHog autocapture is off (`autocapture: false` in `packages/common/posthog-client.ts`), so they send nothing. Copy, reveal, create and remove are all tracked with explicit `track()` calls. Removes `data-attr` from the API key copy and reveal buttons, the delete menu item, both create dialogs, and the initial-setup "Create keys" action. The two docs links and the `DocsButton` `data-attr` prop are removed in #54119 instead, since that PR also edits `DocsButton`. The two PRs touch different files and can merge in either order. No behavior or tracking change. ## Testing - `tsc`, the lint ratchet and the Studio unit tests pass.
129 lines
4.1 KiB
TypeScript
129 lines
4.1 KiB
TypeScript
import { useParams } from 'common'
|
|
import { useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import {
|
|
AlertDialog,
|
|
AlertDialogAction,
|
|
AlertDialogBody,
|
|
AlertDialogCancel,
|
|
AlertDialogContent,
|
|
AlertDialogDescription,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle,
|
|
} from 'ui'
|
|
import { Admonition } from 'ui-patterns/Admonition'
|
|
|
|
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
|
|
import { useAPIKeyCreateMutation } from '@/data/api-keys/api-key-create-mutation'
|
|
import { getWriteGrantToastMessage, getWriteGrantTooltip, useGrant } from '@/lib/grants'
|
|
import { useTrack } from '@/lib/telemetry/track'
|
|
|
|
export const CreateNewAPIKeysButton = () => {
|
|
const { ref: projectRef } = useParams()
|
|
const track = useTrack()
|
|
|
|
const [createKeysDialogOpen, setCreateKeysDialogOpen] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
const createGrant = useGrant('POST /v1/projects/{ref}/api-keys', { projectRef })
|
|
|
|
const { mutateAsync: createAPIKey } = useAPIKeyCreateMutation({ onError: () => {} })
|
|
|
|
const handleCreateNewApiKeys = async () => {
|
|
if (!projectRef) return
|
|
if (!createGrant.grant) {
|
|
toast.error(getWriteGrantToastMessage(createGrant.status, 'create', 'API keys'))
|
|
return
|
|
}
|
|
|
|
try {
|
|
setError(null)
|
|
|
|
// Create publishable key
|
|
try {
|
|
await createAPIKey({
|
|
projectRef,
|
|
grant: createGrant.grant,
|
|
type: 'publishable',
|
|
name: 'default',
|
|
})
|
|
track(
|
|
'api_key_created',
|
|
{ keyType: 'publishable', source: 'initial_setup' },
|
|
{ project: projectRef }
|
|
)
|
|
} catch (error: any) {
|
|
setError(`Failed to create the default publishable key: ${error.message}`)
|
|
throw error
|
|
}
|
|
|
|
// Create secret key
|
|
try {
|
|
await createAPIKey({
|
|
projectRef,
|
|
grant: createGrant.grant,
|
|
type: 'secret',
|
|
name: 'default',
|
|
})
|
|
track(
|
|
'api_key_created',
|
|
{ keyType: 'secret', source: 'initial_setup' },
|
|
{ project: projectRef }
|
|
)
|
|
} catch (error: any) {
|
|
setError(
|
|
`The default publishable key was created, but the default secret key failed: ${error.message}`
|
|
)
|
|
throw error
|
|
}
|
|
|
|
setCreateKeysDialogOpen(false)
|
|
toast.success('Successfully created a new set of API keys!')
|
|
} catch (error) {
|
|
console.error('Failed to create API keys:', error)
|
|
throw error
|
|
}
|
|
}
|
|
|
|
return (
|
|
<AlertDialog open={createKeysDialogOpen} onOpenChange={setCreateKeysDialogOpen}>
|
|
<ButtonTooltip
|
|
variant="primary"
|
|
onClick={() => setCreateKeysDialogOpen(true)}
|
|
disabled={createGrant.status !== 'granted'}
|
|
tooltip={{
|
|
content: {
|
|
side: 'bottom',
|
|
text: getWriteGrantTooltip(createGrant.status, 'create', 'API keys'),
|
|
},
|
|
}}
|
|
>
|
|
Create new API keys
|
|
</ButtonTooltip>
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Create new API keys</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
This will create a default publishable key and a default secret key both named{' '}
|
|
<code className="break-keep!">default</code>. These keys are required to connect your
|
|
application to your Supabase project. Your existing legacy API keys (
|
|
<code className="break-keep!">anon</code> and{' '}
|
|
<code className="break-keep!">service_role</code>) are not affected and remain valid
|
|
until you disable them in a separate step.
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
{error && (
|
|
<AlertDialogBody>
|
|
<Admonition type="destructive" title="Unable to create API keys" description={error} />
|
|
</AlertDialogBody>
|
|
)}
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
|
<AlertDialogAction onClick={handleCreateNewApiKeys}>Create keys</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
)
|
|
}
|