Skip to content

Commit 6b08954

Browse files
hkiratclaude
andcommitted
fix: normalize Notion recordMap to fix toggles and crashes
The Notion API now returns blocks with nested value.value structure and role-only entries that crash react-notion-x. Normalize the recordMap before rendering — skip role-only stubs, unwrap nested blocks, and preserve all valid blocks including toggle children. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 109c526 commit 6b08954

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

apps/web/app/pdf/[...pdfId]/page.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ import { authOptions } from "../../../lib/auth";
99

1010
const notion = new NotionAPI();
1111

12+
function normalizeRecordMap(recordMap: any) {
13+
if (!recordMap?.block) return recordMap;
14+
const normalizedBlock: any = {};
15+
for (const [key, block] of Object.entries(recordMap.block) as any) {
16+
if (!block?.value) {
17+
continue;
18+
}
19+
if (block.value.value?.type) {
20+
normalizedBlock[key] = { ...block, value: block.value.value };
21+
} else {
22+
normalizedBlock[key] = block;
23+
}
24+
}
25+
return { ...recordMap, block: normalizedBlock };
26+
}
27+
1228
export default async function TrackComponent({ params }: { params: { pdfId: string[] } }) {
1329
const trackId: string = params.pdfId[0] || "";
1430
const problemId = params.pdfId[1];
@@ -27,7 +43,7 @@ export default async function TrackComponent({ params }: { params: { pdfId: stri
2743
// notionRecordMaps = await notion.getPage(problemDetails.notionDocId);
2844
notionRecordMaps = await Promise.all(
2945
trackDetails.problems.map(
30-
async (problem: any) => await notion.getPage((await getProblem(problem.id))?.notionDocId!)
46+
async (problem: any) => normalizeRecordMap(await notion.getPage((await getProblem(problem.id))?.notionDocId!))
3147
)
3248
);
3349
}

apps/web/app/tracks/[...trackIds]/page.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ import { cache } from "react";
66
import { LessonView } from "../../../components/LessonView";
77

88
const notion = new NotionAPI();
9+
10+
function normalizeRecordMap(recordMap: any) {
11+
if (!recordMap?.block) return recordMap;
12+
const normalizedBlock: any = {};
13+
for (const [key, block] of Object.entries(recordMap.block) as any) {
14+
if (!block?.value) {
15+
continue;
16+
}
17+
if (block.value.value?.type) {
18+
normalizedBlock[key] = { ...block, value: block.value.value };
19+
} else {
20+
normalizedBlock[key] = block;
21+
}
22+
}
23+
return { ...recordMap, block: normalizedBlock };
24+
}
925
export const dynamic = "auto";
1026
// Dynamic Metadata
1127
export async function generateMetadata({ params }: { params: { trackIds: string[] } }) {
@@ -80,7 +96,7 @@ export default async function TrackComponent({ params }: { params: { trackIds: s
8096
}
8197

8298
if (problemDetails?.notionDocId) {
83-
notionRecordMap = await notion.getPage(problemDetails.notionDocId);
99+
notionRecordMap = normalizeRecordMap(await notion.getPage(problemDetails.notionDocId));
84100
}
85101

86102
if (trackDetails && problemDetails) {

0 commit comments

Comments
 (0)