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 APISectionComponents from '~/components/plugins/api/APISectionComponents'; 7import APISectionConstants from '~/components/plugins/api/APISectionConstants'; 8import APISectionEnums from '~/components/plugins/api/APISectionEnums'; 9import APISectionInterfaces from '~/components/plugins/api/APISectionInterfaces'; 10import APISectionMethods from '~/components/plugins/api/APISectionMethods'; 11import APISectionProps from '~/components/plugins/api/APISectionProps'; 12import APISectionTypes from '~/components/plugins/api/APISectionTypes'; 13import { TypeDocKind } from '~/components/plugins/api/APISectionUtils'; 14 15const LATEST_VERSION = `v${require('~/package.json').version}`; 16 17type Props = { 18 packageName: string; 19 apiName?: string; 20 forceVersion?: string; 21}; 22 23const filterDataByKind = ( 24 entries: GeneratedData[] = [], 25 kind: TypeDocKind | TypeDocKind[], 26 additionalCondition: (entry: GeneratedData) => boolean = () => true 27) => 28 entries.filter( 29 (entry: GeneratedData) => 30 (Array.isArray(kind) ? kind.includes(entry.kind) : entry.kind === kind) && 31 additionalCondition(entry) 32 ); 33 34const isHook = ({ name }: GeneratedData) => 35 name.startsWith('use') && 36 // note(simek): hardcode this exception until the method will be renamed 37 name !== 'useSystemBrightnessAsync'; 38 39const isListener = ({ name }: GeneratedData) => 40 name.endsWith('Listener') || name.endsWith('Listeners'); 41 42const isProp = ({ name }: GeneratedData) => name.includes('Props') && name !== 'ErrorRecoveryProps'; 43 44const isComponent = ({ type, extendedTypes }: GeneratedData) => 45 type?.name === 'React.FC' || 46 (extendedTypes && extendedTypes.length ? extendedTypes[0].name === 'Component' : false); 47 48const isConstant = ({ flags, name, type }: GeneratedData) => 49 (flags?.isConst || false) && name !== 'default' && type?.name !== 'React.FC'; 50 51const renderAPI = ( 52 packageName: string, 53 version: string = 'unversioned', 54 apiName?: string, 55 isTestMode: boolean = false 56): JSX.Element => { 57 try { 58 // note(simek): When the path prefix is interpolated Next or Webpack fails to locate the file 59 const { children: data } = isTestMode 60 ? require(`../../public/static/data/${version}/${packageName}.json`) 61 : require(`~/public/static/data/${version}/${packageName}.json`); 62 63 const methods = filterDataByKind( 64 data, 65 TypeDocKind.Function, 66 entry => !isListener(entry) && !isHook(entry) 67 ); 68 const hooks = filterDataByKind(data, TypeDocKind.Function, isHook); 69 const eventSubscriptions = filterDataByKind(data, TypeDocKind.Function, isListener); 70 71 const types = filterDataByKind( 72 data, 73 TypeDocKind.TypeAlias, 74 entry => 75 !isProp(entry) && 76 !!( 77 entry.type.declaration || 78 entry.type.types || 79 entry.type.type || 80 entry.type.typeArguments 81 ) 82 ); 83 84 const props = filterDataByKind( 85 data, 86 TypeDocKind.TypeAlias, 87 entry => isProp(entry) && !!(entry.type.types || entry.type.declaration?.children) 88 ); 89 const defaultProps = filterDataByKind( 90 data 91 .filter((entry: GeneratedData) => entry.kind === TypeDocKind.Class) 92 .map((entry: GeneratedData) => entry.children) 93 .flat(), 94 TypeDocKind.Property, 95 entry => entry.name === 'defaultProps' 96 )[0]; 97 98 const enums = filterDataByKind(data, [TypeDocKind.Enum, TypeDocKind.LegacyEnum]); 99 const interfaces = filterDataByKind(data, TypeDocKind.Interface); 100 const constants = filterDataByKind(data, TypeDocKind.Variable, entry => isConstant(entry)); 101 102 const components = filterDataByKind(data, [TypeDocKind.Variable, TypeDocKind.Class], entry => 103 isComponent(entry) 104 ); 105 const componentsPropNames = components.map(component => `${component.name}Props`); 106 const componentsProps = filterDataByKind(props, TypeDocKind.TypeAlias, entry => 107 componentsPropNames.includes(entry.name) 108 ); 109 110 return ( 111 <> 112 <APISectionComponents data={components} componentsProps={componentsProps} /> 113 <APISectionConstants data={constants} apiName={apiName} /> 114 <APISectionMethods data={hooks} header="Hooks" /> 115 <APISectionMethods data={methods} apiName={apiName} /> 116 <APISectionMethods 117 data={eventSubscriptions} 118 apiName={apiName} 119 header="Event Subscriptions" 120 /> 121 {props && !componentsProps.length ? ( 122 <APISectionProps data={props} defaultProps={defaultProps} /> 123 ) : null} 124 <APISectionTypes data={types} /> 125 <APISectionInterfaces data={interfaces} /> 126 <APISectionEnums data={enums} /> 127 </> 128 ); 129 } catch (error) { 130 return <P>No API data file found, sorry!</P>; 131 } 132}; 133 134const APISection = ({ packageName, apiName, forceVersion }: Props) => { 135 const { version } = useContext(DocumentationPageContext); 136 const resolvedVersion = 137 forceVersion || 138 (version === 'unversioned' ? version : version === 'latest' ? LATEST_VERSION : version); 139 return renderAPI(packageName, resolvedVersion, apiName, !!forceVersion); 140}; 141 142export default APISection; 143