feat: support arbitrary block depth in updatePageContent

This commit is contained in:
Sébastien Arod 2024-12-17 22:44:27 +01:00
parent d2b43f5706
commit d213191fc3
2 changed files with 80 additions and 15 deletions

View file

@ -16,8 +16,78 @@ export async function updatePageContent(
const blocksIdsToRemove = childrenBlocks.map((b) => b.id);
await removeBlocks(notionClient, blocksIdsToRemove);
await notionClient.blocks.children.append({
block_id: notionPageId,
children: [...newBlocks],
const useLayeredAppend = true;
if (useLayeredAppend) {
await appendBlocksByDepths(notionClient, notionPageId, newBlocks);
} else {
await notionClient.blocks.children.append({
block_id: notionPageId,
children: [...newBlocks],
});
}
}
/**
* This approach works around the depth limit of Notion API by appending children as a second step.
* @param notionClient
* @param parentBlockId
* @param blocksToAppend
*/
async function appendBlocksByDepths(notionClient: Client,
parentBlockId: string,
blocksToAppend: BlockObjectRequest[]
) {
const { blocksWithoutChildren, extractedChildren } = extractChildren(blocksToAppend);
const appendResponse = await notionClient.blocks.children.append({
block_id: parentBlockId,
children: [...blocksWithoutChildren],
});
appendResponse.results.forEach((b, index) => {
if (extractedChildren[index].length !== 0) {
appendBlocksByDepths(notionClient, b.id, extractedChildren[index]);
}
});
}
function extractChildren(blocks: BlockObjectRequest[]): {
blocksWithoutChildren: BlockObjectRequest[],
extractedChildren: BlockObjectRequest[][]
} {
const extractedChildren: BlockObjectRequest[][] = [];
const blocksWithoutChildren = blocks.map(b => {
if ("bulleted_list_item" in b) {
extractedChildren.push(b.bulleted_list_item.children || []);
return {
...b,
bulleted_list_item: {
...b.bulleted_list_item,
children: []
}};
} else if ("toggle" in b) {
extractedChildren.push(b.toggle.children|| []);
return {
...b,
toggle: {
...b.toggle,
children: []
}};
} else if ("paragraph" in b) {
extractedChildren.push(b.paragraph.children || []);
return {
...b,
paragraph: {
...b.paragraph,
children: []
}};
} else {
extractedChildren.push([]);
return b;
}
});
return {
blocksWithoutChildren,
extractedChildren: extractedChildren
};
}

View file

@ -15,7 +15,6 @@ import { filterFamillesWithOneOfEvenements } from "../filterFamillesWithOneOfEve
import { filterFamillesWithOneOfEvenementsOfType } from "../filterFamillesWithOneOfEvenementsOfType";
import { StatsPenales } from "./StatsPenales";
import { nbFamillesAvecPagesLiees } from "./nbFamillesAvecPagesLiees";
import { StatsData } from "../desc/StatsDesc";
type FamilleAvecInfoTribunaux = Famille & {
evtTribunal1?: EvenementFamille;
@ -148,9 +147,9 @@ function computeCrpc(
);
return {
nbFamilles: nbFamilles(famillesConcernees),
acceptees: nbFamilles(acceptees),
refusees: nbFamilles(refusees),
nbFamilles: nbFamillesAvecPagesLiees(famillesConcernees),
acceptees: nbFamillesAvecPagesLiees(acceptees),
refusees: nbFamillesAvecPagesLiees(refusees),
};
}
@ -171,9 +170,9 @@ function computeCompositionPenales(
);
return {
nbFamilles: nbFamilles(famillesConcernees),
acceptees: nbFamilles(acceptees),
refusees: nbFamilles(refusees),
nbFamilles: nbFamillesAvecPagesLiees(famillesConcernees),
acceptees: nbFamillesAvecPagesLiees(acceptees),
refusees: nbFamillesAvecPagesLiees(refusees),
};
}
@ -284,8 +283,4 @@ function computeIntervalProcureurTribunalCorrectionnel(
function isTribunalCorrectionnel(e: EvenementFamille): boolean {
return e.Type === "Tribunal correctionnel";
}
export function nbFamilles(familles: Famille[]): StatsData {
return familles.length;
}
}