Files
supabase/apps/studio/components/interfaces/APIKeys/ApiKeyPill.tsx
Jonathan Summers-Muir 4b0bd73312 chore: jwt keys page table layout (#37097)
* Improve layout and truncation in SigningKeyRow

Added flex and truncation classes to ensure status labels and key IDs are properly truncated and aligned. This enhances the table's appearance and prevents overflow issues for long text.

* Improve icon layout and badge styling in UI components

Added flex-shrink-0 to icons in AlgorithmHoverCard for better alignment. Updated InfoPill to use min-w-0, overflow-hidden, and improved badge and label layout for consistent appearance and handling of long content.

* Remove unused cn import from InfoPill component

The cn utility import was removed from InfoPill.tsx as it is no longer used. The Badge component now uses a direct className string instead.

* Update JWT key table columns and add rotation info

Removed the 'Key ID' column and added a 'Last rotated at' column to the JWT secret keys table. The signing key row now displays the relative time since the key was last updated for previously used keys.

* Add Key ID column to JWT secret keys table

Introduces a new 'Key ID' column to the JWTSecretKeysTable component for improved visibility and management of JWT secret keys.

* Improve JWT key table UI and add tooltip to key ID

Updated the JWT secret keys table to enhance the empty state with an icon and explanatory text, and adjusted column alignment and visibility for 'Last rotated at'. Added a tooltip to the key ID for better accessibility and ensured the 'Last rotated at' column displays for both previously used and revoked keys.

* Improve API key table layout and responsiveness

API key name and description are now grouped together, with description shown under the name. The last seen column is hidden on smaller screens and displays 'Never used' when appropriate. ApiKeyPill max width is now responsive to screen size. The description column was removed from SecretAPIKeys to streamline the table.

* Adjust API key pill and input sizing for responsiveness

Reduced max-widths for ApiKeyPill and updated PublishableAPIKeys layout to improve responsiveness. ApiKeyInput now uses dynamic min/max widths for better display across breakpoints.

* Adjust lg breakpoint min-width for API key input

Changed the lg:min-w value from 40rem to 24rem for the API key input field to improve layout responsiveness at large screen sizes.

* Update PublishableAPIKeys.tsx

* Remove unused file

* Minor refactors

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-07-14 19:40:17 +08:00

202 lines
6.6 KiB
TypeScript

import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useQueryClient } from '@tanstack/react-query'
import { AnimatePresence, motion } from 'framer-motion'
import { Eye } from 'lucide-react'
import { useEffect, useState } from 'react'
import { toast } from 'sonner'
import { InputVariants } from '@ui/components/shadcn/ui/input'
import { useParams } from 'common'
import CopyButton from 'components/ui/CopyButton'
import { useAPIKeyIdQuery } from 'data/api-keys/[id]/api-key-id-query'
import { APIKeysData } from 'data/api-keys/api-keys-query'
import { apiKeysKeys } from 'data/api-keys/keys'
import { useAsyncCheckProjectPermissions } from 'hooks/misc/useCheckPermissions'
import { Button, cn, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
export function ApiKeyPill({
apiKey,
}: {
apiKey: Extract<APIKeysData[number], { type: 'secret' | 'publishable' }>
}) {
const queryClient = useQueryClient()
const { ref: projectRef } = useParams()
// State that controls whether to show the full API key
const [show, setShowState] = useState(false)
const isSecret = apiKey.type === 'secret'
// Permission check for revealing/copying secret API keys
const { can: canManageSecretKeys, isLoading: isLoadingPermission } =
useAsyncCheckProjectPermissions(PermissionAction.READ, 'service_api_keys')
// This query only runs when show=true (enabled: show)
// It fetches the fully revealed API key when needed
const {
data,
error,
refetch: refetchApiKey,
} = useAPIKeyIdQuery(
{
projectRef,
id: apiKey.id as string,
reveal: true, // Request the unmasked key
},
{
enabled: show, // Only run query when show is true
staleTime: 0, // Always consider data stale
cacheTime: 0, // Don't cache the key data
}
)
// Auto-hide timer for the API key (security feature)
useEffect(() => {
if (show && data?.api_key) {
// Auto-hide the key after 10 seconds
const timer = setTimeout(() => {
setShowState(false)
// Clear the cached key from memory
queryClient.removeQueries({
queryKey: apiKeysKeys.single(projectRef, apiKey.id as string),
exact: true,
})
}, 10000) // Hide after 10 seconds
return () => clearTimeout(timer)
}
}, [show, data?.api_key, projectRef, queryClient, apiKey.id])
async function onSubmitShow() {
// Don't reveal key if not allowed or loading
if (isSecret && !canManageSecretKeys) return
if (isLoadingPermission) return
// This will enable the API key query to fetch and reveal the key
setShowState(true)
}
async function onCopy() {
// If key is already revealed, use that value
if (data?.api_key) return data?.api_key ?? ''
try {
// Fetch full key and immediately clear from cache after copying
const result = await refetchApiKey()
queryClient.removeQueries({
queryKey: apiKeysKeys.single(projectRef, apiKey.id as string),
exact: true,
})
if (result.isSuccess) return result.data.api_key ?? ''
if (error) {
toast.error('Failed to copy secret API key')
return ''
}
} catch (error) {
console.error('Failed to fetch API key:', error)
return ''
}
// Fallback to the masked version if fetch fails
return apiKey.api_key
}
// States for disabling buttons/showing tooltips
const isRestricted = isSecret && !canManageSecretKeys
return (
<>
<AnimatePresence mode="wait" initial={false}>
<div
className={cn(
InputVariants({ size: 'tiny' }),
'flex-1 grow gap-0 font-mono rounded-full',
isSecret ? 'overflow-hidden' : '',
show ? 'ring-1 ring-foreground-lighter ring-opacity-50' : 'ring-0 ring-opacity-0',
'transition-all',
'max-w-[100px] sm:max-w-[140px] md:max-w-[180px] lg:max-w-[340px]',
'cursor-text',
'relative'
)}
style={{ userSelect: 'all' }}
>
{isSecret ? (
<>
<span>{apiKey?.api_key.slice(0, 15)}</span>
<motion.span
key={show ? 'shown' : 'hidden'}
initial={{ opacity: 0, y: show ? 16 : -16 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: show ? 16 : -16 }}
transition={{
duration: 0.1,
y: { type: 'spring', stiffness: 1000, damping: 55 },
}}
>
{show && data?.api_key ? data?.api_key.slice(15) : '••••••••••••••••'}
</motion.span>
</>
) : (
<span>{apiKey?.api_key}</span>
)}
</div>
</AnimatePresence>
{/* Reveal button - only shown for secret keys and when not already revealed */}
{isSecret && (
<AnimatePresence initial={false}>
{!show && (
<motion.div
initial={{ opacity: 0, scale: 1, width: 0 }}
animate={{ opacity: 1, scale: 1, width: 'auto' }}
exit={{ opacity: 0, scale: 1, width: 0 }}
transition={{ duration: 0.12 }}
style={{ overflow: 'hidden' }}
>
<Tooltip>
<TooltipTrigger asChild>
<Button
type="outline"
className="rounded-full px-2 pointer-events-auto cursor-default"
icon={<Eye strokeWidth={2} />}
onClick={onSubmitShow}
disabled={isRestricted}
/>
</TooltipTrigger>
<TooltipContent side="bottom">
{isRestricted
? 'You need additional permissions to reveal secret API keys'
: isLoadingPermission
? 'Loading permissions...'
: 'Reveal API key'}
</TooltipContent>
</Tooltip>
</motion.div>
)}
</AnimatePresence>
)}
<Tooltip>
<TooltipTrigger asChild>
<CopyButton
type="default"
asyncText={onCopy}
iconOnly
className="rounded-full px-2 pointer-events-auto cursor-default"
disabled={isRestricted || isLoadingPermission}
/>
</TooltipTrigger>
<TooltipContent side="bottom">
{isRestricted
? 'You need additional permissions to copy secret API keys'
: isLoadingPermission
? 'Loading permissions...'
: 'Copy API key'}
</TooltipContent>
</Tooltip>
</>
)
}