/* eslint-disable max-len */ import { BoxProps, HeaderProps, TextProps, BadgeProps, ButtonProps, LinkProps, LabelProps, IconProps, } from '@adminjs/design-system' import { PropsWithChildren } from 'react' import { CurrentAdmin } from '../../../current-admin.interface.js' export type LayoutElement = string | Array | Array | [string, PropsWithChildren] | [PropsWithChildren, Array] | ['@Header', PropsWithChildren] | ['@H1', PropsWithChildren] | ['@H2', PropsWithChildren] | ['@H3', PropsWithChildren] | ['@H4', PropsWithChildren] | ['@H5', PropsWithChildren] | ['@Text', PropsWithChildren] | ['@Badge', PropsWithChildren] | ['@Button', PropsWithChildren] | ['@Link', PropsWithChildren] | ['@Label', PropsWithChildren] | ['@Icon', PropsWithChildren] | [string, PropsWithChildren] /** * Function returning Array used by {@link Action#layout} * * @return {Array} * @memberof Action * @alias LayoutElementFunction */ export type LayoutElementFunction = (currentAdmin?: CurrentAdmin) => Array /** * It is generated from {@link Array} passed in {@link Action#layout} * * @alias ParsedLayoutElement * @memberof ActionJSON */ export type ParsedLayoutElement = { /** List of paths to properties which should be rendered by given element */ properties: Array; /** props passed to React component which wraps elements */ props: PropsWithChildren; /** Nested layout elements */ layoutElements: Array; /** Component which should be used as a wrapper */ component: string; } const isProp = (element): boolean => !!element && typeof element === 'object' && !Array.isArray(element) const isComponentTag = (layoutElement: LayoutElement): boolean => ( Array.isArray(layoutElement) && typeof layoutElement[0] === 'string' && layoutElement[0].startsWith('@') && isProp(layoutElement[1]) ) const hasOnlyStringsProperties = function ( layoutElement: LayoutElement, ): layoutElement is [string] { return Array.isArray(layoutElement) && layoutElement.length > 0 && !isComponentTag(layoutElement) && !(layoutElement as Array).find((el) => (typeof el !== 'string')) } const hasArrayOfLayoutElements = function ( layoutElement: LayoutElement, ): layoutElement is [LayoutElement] { return Array.isArray(layoutElement) && layoutElement.length > 0 && !(layoutElement as Array).find((element) => !Array.isArray(element)) } const hasFirstStringProperty = function (layoutElement: LayoutElement): boolean { return Array.isArray(layoutElement) && typeof layoutElement[0] === 'string' && !isComponentTag(layoutElement) } const getPropertyNames = (layoutElement: LayoutElement): Array => { if (typeof layoutElement === 'string') { return [layoutElement] } if (hasOnlyStringsProperties(layoutElement)) { return layoutElement } if (hasFirstStringProperty(layoutElement)) { return [layoutElement[0]] as Array } return [] } const getInnerLayoutElements = (layoutElement: LayoutElement): Array => { // only cases like [{}, layoutElement] (whatever follows props) if (Array.isArray(layoutElement) && isProp(layoutElement[0])) { return layoutElement[1] as Array } if (hasArrayOfLayoutElements(layoutElement)) { return layoutElement } return [] } const getComponent = (layoutElement: LayoutElement): string => { if (isComponentTag(layoutElement)) { return (layoutElement[0] as string).slice(1) } return 'Box' } const getProps = (layoutElement: LayoutElement): BoxProps => { if (Array.isArray(layoutElement) && layoutElement.length) { const boxProps = (layoutElement as Array).find(isProp) return boxProps as unknown as BoxProps || {} } return {} } export const layoutElementParser = (layoutElement: LayoutElement): ParsedLayoutElement => { const props = getProps(layoutElement) const innerLayoutElements = getInnerLayoutElements(layoutElement) const properties = getPropertyNames(layoutElement) const component = getComponent(layoutElement) return { props, layoutElements: innerLayoutElements.map((el) => layoutElementParser(el)), properties, component, } } export default layoutElementParser /** * @load layout-element.doc.md * @name LayoutElement * @typedef {String | Array} LayoutElement * @memberof Action * @alias LayoutElement */