feat: header d'info sur chaque page

wip-related-pages
Sébastien Arod 2024-09-17 16:01:16 +02:00
parent a1dfc7b087
commit 275911b27c
4 changed files with 120 additions and 70 deletions

View File

@ -15,6 +15,7 @@ import { statsSocialesDesc } from "./statistiques/v2/sociales/StatsSociales";
import { checkDbSchemas } from "./notion/fetch/schemaCheck/checkDbSchemas";
import { computeSequencEvtPenalSankeyData } from "./statistiques/v2/penales/computeSankeyData";
import { sankeyDataToMermaidDiagram } from "./statistiques/v2/sankey/sankeyDataToMermaidDiagram";
import { formatDate } from "date-fns";
type ProcessOptions = {
dryRun: boolean;
@ -111,21 +112,29 @@ function buildProcessOptions(): ProcessOptions {
console.log("Publishing statistics...");
await publishStatisticsToNotion(elStats, currentDate, notionClient);
const header = `
Note: Sauf mention contraire les statistiques sont basées sur les familles résistantes & ex-résistantes.
Dernière mise à jour le ${formatDate(currentDate, "dd/MM/yyyy à HH:mm")}
`;
await publishStatsToPage(
notionClient,
"313751fb-daed-4b33-992f-c86d7ac2de37",
header,
statsGeneralesDesc,
statsGenerales
);
await publishStatsToPage(
notionClient,
"969eac5c-a4eb-49d4-b4ad-c341c9c4c785",
header,
statsPenalesDesc,
statsPenales
);
await publishStatsToPage(
notionClient,
"68a8c03a-a4a2-4ff3-b044-7c8dbbdc28ce",
header,
statsSocialesDesc,
statsSociales
);

View File

@ -0,0 +1,72 @@
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),
},
},
],
},
};
}

View File

@ -1,94 +1,40 @@
import { Client, isFullBlock } from "@notionhq/client";
import { BlockObjectRequest } from "@notionhq/client/build/src/api-endpoints";
import { formatValue } from "../../../format/formatValue";
import { Client } from "@notionhq/client";
import {
isStatGroupDesc,
StatDesc,
StatGroupDesc,
StatsType,
} from "../../../statistiques/v2/desc/StatsDesc";
import { listAllChildrenBlocks } from "../../utils/listAllChildrenBlocks";
import { removeBlocks } from "../../utils/removeBlocks";
import { createStatGroupChildrenListItemBlock } from "./createStatGroupListItemBlock";
import { updatePageContent } from "./updatePageContent";
import { BlockObjectRequest } from "@notionhq/client/build/src/api-endpoints";
export async function publishStatsToPage<D extends StatGroupDesc>(
notionClient: Client,
statsPageId: string,
pageHeader: string,
descriptor: D,
stats: StatsType<D>
) {
const childrenBlocks = (
await listAllChildrenBlocks(notionClient, {
block_id: statsPageId,
})
).filter(isFullBlock);
const blocksIdsToRemove = childrenBlocks.map((b) => b.id);
await removeBlocks(notionClient, blocksIdsToRemove);
const statsBlocks = createStatGroupChildrenListItemBlock(descriptor, stats);
const newBlocks = createStatGroupChildrenListItemBlock(descriptor, stats);
await notionClient.blocks.children.append({
block_id: statsPageId,
children: [...newBlocks],
});
const textBlock = createParagraphBlock(pageHeader);
await updatePageContent(notionClient, statsPageId, [
textBlock,
...statsBlocks,
]);
}
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,
},
};
}
type BulletedListItemBlockObjectRequest = Extract<
type ParagraphBlockObjectRequest = Extract<
BlockObjectRequest,
{ bulleted_list_item: object }
{ paragraph: unknown }
>;
type BulletedListItemChildren =
BulletedListItemBlockObjectRequest["bulleted_list_item"]["children"];
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 {
function createParagraphBlock(content: string): ParagraphBlockObjectRequest {
return {
bulleted_list_item: {
paragraph: {
rich_text: [
{
text: {
content:
descriptor.label + ": " + formatValue(statValue, descriptor),
content: content,
},
},
],

View File

@ -0,0 +1,23 @@
import { Client, isFullBlock } from "@notionhq/client";
import { BlockObjectRequest } from "@notionhq/client/build/src/api-endpoints";
import { listAllChildrenBlocks } from "../../utils/listAllChildrenBlocks";
import { removeBlocks } from "../../utils/removeBlocks";
export async function updatePageContent(
notionClient: Client,
notionPageId: string,
newBlocks: BlockObjectRequest[]
) {
const childrenBlocks = (
await listAllChildrenBlocks(notionClient, {
block_id: notionPageId,
})
).filter(isFullBlock);
const blocksIdsToRemove = childrenBlocks.map((b) => b.id);
await removeBlocks(notionClient, blocksIdsToRemove);
await notionClient.blocks.children.append({
block_id: notionPageId,
children: [...newBlocks],
});
}