Skip to content

Commit

Permalink
Merge pull request #62 from linyows/remove-unnecessary-api-req
Browse files Browse the repository at this point in the history
Remove unnecessary API requests
  • Loading branch information
linyows authored May 20, 2024
2 parents bebce81 + 4e24d09 commit 9000b1b
Show file tree
Hide file tree
Showing 63 changed files with 645 additions and 1,911 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rotion",
"version": "0.3.3",
"version": "0.4.0",
"license": "MIT",
"repository": "linyows/rotion",
"description": "This is react components that uses the notion API to display the notion's database and page.",
Expand Down Expand Up @@ -50,6 +50,7 @@
"ui:typecheck": "tsc -p src/ui/tsconfig.typecheck.json",
"ui:lint": "eslint ./src/ui/components --ext .js,.jsx,.ts,.tsx -c ./src/ui/.eslintrc.json",
"ui:test": "uvu -r tsm ./src/ui/components",
"ui:fixtures": "jq '.' website/.cache/notion.databases.query-* | sed 's/?.*\",/\",/g' > src/ui/fixtures/fetch_database_response.json",
"story": "storybook dev -p 6006 --ci",
"build-story": "storybook build",
"prepublishOnly": "npm install && npm run test && npm run build"
Expand Down
11 changes: 0 additions & 11 deletions src/exporter/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,7 @@ export const FetchDatabase = async (params: FetchDatabaseArgs): Promise<FetchDat
const page: PageObjectResponseEx = result
await savePageCover(page)
await savePageIcon(page)
// Set page property items
page.property_items = []
for (const [, v] of Object.entries(page.properties)) {
const page_id = page.id
const property_id = v.id
const props = await reqAPIWithBackoffAndCache<GetPagePropertyResponse>({
name: 'notion.pages.properties.retrieve',
func: notion.pages.properties.retrieve,
args: { page_id, property_id },
count: 3,
})
page.property_items.push(props)
// Save avatar in people property type
if (v.type === 'people') {
const peoples = v.people as unknown as PersonUserObjectResponseEx[]
Expand Down
216 changes: 216 additions & 0 deletions src/exporter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,219 @@ export type Parent =
| { type: "page_id"; page_id: string }
| { type: "block_id"; block_id: string }
| { type: "workspace"; workspace: true }

export type DatabaseProperty = DatabasePropertyConfigResponse

export type DatabasePropertyConfigResponse =
| NumberDatabasePropertyConfigResponse
| FormulaDatabasePropertyConfigResponse
| SelectDatabasePropertyConfigResponse
| MultiSelectDatabasePropertyConfigResponse
| StatusDatabasePropertyConfigResponse
| RelationDatabasePropertyConfigResponse
| RollupDatabasePropertyConfigResponse
| UniqueIdDatabasePropertyConfigResponse
| TitleDatabasePropertyConfigResponse
| RichTextDatabasePropertyConfigResponse
| UrlDatabasePropertyConfigResponse
| PeopleDatabasePropertyConfigResponse
| FilesDatabasePropertyConfigResponse
| EmailDatabasePropertyConfigResponse
| PhoneNumberDatabasePropertyConfigResponse
| DateDatabasePropertyConfigResponse
| CheckboxDatabasePropertyConfigResponse
| CreatedByDatabasePropertyConfigResponse
| CreatedTimeDatabasePropertyConfigResponse
| LastEditedByDatabasePropertyConfigResponse
| LastEditedTimeDatabasePropertyConfigResponse

type RollupFunction =
| "count"
| "count_values"
| "empty"
| "not_empty"
| "unique"
| "show_unique"
| "percent_empty"
| "percent_not_empty"
| "sum"
| "average"
| "median"
| "min"
| "max"
| "range"
| "earliest_date"
| "latest_date"
| "date_range"
| "checked"
| "unchecked"
| "percent_checked"
| "percent_unchecked"
| "count_per_group"
| "percent_per_group"
| "show_original"

export type NumberDatabasePropertyConfigResponse = {
type: "number"
number: number | null
id: string
}

export type FormulaDatabasePropertyConfigResponse = {
type: "formula"
formula: { expression: string }
id: string
}

export type SelectDatabasePropertyConfigResponse = {
type: "select"
select: SelectPropertyResponse
id: string
}

export type MultiSelectDatabasePropertyConfigResponse = {
type: "multi_select"
multi_select: SelectPropertyResponse[]
id: string
}

export type StatusPropertyResponse = {
id: StringRequest
name: StringRequest
color: SelectColor
}

export type StatusDatabasePropertyConfigResponse = {
type: "status"
status: {
options: Array<StatusPropertyResponse>
groups: Array<{
id: StringRequest
name: StringRequest
color: SelectColor
option_ids: Array<string>
}>
}
id: string
}

export type SinglePropertyDatabasePropertyRelationConfigResponse = {
type: "single_property"
single_property: EmptyObject
database_id: IdRequest
}

export type DualPropertyDatabasePropertyRelationConfigResponse = {
type: "dual_property"
dual_property: {
synced_property_id: StringRequest
synced_property_name: StringRequest
}
database_id: IdRequest
}

export type DatabasePropertyRelationConfigResponse =
| SinglePropertyDatabasePropertyRelationConfigResponse
| DualPropertyDatabasePropertyRelationConfigResponse

export type RelationDatabasePropertyConfigResponse = {
type: "relation"
relation: DatabasePropertyRelationConfigResponse
id: string
}

export type RollupDatabasePropertyConfigResponse = {
type: "rollup"
rollup: {
rollup_property_name: string
relation_property_name: string
rollup_property_id: string
relation_property_id: string
function: RollupFunction
}
id: string
}

export type UniqueIdDatabasePropertyConfigResponse = {
type: "unique_id"
unique_id: { prefix: string | null }
id: string
}

export type TitleDatabasePropertyConfigResponse = {
type: "title"
title: TextRichTextItemResponse[]
id: string
}

export type RichTextDatabasePropertyConfigResponse = {
type: "rich_text"
rich_text: TextRichTextItemResponse[]
id: string
}

export type UrlDatabasePropertyConfigResponse = {
type: "url"
url: string | null
id: string
}

export type PeopleDatabasePropertyConfigResponse = {
type: "people"
people: EmptyObject
id: string
}

export type FilesDatabasePropertyConfigResponse = {
type: "files"
files: EmptyObject
id: string
}

export type EmailDatabasePropertyConfigResponse = {
type: "email"
email: string | null
id: string
}

export type PhoneNumberDatabasePropertyConfigResponse = {
type: "phone_number"
phone_number: number | null
id: string
}

export type DateDatabasePropertyConfigResponse = {
type: "date"
date: DateResponse | null
id: string
}

export type CheckboxDatabasePropertyConfigResponse = {
type: "checkbox"
checkbox: boolean
id: string
}

export type CreatedByDatabasePropertyConfigResponse = {
type: "created_by"
created_by: EmptyObject
id: string
}

export type CreatedTimeDatabasePropertyConfigResponse = {
type: "created_time"
created_time: EmptyObject
id: string
}

export type LastEditedByDatabasePropertyConfigResponse = {
type: "last_edited_by"
last_edited_by: EmptyObject
id: string
}

export type LastEditedTimeDatabasePropertyConfigResponse = {
type: "last_edited_time"
last_edited_time: EmptyObject
id: string
}
13 changes: 2 additions & 11 deletions src/ui/components/Gallery/GalleryCard/GalleryCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import type {
DatabaseProperty,
GetPageResponse,
PageObjectResponseEx,
} from '../../../../exporter'
Expand Down Expand Up @@ -37,16 +38,6 @@ function buildHref (page: PageObjectResponseEx, link?: string) {
return `${path}${slug}`
}

function findItems (name: string, page: PageObjectResponseEx) {
let propertyId = ''
for (const [k, v] of Object.entries(page.properties)) {
if (k === name) {
propertyId = v.id
}
}
return page.property_items.find(v => ((v.object === 'property_item' && v.id === propertyId) || (v.object === 'list' && v.property_item.id === propertyId)))
}

const GalleryCard = ({ keys, page, href, link, query, preview, size, fit }: GalleryCardProps) => {
const path = getLinkPathAndLinkKey(href)[0]

Expand All @@ -57,7 +48,7 @@ const GalleryCard = ({ keys, page, href, link, query, preview, size, fit }: Gall
<div className="rotion-gallery-card-text">
{keys.map((name, i) => (
<div key={`${page.id}${name}`} className={`field${i}`}>
<GalleryHandler items={findItems(name, page)} path={path} query={query} size={size} />
<GalleryHandler property={page.properties[name] as DatabaseProperty | undefined} path={path} query={query} size={size} />
</div>
))}
</div>
Expand Down
Loading

0 comments on commit 9000b1b

Please sign in to comment.