statistiques/src/notion/publish/v2/createStatGroupListItemBlock.ts

47 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-09-17 16:01:16 +02:00
import {
isStatGroupDesc,
StatGroupDesc,
StatsType,
} from "../../../statistiques/v2/desc/StatsDesc";
2024-09-20 12:55:26 +02:00
import { createStatListItemBlock } from "./createStatListItemBlock";
import { BulletedListItemBlockObjectRequest } from "../blocks/BulletedListItemBlockObjectRequest";
import { BulletedListItemChildren } from "../blocks/BulletedListItemChildren";
2024-09-17 16:01:16 +02:00
export function createStatGroupListItemBlock<D extends StatGroupDesc>(
descriptor: D,
stats: StatsType<D>
): BulletedListItemBlockObjectRequest {
return {
bulleted_list_item: {
rich_text: [
{
text: {
content: descriptor.label,
},
},
],
children: createStatGroupChildrenListItemBlock(
descriptor,
stats
) as BulletedListItemChildren,
},
};
}
export function createStatGroupChildrenListItemBlock<D extends StatGroupDesc>(
descriptor: D,
stats: StatsType<D>
): BulletedListItemBlockObjectRequest[] {
return Object.keys(descriptor.stats).map((statName) => {
const childStatDesc = descriptor.stats[statName];
const childStatValue = stats[statName];
return isStatGroupDesc(childStatDesc)
? createStatGroupListItemBlock(
childStatDesc,
childStatValue as StatsType<typeof childStatDesc>
)
: createStatListItemBlock(childStatDesc, childStatValue as number);
});
}