import style from '../styles/lists.module.css'; import React, { ReactElement } from 'react'; export interface listItem { [x: string]: any; children?: listItem[] | string[]; url?: string; type?: string; title: string; description?: string; }; export function toListItem(record: Record): listItem | null { if (!record.title) return null; let children: listItem[] | string[] = []; if (Array.isArray(record.children) && record.children.length) { let lchildren: listItem[] = []; let schildren: string[] = []; for (const child of record.children) { if (typeof child === 'string') { schildren.push(child); continue; } const lChild = toListItem(child); if (lChild) lchildren.push(lChild); } if (!lchildren.length) { children = schildren; } else { children = [...lchildren, ...schildren.map((s: string): listItem => { return { title: s }; })]; } } return Object.assign(record, { title: record.title, url: record.url, children: children.length ? children : undefined, type: record.type?.length ? record.type : undefined, description: record.description, }); } const s = { "af": 123, "asdf" : 123 } export function mapChild( obj: listItem | string, level: number, typeMap? : Record JSX.Element> ) { if (typeof obj === 'string') { if (obj === '') return <> return {obj} } if (obj.title === '') return <> const desc = obj.description ? {obj.description} : <>; if (obj.url) return ( <> {obj.title} {desc} ); if (!obj.children) { let cb; if (obj.type && typeMap) { cb = typeMap[obj.type] } return cb ? cb(obj) : (<>{obj.title}{desc}); } let title: ReactElement; if (level >= 0 && level <= 4) title = React.createElement(`h${level + 2}`, {}, obj.title); else title = React.createElement('strong', {}, obj.title); return (
{title} {obj.description ?

{obj.description}

: <>}
{obj.children.map(l => mapChild(l, level + 1, typeMap))}
); }