import { useState } from 'react' import { SidePanel, IconBookOpen } from 'ui' import { useStore } from 'hooks' import { useParams } from 'common/hooks' import { GeneralContent, ResourceContent } from '../Docs' import LangSelector from '../Docs/LangSelector' import GeneratingTypes from '../Docs/GeneratingTypes' import ActionBar from './SidePanelEditor/ActionBar' import { useProjectApiQuery } from 'data/config/project-api-query' import { useProjectJsonSchemaQuery } from 'data/docs/project-json-schema-query' import { snakeToCamel } from 'lib/helpers' interface APIDocumentationPanelProps { visible: boolean onClose: () => void } const APIDocumentationPanel = ({ visible, onClose }: APIDocumentationPanelProps) => { const { meta } = useStore() const { ref, page, id } = useParams() const { data: jsonSchema, error: jsonSchemaError, refetch, } = useProjectJsonSchemaQuery({ projectRef: ref }) const { data: settings } = useProjectApiQuery({ projectRef: ref }) const DEFAULT_KEY = { name: 'hide', key: 'SUPABASE_KEY' } const [selectedLang, setSelectedLang] = useState('js') const [showApiKey, setShowApiKey] = useState(DEFAULT_KEY) const tables = meta.tables.list() const autoApiService = { ...settings?.autoApiService, endpoint: `${settings?.autoApiService.protocol ?? 'https'}://${ settings?.autoApiService.endpoint ?? '-' }`, } const apiService = settings?.autoApiService const anonKey = apiService?.service_api_keys.find((x) => x.name === 'anon key') ? apiService.defaultApiKey : undefined const resources = getResourcesFromJsonSchema(jsonSchema) function getResourcesFromJsonSchema(value: any) { const { paths } = value || {} const functionPath = 'rpc/' let resources: any = {} Object.entries(paths || []).forEach(([name, val]) => { let trimmed = name.slice(1) let id = trimmed.replace(functionPath, '') let displayName = id.replace(/_/g, ' ') let camelCase = snakeToCamel(id) let enriched = { id, displayName, camelCase } if (!trimmed.length) return else resources[id] = enriched }) return resources } return ( onClose()} header={ API } customFooter={ onClose()} /> } >
{jsonSchemaError ? (

Error connecting to API

{`${jsonSchemaError}`}

) : ( <> {jsonSchema ? ( <>
{jsonSchema?.definitions && ( table.id === Number(id))?.name} resources={resources} definitions={jsonSchema.definitions} paths={jsonSchema.paths} showApiKey={showApiKey.key} refreshDocs={async () => await refetch()} /> )} ) : (

Building docs ...

)} )}
) } export default APIDocumentationPanel