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 => !!(entry.type.declaration || entry.type.types) 50 ); 51 const props = filterDataByKind( 52 data, 53 TypeDocKind.TypeAlias, 54 entry => entry.name.includes('Props') && !!entry.type.types 55 ); 56 const defaultProps = filterDataByKind( 57 data 58 .filter((entry: GeneratedData) => entry.kind === TypeDocKind.Class) 59 .map((entry: GeneratedData) => entry.children) 60 .flat(), 61 TypeDocKind.Property, 62 entry => entry.name === 'defaultProps' 63 )[0]; 64 const enums = filterDataByKind(data, TypeDocKind.Enum); 65 const interfaces = filterDataByKind(data, TypeDocKind.Interface); 66 const constants = filterDataByKind(data, TypeDocKind.Variable, entry => entry.flags.isConst); 67 68 return ( 69 <> 70 <APISectionConstants data={constants} apiName={apiName} /> 71 <APISectionMethods data={methods} apiName={apiName} /> 72 <APISectionMethods 73 data={eventSubscriptions} 74 apiName={apiName} 75 header="Event Subscriptions" 76 /> 77 <APISectionProps data={props} defaultProps={defaultProps} /> 78 <APISectionTypes data={types} /> 79 <APISectionInterfaces data={interfaces} /> 80 <APISectionEnums data={enums} /> 81 </> 82 ); 83 } catch (error) { 84 return <P>No API data file found, sorry!</P>; 85 } 86}; 87 88const APISection: React.FC<Props> = ({ packageName, apiName }) => { 89 const { version } = useContext(DocumentationPageContext); 90 const resolvedVersion = 91 version === 'unversioned' ? version : version === 'latest' ? LATEST_VERSION : version; 92 return renderAPI(packageName, resolvedVersion, apiName); 93}; 94 95export default APISection; 96