xref: /expo/docs/components/plugins/APISection.tsx (revision 040cc41c)
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' ||
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  name !== 'default' && name !== 'Constants' && type?.name !== 'React.FC';
55
56const renderAPI = (
57  packageName: string,
58  version: string = 'unversioned',
59  apiName?: string,
60  isTestMode: boolean = false
61): JSX.Element => {
62  try {
63    // note(simek): When the path prefix is interpolated Next or Webpack fails to locate the file
64    const { children: data } = isTestMode
65      ? require(`../../public/static/data/${version}/${packageName}.json`)
66      : require(`~/public/static/data/${version}/${packageName}.json`);
67
68    const methods = filterDataByKind(
69      data,
70      TypeDocKind.Function,
71      entry => !isListener(entry) && !isHook(entry) && !isComponent(entry)
72    );
73    const hooks = filterDataByKind(data, TypeDocKind.Function, isHook);
74    const eventSubscriptions = filterDataByKind(data, TypeDocKind.Function, isListener);
75
76    const types = filterDataByKind(
77      data,
78      TypeDocKind.TypeAlias,
79      entry =>
80        !isProp(entry) &&
81        !!(
82          entry.type.declaration ||
83          entry.type.types ||
84          entry.type.type ||
85          entry.type.typeArguments
86        )
87    );
88
89    const props = filterDataByKind(
90      data,
91      TypeDocKind.TypeAlias,
92      entry => isProp(entry) && !!(entry.type.types || entry.type.declaration?.children)
93    );
94    const defaultProps = filterDataByKind(
95      data
96        .filter((entry: GeneratedData) => entry.kind === TypeDocKind.Class)
97        .map((entry: GeneratedData) => entry.children)
98        .flat(),
99      TypeDocKind.Property,
100      entry => entry.name === 'defaultProps'
101    )[0];
102
103    const enums = filterDataByKind(data, [TypeDocKind.Enum, TypeDocKind.LegacyEnum]);
104    const interfaces = filterDataByKind(data, TypeDocKind.Interface);
105    const constants = filterDataByKind(data, TypeDocKind.Variable, entry => isConstant(entry));
106
107    const components = filterDataByKind(
108      data,
109      [TypeDocKind.Variable, TypeDocKind.Class, TypeDocKind.Function],
110      entry => isComponent(entry)
111    );
112    const componentsPropNames = components.map(component => `${component.name}Props`);
113    const componentsProps = filterDataByKind(props, TypeDocKind.TypeAlias, entry =>
114      componentsPropNames.includes(entry.name)
115    );
116
117    const classes = filterDataByKind(
118      data,
119      TypeDocKind.Class,
120      entry => !isComponent(entry) && (apiName ? !entry.name.includes(apiName) : true)
121    );
122
123    return (
124      <>
125        <APISectionComponents data={components} componentsProps={componentsProps} />
126        <APISectionConstants data={constants} apiName={apiName} />
127        <APISectionMethods data={hooks} header="Hooks" />
128        <APISectionClasses data={classes} />
129        {props && !componentsProps.length ? (
130          <APISectionProps data={props} defaultProps={defaultProps} />
131        ) : null}
132        <APISectionMethods data={methods} apiName={apiName} />
133        <APISectionMethods
134          data={eventSubscriptions}
135          apiName={apiName}
136          header="Event Subscriptions"
137        />
138        <APISectionTypes data={types} />
139        <APISectionInterfaces data={interfaces} />
140        <APISectionEnums data={enums} />
141      </>
142    );
143  } catch (error) {
144    return <P>No API data file found, sorry!</P>;
145  }
146};
147
148const APISection = ({ packageName, apiName, forceVersion }: Props) => {
149  const { version } = useContext(DocumentationPageContext);
150  const resolvedVersion =
151    forceVersion ||
152    (version === 'unversioned' ? version : version === 'latest' ? LATEST_VERSION : version);
153  return renderAPI(packageName, resolvedVersion, apiName, !!forceVersion);
154};
155
156export default APISection;
157