2022-04-24 00:35:53 +00:00
|
|
|
import style from '../styles/lists.module.css';
|
|
|
|
import React, { ReactElement } from 'react';
|
|
|
|
|
|
|
|
interface listItem {
|
|
|
|
children?: listItem[] | string[];
|
|
|
|
url?: string;
|
|
|
|
title: string;
|
|
|
|
description?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function toListItem(record: Record<string, any>): 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 {
|
2022-04-24 04:27:51 +00:00
|
|
|
children = [...lchildren, ...schildren.map((s: string): listItem => {
|
2022-04-24 00:35:53 +00:00
|
|
|
return { title: s };
|
2022-04-24 04:27:51 +00:00
|
|
|
})];
|
2022-04-24 00:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
title: record.title,
|
|
|
|
url: record.url,
|
2022-04-24 04:27:51 +00:00
|
|
|
children: children.length ? children : undefined,
|
2022-04-24 00:35:53 +00:00
|
|
|
description: record.description,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function mapChild(obj: listItem | string, level: number) {
|
|
|
|
if (typeof obj === 'string') {
|
|
|
|
if (obj === '')
|
|
|
|
return <></>
|
|
|
|
return <span className={style.listItem}>{obj}</span>
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj.title === '')
|
|
|
|
return <></>
|
|
|
|
|
|
|
|
if (obj.url)
|
|
|
|
return <span className={style.listItem}><a href={obj.url}>{obj.title}</a></span>
|
|
|
|
|
|
|
|
if (!obj.children)
|
|
|
|
return <span className={style.listItem}>{obj.title}</span>
|
|
|
|
|
|
|
|
let title: ReactElement;
|
|
|
|
|
|
|
|
if (level >= 0 && level <= 4)
|
|
|
|
title = React.createElement(`h${level + 2}`, {}, obj.title);
|
|
|
|
else
|
|
|
|
title = React.createElement('strong', {}, obj.title);
|
|
|
|
|
|
|
|
return (
|
2022-04-27 09:10:49 +00:00
|
|
|
<section className={level < 5 ? 'block' : ''}>
|
2022-04-24 00:35:53 +00:00
|
|
|
{title}
|
|
|
|
{obj.description ? <p>{obj.description}</p> : <></>}
|
|
|
|
<div>
|
|
|
|
{obj.children.map(l => mapChild(l, level + 1))}
|
|
|
|
</div>
|
2022-04-27 09:10:49 +00:00
|
|
|
</section>
|
2022-04-24 00:35:53 +00:00
|
|
|
);
|
|
|
|
}
|