feat; ajouter le support pour l'affichage des données liées

This commit is contained in:
Sébastien Arod 2024-12-17 17:39:43 +01:00
parent a04f080974
commit cfb4bcc772
4 changed files with 74 additions and 29 deletions

View file

@ -1,7 +1,9 @@
import { BlockObjectRequest } from "@notionhq/client/build/src/api-endpoints"; import { BlockObjectRequest } from "@notionhq/client/build/src/api-endpoints";
import { ValueFormatOptions } from "../../../format/ValueFormatOptions"; import { ValueFormatOptions } from "../../../format/ValueFormatOptions";
import { MultiValueStatDesc } from "../../../statistiques/v2/desc/StatsDesc"; import {
import { BulletedListItemBlockObjectRequest } from "../blocks/BulletedListItemBlockObjectRequest"; MultiValueStatDesc,
StatsData,
} from "../../../statistiques/v2/desc/StatsDesc";
import { BulletedListItemChildren } from "../blocks/BulletedListItemChildren"; import { BulletedListItemChildren } from "../blocks/BulletedListItemChildren";
import { createSingleValueStatListItemBlock } from "./createSingleValueStatListItemBlock"; import { createSingleValueStatListItemBlock } from "./createSingleValueStatListItemBlock";
@ -12,7 +14,7 @@ export type ToggleBlockObjectRequest = Extract<
export function createMultiValueStatListItemBlock( export function createMultiValueStatListItemBlock(
descriptor: MultiValueStatDesc, descriptor: MultiValueStatDesc,
statValue: Record<string, number> statData: Record<string, StatsData>
): ToggleBlockObjectRequest { ): ToggleBlockObjectRequest {
return { return {
toggle: { toggle: {
@ -25,7 +27,7 @@ export function createMultiValueStatListItemBlock(
], ],
children: createMultiValueStatListItemBlockChildren( children: createMultiValueStatListItemBlockChildren(
descriptor, descriptor,
statValue statData
) as BulletedListItemChildren, ) as BulletedListItemChildren,
}, },
}; };
@ -33,14 +35,10 @@ export function createMultiValueStatListItemBlock(
function createMultiValueStatListItemBlockChildren( function createMultiValueStatListItemBlockChildren(
formatOptions: ValueFormatOptions, formatOptions: ValueFormatOptions,
statsValues: Record<string, number> statsValues: Record<string, StatsData>
): BulletedListItemBlockObjectRequest[] { ): BlockObjectRequest[] {
return Object.keys(statsValues).map((keyName) => { return Object.keys(statsValues).map((keyName) => {
const statValue = statsValues[keyName]; const statData = statsValues[keyName];
return createSingleValueStatListItemBlock( return createSingleValueStatListItemBlock(keyName, formatOptions, statData);
keyName,
formatOptions,
statValue
);
}); });
} }

View file

@ -1,21 +1,65 @@
import { formatValue } from "../../../format/formatValue"; import { formatValue } from "../../../format/formatValue";
import { BulletedListItemBlockObjectRequest } from "../blocks/BulletedListItemBlockObjectRequest";
import { ValueFormatOptions } from "../../../format/ValueFormatOptions"; import { ValueFormatOptions } from "../../../format/ValueFormatOptions";
import { StatsData } from "../../../statistiques/v2/desc/StatsDesc";
import { isNumber } from "lodash";
import { BlockObjectRequest } from "@notionhq/client/build/src/api-endpoints";
export function createSingleValueStatListItemBlock( export function createSingleValueStatListItemBlock(
label: string, label: string,
formatOptions: ValueFormatOptions, formatOptions: ValueFormatOptions,
value: number data: StatsData
): BulletedListItemBlockObjectRequest { ): BlockObjectRequest {
if (isNumber(data)) {
return { return {
bulleted_list_item: { bulleted_list_item: {
rich_text: [ rich_text: [
{ {
text: { text: {
content: label + ": " + formatValue(value, formatOptions), content: label + ": " + formatValue(data, formatOptions),
}, },
}, },
], ],
}, },
}; };
} else {
return {
toggle: {
rich_text: [
{
text: {
content: label + ": " + formatValue(data.value, formatOptions),
},
},
],
children: [
{
paragraph: {
rich_text: [
{
text: {
content: "Page concernées:",
},
},
...data.relatedPageIds.map((pageId) =>
createPageMentionRichTextElement(pageId)
),
],
},
},
],
},
};
}
}
function createPageMentionRichTextElement(pageId: string): {
mention: { page: { id: string } };
} {
return {
mention: {
page: {
id: pageId,
},
},
};
} }

View file

@ -3,6 +3,7 @@ import {
isSingleValueStatDesc, isSingleValueStatDesc,
isStatGroupDesc, isStatGroupDesc,
StatGroupDesc, StatGroupDesc,
StatsData,
StatsType, StatsType,
} from "../../../statistiques/v2/desc/StatsDesc"; } from "../../../statistiques/v2/desc/StatsDesc";
import { createSingleValueStatListItemBlock } from "./createSingleValueStatListItemBlock"; import { createSingleValueStatListItemBlock } from "./createSingleValueStatListItemBlock";
@ -39,26 +40,26 @@ export function createStatGroupChildrenListItemBlock<D extends StatGroupDesc>(
): BlockObjectRequest[] { ): BlockObjectRequest[] {
return Object.keys(descriptor.stats).map((statName) => { return Object.keys(descriptor.stats).map((statName) => {
const childStatDesc = descriptor.stats[statName]; const childStatDesc = descriptor.stats[statName];
const childStatValue = stats[statName]; const childStatData = stats[statName];
if (isStatGroupDesc(childStatDesc)) { if (isStatGroupDesc(childStatDesc)) {
return createStatGroupListItemBlock( return createStatGroupListItemBlock(
childStatDesc, childStatDesc,
childStatValue as StatsType<typeof childStatDesc> childStatData as StatsType<typeof childStatDesc>
); );
} }
if (isSingleValueStatDesc(childStatDesc)) { if (isSingleValueStatDesc(childStatDesc)) {
return createSingleValueStatListItemBlock( return createSingleValueStatListItemBlock(
childStatDesc.label, childStatDesc.label,
childStatDesc as ValueFormatOptions, childStatDesc as ValueFormatOptions,
childStatValue as number childStatData as StatsData
); );
} }
if (isMultiValueStatDesc(childStatDesc)) { if (isMultiValueStatDesc(childStatDesc)) {
return createMultiValueStatListItemBlock( return createMultiValueStatListItemBlock(
childStatDesc, childStatDesc,
childStatValue as Record<string, number> childStatData as Record<string, StatsData>
); );
} }
throw "Mussing case"; throw "Mussing case";

View file

@ -42,10 +42,12 @@ export function isMultiValueStatDesc(x: StatDesc): x is MultiValueStatDesc {
return "type" in x && x.type === "multi"; return "type" in x && x.type === "multi";
} }
export type StatsData = number | { value: number; relatedPageIds: string[] };
export type StatsType<T extends StatGroupDesc> = { export type StatsType<T extends StatGroupDesc> = {
[Property in keyof T["stats"]]: T["stats"][Property] extends StatGroupDesc [Property in keyof T["stats"]]: T["stats"][Property] extends StatGroupDesc
? StatsType<T["stats"][Property]> ? StatsType<T["stats"][Property]>
: T["stats"][Property] extends MultiValueStatDesc : T["stats"][Property] extends MultiValueStatDesc
? Record<string, number> ? Record<string, StatsData>
: number; : StatsData;
}; };