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_NOT_EXPOSED_HEADER,
26adede39aSŁukasz Kosmaty  TypeDocKind,
27a16a3d18SBartosz Kaszubowski  H3Code,
28a16a3d18SBartosz Kaszubowski  H4Code,
295990cc31SBartosz Kaszubowski  getTagData,
305990cc31SBartosz Kaszubowski  getCommentContent,
31*be43ea08SBartosz Kaszubowski  BoxSectionHeader,
32299f02f2SBartosz Kaszubowski} from '~/components/plugins/api/APISectionUtils';
33*be43ea08SBartosz Kaszubowskiimport { H2, 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            <>
82*be43ea08SBartosz Kaszubowski              <BoxSectionHeader text="Returns" />
8312abeb84SBartosz Kaszubowski              <UL css={STYLES_NO_BULLET_LIST}>
843e972ef8SBartosz Kaszubowski                <LI>
85dd9570b9SBartosz Kaszubowski                  <UndoIcon
86dd9570b9SBartosz Kaszubowski                    color={theme.icon.secondary}
8714c78e61SJon Samp                    size={iconSize.sm}
88dd9570b9SBartosz Kaszubowski                    css={returnIconStyles}
89dd9570b9SBartosz Kaszubowski                  />
90504f93b3SBartosz Kaszubowski                  <APIDataType typeDefinition={type} />
91299f02f2SBartosz Kaszubowski                </LI>
92299f02f2SBartosz Kaszubowski              </UL>
9312abeb84SBartosz Kaszubowski              <>
9412abeb84SBartosz Kaszubowski                <br />
955990cc31SBartosz Kaszubowski                {returnComment ? (
965990cc31SBartosz Kaszubowski                  <ReactMarkdown components={mdComponents}>
975990cc31SBartosz Kaszubowski                    {getCommentContent(returnComment.content)}
985990cc31SBartosz Kaszubowski                  </ReactMarkdown>
995990cc31SBartosz Kaszubowski                ) : undefined}
10012abeb84SBartosz Kaszubowski              </>
101af644ed4SBartosz Kaszubowski            </>
1026a5c065cSBartosz Kaszubowski          )}
103299f02f2SBartosz Kaszubowski        </div>
1045990cc31SBartosz Kaszubowski      );
1055990cc31SBartosz Kaszubowski    }
106dd9570b9SBartosz Kaszubowski  );
1071886d870SBartosz Kaszubowski};
108299f02f2SBartosz Kaszubowski
109adede39aSŁukasz Kosmatyconst APISectionMethods = ({
110adede39aSŁukasz Kosmaty  data,
111adede39aSŁukasz Kosmaty  apiName,
112adede39aSŁukasz Kosmaty  header = 'Methods',
113adede39aSŁukasz Kosmaty  exposeInSidebar = true,
114adede39aSŁukasz Kosmaty}: APISectionMethodsProps) =>
115299f02f2SBartosz Kaszubowski  data?.length ? (
116299f02f2SBartosz Kaszubowski    <>
11707ffa84cSBartosz Kaszubowski      <H2 key={`${header}-header`}>{header}</H2>
1184c73bbf6SBartosz Kaszubowski      {data.map((method: MethodDefinitionData | PropData) =>
119adede39aSŁukasz Kosmaty        renderMethod(method, { apiName, header, exposeInSidebar })
120e3060685SBartosz Kaszubowski      )}
121299f02f2SBartosz Kaszubowski    </>
122299f02f2SBartosz Kaszubowski  ) : null;
123299f02f2SBartosz Kaszubowski
1243e972ef8SBartosz Kaszubowskiconst returnIconStyles = css({
1253e972ef8SBartosz Kaszubowski  transform: 'rotate(180deg)',
1263e972ef8SBartosz Kaszubowski  marginRight: spacing[2],
1273e972ef8SBartosz Kaszubowski  verticalAlign: 'middle',
1283e972ef8SBartosz Kaszubowski});
1293e972ef8SBartosz Kaszubowski
130299f02f2SBartosz Kaszubowskiexport default APISectionMethods;
131adede39aSŁukasz Kosmaty
132adede39aSŁukasz Kosmatyexport const APIMethod = ({
133adede39aSŁukasz Kosmaty  name,
134adede39aSŁukasz Kosmaty  comment,
135adede39aSŁukasz Kosmaty  returnTypeName,
136adede39aSŁukasz Kosmaty  isProperty = false,
137adede39aSŁukasz Kosmaty  isReturnTypeReference = false,
138adede39aSŁukasz Kosmaty  exposeInSidebar = false,
139adede39aSŁukasz Kosmaty  parameters = [],
140c468593bSŁukasz Kosmaty  platforms = [],
141adede39aSŁukasz Kosmaty}: {
142adede39aSŁukasz Kosmaty  exposeInSidebar?: boolean;
143adede39aSŁukasz Kosmaty  name: string;
144adede39aSŁukasz Kosmaty  comment: string;
145adede39aSŁukasz Kosmaty  returnTypeName: string;
146adede39aSŁukasz Kosmaty  isProperty: boolean;
147adede39aSŁukasz Kosmaty  isReturnTypeReference: boolean;
148c468593bSŁukasz Kosmaty  platforms: ('Android' | 'iOS' | 'Web')[];
149adede39aSŁukasz Kosmaty  parameters: {
150adede39aSŁukasz Kosmaty    name: string;
151adede39aSŁukasz Kosmaty    comment?: string;
152adede39aSŁukasz Kosmaty    typeName: string;
153adede39aSŁukasz Kosmaty    isReference?: boolean;
154adede39aSŁukasz Kosmaty  }[];
1555990cc31SBartosz Kaszubowski}) => {
1565990cc31SBartosz Kaszubowski  const parsedParameters = parameters.map(
1575990cc31SBartosz Kaszubowski    param =>
1585990cc31SBartosz Kaszubowski      ({
159adede39aSŁukasz Kosmaty        name: param.name,
160adede39aSŁukasz Kosmaty        type: { name: param.typeName, type: param.isReference ? 'reference' : 'literal' },
161adede39aSŁukasz Kosmaty        comment: {
1625990cc31SBartosz Kaszubowski          summary: [{ kind: 'text', text: param.comment }],
163adede39aSŁukasz Kosmaty        },
1645990cc31SBartosz Kaszubowski      } as MethodParamData)
1655990cc31SBartosz Kaszubowski  );
166adede39aSŁukasz Kosmaty  return renderMethod(
167adede39aSŁukasz Kosmaty    {
168adede39aSŁukasz Kosmaty      name,
169adede39aSŁukasz Kosmaty      signatures: [
170adede39aSŁukasz Kosmaty        {
171adede39aSŁukasz Kosmaty          name,
172adede39aSŁukasz Kosmaty          parameters: parsedParameters,
173adede39aSŁukasz Kosmaty          comment: {
1745990cc31SBartosz Kaszubowski            summary: [{ kind: 'text', text: comment }],
1755990cc31SBartosz Kaszubowski            blockTags: platforms.map(text => ({
176c468593bSŁukasz Kosmaty              tag: 'platform',
1775990cc31SBartosz Kaszubowski              content: [{ kind: 'text', text }],
178c468593bSŁukasz Kosmaty            })),
179adede39aSŁukasz Kosmaty          },
180adede39aSŁukasz Kosmaty          type: { name: returnTypeName, type: isReturnTypeReference ? 'reference' : 'literal' },
181adede39aSŁukasz Kosmaty        },
182adede39aSŁukasz Kosmaty      ],
183adede39aSŁukasz Kosmaty      kind: isProperty ? TypeDocKind.Property : TypeDocKind.Function,
184adede39aSŁukasz Kosmaty    },
185adede39aSŁukasz Kosmaty    { exposeInSidebar }
186adede39aSŁukasz Kosmaty  );
187adede39aSŁukasz Kosmaty};
18812abeb84SBartosz Kaszubowski
18912abeb84SBartosz Kaszubowskiconst STYLES_NO_BULLET_LIST = css({
19012abeb84SBartosz Kaszubowski  listStyle: 'none',
19112abeb84SBartosz Kaszubowski  marginLeft: spacing[2],
19212abeb84SBartosz Kaszubowski});
193