import { useQuery } from '@tanstack/react-query' import { useParams } from 'common' import { EyeOffIcon, KeyIcon } from 'lucide-react' import { useMemo } from 'react' import { Button, DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuLabel, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator, DropdownMenuTrigger, ToggleGroup, ToggleGroupItem, } from 'ui' import type { ShowApiKey } from '@/components/interfaces/Docs/Docs.types' import { apiKeysQueryOptions } from '@/data/api-keys/api-keys-query' import { useGrant } from '@/lib/grants' const DEFAULT_KEY = { name: 'hide', key: 'SUPABASE_KEY' } interface LangSelectorProps { selectedLang: 'js' | 'bash' selectedApiKey: ShowApiKey setSelectedLang: (selectedLang: 'js' | 'bash') => void setSelectedApiKey: (key: ShowApiKey) => void } export const LangSelector = ({ selectedLang, selectedApiKey, setSelectedLang, setSelectedApiKey, }: LangSelectorProps) => { const { ref: projectRef } = useParams() // Without access, the key picker is hidden and the commands keep their placeholder key const apiKeysGrant = useGrant('GET /v1/projects/{ref}/api-keys', { projectRef }) const { data: apiKeys = [], isPending: isLoadingAPIKeys } = useQuery( apiKeysQueryOptions({ projectRef, grant: apiKeysGrant.grant }) ) const canReadAPIKeys = apiKeysGrant.status === 'granted' const legacyKeys = useMemo(() => apiKeys.filter(({ type }) => type === 'legacy'), [apiKeys]) const publishableKeys = useMemo( () => apiKeys.filter(({ type }) => type === 'publishable'), [apiKeys] ) const secretKeys = useMemo(() => apiKeys.filter(({ type }) => type === 'secret'), [apiKeys]) return (
{ if (value) setSelectedLang(value as 'js' | 'bash') }} size="sm" className="flex-1 flex" > JavaScript Bash {selectedLang == 'bash' ? ( canReadAPIKeys && !isLoadingAPIKeys && apiKeys && apiKeys.length > 0 && ( setSelectedApiKey(DEFAULT_KEY)} > Hide keys {publishableKeys.length > 0 && ( <> Publishable keys {publishableKeys.map((key) => { const value = key.api_key return ( setSelectedApiKey({ name: `Publishable key: ${key.name}`, key: value, }) } > {key.name} ) })} )} {secretKeys.length > 0 && ( <> Secret keys {secretKeys.map((key) => { const value = key.prefix + '...' return ( setSelectedApiKey({ name: `Secret key: ${key.name}`, key: value }) } > {key.name} ) })} )} {legacyKeys.length > 0 && ( <> JWT-based legacy keys {legacyKeys.map((key) => { const value = key.api_key return ( setSelectedApiKey({ name: `Legacy key: ${key.name}`, key: value }) } > {key.name} ) })} )} ) ) : (
)}
) }