mirror of
https://framagit.org/enfance-libre/statistiques
synced 2025-12-16 08:13:46 +00:00
73 lines
1.8 KiB
TypeScript
73 lines
1.8 KiB
TypeScript
|
|
import { BlockObjectRequest } from "@notionhq/client/build/src/api-endpoints";
|
||
|
|
import {
|
||
|
|
isStatGroupDesc,
|
||
|
|
StatDesc,
|
||
|
|
StatGroupDesc,
|
||
|
|
StatsType,
|
||
|
|
} from "../../../statistiques/v2/desc/StatsDesc";
|
||
|
|
import { formatValue } from "../../../format/formatValue";
|
||
|
|
|
||
|
|
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 type BulletedListItemBlockObjectRequest = Extract<
|
||
|
|
BlockObjectRequest,
|
||
|
|
{ bulleted_list_item: object }
|
||
|
|
>;
|
||
|
|
|
||
|
|
export type BulletedListItemChildren =
|
||
|
|
BulletedListItemBlockObjectRequest["bulleted_list_item"]["children"];
|
||
|
|
|
||
|
|
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);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
function createStatListItemBlock(
|
||
|
|
descriptor: StatDesc,
|
||
|
|
statValue: number
|
||
|
|
): BulletedListItemBlockObjectRequest {
|
||
|
|
return {
|
||
|
|
bulleted_list_item: {
|
||
|
|
rich_text: [
|
||
|
|
{
|
||
|
|
text: {
|
||
|
|
content:
|
||
|
|
descriptor.label + ": " + formatValue(statValue, descriptor),
|
||
|
|
},
|
||
|
|
},
|
||
|
|
],
|
||
|
|
},
|
||
|
|
};
|
||
|
|
}
|