173ab0ab9SBartosz Kaszubowskiimport {
2144e76edSBartosz Kaszubowski  CommentData,
373ab0ab9SBartosz Kaszubowski  GeneratedData,
4144e76edSBartosz Kaszubowski  MethodSignatureData,
573ab0ab9SBartosz Kaszubowski  PropsDefinitionData,
673ab0ab9SBartosz Kaszubowski} from '~/components/plugins/api/APIDataTypes';
725b16883SBartosz Kaszubowskiimport { APISectionDeprecationNote } from '~/components/plugins/api/APISectionDeprecationNote';
81ef472c3SBartosz Kaszubowskiimport APISectionProps from '~/components/plugins/api/APISectionProps';
99f72d43bSBartosz Kaszubowskiimport {
109f72d43bSBartosz Kaszubowski  CommentTextBlock,
119f72d43bSBartosz Kaszubowski  resolveTypeName,
129f72d43bSBartosz Kaszubowski  getComponentName,
136a5c065cSBartosz Kaszubowski  STYLES_APIBOX,
14c4c6b9d1SBartosz Kaszubowski  getTagNamesList,
15a16a3d18SBartosz Kaszubowski  H3Code,
16*f4b1168bSBartosz Kaszubowski  ELEMENT_SPACING,
179f72d43bSBartosz Kaszubowski} from '~/components/plugins/api/APISectionUtils';
183324c13cSBartosz Kaszubowskiimport { H2, BOLD, P, CODE, MONOSPACE } from '~/ui/components/Text';
191ef472c3SBartosz Kaszubowski
201ef472c3SBartosz Kaszubowskiexport type APISectionComponentsProps = {
21f31f564bSBartosz Kaszubowski  data: GeneratedData[];
221ef472c3SBartosz Kaszubowski  componentsProps: PropsDefinitionData[];
231ef472c3SBartosz Kaszubowski};
241ef472c3SBartosz Kaszubowski
25144e76edSBartosz Kaszubowskiconst getComponentComment = (comment: CommentData, signatures: MethodSignatureData[]) =>
26144e76edSBartosz Kaszubowski  comment || (signatures?.[0]?.comment ?? undefined);
27144e76edSBartosz Kaszubowski
285990cc31SBartosz Kaszubowskiconst getComponentType = ({ signatures }: Partial<GeneratedData>) => {
295990cc31SBartosz Kaszubowski  if (signatures?.length && signatures[0].type.types) {
305990cc31SBartosz Kaszubowski    return 'React.' + signatures[0].type.types.filter(t => t.type === 'reference')[0]?.name;
315990cc31SBartosz Kaszubowski  }
325990cc31SBartosz Kaszubowski  return 'React.Element';
335990cc31SBartosz Kaszubowski};
345990cc31SBartosz Kaszubowski
355990cc31SBartosz Kaszubowskiconst getComponentTypeParameters = ({
365990cc31SBartosz Kaszubowski  extendedTypes,
375990cc31SBartosz Kaszubowski  type,
385990cc31SBartosz Kaszubowski  signatures,
395990cc31SBartosz Kaszubowski}: Partial<GeneratedData>) => {
405990cc31SBartosz Kaszubowski  if (extendedTypes?.length) {
415990cc31SBartosz Kaszubowski    return extendedTypes[0];
425990cc31SBartosz Kaszubowski  } else if (signatures?.length && signatures[0].parameters.length) {
435990cc31SBartosz Kaszubowski    return signatures?.[0].parameters[0].type;
445990cc31SBartosz Kaszubowski  }
455990cc31SBartosz Kaszubowski  return type;
465990cc31SBartosz Kaszubowski};
475990cc31SBartosz Kaszubowski
481ef472c3SBartosz Kaszubowskiconst renderComponent = (
49144e76edSBartosz Kaszubowski  { name, comment, type, extendedTypes, children, signatures }: GeneratedData,
501ef472c3SBartosz Kaszubowski  componentsProps?: PropsDefinitionData[]
51f31f564bSBartosz Kaszubowski): JSX.Element => {
525990cc31SBartosz Kaszubowski  const resolvedType = getComponentType({ signatures });
535990cc31SBartosz Kaszubowski  const resolvedTypeParameters = getComponentTypeParameters({ type, extendedTypes, signatures });
5473ab0ab9SBartosz Kaszubowski  const resolvedName = getComponentName(name, children);
5525b16883SBartosz Kaszubowski  const extractedComment = getComponentComment(comment, signatures);
56f31f564bSBartosz Kaszubowski  return (
576a5c065cSBartosz Kaszubowski    <div key={`component-definition-${resolvedName}`} css={STYLES_APIBOX}>
5825b16883SBartosz Kaszubowski      <APISectionDeprecationNote comment={extractedComment} />
59c4c6b9d1SBartosz Kaszubowski      <H3Code tags={getTagNamesList(comment)}>
603324c13cSBartosz Kaszubowski        <MONOSPACE weight="medium">{resolvedName}</MONOSPACE>
611ef472c3SBartosz Kaszubowski      </H3Code>
625990cc31SBartosz Kaszubowski      {resolvedType && resolvedTypeParameters && (
63*f4b1168bSBartosz Kaszubowski        <P className={ELEMENT_SPACING}>
645990cc31SBartosz Kaszubowski          <BOLD>Type:</BOLD>{' '}
655990cc31SBartosz Kaszubowski          <CODE>
665990cc31SBartosz Kaszubowski            {extendedTypes ? (
675990cc31SBartosz Kaszubowski              <>React.{resolveTypeName(resolvedTypeParameters)}</>
685990cc31SBartosz Kaszubowski            ) : (
695990cc31SBartosz Kaszubowski              <>
705990cc31SBartosz Kaszubowski                {resolvedType}&lt;{resolveTypeName(resolvedTypeParameters)}&gt;
715990cc31SBartosz Kaszubowski              </>
725990cc31SBartosz Kaszubowski            )}
735990cc31SBartosz Kaszubowski          </CODE>
741ef472c3SBartosz Kaszubowski        </P>
75f31f564bSBartosz Kaszubowski      )}
7625b16883SBartosz Kaszubowski      <CommentTextBlock comment={extractedComment} />
771ef472c3SBartosz Kaszubowski      {componentsProps && componentsProps.length ? (
7813032b48SBartosz Kaszubowski        <APISectionProps
7913032b48SBartosz Kaszubowski          data={componentsProps}
8013032b48SBartosz Kaszubowski          header={componentsProps.length === 1 ? 'Props' : `${resolvedName}Props`}
8113032b48SBartosz Kaszubowski        />
821ef472c3SBartosz Kaszubowski      ) : null}
831ef472c3SBartosz Kaszubowski    </div>
841ef472c3SBartosz Kaszubowski  );
85f31f564bSBartosz Kaszubowski};
861ef472c3SBartosz Kaszubowski
87558a63feSBartosz Kaszubowskiconst APISectionComponents = ({ data, componentsProps }: APISectionComponentsProps) =>
881ef472c3SBartosz Kaszubowski  data?.length ? (
891ef472c3SBartosz Kaszubowski    <>
9013032b48SBartosz Kaszubowski      <H2 key="components-header">{data.length === 1 ? 'Component' : 'Components'}</H2>
911ef472c3SBartosz Kaszubowski      {data.map(component =>
921ef472c3SBartosz Kaszubowski        renderComponent(
931ef472c3SBartosz Kaszubowski          component,
949f72d43bSBartosz Kaszubowski          componentsProps.filter(cp =>
959f72d43bSBartosz Kaszubowski            cp.name.includes(getComponentName(component.name, component.children))
969f72d43bSBartosz Kaszubowski          )
971ef472c3SBartosz Kaszubowski        )
981ef472c3SBartosz Kaszubowski      )}
991ef472c3SBartosz Kaszubowski    </>
1001ef472c3SBartosz Kaszubowski  ) : null;
1011ef472c3SBartosz Kaszubowski
1021ef472c3SBartosz Kaszubowskiexport default APISectionComponents;
103