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

66 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-09-17 16:01:16 +02:00
import {
isMultiValueStatDesc,
isSingleValueStatDesc,
2024-09-17 16:01:16 +02:00
isStatGroupDesc,
StatGroupDesc,
StatsType,
} from "../../../statistiques/v2/desc/StatsDesc";
import { createSingleValueStatListItemBlock } from "./createSingleValueStatListItemBlock";
2024-09-20 12:55:26 +02:00
import { BulletedListItemBlockObjectRequest } from "../blocks/BulletedListItemBlockObjectRequest";
import { BulletedListItemChildren } from "../blocks/BulletedListItemChildren";
import { createMultiValueStatListItemBlock } from "./createMultiValueStatListItemBlock";
import { ValueFormatOptions } from "../../../format/ValueFormatOptions";
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];
if (isStatGroupDesc(childStatDesc)) {
return createStatGroupListItemBlock(
childStatDesc,
childStatValue as StatsType<typeof childStatDesc>
);
}
if (isSingleValueStatDesc(childStatDesc)) {
return createSingleValueStatListItemBlock(
childStatDesc.label,
childStatDesc as ValueFormatOptions,
childStatValue as number
);
}
if (isMultiValueStatDesc(childStatDesc)) {
return createMultiValueStatListItemBlock(
childStatDesc,
childStatValue as Record<string, number>
);
}
throw "Mussing case";
2024-09-17 16:01:16 +02:00
});
}