1import { renderMethod } from './APISectionMethods';
2
3import { APIDataType } from '~/components/plugins/api/APIDataType';
4import {
5  CommentData,
6  InterfaceDefinitionData,
7  MethodSignatureData,
8  PropData,
9} from '~/components/plugins/api/APIDataTypes';
10import { APISectionDeprecationNote } from '~/components/plugins/api/APISectionDeprecationNote';
11import { APISectionPlatformTags } from '~/components/plugins/api/APISectionPlatformTags';
12import {
13  CommentTextBlock,
14  getTagData,
15  mdComponents,
16  parseCommentContent,
17  renderFlags,
18  renderParamRow,
19  ParamsTableHeadRow,
20  resolveTypeName,
21  renderDefaultValue,
22  STYLES_APIBOX,
23  STYLES_NESTED_SECTION_HEADER,
24  getTagNamesList,
25  STYLES_APIBOX_NESTED,
26  STYLES_ELEMENT_SPACING,
27  H3Code,
28} from '~/components/plugins/api/APISectionUtils';
29import { Cell, Row, Table } from '~/ui/components/Table';
30import { H2, H4, BOLD, P, CODE } from '~/ui/components/Text';
31
32export type APISectionInterfacesProps = {
33  data: InterfaceDefinitionData[];
34};
35
36const renderInterfaceComment = (
37  comment?: CommentData,
38  signatures?: MethodSignatureData[],
39  defaultValue?: string
40) => {
41  if (signatures && signatures.length) {
42    const { type, parameters, comment: signatureComment } = signatures[0];
43    const initValue = defaultValue || getTagData('default', signatureComment)?.text;
44    return (
45      <>
46        {parameters?.length ? parameters.map(param => renderParamRow(param)) : null}
47        <BOLD>Returns: </BOLD>
48        <CODE>{resolveTypeName(type)}</CODE>
49        {signatureComment && (
50          <>
51            <br />
52            <APISectionDeprecationNote comment={comment} />
53            <CommentTextBlock
54              comment={signatureComment}
55              components={mdComponents}
56              afterContent={renderDefaultValue(initValue)}
57            />
58          </>
59        )}
60      </>
61    );
62  } else {
63    const initValue = defaultValue || getTagData('default', comment)?.text;
64    return (
65      <>
66        <APISectionDeprecationNote comment={comment} />
67        <CommentTextBlock
68          comment={comment}
69          components={mdComponents}
70          afterContent={renderDefaultValue(initValue)}
71          emptyCommentFallback="-"
72        />
73      </>
74    );
75  }
76};
77
78const renderInterfacePropertyRow = ({
79  name,
80  flags,
81  type,
82  comment,
83  signatures,
84  defaultValue,
85}: PropData): JSX.Element => {
86  const initValue = parseCommentContent(defaultValue || getTagData('default', comment)?.text);
87  return (
88    <Row key={name}>
89      <Cell fitContent>
90        <BOLD>{name}</BOLD>
91        {renderFlags(flags, initValue)}
92      </Cell>
93      <Cell fitContent>
94        <APIDataType typeDefinition={type} />
95      </Cell>
96      <Cell fitContent>{renderInterfaceComment(comment, signatures, initValue)}</Cell>
97    </Row>
98  );
99};
100
101const renderInterface = ({
102  name,
103  children,
104  comment,
105  extendedTypes,
106}: InterfaceDefinitionData): JSX.Element | null => {
107  const interfaceChildren = children?.filter(child => !child?.inheritedFrom) || [];
108
109  if (!interfaceChildren.length) return null;
110
111  const interfaceMethods = interfaceChildren.filter(child => child.signatures);
112  const interfaceFields = interfaceChildren.filter(child => !child.signatures);
113
114  return (
115    <div key={`interface-definition-${name}`} css={[STYLES_APIBOX, STYLES_APIBOX_NESTED]}>
116      <APISectionDeprecationNote comment={comment} />
117      <APISectionPlatformTags comment={comment} prefix="Only for:" />
118      <H3Code tags={getTagNamesList(comment)}>
119        <CODE>{name}</CODE>
120      </H3Code>
121      {extendedTypes?.length ? (
122        <P css={STYLES_ELEMENT_SPACING}>
123          <BOLD>Extends: </BOLD>
124          {extendedTypes.map(extendedType => (
125            <CODE key={`extend-${extendedType.name}`}>{resolveTypeName(extendedType)}</CODE>
126          ))}
127        </P>
128      ) : null}
129      <CommentTextBlock comment={comment} includePlatforms={false} />
130      {interfaceMethods.length ? (
131        <>
132          <div css={STYLES_NESTED_SECTION_HEADER}>
133            <H4>{name} Methods</H4>
134          </div>
135          {interfaceMethods.map(method => renderMethod(method, { exposeInSidebar: false }))}
136        </>
137      ) : undefined}
138      {interfaceFields.length ? (
139        <>
140          <div css={STYLES_NESTED_SECTION_HEADER}>
141            <H4>{name} Properties</H4>
142          </div>
143          <Table>
144            <ParamsTableHeadRow />
145            <tbody>{interfaceFields.map(renderInterfacePropertyRow)}</tbody>
146          </Table>
147          <br />
148        </>
149      ) : undefined}
150    </div>
151  );
152};
153
154const APISectionInterfaces = ({ data }: APISectionInterfacesProps) =>
155  data?.length ? (
156    <>
157      <H2 key="interfaces-header">Interfaces</H2>
158      {data.map(renderInterface)}
159    </>
160  ) : null;
161
162export default APISectionInterfaces;
163