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