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