1import { css } from '@emotion/react';
2import { theme } from '@expo/styleguide';
3import React from 'react';
4import ReactMarkdown from 'react-markdown';
5
6import { Code, InlineCode } from '~/components/base/code';
7import { H4 } from '~/components/base/headings';
8import Link from '~/components/base/link';
9import { LI, UL } from '~/components/base/list';
10import { B, P, Quote } from '~/components/base/paragraph';
11import {
12  CommentData,
13  MethodParamData,
14  TypeDefinitionData,
15} from '~/components/plugins/api/APIDataTypes';
16
17export enum TypeDocKind {
18  Enum = 4,
19  Variable = 32,
20  Function = 64,
21  Class = 128,
22  Interface = 256,
23  Property = 1024,
24  TypeAlias = 4194304,
25}
26
27export type MDRenderers = React.ComponentProps<typeof ReactMarkdown>['renderers'];
28
29export const mdRenderers: MDRenderers = {
30  blockquote: ({ children }) => (
31    <Quote>
32      {React.Children.map(children, child =>
33        child.type.name === 'paragraph' ? child.props.children : child
34      )}
35    </Quote>
36  ),
37  code: ({ value, language }) => <Code className={`language-${language}`}>{value}</Code>,
38  heading: ({ children }) => <H4>{children}</H4>,
39  inlineCode: ({ value }) => <InlineCode>{value}</InlineCode>,
40  list: ({ children }) => <UL>{children}</UL>,
41  listItem: ({ children }) => <LI>{children}</LI>,
42  link: ({ href, children }) => <Link href={href}>{children}</Link>,
43  paragraph: ({ children }) => (children ? <P>{children}</P> : null),
44  strong: ({ children }) => <B>{children}</B>,
45  text: ({ value }) => (value ? <span>{value}</span> : null),
46};
47
48export const mdInlineRenderers: MDRenderers = {
49  ...mdRenderers,
50  paragraph: ({ children }) => (children ? <span>{children}</span> : null),
51};
52
53const nonLinkableTypes = ['Date', 'T', 'TaskOptions', 'Uint8Array'];
54
55export const resolveTypeName = ({
56  elementType,
57  name,
58  type,
59  types,
60  typeArguments,
61  declaration,
62  value,
63}: TypeDefinitionData): string | JSX.Element | (string | JSX.Element)[] => {
64  if (name) {
65    if (type === 'reference') {
66      if (typeArguments) {
67        if (name === 'Promise') {
68          return (
69            <span>
70              {name}&lt;{typeArguments.map(resolveTypeName)}&gt;
71            </span>
72          );
73        } else if (name === 'Record') {
74          return (
75            <span>
76              {name}&lt;{typeArguments.map(resolveTypeName).join(',')}&gt;
77            </span>
78          );
79        } else {
80          return `${typeArguments.map(resolveTypeName)}`;
81        }
82      } else {
83        if (nonLinkableTypes.includes(name)) {
84          return name;
85        } else {
86          return (
87            <Link href={`#${name.toLowerCase()}`} key={`type-link-${name}`}>
88              {name}
89            </Link>
90          );
91        }
92      }
93    } else {
94      return name;
95    }
96  } else if (elementType?.name) {
97    if (elementType.type === 'reference') {
98      return (
99        <Link href={`#${elementType.name?.toLowerCase()}`} key={`type-link-${elementType.name}`}>
100          {elementType.name}
101          {type === 'array' && '[]'}
102        </Link>
103      );
104    }
105    if (type === 'array') {
106      return elementType.name + '[]';
107    }
108    return elementType.name + type;
109  } else if (type === 'union' && types?.length) {
110    return types
111      .map((t: TypeDefinitionData) =>
112        t.type === 'reference' ? (
113          <Link href={`#${t.name?.toLowerCase()}`} key={`type-link-${t.name}`}>
114            {t.name}
115          </Link>
116        ) : t.type === 'array' ? (
117          `${t.elementType?.name}[]`
118        ) : t.type === 'literal' && typeof t.value === 'string' ? (
119          `'${t.name || t.value}'`
120        ) : (
121          `${t.name || t.value}`
122        )
123      )
124      .map((valueToRender, index) => (
125        <span key={`union-type-${index}`}>
126          {valueToRender}
127          {index + 1 !== types.length && ' | '}
128        </span>
129      ));
130  } else if (declaration?.signatures) {
131    const baseSignature = declaration.signatures[0];
132    if (baseSignature?.parameters?.length) {
133      return (
134        <>
135          (
136          {baseSignature.parameters?.map((param, index) => (
137            <span key={`param-${index}-${param.name}`}>
138              {param.name}: {resolveTypeName(param.type)}
139              {index + 1 !== baseSignature.parameters?.length && ', '}
140            </span>
141          ))}
142          ) {'=>'} {resolveTypeName(baseSignature.type)}
143        </>
144      );
145    } else {
146      return (
147        <>
148          {'() =>'} {resolveTypeName(baseSignature.type)}
149        </>
150      );
151    }
152  } else if (type === 'literal' && value) {
153    return `'${value}'`;
154  }
155  return 'undefined';
156};
157
158export const renderParam = ({ comment, name, type }: MethodParamData): JSX.Element => (
159  <LI key={`param-${name}`}>
160    <B>
161      {name} (<InlineCode>{resolveTypeName(type)}</InlineCode>)
162    </B>
163    <CommentTextBlock comment={comment} renderers={mdInlineRenderers} withDash />
164  </LI>
165);
166
167export type CommentTextBlockProps = {
168  comment?: CommentData;
169  renderers?: MDRenderers;
170  withDash?: boolean;
171};
172
173export const CommentTextBlock: React.FC<CommentTextBlockProps> = ({
174  comment,
175  renderers = mdRenderers,
176  withDash,
177}) => {
178  const shortText = comment?.shortText?.trim().length ? (
179    <ReactMarkdown renderers={renderers}>{comment.shortText}</ReactMarkdown>
180  ) : null;
181  const text = comment?.text?.trim().length ? (
182    <ReactMarkdown renderers={renderers}>{comment.text}</ReactMarkdown>
183  ) : null;
184
185  const example = comment?.tags?.filter(tag => tag.tag === 'example')[0];
186  const exampleText = example ? (
187    <ReactMarkdown renderers={renderers}>{`__Example:__ ${example.text}`}</ReactMarkdown>
188  ) : null;
189
190  return (
191    <>
192      {withDash && (shortText || text) ? ' - ' : null}
193      {shortText}
194      {text}
195      {exampleText}
196    </>
197  );
198};
199
200export const STYLES_OPTIONAL = css`
201  color: ${theme.text.secondary};
202  font-size: 90%;
203  padding-top: 22px;
204`;
205
206export const STYLES_SECONDARY = css`
207  color: ${theme.text.secondary};
208  font-size: 90%;
209  font-weight: 600;
210`;
211