Skip to content

Fix the bug of traversing collections #658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 55 additions & 14 deletions packages/notion-client/src/notion-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import { promises as fs } from 'fs'
import type * as notion from 'notion-types'
import ky, { type Options as KyOptions } from 'ky'
import { type Options as KyOptions } from 'ky'
import {
getBlockCollectionId,
getPageContentBlockIds,
Expand Down Expand Up @@ -707,6 +707,51 @@ export class NotionAPI {
})
}

// public async fetch<T>({
// endpoint,
// body,
// kyOptions,
// headers: clientHeaders
// }: {
// endpoint: string
// body: object
// kyOptions?: KyOptions
// headers?: any
// }): Promise<T> {
// const headers: any = {
// ...clientHeaders,
// ...this._kyOptions?.headers,
// ...kyOptions?.headers,
// 'Content-Type': 'application/json'
// }

// if (this._authToken) {
// headers.cookie = `token_v2=${this._authToken}`
// }

// if (this._activeUser) {
// headers['x-notion-active-user-header'] = this._activeUser
// }

// const url = `${this._apiBaseUrl}/${endpoint}`

// const res = await ky.post(url, {
// mode: 'no-cors',
// ...this._kyOptions,
// ...kyOptions,
// json: body,
// headers
// })

// // TODO: we're awaiting the first fetch and then separately awaiting
// // `res.json()` because there seems to be some weird error which repros
// // sporadically when loading collections where the body is already used.
// // No idea why, but from my testing, separating these into two separate
// // steps seems to fix the issue locally for me...
// // console.log(endpoint, { bodyUsed: res.bodyUsed })

// return res.json<T>()
// }
public async fetch<T>({
endpoint,
body,
Expand Down Expand Up @@ -735,21 +780,17 @@ export class NotionAPI {

const url = `${this._apiBaseUrl}/${endpoint}`

const res = await ky.post(url, {
mode: 'no-cors',
...this._kyOptions,
...kyOptions,
json: body,
headers
const res = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(body),
...kyOptions
})

// TODO: we're awaiting the first fetch and then separately awaiting
// `res.json()` because there seems to be some weird error which repros
// sporadically when loading collections where the body is already used.
// No idea why, but from my testing, separating these into two separate
// steps seems to fix the issue locally for me...
// console.log(endpoint, { bodyUsed: res.bodyUsed })
if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`)
}

return res.json<T>()
return res.json() as Promise<T>
}
}
4 changes: 2 additions & 2 deletions packages/notion-utils/src/get-all-pages-in-space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ export async function getAllPagesInSpace(
page.collection_query
)) {
for (const collectionData of Object.values(collectionViews)) {
const { blockIds } = collectionData

const { blockIds } =
collectionData?.collection_group_results || {}
if (blockIds) {
for (const collectionItemId of blockIds) {
void processPage(collectionItemId, depth + 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function getCollectionGroups(
}

// TODO: review dates format based on value.type ('week'|'month'|'year')
queryValue = format(new Date(queryLabel), 'MMM d, YYY hh:mm aa')
queryValue = format(new Date(queryLabel), 'MMM d, yyy hh:mm aa')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The date format pattern yyy will display only 3 digits for the year (e.g., "202" instead of "2023"). To correctly display a 4-digit year, the pattern should be yyyy. This same issue appears in multiple places throughout the PR and should be fixed in all occurrences.

Suggested change
queryValue = format(new Date(queryLabel), 'MMM d, yyy hh:mm aa')
queryValue = format(new Date(queryLabel), 'MMM d, yyyy hh:mm aa')

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

}

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-notion-x/src/third-party/eval-formula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ function evalFunctionFormula(

case 'object':
if (value instanceof Date) {
return format(value as Date, 'MMM d, YYY')
return format(value as Date, 'MMM d, yyy')
} else {
// shouldn't ever get here
return `${value}`
Expand Down
6 changes: 3 additions & 3 deletions packages/react-notion-x/src/third-party/property.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function PropertyImpl(props: IPropertyProps) {
}

if (content instanceof Date) {
content = format(content, 'MMM d, YYY hh:mm aa')
content = format(content, 'MMM d, yyy hh:mm aa')
}
} catch {
// console.log('error evaluating formula', schema.formula, err)
Expand Down Expand Up @@ -437,15 +437,15 @@ export function PropertyImpl(props: IPropertyProps) {
const renderCreatedTimeValue = React.useMemo(
() =>
function CreatedTimeProperty() {
return format(new Date(block!.created_time), 'MMM d, YYY hh:mm aa')
return format(new Date(block!.created_time), 'MMM d, yyy hh:mm aa')
},
[block]
)

const renderLastEditedTimeValue = React.useMemo(
() =>
function LastEditedTimeProperty() {
return format(new Date(block!.last_edited_time), 'MMM d, YYY hh:mm aa')
return format(new Date(block!.last_edited_time), 'MMM d, yyy hh:mm aa')
},
[block]
)
Expand Down