Skip to content

Commit c152892

Browse files
committed
fix: 增加了透传的类型 & 增加了找不到组件的警告
1 parent 8c7465d commit c152892

File tree

7 files changed

+139
-40
lines changed

7 files changed

+139
-40
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# @july_cm/react-notion
2+
3+
4+
## 工具函数
5+
6+
### isCompleteBlock
7+
8+
检查是否是完整的 Block
9+
10+
```typescript
11+
type isCompleteBlock = (block: PartialBlockObjectResponse | BlockObjectResponse) => boolean;
12+
```
13+
14+
### isCompletePageObject
15+
16+
检查是否是完整的 Page
17+
18+
```typescript
19+
type isCompletePageObject = (page:
20+
| PageObjectResponse
21+
| PartialPageObjectResponse
22+
| PartialDatabaseObjectResponse
23+
| DatabaseObjectResponse) => boolean;
24+
```

src/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,26 @@ export { Notion } from './rsc';
22
export { NotionRenderProvider } from './provider';
33
export { serialize } from './serialize';
44

5+
/**
6+
* 工具函数
7+
*/
8+
export { isCompleteBlock, isCompletePageObject } from './utils';
9+
510
/**
611
* 透传
712
*/
813
export { Client } from '@notionhq/client';
914
/**
1015
* 类型透传
1116
*/
12-
export type { ParagraphBlockObjectResponse, RichTextItemResponse } from './types';
17+
export type {
18+
PageObjectResponse,
19+
PartialPageObjectResponse,
20+
HeadingBlockObjectResponse,
21+
ParagraphBlockObjectResponse,
22+
BulletedListItemBlockObjectResponse,
23+
ColumnBlockObjectResponse,
24+
QuoteBlockObjectResponse,
25+
CodeBlockObjectResponse,
26+
RichTextItemResponse,
27+
} from './types';

src/provider.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
'use client';
22
import React, { createContext, useContext, useMemo } from 'react';
33

4+
import { CustomComponents } from './types';
5+
46
const context = createContext({} as any);
57

68
interface NotionProviderProps {
7-
components: Record<string, any>;
9+
components: CustomComponents;
810
}
911

1012
export const NotionRenderProvider: React.FC<React.PropsWithChildren<NotionProviderProps>> = ({

src/render.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const Render: React.FC<RenderProps> = ({ data }) => {
1515
{data.map((block) => {
1616
const children = block.has_children ? <Render data={block.children} /> : undefined;
1717
const Comp = components[block.type] ?? React.Fragment;
18+
if (!components[block.type]) {
19+
console.warn(`[@july_cm/react-notion]: Can not found type '${block.type}' component`);
20+
}
1821

1922
return <Comp key={block.id} block={block} children={children} />;
2023
})}

src/types.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import React from 'react';
2+
3+
import { Client } from '@notionhq/client';
4+
5+
import type {
6+
PartialBlockObjectResponse,
7+
BlockObjectResponse,
8+
DatabaseObjectResponse,
9+
PartialDatabaseObjectResponse,
10+
PageObjectResponse,
11+
PartialPageObjectResponse,
12+
Heading1BlockObjectResponse,
13+
Heading2BlockObjectResponse,
14+
Heading3BlockObjectResponse,
15+
ParagraphBlockObjectResponse,
16+
DividerBlockObjectResponse,
17+
BulletedListItemBlockObjectResponse,
18+
ColumnListBlockObjectResponse,
19+
ColumnBlockObjectResponse,
20+
QuoteBlockObjectResponse,
21+
CodeBlockObjectResponse,
22+
RichTextItemResponse,
23+
} from '@notionhq/client/build/src/api-endpoints';
24+
25+
export interface CustomComponentProps<T> {
26+
block: T;
27+
children?: React.ReactNode;
28+
}
29+
30+
/**
31+
* 自定义组件
32+
*/
33+
export type CustomComponent<T> = React.FC<CustomComponentProps<T>>;
34+
35+
export interface CustomComponents {
36+
block?: React.FC<CustomComponentProps<Record<string, unknown>>>;
37+
heading_1?: CustomComponent<Heading1BlockObjectResponse>;
38+
heading_2?: CustomComponent<Heading2BlockObjectResponse>;
39+
heading_3?: CustomComponent<Heading3BlockObjectResponse>;
40+
paragraph?: CustomComponent<ParagraphBlockObjectResponse>;
41+
[key: string]: CustomComponent<any>;
42+
}
43+
44+
/**
45+
* heading 类型
46+
*/
47+
export type HeadingBlockObjectResponse =
48+
| Heading1BlockObjectResponse
49+
| Heading2BlockObjectResponse
50+
| Heading3BlockObjectResponse;
51+
52+
/**
53+
* notion 原生类型透传
54+
*/
55+
export type {
56+
Client,
57+
PartialBlockObjectResponse,
58+
BlockObjectResponse,
59+
DatabaseObjectResponse,
60+
PartialDatabaseObjectResponse,
61+
PageObjectResponse,
62+
PartialPageObjectResponse,
63+
Heading1BlockObjectResponse,
64+
Heading2BlockObjectResponse,
65+
Heading3BlockObjectResponse,
66+
ParagraphBlockObjectResponse,
67+
DividerBlockObjectResponse,
68+
BulletedListItemBlockObjectResponse,
69+
ColumnListBlockObjectResponse,
70+
ColumnBlockObjectResponse,
71+
QuoteBlockObjectResponse,
72+
CodeBlockObjectResponse,
73+
RichTextItemResponse,
74+
};

src/types/index.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/utils.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1-
import { PartialBlockObjectResponse, BlockObjectResponse } from './types';
1+
import type {
2+
PartialBlockObjectResponse,
3+
BlockObjectResponse,
4+
PageObjectResponse,
5+
PartialPageObjectResponse,
6+
PartialDatabaseObjectResponse,
7+
DatabaseObjectResponse,
8+
} from './types';
29

310
/**
411
* 是否是完整的 Block
512
*/
613
export const isCompleteBlock = (
714
block: PartialBlockObjectResponse | BlockObjectResponse
815
): block is BlockObjectResponse => !!(block as BlockObjectResponse).type;
16+
17+
/**
18+
* 是完整的 Page
19+
*/
20+
export const isCompletePageObject = (
21+
page:
22+
| PageObjectResponse
23+
| PartialPageObjectResponse
24+
| PartialDatabaseObjectResponse
25+
| DatabaseObjectResponse
26+
): page is PageObjectResponse => page.object === 'page' && !!(page as PageObjectResponse).parent;

0 commit comments

Comments
 (0)