1import React, { useContext } from 'react'; 2 3import DocumentationPageContext from '~/components/DocumentationPageContext'; 4import { P } from '~/components/base/paragraph'; 5import { GeneratedData } from '~/components/plugins/api/APIDataTypes'; 6import APISectionConstants from '~/components/plugins/api/APISectionConstants'; 7import APISectionEnums from '~/components/plugins/api/APISectionEnums'; 8import APISectionInterfaces from '~/components/plugins/api/APISectionInterfaces'; 9import APISectionMethods from '~/components/plugins/api/APISectionMethods'; 10import APISectionProps from '~/components/plugins/api/APISectionProps'; 11import APISectionTypes from '~/components/plugins/api/APISectionTypes'; 12import { TypeDocKind } from '~/components/plugins/api/APISectionUtils'; 13 14const LATEST_VERSION = `v${require('~/package.json').version}`; 15 16type Props = { 17 packageName: string; 18 apiName?: string; 19}; 20 21const filterDataByKind = ( 22 entries: GeneratedData[], 23 kind: TypeDocKind, 24 additionalCondition: (entry: GeneratedData) => boolean = () => true 25) => 26 entries 27 ? entries.filter((entry: GeneratedData) => entry.kind === kind && additionalCondition(entry)) 28 : []; 29 30const renderAPI = ( 31 packageName: string, 32 version: string = 'unversioned', 33 apiName?: string 34): JSX.Element => { 35 try { 36 const data = require(`~/public/static/data/${version}/${packageName}.json`).children; 37 38 const methods = filterDataByKind( 39 data, 40 TypeDocKind.Function, 41 entry => !entry.name.includes('Listener') 42 ); 43 const eventSubscriptions = filterDataByKind(data, TypeDocKind.Function, entry => 44 entry.name.includes('Listener') 45 ); 46 const types = filterDataByKind( 47 data, 48 TypeDocKind.TypeAlias, 49 entry => 50 !!( 51 entry.type.declaration || 52 entry.type.types || 53 entry.type.type || 54 entry.type.typeArguments 55 ) 56 ); 57 58 const props = filterDataByKind( 59 data, 60 TypeDocKind.TypeAlias, 61 entry => entry.name.includes('Props') && !!entry.type.types 62 ); 63 const defaultProps = filterDataByKind( 64 data 65 .filter((entry: GeneratedData) => entry.kind === TypeDocKind.Class) 66 .map((entry: GeneratedData) => entry.children) 67 .flat(), 68 TypeDocKind.Property, 69 entry => entry.name === 'defaultProps' 70 )[0]; 71 const enums = filterDataByKind(data, TypeDocKind.Enum); 72 const interfaces = filterDataByKind(data, TypeDocKind.Interface); 73 const constants = filterDataByKind( 74 data, 75 TypeDocKind.Variable, 76 entry => entry?.flags?.isConst || false 77 ); 78 79 return ( 80 <> 81 <APISectionConstants data={constants} apiName={apiName} /> 82 <APISectionMethods data={methods} apiName={apiName} /> 83 <APISectionMethods 84 data={eventSubscriptions} 85 apiName={apiName} 86 header="Event Subscriptions" 87 /> 88 <APISectionProps data={props} defaultProps={defaultProps} /> 89 <APISectionTypes data={types} /> 90 <APISectionInterfaces data={interfaces} /> 91 <APISectionEnums data={enums} /> 92 </> 93 ); 94 } catch (error) { 95 return <P>No API data file found, sorry!</P>; 96 } 97}; 98 99const APISection: React.FC<Props> = ({ packageName, apiName }) => { 100 const { version } = useContext(DocumentationPageContext); 101 const resolvedVersion = 102 version === 'unversioned' ? version : version === 'latest' ? LATEST_VERSION : version; 103 return renderAPI(packageName, resolvedVersion, apiName); 104}; 105 106export default APISection; 107