import { useQuery } from '@tanstack/react-query' import { useParams } from 'common' import { CopyIcon } from 'lucide-react' import { useEffect, useState } from 'react' import { Button, copyToClipboard } from 'ui' import { Input } from 'ui-patterns/DataInputs/Input' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import ContentSnippet from '../ContentSnippet' import { DOCS_CONTENT } from '../ProjectAPIDocs.constants' import type { ContentProps } from './Content.types' import { apiKeysQueryOptions, getKeys } from '@/data/api-keys/api-keys-query' import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query' import { useGrant } from '@/lib/grants' import { useTrack } from '@/lib/telemetry/track' export const Introduction = ({ showKeys, language, apikey, endpoint }: ContentProps) => { const { ref } = useParams() const apiKeysGrant = useGrant('GET /v1/projects/{ref}/api-keys', { projectRef: ref }) const { data: apiKeysData } = useQuery( apiKeysQueryOptions({ projectRef: ref, grant: apiKeysGrant.grant }, { select: getKeys }) ) useProjectSettingsV2Query({ projectRef: ref }) const { anonKey, serviceKey } = apiKeysData ?? {} const track = useTrack() const [copied, setCopied] = useState<'anon' | 'service'>() useEffect(() => { if (copied !== undefined) setTimeout(() => setCopied(undefined), 2000) }, [copied]) const anonApiKey = anonKey?.api_key const serviceApiKey = serviceKey?.api_key ?? 'SUPABASE_CLIENT_SERVICE_KEY' return ( <> } onClick={() => { setCopied('anon') copyToClipboard(anonApiKey ?? 'SUPABASE_CLIENT_ANON_KEY') track('api_docs_code_copy_button_clicked', { title: 'Client API key', selectedLanguage: language, }) }} > {copied === 'anon' ? 'Copied' : 'Copy'} , ]} /> This key has the ability to bypass Row Level Security.{' '} Never share it publicly. } > } onClick={() => { setCopied('service') copyToClipboard(serviceApiKey) track('api_docs_code_copy_button_clicked', { title: 'Service key', selectedLanguage: language, }) }} > {copied === 'service' ? 'Copied' : 'Copy'} , ]} /> > ) }