Files
supabase/apps/studio/components/interfaces/APIKeys/CreatePublishableAPIKeyDialog.tsx
T
Drew McLellan ed1a436a4b chore(studio): remove unused data-attr from API keys pages (#54137)
## 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.
2026-11-26 15:36:29 +00:00

199 lines
6.2 KiB
TypeScript

import { zodResolver } from '@hookform/resolvers/zod'
import { useParams } from 'common'
import { PlusIcon } from 'lucide-react'
import { parseAsString, useQueryState } from 'nuqs'
import { useRef } from 'react'
import { SubmitHandler, useForm } from 'react-hook-form'
import { toast } from 'sonner'
import {
Button,
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogSection,
DialogSectionSeparator,
DialogTitle,
Form,
FormControl,
FormField,
Input,
} from 'ui'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import * as z from 'zod'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { Shortcut } from '@/components/ui/Shortcut'
import { useAPIKeyCreateMutation } from '@/data/api-keys/api-key-create-mutation'
import { getWriteGrantToastMessage, getWriteGrantTooltip, useGrant } from '@/lib/grants'
import { useTrack } from '@/lib/telemetry/track'
import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
const FORM_ID = 'create-publishable-api-key'
const SCHEMA = z.object({
name: z.string(),
description: z.string().trim(),
})
export interface CreatePublishableAPIKeyDialogProps {
projectRef: string
}
export const CreatePublishableAPIKeyDialog = () => {
const { ref: projectRef } = useParams()
const formRef = useRef<HTMLFormElement>(null)
const [visible, setVisible] = useQueryState('new', parseAsString.withDefault(''))
const onOpenChange = (value: boolean) => {
if (value) setVisible('publishable')
else setVisible('')
}
const openDialog = () => {
if (canCreateAPIKeys) setVisible('publishable')
}
const defaultValues = { name: '', description: '' }
const form = useForm<z.infer<typeof SCHEMA>>({
resolver: zodResolver(SCHEMA),
defaultValues: {
name: '',
description: '',
},
})
const track = useTrack()
const createGrant = useGrant('POST /v1/projects/{ref}/api-keys', { projectRef })
const canCreateAPIKeys = createGrant.status === 'granted'
const { mutate: createAPIKey, isPending: isCreatingAPIKey } = useAPIKeyCreateMutation({
// Hook-level so the event still fires if the dialog unmounts before the request finishes
onSuccess: (_data, variables) =>
track(
'api_key_created',
{ keyType: 'publishable', source: 'create_dialog' },
{ project: variables.projectRef }
),
})
const onSubmit: SubmitHandler<z.infer<typeof SCHEMA>> = async (values) => {
if (!createGrant.grant) {
toast.error(getWriteGrantToastMessage(createGrant.status, 'create', 'API keys'))
return
}
createAPIKey(
{
projectRef,
grant: createGrant.grant,
type: 'publishable',
name: values.name,
description: values.description,
},
{
onSuccess: () => {
form.reset(defaultValues)
onOpenChange(false)
},
}
)
}
return (
<Dialog open={visible === 'publishable'} onOpenChange={onOpenChange}>
<Shortcut
id={SHORTCUT_IDS.API_KEYS_NEW_PUBLISHABLE}
onTrigger={openDialog}
side="bottom"
// Hide the shortcut hint while blocked so the permission tooltip shows instead
tooltipOpen={visible === 'publishable' || !canCreateAPIKeys ? false : undefined}
>
<ButtonTooltip
icon={<PlusIcon />}
onClick={openDialog}
disabled={!canCreateAPIKeys}
tooltip={{
content: {
side: 'bottom',
text: getWriteGrantTooltip(createGrant.status, 'create', 'API keys'),
},
}}
>
New publishable key
</ButtonTooltip>
</Shortcut>
<DialogContent>
<DialogHeader>
<DialogTitle>Create new publishable API key</DialogTitle>
<DialogDescription>
Publishable API keys are used to authorize requests to your project from the web, mobile
or desktop apps, CLIs or other public components of your application. They are safe to
be published online and embedded in code.
</DialogDescription>
</DialogHeader>
<DialogSectionSeparator />
<DialogSection className="flex flex-col gap-4">
<Form {...form}>
<form
ref={formRef}
className="flex flex-col gap-4"
id={FORM_ID}
onSubmit={form.handleSubmit(onSubmit)}
>
<FormField
key="name"
name="name"
control={form.control}
render={({ field }) => (
<FormItemLayout
label="Name"
description="A short name of lowercase alphanumeric characters and underscore, must start with letter or underscore."
>
<FormControl>
<Input {...field} />
</FormControl>
</FormItemLayout>
)}
/>
<FormField
key="description"
name="description"
control={form.control}
render={({ field }) => (
<FormItemLayout
label="Description"
description="Provide a description about what this key is used for."
>
<FormControl>
<Input {...field} placeholder="(Optional)" />
</FormControl>
</FormItemLayout>
)}
/>
</form>
</Form>
</DialogSection>
<DialogFooter>
<Shortcut
id={SHORTCUT_IDS.API_KEYS_CREATE_PUBLISHABLE}
onTrigger={() => formRef.current?.requestSubmit()}
options={{ enabled: visible === 'publishable' && !isCreatingAPIKey }}
side="top"
>
<Button
variant="primary"
form={FORM_ID}
type="submit"
loading={isCreatingAPIKey}
disabled={!canCreateAPIKeys}
>
Create Publishable API key
</Button>
</Shortcut>
</DialogFooter>
</DialogContent>
</Dialog>
)
}