Skip to content

Commit 3dd720e

Browse files
feat: fix indirect getBlockCollectionId
1 parent be92ebd commit 3dd720e

File tree

8 files changed

+34
-50
lines changed

8 files changed

+34
-50
lines changed

packages/notion-client/src/notion-api.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,28 +118,13 @@ export class NotionAPI {
118118
block &&
119119
(block.type === 'collection_view' ||
120120
block.type === 'collection_view_page') &&
121-
getBlockCollectionId(block)
121+
getBlockCollectionId(block, recordMap)
122122

123123
if (collectionId) {
124124
return block.view_ids?.map((collectionViewId) => ({
125125
collectionId,
126126
collectionViewId
127127
}))
128-
} else if (block && (block as any).view_ids) {
129-
return (block as any).view_ids.map((collectionViewId) => {
130-
if (recordMap.collection_view[collectionViewId]) {
131-
const viewBlock =
132-
recordMap.collection_view[collectionViewId].value
133-
const collectionId = viewBlock.format?.collection_pointer?.id
134-
if (collectionId) {
135-
return {
136-
collectionId,
137-
collectionViewId
138-
}
139-
}
140-
}
141-
return null
142-
})
143128
} else {
144129
return []
145130
}
Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1-
import { Block } from 'notion-types'
1+
import { Block, ExtendedRecordMap } from 'notion-types'
22

3-
export function getBlockCollectionId(block: Block): string | null {
4-
return (
3+
export function getBlockCollectionId(
4+
block: Block,
5+
recordMap: ExtendedRecordMap
6+
): string | null {
7+
const collectionId =
58
(block as any).collection_id ||
6-
(block as any).format?.collection_pointer?.id ||
7-
null
8-
)
9+
(block as any).format?.collection_pointer?.id
10+
11+
if (collectionId) {
12+
return collectionId
13+
}
14+
15+
const collectionViewId = (block as any)?.view_ids?.[0]
16+
if (collectionViewId) {
17+
const collectionView = recordMap.collection_view?.[collectionViewId]?.value
18+
if (collectionView) {
19+
const collectionId = collectionView.format?.collection_pointer?.id
20+
return collectionId
21+
}
22+
}
23+
24+
return null
925
}

packages/notion-utils/src/get-block-collection-pointer-id.ts

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

packages/notion-utils/src/get-block-icon.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function getBlockIcon(block: Block, recordMap: ExtendedRecordMap) {
1010
block.type === 'collection_view_page' ||
1111
block.type === 'collection_view'
1212
) {
13-
const collectionId = getBlockCollectionId(block)
13+
const collectionId = getBlockCollectionId(block, recordMap)
1414
if (collectionId) {
1515
const collection = recordMap.collection[collectionId]?.value
1616

packages/notion-utils/src/get-block-title.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ export function getBlockTitle(block: Block, recordMap: ExtendedRecordMap) {
1111
block.type === 'collection_view_page' ||
1212
block.type === 'collection_view'
1313
) {
14-
const collection = recordMap.collection[getBlockCollectionId(block)]?.value
14+
const collectionId = getBlockCollectionId(block, recordMap)
1515

16-
if (collection) {
17-
return getTextContent(collection.name)
16+
if (collectionId) {
17+
const collection = recordMap.collection[collectionId]?.value
18+
19+
if (collection) {
20+
return getTextContent(collection.name)
21+
}
1822
}
1923
}
2024

packages/notion-utils/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@ export * from './normalize-title'
2121
export * from './merge-record-maps'
2222
export * from './format-date'
2323
export * from './format-notion-date-time'
24-
export * from './get-block-collection-pointer-id'

packages/react-notion-x/src/block.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ export const Block: React.FC<BlockProps> = (props) => {
120120
? block.properties
121121
: {
122122
title:
123-
recordMap.collection[getBlockCollectionId(block)]?.value
124-
?.name
123+
recordMap.collection[getBlockCollectionId(block, recordMap)]
124+
?.value?.name
125125
}
126126

127127
const coverPosition = (1 - (page_cover_position || 0.5)) * 100

packages/react-notion-x/src/third-party/collection.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as React from 'react'
22
import * as types from 'notion-types'
33
import {
44
getBlockCollectionId,
5-
getBlockCollectionPointerId,
65
getBlockParentPage,
76
getTextContent
87
} from 'notion-utils'
@@ -84,8 +83,7 @@ const CollectionViewBlock: React.FC<{
8483
}> = ({ block, className }) => {
8584
const { recordMap, showCollectionViewDropdown } = useNotionContext()
8685
const { view_ids: viewIds } = block
87-
const collectionId =
88-
getBlockCollectionId(block) ?? getBlockCollectionPointerId(recordMap, block)
86+
const collectionId = getBlockCollectionId(block, recordMap)
8987

9088
const [isMounted, setIsMounted] = React.useState(false)
9189
React.useEffect(() => {

0 commit comments

Comments
 (0)