Files
supabase/apps/studio/components/interfaces/Integrations/Integration/ConnectedResourceGroupSection.tsx
Charis 121de3dff7 feat(studio): gate api key create and delete on endpoint grants (#52966)
Part 6 of 6 in a stack for
[FE-4731](https://linear.app/supabase/issue/FE-4731/grants-rollout-api-keys-consumers-that-only-need-publishable-keys).
Based on #52965.

## Stack
1. #52961 Docs and project API docs consumers
2. #52962 Edge functions and vector bucket examples
3. #52963 Integrations landing data
4. #52964 Connect sheet and project connection popover; remove the
transitional api-keys hooks
5. #52965 `reveal` refinement for single api-key reads; API key pill
6. 👉 #52966 API key create and delete

## Changes
Gates the api-key write endpoints (both require
`api_gateway_keys_write`) on grants instead of `unguarded.migrating`:
- `api-key-create-mutation` and `api-key-delete-mutation` take the grant
in their variables.
- `APIKeyDeleteDialog` uses the `DELETE` grant instead of its V2 check,
and is disabled with a tooltip when denied or errored. `SecretAPIKeys`
and `PublishableAPIKeys` pass the grant to the mutation.
- `CreateNewAPIKeysButton` and the create publishable and secret key
dialogs mint the `POST` grant. Their triggers and submit buttons are
disabled with a tooltip unless granted.
- `useConnectedResourceMutations`: the api-key delete call takes the
grant. Without it, the mutation shows an error toast and throws.

**New guard:** creating API keys had no UI guard. Users without
`api_gateway_keys_write` could open the dialogs and get a 403; the
controls are now disabled for them. The delete guard already matched the
annotation.

Out of scope: the legacy API key toggle and status (FE-4732).

## Testing
- `CreateAPIKeyDialogs.platform.test.tsx` (renamed from
`CreateAPIKeyDialogs.test.tsx`, the mutation mock removed): denied and
permission-check error disable the triggers and the submit button.
- `APIKeyDeleteDialog.platform.test.tsx`: granted, denied, and
permission-check error.
- `data/api-keys/api-key-write-mutations.test.ts`: create and delete
send their requests with a grant.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-11-10 14:01:40 +00:00

110 lines
3.8 KiB
TypeScript

import { SettingsIcon, Trash2Icon, TriangleAlertIcon } from 'lucide-react'
import Link from 'next/link'
import { Badge, Button } from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
import { type ResourceGroup } from './MarketplaceIntegrationSettingsTab.types'
import { type ConnectedResource } from '@/components/interfaces/Integrations/Landing/Landing.utils'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
export const ResourceGroupSection = ({
group,
onRemove,
getRemoveDisabledReason,
}: {
group: ResourceGroup
onRemove: (resource: ConnectedResource) => void
/** Returns why a resource can't be removed, which disables its Remove button. */
getRemoveDisabledReason?: (resource: ConnectedResource) => string | undefined
}) => {
return (
<section className="flex flex-col gap-y-4 border-b py-8 first:pt-0 last:border-b-0">
<div className="flex flex-col gap-y-2">
<div className="flex items-center gap-x-2">
<h3 className="text-base text-foreground">{group.title}</h3>
{group.missing ? (
<Badge variant="warning" className="gap-x-1.5">
<TriangleAlertIcon size={12} strokeWidth={1.5} />
Not connected
</Badge>
) : (
group.badge && (
<Badge variant="success" className="gap-x-1.5">
<span className="h-1.5 w-1.5 rounded-full bg-primary-bright" />
{group.badge}
</Badge>
)
)}
</div>
<p className="text-sm text-foreground-light max-w-2xl">{group.description}</p>
</div>
<Admonition
type={group.missing ? 'warning' : 'default'}
className="m-0 max-w-2xl"
title={group.missing ? 'This resource is missing' : undefined}
>
{group.missing ? group.missingNote : group.note}
</Admonition>
{group.missing ? (
group.manageAction && (
<div className="max-w-2xl">
<Button asChild icon={<SettingsIcon />}>
<Link href={group.manageAction.href}>{group.manageAction.label}</Link>
</Button>
</div>
)
) : (
<div className="max-w-2xl divide-y rounded-md border bg-surface-100">
{group.items.map((item) => (
<div
key={item.resource.key}
className="flex items-center justify-between gap-x-4 px-4 py-3"
>
<div className="flex min-w-0 flex-col">
<code
data-code-style="plain"
className="truncate font-mono text-sm text-foreground"
title={item.identifier}
>
{item.identifier}
</code>
{item.meta && <span className="text-xs text-foreground-lighter">{item.meta}</span>}
</div>
<div className="flex shrink-0 items-center gap-x-2">
{group.manageAction && (
<Button asChild icon={<SettingsIcon />}>
<Link href={group.manageAction.href}>{group.manageAction.label}</Link>
</Button>
)}
<RemoveResourceButton
disabledReason={getRemoveDisabledReason?.(item.resource)}
onClick={() => onRemove(item.resource)}
/>
</div>
</div>
))}
</div>
)}
</section>
)
}
const RemoveResourceButton = ({
disabledReason,
onClick,
}: {
disabledReason?: string
onClick: () => void
}) => (
<ButtonTooltip
icon={<Trash2Icon className="text-foreground-light" />}
onClick={onClick}
disabled={disabledReason !== undefined}
tooltip={{ content: { side: 'bottom', text: disabledReason } }}
>
Remove
</ButtonTooltip>
)