import { Fragment } from 'react'; import { APIDataType } from '~/components/plugins/api/APIDataType'; import { PropData, TypeDeclarationContentData, TypeDefinitionData, TypeGeneralData, TypeSignaturesData, } from '~/components/plugins/api/APIDataTypes'; import { APISectionDeprecationNote } from '~/components/plugins/api/APISectionDeprecationNote'; import { APISectionPlatformTags } from '~/components/plugins/api/APISectionPlatformTags'; import { resolveTypeName, renderFlags, CommentTextBlock, parseCommentContent, renderTypeOrSignatureType, getCommentOrSignatureComment, getTagData, renderParams, ParamsTableHeadRow, renderDefaultValue, renderIndexSignature, STYLES_APIBOX, getTagNamesList, H3Code, getCommentContent, } from '~/components/plugins/api/APISectionUtils'; import { Cell, Row, Table } from '~/ui/components/Table'; import { H2, BOLD, P, CODE, MONOSPACE } from '~/ui/components/Text'; export type APISectionTypesProps = { data: TypeGeneralData[]; }; const defineLiteralType = (types: TypeDefinitionData[]): JSX.Element | null => { const uniqueTypes = Array.from( new Set(types.map((t: TypeDefinitionData) => t.value && typeof t.value)) ); if (uniqueTypes.length === 1 && uniqueTypes.filter(Boolean).length === 1) { return ( <> {uniqueTypes[0]} {' - '} ); } return null; }; const renderTypeDeclarationTable = ( { children, indexSignature, comment }: TypeDeclarationContentData, index?: number ): JSX.Element => ( child.name).join('-')}`}> {index && index > 0 ?
: undefined} {children?.map(renderTypePropertyRow)} {indexSignature?.parameters && indexSignature.parameters.map(renderTypePropertyRow)}
); const renderTypePropertyRow = ({ name, flags, type, comment, defaultValue, signatures, kind, }: PropData): JSX.Element => { const defaultTag = getTagData('default', comment); const initValue = parseCommentContent( defaultValue || (defaultTag ? getCommentContent(defaultTag.content) : undefined) ); const commentData = getCommentOrSignatureComment(comment, signatures); const hasDeprecationNote = Boolean(getTagData('deprecated', comment)); return ( {name} {renderFlags(flags, initValue)} {kind && renderIndexSignature(kind)} {renderTypeOrSignatureType(type, signatures, true)} ); }; const renderType = ({ name, comment, type, typeParameter, }: TypeGeneralData): JSX.Element | undefined => { if (type.declaration) { // Object Types return (
{name} {type.declaration.signatures ? '()' : ''} {type.declaration.children && renderTypeDeclarationTable(type.declaration)} {type.declaration.signatures ? type.declaration.signatures.map(({ parameters, comment }: TypeSignaturesData) => (
{parameters && renderParams(parameters)}
)) : null}
); } else if (type.types && ['union', 'intersection'].includes(type.type)) { const literalTypes = type.types.filter((t: TypeDefinitionData) => ['literal', 'intrinsic', 'reference', 'tuple'].includes(t.type) ); const propTypes = type.types.filter((t: TypeDefinitionData) => t.type === 'reflection'); if (propTypes.length) { return (
{name} {type.type === 'intersection' ? ( <>

{type.types .filter(type => ['reference', 'union', 'intersection'].includes(type.type)) .map(validType => ( {resolveTypeName(validType)}{' '} ))} extended by:


) : null} {propTypes.map( (propType, index) => propType.declaration && renderTypeDeclarationTable(propType.declaration, index) )}
); } else if (literalTypes.length) { return (
{name}

{defineLiteralType(literalTypes)} Acceptable values are:{' '} {literalTypes.map((lt, index) => ( {resolveTypeName(lt)} {index + 1 !== literalTypes.length ? ', ' : '.'} ))}

); } } else if ((type.name === 'Record' && type.typeArguments) || type.type === 'reference') { return (
{name}
Type:
); } else if (type.type === 'intrinsic') { return (
{name}

Type: {type.name}

); } else if (type.type === 'conditional' && type.checkType) { return (
{name}<{type.checkType.name}>

Generic: {type.checkType.name} {typeParameter && <> extends {resolveTypeName(typeParameter[0].type)}}

Type: {type.checkType.name} {typeParameter && <> extends {type.extendsType && resolveTypeName(type.extendsType)}} {' ? '} {type.trueType && resolveTypeName(type.trueType)} {' : '} {type.falseType && resolveTypeName(type.falseType)}

); } return undefined; }; const APISectionTypes = ({ data }: APISectionTypesProps) => data?.length ? ( <>

Types

{data.map(renderType)} ) : null; export default APISectionTypes;