mirror of
https://framagit.org/enfance-libre/statistiques
synced 2025-12-07 07:23:44 +00:00
feat; ajouter le support pour l'affichage des données liées
This commit is contained in:
parent
a04f080974
commit
cfb4bcc772
4 changed files with 74 additions and 29 deletions
|
|
@ -1,7 +1,9 @@
|
|||
import { BlockObjectRequest } from "@notionhq/client/build/src/api-endpoints";
|
||||
import { ValueFormatOptions } from "../../../format/ValueFormatOptions";
|
||||
import { MultiValueStatDesc } from "../../../statistiques/v2/desc/StatsDesc";
|
||||
import { BulletedListItemBlockObjectRequest } from "../blocks/BulletedListItemBlockObjectRequest";
|
||||
import {
|
||||
MultiValueStatDesc,
|
||||
StatsData,
|
||||
} from "../../../statistiques/v2/desc/StatsDesc";
|
||||
import { BulletedListItemChildren } from "../blocks/BulletedListItemChildren";
|
||||
import { createSingleValueStatListItemBlock } from "./createSingleValueStatListItemBlock";
|
||||
|
||||
|
|
@ -12,7 +14,7 @@ export type ToggleBlockObjectRequest = Extract<
|
|||
|
||||
export function createMultiValueStatListItemBlock(
|
||||
descriptor: MultiValueStatDesc,
|
||||
statValue: Record<string, number>
|
||||
statData: Record<string, StatsData>
|
||||
): ToggleBlockObjectRequest {
|
||||
return {
|
||||
toggle: {
|
||||
|
|
@ -25,7 +27,7 @@ export function createMultiValueStatListItemBlock(
|
|||
],
|
||||
children: createMultiValueStatListItemBlockChildren(
|
||||
descriptor,
|
||||
statValue
|
||||
statData
|
||||
) as BulletedListItemChildren,
|
||||
},
|
||||
};
|
||||
|
|
@ -33,14 +35,10 @@ export function createMultiValueStatListItemBlock(
|
|||
|
||||
function createMultiValueStatListItemBlockChildren(
|
||||
formatOptions: ValueFormatOptions,
|
||||
statsValues: Record<string, number>
|
||||
): BulletedListItemBlockObjectRequest[] {
|
||||
statsValues: Record<string, StatsData>
|
||||
): BlockObjectRequest[] {
|
||||
return Object.keys(statsValues).map((keyName) => {
|
||||
const statValue = statsValues[keyName];
|
||||
return createSingleValueStatListItemBlock(
|
||||
keyName,
|
||||
formatOptions,
|
||||
statValue
|
||||
);
|
||||
const statData = statsValues[keyName];
|
||||
return createSingleValueStatListItemBlock(keyName, formatOptions, statData);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,65 @@
|
|||
import { formatValue } from "../../../format/formatValue";
|
||||
import { BulletedListItemBlockObjectRequest } from "../blocks/BulletedListItemBlockObjectRequest";
|
||||
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(
|
||||
label: string,
|
||||
formatOptions: ValueFormatOptions,
|
||||
value: number
|
||||
): BulletedListItemBlockObjectRequest {
|
||||
return {
|
||||
bulleted_list_item: {
|
||||
rich_text: [
|
||||
{
|
||||
text: {
|
||||
content: label + ": " + formatValue(value, formatOptions),
|
||||
data: StatsData
|
||||
): BlockObjectRequest {
|
||||
if (isNumber(data)) {
|
||||
return {
|
||||
bulleted_list_item: {
|
||||
rich_text: [
|
||||
{
|
||||
text: {
|
||||
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,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import {
|
|||
isSingleValueStatDesc,
|
||||
isStatGroupDesc,
|
||||
StatGroupDesc,
|
||||
StatsData,
|
||||
StatsType,
|
||||
} from "../../../statistiques/v2/desc/StatsDesc";
|
||||
import { createSingleValueStatListItemBlock } from "./createSingleValueStatListItemBlock";
|
||||
|
|
@ -39,26 +40,26 @@ export function createStatGroupChildrenListItemBlock<D extends StatGroupDesc>(
|
|||
): BlockObjectRequest[] {
|
||||
return Object.keys(descriptor.stats).map((statName) => {
|
||||
const childStatDesc = descriptor.stats[statName];
|
||||
const childStatValue = stats[statName];
|
||||
const childStatData = stats[statName];
|
||||
|
||||
if (isStatGroupDesc(childStatDesc)) {
|
||||
return createStatGroupListItemBlock(
|
||||
childStatDesc,
|
||||
childStatValue as StatsType<typeof childStatDesc>
|
||||
childStatData as StatsType<typeof childStatDesc>
|
||||
);
|
||||
}
|
||||
if (isSingleValueStatDesc(childStatDesc)) {
|
||||
return createSingleValueStatListItemBlock(
|
||||
childStatDesc.label,
|
||||
childStatDesc as ValueFormatOptions,
|
||||
childStatValue as number
|
||||
childStatData as StatsData
|
||||
);
|
||||
}
|
||||
|
||||
if (isMultiValueStatDesc(childStatDesc)) {
|
||||
return createMultiValueStatListItemBlock(
|
||||
childStatDesc,
|
||||
childStatValue as Record<string, number>
|
||||
childStatData as Record<string, StatsData>
|
||||
);
|
||||
}
|
||||
throw "Mussing case";
|
||||
|
|
|
|||
|
|
@ -42,10 +42,12 @@ export function isMultiValueStatDesc(x: StatDesc): x is MultiValueStatDesc {
|
|||
return "type" in x && x.type === "multi";
|
||||
}
|
||||
|
||||
export type StatsData = number | { value: number; relatedPageIds: string[] };
|
||||
|
||||
export type StatsType<T extends StatGroupDesc> = {
|
||||
[Property in keyof T["stats"]]: T["stats"][Property] extends StatGroupDesc
|
||||
? StatsType<T["stats"][Property]>
|
||||
: T["stats"][Property] extends MultiValueStatDesc
|
||||
? Record<string, number>
|
||||
: number;
|
||||
? Record<string, StatsData>
|
||||
: StatsData;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue