Files
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

169 lines
5.0 KiB
TypeScript

import { useParams } from 'common'
import { EyeIcon, EyeOffIcon } from 'lucide-react'
import { useEffect, useState } from 'react'
import { toast } from 'sonner'
import { Button, cn, InputVariants, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
import { useRevealedSecret } from './useRevealedSecret'
import CopyButton from '@/components/ui/CopyButton'
import { APIKeysData } from '@/data/api-keys/api-keys-query'
import { useTrack } from '@/lib/telemetry/track'
export function ApiKeyPill({
apiKey,
}: {
apiKey: Extract<APIKeysData[number], { type: 'secret' | 'publishable' }>
}) {
const { ref: projectRef } = useParams()
const track = useTrack()
const [show, setShow] = useState(false)
const isSecret = apiKey.type === 'secret'
const {
data: revealedKey,
isLoading,
reveal,
clear,
revealGrant,
} = useRevealedSecret({
projectRef,
id: apiKey.id as string,
})
// Publishable keys are shown as-is, so only secret keys depend on the reveal grant
const isLoadingPermission = isSecret && revealGrant.status === 'loading'
const canManageSecretKeys = revealGrant.status === 'granted'
// Auto-hide timer for the API key (security feature)
useEffect(() => {
if (show && revealedKey) {
const timer = setTimeout(() => {
setShow(false)
clear()
}, 10000)
return () => clearTimeout(timer)
}
}, [show, revealedKey, clear])
async function onToggleShow() {
if (isSecret && !canManageSecretKeys) return
if (isLoadingPermission) return
if (show) {
setShow(false)
clear()
} else {
track('api_key_reveal_clicked', { keyType: 'secret' })
setShow(true)
try {
await reveal()
} catch {
toast.error('Failed to reveal secret API key')
setShow(false)
}
}
}
async function onCopy() {
if (!isSecret) return apiKey.api_key
if (revealedKey) return revealedKey
try {
const key = await reveal()
clear()
return key ?? ''
} catch {
toast.error('Failed to copy secret API key')
return ''
}
}
// Fires after the clipboard write succeeds, so failed writes aren't recorded. A failed reveal
// copies an empty string, which isn't a copied key either. Each copy carries its own text, so
// overlapping copies can't affect each other.
function onCopied(copiedText: string) {
if (copiedText) track('api_key_copied', { keyType: apiKey.type })
}
const isRestricted = isSecret && !isLoadingPermission && !canManageSecretKeys
const restrictedReason = (action: 'reveal' | 'copy') =>
revealGrant.status === 'error'
? `Failed to check permissions to ${action} secret API keys`
: `You need additional permissions to ${action} secret API keys`
return (
<>
<div
className={cn(
InputVariants({ size: 'tiny' }),
'w-[100px] sm:w-[140px] md:w-[180px] lg:w-[340px] gap-0 font-mono rounded-full',
isSecret ? 'overflow-hidden' : '',
show ? 'ring-1 ring-foreground-lighter/50' : 'ring-0 ring-foreground-lighter/0',
'transition-all cursor-text relative'
)}
style={{ userSelect: 'all' }}
>
{isSecret ? (
<>
<span>{apiKey?.api_key.slice(0, 15)}</span>
<span>{show && revealedKey ? revealedKey.slice(15) : '••••••••••••••••'}</span>
</>
) : (
<span title={apiKey.api_key} className="truncate">
{apiKey.api_key}
</span>
)}
</div>
{/* Toggle button */}
{isSecret && (
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="outline"
aria-label={show ? 'Hide API key' : 'Reveal API key'}
className="rounded-full px-2 pointer-events-auto"
loading={show && isLoading}
icon={show ? <EyeOffIcon strokeWidth={2} /> : <EyeIcon strokeWidth={2} />}
onClick={onToggleShow}
disabled={isRestricted || isLoadingPermission}
/>
</TooltipTrigger>
<TooltipContent side="bottom">
{isRestricted
? restrictedReason('reveal')
: isLoadingPermission
? 'Loading permissions...'
: show
? 'Hide API key'
: 'Reveal API key'}
</TooltipContent>
</Tooltip>
)}
<Tooltip>
<TooltipTrigger asChild>
<CopyButton
variant="default"
aria-label="Copy API key"
asyncText={onCopy}
onCopied={onCopied}
iconOnly
className="rounded-full px-2 pointer-events-auto"
disabled={isRestricted || isLoadingPermission}
/>
</TooltipTrigger>
<TooltipContent side="bottom">
{isRestricted
? restrictedReason('copy')
: isLoadingPermission
? 'Loading permissions...'
: 'Copy API key'}
</TooltipContent>
</Tooltip>
</>
)
}