Skip to content

Commit fd28633

Browse files
committed
Improve typings and simplify API
1 parent 4eee074 commit fd28633

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

example/pages/index.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { NotionRenderer } from "react-notion";
1+
import { NotionRenderer, LoadPageChunkData } from "react-notion";
22
import pageData from "../mock/page.json";
3+
4+
const {
5+
recordMap: { block: blockMap }
6+
} = pageData as LoadPageChunkData;
7+
38
const Home = () => (
4-
<div style={{ maxWidth: 760, marginLeft: "auto", marginRight: "auto" }}>
5-
<NotionRenderer
6-
level={0}
7-
blockMap={(pageData.recordMap.block as any) || []}
8-
currentID={"79c956db-2466-4214-8f15-fcfbf12b14d3"}
9-
/>
9+
<div style={{ maxWidth: 760, margin: "0 auto" }}>
10+
<NotionRenderer blockMap={blockMap} />
1011
</div>
1112
);
1213

src/block.tsx

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from "react";
2-
import { LoadPageChunkData, DecorationType } from "./types";
2+
import { DecorationType, BlockMapType, BlockType } from "./types";
33
import Asset from "./components/asset";
44
import Code from "./components/code";
55

@@ -43,7 +43,7 @@ export const renderChildText = (properties: DecorationType[]) => {
4343
};
4444

4545
interface Block {
46-
block: LoadPageChunkData["recordMap"]["block"][""];
46+
block: BlockType;
4747
level: number;
4848
}
4949

@@ -130,7 +130,7 @@ export const Block: React.FC<Block> = props => {
130130
};
131131

132132
interface ChildProps {
133-
blockMap: LoadPageChunkData["recordMap"]["block"];
133+
blockMap: BlockMapType;
134134
level: number;
135135
ids: string[];
136136
}
@@ -235,26 +235,29 @@ export const Child: React.FC<ChildProps> = props => {
235235
};
236236

237237
interface NotionProps {
238-
blockMap: LoadPageChunkData["recordMap"]["block"];
239-
currentID: string;
240-
level: number;
238+
blockMap: BlockMapType;
239+
currentID?: string;
240+
level?: number;
241241
}
242242

243-
export const NotionRenderer: React.FC<NotionProps> = props => {
244-
const currentBlock = props.blockMap[props.currentID];
243+
export const NotionRenderer: React.FC<NotionProps> = ({
244+
level = 0,
245+
currentID,
246+
blockMap
247+
}) => {
248+
const id = currentID || Object.keys(blockMap)[0];
249+
const currentBlock = blockMap[id];
245250

246-
const renderChildren = !(
247-
currentBlock?.value?.type === "page" && props.level > 0
248-
);
251+
const renderChildren = !(currentBlock?.value?.type === "page" && level > 0);
249252

250253
return (
251254
<>
252-
<Block level={props.level} block={currentBlock} />
255+
<Block level={level} block={currentBlock} />
253256
{currentBlock?.value?.content && renderChildren && (
254257
<Child
255-
level={props.level}
258+
level={level}
256259
ids={currentBlock?.value?.content}
257-
blockMap={props.blockMap}
260+
blockMap={blockMap}
258261
/>
259262
)}
260263
</>

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { NotionRenderer } from "./block";
2+
export * from "./types";

src/types.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export interface ContentValueType extends BaseValueType {
160160
block_aspect_ratio: number;
161161
block_preserve_scale: boolean;
162162
};
163-
file_ids: string[];
163+
file_ids?: string[];
164164
}
165165

166166
interface ImageValueType extends ContentValueType {
@@ -216,9 +216,8 @@ export interface NotionUserType {
216216
given_name: string;
217217
family_name: string;
218218
profile_photo: string;
219-
onboarding_complete: boolean;
220-
mobile_onboarding_complete: boolean;
221-
clipper_onboarding_complete: boolean;
219+
onboarding_completed: boolean;
220+
mobile_onboarding_completed: boolean;
222221
};
223222
}
224223

@@ -231,6 +230,8 @@ export interface RecordMapType {
231230
};
232231
}
233232

233+
export type BlockMapType = LoadPageChunkData["recordMap"]["block"];
234+
234235
export interface LoadPageChunkData {
235236
recordMap: RecordMapType;
236237
cursor: {

0 commit comments

Comments
 (0)