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 { InternalLink } 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 }) => <InternalLink href={href}>{children}</InternalLink>,
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            <InternalLink href={`#${name.toLowerCase()}`} key={`type-link-${name}`}>
84              {name}
85            </InternalLink>
86          );
87        }
88      }
89    } else {
90      return name;
91    }
92  } else if (elementType?.name) {
93    if (type === 'array') {
94      return elementType.name + '[]';
95    }
96    return elementType.name + type;
97  } else if (type === 'union' && types?.length) {
98    return types
99      .map((t: TypeDefinitionTypesData) =>
100        t.type === 'reference' ? (
101          <InternalLink href={`#${t.name?.toLowerCase()}`} key={`type-link-${t.name}`}>
102            {t.name}
103          </InternalLink>
104        ) : t.type === 'array' ? (
105          `${t.elementType?.name}[]`
106        ) : (
107          `${t.name || t.value}`
108        )
109      )
110      .map((valueToRender, index) => (
111        <>
112          {valueToRender}
113          {index + 1 !== types.length && ' | '}
114        </>
115      ));
116  } else if (declaration?.signatures) {
117    const baseSignature = declaration.signatures[0];
118    if (baseSignature?.parameters?.length) {
119      return (
120        <>
121          (
122          {baseSignature.parameters?.map((param, index) => (
123            <span key={`param-${index}-${param.name}`}>
124              {param.name}: {resolveTypeName(param.type)}
125              {index + 1 !== baseSignature.parameters.length && ', '}
126            </span>
127          ))}
128          ) {'=>'} {resolveTypeName(baseSignature.type)}
129        </>
130      );
131    } else {
132      return `() => ${resolveTypeName(baseSignature.type)}`;
133    }
134  }
135  return 'undefined';
136};
137
138export const renderParam = ({ comment, name, type }: MethodParamData): JSX.Element => (
139  <LI key={`param-${name}`}>
140    <B>
141      {name} (<InlineCode>{resolveTypeName(type)}</InlineCode>)
142    </B>
143    <CommentTextBlock comment={comment} renderers={mdInlineRenderers} withDash />
144  </LI>
145);
146
147export type CommentTextBlockProps = {
148  comment?: CommentData;
149  renderers?: MDRenderers;
150  withDash?: boolean;
151};
152
153export const CommentTextBlock: React.FC<CommentTextBlockProps> = ({
154  comment,
155  renderers = mdRenderers,
156  withDash,
157}) => {
158  const shortText = comment?.shortText?.trim().length ? (
159    <ReactMarkdown renderers={renderers}>{comment.shortText}</ReactMarkdown>
160  ) : null;
161  const text = comment?.text?.trim().length ? (
162    <ReactMarkdown renderers={renderers}>{comment.text}</ReactMarkdown>
163  ) : null;
164  return (
165    <>
166      {withDash && (shortText || text) ? ' - ' : null}
167      {shortText}
168      {text}
169    </>
170  );
171};
172
173export const STYLES_OPTIONAL = css`
174  color: ${theme.text.secondary};
175  font-size: 90%;
176  padding-top: 22px;
177`;
178
179export const STYLES_SECONDARY = css`
180  color: ${theme.text.secondary};
181  font-size: 90%;
182  font-weight: 600;
183`;
184