1import { H2, H3Code } from '~/components/plugins/Headings';
2import {
3  CommentData,
4  GeneratedData,
5  MethodSignatureData,
6  PropsDefinitionData,
7} from '~/components/plugins/api/APIDataTypes';
8import { APISectionDeprecationNote } from '~/components/plugins/api/APISectionDeprecationNote';
9import APISectionProps from '~/components/plugins/api/APISectionProps';
10import {
11  CommentTextBlock,
12  resolveTypeName,
13  getComponentName,
14  STYLES_APIBOX,
15  getTagNamesList,
16} from '~/components/plugins/api/APISectionUtils';
17import { BOLD, P, CODE } from '~/ui/components/Text';
18
19export type APISectionComponentsProps = {
20  data: GeneratedData[];
21  componentsProps: PropsDefinitionData[];
22};
23
24const getComponentComment = (comment: CommentData, signatures: MethodSignatureData[]) =>
25  comment || (signatures?.[0]?.comment ?? undefined);
26
27const renderComponent = (
28  { name, comment, type, extendedTypes, children, signatures }: GeneratedData,
29  componentsProps?: PropsDefinitionData[]
30): JSX.Element => {
31  const resolvedType = extendedTypes?.length ? extendedTypes[0] : type;
32  const resolvedName = getComponentName(name, children);
33  const extractedComment = getComponentComment(comment, signatures);
34  return (
35    <div key={`component-definition-${resolvedName}`} css={STYLES_APIBOX}>
36      <APISectionDeprecationNote comment={extractedComment} />
37      <H3Code tags={getTagNamesList(comment)}>
38        <CODE>{resolvedName}</CODE>
39      </H3Code>
40      {resolvedType && (
41        <P>
42          <BOLD>Type:</BOLD> <CODE>{resolveTypeName(resolvedType)}</CODE>
43        </P>
44      )}
45      <CommentTextBlock comment={extractedComment} />
46      {componentsProps && componentsProps.length ? (
47        <APISectionProps
48          data={componentsProps}
49          header={componentsProps.length === 1 ? 'Props' : `${resolvedName}Props`}
50        />
51      ) : null}
52    </div>
53  );
54};
55
56const APISectionComponents = ({ data, componentsProps }: APISectionComponentsProps) =>
57  data?.length ? (
58    <>
59      <H2 key="components-header">{data.length === 1 ? 'Component' : 'Components'}</H2>
60      {data.map(component =>
61        renderComponent(
62          component,
63          componentsProps.filter(cp =>
64            cp.name.includes(getComponentName(component.name, component.children))
65          )
66        )
67      )}
68    </>
69  ) : null;
70
71export default APISectionComponents;
72