mirror of
https://framagit.org/enfance-libre/statistiques
synced 2025-12-15 17:53:45 +00:00
65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
import {
|
|
isMultiValueStatDesc,
|
|
isSingleValueStatDesc,
|
|
isStatGroupDesc,
|
|
StatGroupDesc,
|
|
StatsType,
|
|
} from "../../../statistiques/v2/desc/StatsDesc";
|
|
import { createSingleValueStatListItemBlock } from "./createSingleValueStatListItemBlock";
|
|
import { BulletedListItemBlockObjectRequest } from "../blocks/BulletedListItemBlockObjectRequest";
|
|
import { BulletedListItemChildren } from "../blocks/BulletedListItemChildren";
|
|
import { createMultiValueStatListItemBlock } from "./createMultiValueStatListItemBlock";
|
|
import { ValueFormatOptions } from "../../../format/ValueFormatOptions";
|
|
|
|
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";
|
|
});
|
|
}
|