13e972ef8SBartosz Kaszubowskiimport { css } from '@emotion/react';
23e972ef8SBartosz Kaszubowskiimport { theme, spacing, UndoIcon, iconSize } from '@expo/styleguide';
3299f02f2SBartosz Kaszubowskiimport ReactMarkdown from 'react-markdown';
4299f02f2SBartosz Kaszubowski
5504f93b3SBartosz Kaszubowskiimport { APIDataType } from '~/components/plugins/api/APIDataType';
6d8d79196SBartosz Kaszubowskiimport {
76b7802baSBartosz Kaszubowski  AccessorDefinitionData,
8d8d79196SBartosz Kaszubowski  MethodDefinitionData,
95990cc31SBartosz Kaszubowski  MethodParamData,
10d8d79196SBartosz Kaszubowski  MethodSignatureData,
11d8d79196SBartosz Kaszubowski  PropData,
12dd9570b9SBartosz Kaszubowski  TypeSignaturesData,
13d8d79196SBartosz Kaszubowski} from '~/components/plugins/api/APIDataTypes';
1425b16883SBartosz Kaszubowskiimport { APISectionDeprecationNote } from '~/components/plugins/api/APISectionDeprecationNote';
15b3bd70ceSTomasz Sapetaimport { APISectionPlatformTags } from '~/components/plugins/api/APISectionPlatformTags';
16299f02f2SBartosz Kaszubowskiimport {
17299f02f2SBartosz Kaszubowski  CommentTextBlock,
18adede39aSŁukasz Kosmaty  getMethodName,
19c4c6b9d1SBartosz Kaszubowski  getTagNamesList,
20dc379e46SBartosz Kaszubowski  mdComponents,
217534da61SBartosz Kaszubowski  renderParams,
22299f02f2SBartosz Kaszubowski  resolveTypeName,
236a5c065cSBartosz Kaszubowski  STYLES_APIBOX,
246a5c065cSBartosz Kaszubowski  STYLES_APIBOX_NESTED,
256a5c065cSBartosz Kaszubowski  STYLES_NESTED_SECTION_HEADER,
266a5c065cSBartosz Kaszubowski  STYLES_NOT_EXPOSED_HEADER,
27adede39aSŁukasz Kosmaty  TypeDocKind,
28a16a3d18SBartosz Kaszubowski  H3Code,
29a16a3d18SBartosz Kaszubowski  H4Code,
305990cc31SBartosz Kaszubowski  getTagData,
315990cc31SBartosz Kaszubowski  getCommentContent,
32299f02f2SBartosz Kaszubowski} from '~/components/plugins/api/APISectionUtils';
33a16a3d18SBartosz Kaszubowskiimport { H2, H4, LI, UL, CODE } from '~/ui/components/Text';
34299f02f2SBartosz Kaszubowski
35299f02f2SBartosz Kaszubowskiexport type APISectionMethodsProps = {
36e3060685SBartosz Kaszubowski  data: (MethodDefinitionData | PropData)[];
37299f02f2SBartosz Kaszubowski  apiName?: string;
381b3dce65SBartosz Kaszubowski  header?: string;
39adede39aSŁukasz Kosmaty  exposeInSidebar?: boolean;
40299f02f2SBartosz Kaszubowski};
41299f02f2SBartosz Kaszubowski
424c73bbf6SBartosz Kaszubowskiexport type RenderMethodOptions = {
434c73bbf6SBartosz Kaszubowski  apiName?: string;
444c73bbf6SBartosz Kaszubowski  header?: string;
454c73bbf6SBartosz Kaszubowski  exposeInSidebar?: boolean;
464c73bbf6SBartosz Kaszubowski};
474c73bbf6SBartosz Kaszubowski
48d8d79196SBartosz Kaszubowskiexport const renderMethod = (
496b7802baSBartosz Kaszubowski  method: MethodDefinitionData | AccessorDefinitionData | PropData,
50dd9570b9SBartosz Kaszubowski  { apiName, exposeInSidebar = true }: RenderMethodOptions = {}
515990cc31SBartosz Kaszubowski) => {
526b7802baSBartosz Kaszubowski  const signatures =
536b7802baSBartosz Kaszubowski    (method as MethodDefinitionData).signatures ||
545990cc31SBartosz Kaszubowski    (method as PropData)?.type?.declaration?.signatures || [
555990cc31SBartosz Kaszubowski      (method as AccessorDefinitionData)?.getSignature,
565990cc31SBartosz Kaszubowski    ] ||
576b7802baSBartosz Kaszubowski    [];
581886d870SBartosz Kaszubowski  const HeaderComponent = exposeInSidebar ? H3Code : H4Code;
59dd9570b9SBartosz Kaszubowski  return signatures.map(
605990cc31SBartosz Kaszubowski    ({ name, parameters, comment, type }: MethodSignatureData | TypeSignaturesData) => {
615990cc31SBartosz Kaszubowski      const returnComment = getTagData('returns', comment);
625990cc31SBartosz Kaszubowski      return (
636a5c065cSBartosz Kaszubowski        <div
64dd9570b9SBartosz Kaszubowski          key={`method-signature-${method.name || name}-${parameters?.length || 0}`}
6512abeb84SBartosz Kaszubowski          css={[STYLES_APIBOX, STYLES_APIBOX_NESTED]}>
6625b16883SBartosz Kaszubowski          <APISectionDeprecationNote comment={comment} />
67d9bd5b6cSBartosz Kaszubowski          <APISectionPlatformTags comment={comment} prefix="Only for:" />
68c4c6b9d1SBartosz Kaszubowski          <HeaderComponent tags={getTagNamesList(comment)}>
6912abeb84SBartosz Kaszubowski            <CODE css={!exposeInSidebar ? STYLES_NOT_EXPOSED_HEADER : undefined}>
70adede39aSŁukasz Kosmaty              {getMethodName(method as MethodDefinitionData, apiName, name, parameters)}
7112abeb84SBartosz Kaszubowski            </CODE>
721886d870SBartosz Kaszubowski          </HeaderComponent>
7312abeb84SBartosz Kaszubowski          {parameters && parameters.length > 0 && (
7412abeb84SBartosz Kaszubowski            <>
7512abeb84SBartosz Kaszubowski              {renderParams(parameters)}
7612abeb84SBartosz Kaszubowski              <br />
7712abeb84SBartosz Kaszubowski            </>
7812abeb84SBartosz Kaszubowski          )}
797534da61SBartosz Kaszubowski          <CommentTextBlock comment={comment} includePlatforms={false} />
806a5c065cSBartosz Kaszubowski          {resolveTypeName(type) !== 'undefined' && (
81af644ed4SBartosz Kaszubowski            <>
826a5c065cSBartosz Kaszubowski              <div css={STYLES_NESTED_SECTION_HEADER}>
83299f02f2SBartosz Kaszubowski                <H4>Returns</H4>
846a5c065cSBartosz Kaszubowski              </div>
8512abeb84SBartosz Kaszubowski              <UL css={STYLES_NO_BULLET_LIST}>
863e972ef8SBartosz Kaszubowski                <LI>
87dd9570b9SBartosz Kaszubowski                  <UndoIcon
88dd9570b9SBartosz Kaszubowski                    color={theme.icon.secondary}
8914c78e61SJon Samp                    size={iconSize.sm}
90dd9570b9SBartosz Kaszubowski                    css={returnIconStyles}
91dd9570b9SBartosz Kaszubowski                  />
92504f93b3SBartosz Kaszubowski                  <APIDataType typeDefinition={type} />
93299f02f2SBartosz Kaszubowski                </LI>
94299f02f2SBartosz Kaszubowski              </UL>
9512abeb84SBartosz Kaszubowski              <>
9612abeb84SBartosz Kaszubowski                <br />
975990cc31SBartosz Kaszubowski                {returnComment ? (
985990cc31SBartosz Kaszubowski                  <ReactMarkdown components={mdComponents}>
995990cc31SBartosz Kaszubowski                    {getCommentContent(returnComment.content)}
1005990cc31SBartosz Kaszubowski                  </ReactMarkdown>
1015990cc31SBartosz Kaszubowski                ) : undefined}
10212abeb84SBartosz Kaszubowski              </>
103af644ed4SBartosz Kaszubowski            </>
1046a5c065cSBartosz Kaszubowski          )}
105299f02f2SBartosz Kaszubowski        </div>
1065990cc31SBartosz Kaszubowski      );
1075990cc31SBartosz Kaszubowski    }
108dd9570b9SBartosz Kaszubowski  );
1091886d870SBartosz Kaszubowski};
110299f02f2SBartosz Kaszubowski
111adede39aSŁukasz Kosmatyconst APISectionMethods = ({
112adede39aSŁukasz Kosmaty  data,
113adede39aSŁukasz Kosmaty  apiName,
114adede39aSŁukasz Kosmaty  header = 'Methods',
115adede39aSŁukasz Kosmaty  exposeInSidebar = true,
116adede39aSŁukasz Kosmaty}: APISectionMethodsProps) =>
117299f02f2SBartosz Kaszubowski  data?.length ? (
118299f02f2SBartosz Kaszubowski    <>
119*07ffa84cSBartosz Kaszubowski      <H2 key={`${header}-header`}>{header}</H2>
1204c73bbf6SBartosz Kaszubowski      {data.map((method: MethodDefinitionData | PropData) =>
121adede39aSŁukasz Kosmaty        renderMethod(method, { apiName, header, exposeInSidebar })
122e3060685SBartosz Kaszubowski      )}
123299f02f2SBartosz Kaszubowski    </>
124299f02f2SBartosz Kaszubowski  ) : null;
125299f02f2SBartosz Kaszubowski
1263e972ef8SBartosz Kaszubowskiconst returnIconStyles = css({
1273e972ef8SBartosz Kaszubowski  transform: 'rotate(180deg)',
1283e972ef8SBartosz Kaszubowski  marginRight: spacing[2],
1293e972ef8SBartosz Kaszubowski  verticalAlign: 'middle',
1303e972ef8SBartosz Kaszubowski});
1313e972ef8SBartosz Kaszubowski
132299f02f2SBartosz Kaszubowskiexport default APISectionMethods;
133adede39aSŁukasz Kosmaty
134adede39aSŁukasz Kosmatyexport const APIMethod = ({
135adede39aSŁukasz Kosmaty  name,
136adede39aSŁukasz Kosmaty  comment,
137adede39aSŁukasz Kosmaty  returnTypeName,
138adede39aSŁukasz Kosmaty  isProperty = false,
139adede39aSŁukasz Kosmaty  isReturnTypeReference = false,
140adede39aSŁukasz Kosmaty  exposeInSidebar = false,
141adede39aSŁukasz Kosmaty  parameters = [],
142c468593bSŁukasz Kosmaty  platforms = [],
143adede39aSŁukasz Kosmaty}: {
144adede39aSŁukasz Kosmaty  exposeInSidebar?: boolean;
145adede39aSŁukasz Kosmaty  name: string;
146adede39aSŁukasz Kosmaty  comment: string;
147adede39aSŁukasz Kosmaty  returnTypeName: string;
148adede39aSŁukasz Kosmaty  isProperty: boolean;
149adede39aSŁukasz Kosmaty  isReturnTypeReference: boolean;
150c468593bSŁukasz Kosmaty  platforms: ('Android' | 'iOS' | 'Web')[];
151adede39aSŁukasz Kosmaty  parameters: {
152adede39aSŁukasz Kosmaty    name: string;
153adede39aSŁukasz Kosmaty    comment?: string;
154adede39aSŁukasz Kosmaty    typeName: string;
155adede39aSŁukasz Kosmaty    isReference?: boolean;
156adede39aSŁukasz Kosmaty  }[];
1575990cc31SBartosz Kaszubowski}) => {
1585990cc31SBartosz Kaszubowski  const parsedParameters = parameters.map(
1595990cc31SBartosz Kaszubowski    param =>
1605990cc31SBartosz Kaszubowski      ({
161adede39aSŁukasz Kosmaty        name: param.name,
162adede39aSŁukasz Kosmaty        type: { name: param.typeName, type: param.isReference ? 'reference' : 'literal' },
163adede39aSŁukasz Kosmaty        comment: {
1645990cc31SBartosz Kaszubowski          summary: [{ kind: 'text', text: param.comment }],
165adede39aSŁukasz Kosmaty        },
1665990cc31SBartosz Kaszubowski      } as MethodParamData)
1675990cc31SBartosz Kaszubowski  );
168adede39aSŁukasz Kosmaty  return renderMethod(
169adede39aSŁukasz Kosmaty    {
170adede39aSŁukasz Kosmaty      name,
171adede39aSŁukasz Kosmaty      signatures: [
172adede39aSŁukasz Kosmaty        {
173adede39aSŁukasz Kosmaty          name,
174adede39aSŁukasz Kosmaty          parameters: parsedParameters,
175adede39aSŁukasz Kosmaty          comment: {
1765990cc31SBartosz Kaszubowski            summary: [{ kind: 'text', text: comment }],
1775990cc31SBartosz Kaszubowski            blockTags: platforms.map(text => ({
178c468593bSŁukasz Kosmaty              tag: 'platform',
1795990cc31SBartosz Kaszubowski              content: [{ kind: 'text', text }],
180c468593bSŁukasz Kosmaty            })),
181adede39aSŁukasz Kosmaty          },
182adede39aSŁukasz Kosmaty          type: { name: returnTypeName, type: isReturnTypeReference ? 'reference' : 'literal' },
183adede39aSŁukasz Kosmaty        },
184adede39aSŁukasz Kosmaty      ],
185adede39aSŁukasz Kosmaty      kind: isProperty ? TypeDocKind.Property : TypeDocKind.Function,
186adede39aSŁukasz Kosmaty    },
187adede39aSŁukasz Kosmaty    { exposeInSidebar }
188adede39aSŁukasz Kosmaty  );
189adede39aSŁukasz Kosmaty};
19012abeb84SBartosz Kaszubowski
19112abeb84SBartosz Kaszubowskiconst STYLES_NO_BULLET_LIST = css({
19212abeb84SBartosz Kaszubowski  listStyle: 'none',
19312abeb84SBartosz Kaszubowski  marginLeft: spacing[2],
19412abeb84SBartosz Kaszubowski});
195