Skip to content

Commit

Permalink
Revert "Revert "[Notion] Population Relation Field with all related d…
Browse files Browse the repository at this point in the history
…atabase pages. (#13579)""

This reverts commit bf734d6.
  • Loading branch information
thomaslombart committed Aug 2, 2024
1 parent 5872833 commit 061a5cb
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 37 deletions.
4 changes: 4 additions & 0 deletions extensions/notion/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Notion Changelog

## [Bug Fix] - 2024-7-30

- The relation property field now shows every page in the related database.

## [Refactor] - 2024-07-03

- Refactor code. No Functional changes.
Expand Down
2 changes: 1 addition & 1 deletion extensions/notion/src/components/DatabaseList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function DatabaseList({ databasePage, setRecentPage, removeRecentPage, us
isLoading,
mutate,
} = useCachedPromise(
(databaseId, searchText, sort) => queryDatabase(databaseId, searchText, sort),
async (databaseId, searchText, sort) => (await queryDatabase(databaseId, { query: searchText, sort })).pages,
[databaseId, searchText, sort],
);
const { data: databaseProperties, isLoading: isLoadingDatabaseProperties } = useDatabaseProperties(databaseId);
Expand Down
7 changes: 1 addition & 6 deletions extensions/notion/src/components/forms/CreatePageForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,7 @@ export function CreatePageForm({ mutate, launchContext, defaults }: CreatePageFo

function itemPropsFor<T extends DatabaseProperty["type"]>(property: DatabaseProperty) {
const id = createPropertyId(property);
return {
...(itemProps[id] as FieldProps<T>),
title: property.name,
key: id,
id,
};
return itemProps[id] as FieldProps<T>;
}

const convertToField = createConvertToFieldFunc(itemPropsFor, relationPages, users);
Expand Down
20 changes: 15 additions & 5 deletions extensions/notion/src/components/forms/PagePropertyField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ export function createConvertToFieldFunc(
users: User[],
) {
return (property: DatabaseProperty) => {
const { name, id } = property;
let placeholder = property.type.replace(/_/g, " ");
placeholder = placeholder.charAt(0).toUpperCase() + placeholder.slice(1);

switch (property.type) {
case "date":
return <Form.DatePicker {...itemPropsFor<typeof property.type>(property)} />;
return <Form.DatePicker {...itemPropsFor<typeof property.type>(property)} title={name} key={id} id={id} />;
case "checkbox":
return <Form.Checkbox {...itemPropsFor<typeof property.type>(property)} label={placeholder} />;
return <Form.Checkbox {...itemPropsFor<typeof property.type>(property)} key={id} id={id} label={placeholder} />;
case "select":
case "status":
return (
<Form.Dropdown {...itemPropsFor<typeof property.type>(property)}>
<Form.Dropdown {...itemPropsFor<typeof property.type>(property)} title={name} key={id} id={id}>
{getPropertyConfig(property, [property.type])?.options.map(createMapOptionsFunc(Form.Dropdown.Item))}
</Form.Dropdown>
);
Expand All @@ -43,7 +44,13 @@ export function createConvertToFieldFunc(
if (relationId) options = relationPages[relationId];
}
return (
<Form.TagPicker placeholder={placeholder} {...itemPropsFor<typeof property.type>(property)}>
<Form.TagPicker
{...itemPropsFor<typeof property.type>(property)}
title={name}
key={id}
id={id}
placeholder={placeholder}
>
{options?.map(createMapOptionsFunc(Form.TagPicker.Item))}
</Form.TagPicker>
);
Expand All @@ -53,9 +60,12 @@ export function createConvertToFieldFunc(
default:
return (
<Form.TextField
{...itemPropsFor<typeof property.type>(property)}
title={name}
key={id}
id={id}
info="Supports a single line of inline Markdown"
placeholder={placeholder}
{...itemPropsFor<typeof property.type>(property)}
/>
);
}
Expand Down
17 changes: 10 additions & 7 deletions extensions/notion/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
search,
fetchPage,
fetchDatabase,
getPropertyConfig,
type Page,
type DatabaseProperty,
} from "../utils/notion";
Expand All @@ -27,13 +26,17 @@ export function useRelations(properties: DatabaseProperty[]) {
async (properties: DatabaseProperty[]) => {
const relationPages: Record<string, Page[]> = {};

for (const property of properties)
if (property.type == "relation") relationPages[property.relation.database_id] = [];

await Promise.all(
properties.map(async (property) => {
const relationId = getPropertyConfig(property, ["relation"])?.database_id;
if (!relationId) return null;
const pages = await queryDatabase(relationId, undefined);
relationPages[relationId] = pages;
return pages;
Object.keys(relationPages).map(async (relationId) => {
let cursor: string | null = null;
do {
const { pages, nextCursor } = await queryDatabase(relationId, { cursor: cursor ?? undefined });
relationPages[relationId].push(...pages);
cursor = nextCursor;
} while (cursor != null);
}),
);

Expand Down
48 changes: 30 additions & 18 deletions extensions/notion/src/utils/notion/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { BlockObjectRequest } from "@notionhq/client/build/src/api-endpoint
import { type Form, showToast, Toast } from "@raycast/api";
import { markdownToBlocks } from "@tryfabric/martian";

import { isWritableProperty } from "..";
import { isWritableProperty, Page } from "..";
import { handleError, isNotNullOrUndefined, pageMapper } from "../global";
import { getNotionClient } from "../oauth";
import { formValueToPropertyValue } from "../page/property";
Expand Down Expand Up @@ -85,37 +85,49 @@ export async function fetchDatabaseProperties(databaseId: string) {

export async function queryDatabase(
databaseId: string,
query: string | undefined,
sort: "last_edited_time" | "created_time" = "last_edited_time",
) {
options?: {
query?: string;
sort?: "last_edited_time" | "created_time";
/** Maximum 100 */
pageSize?: number;
cursor?: string;
},
): Promise<{
pages: Page[];
hasMore: boolean;
nextCursor: string | null;
}> {
try {
const notion = getNotionClient();
const database = await notion.databases.query({
const { results, has_more, next_cursor } = await notion.databases.query({
database_id: databaseId,
page_size: 20,
page_size: options?.pageSize ?? 20,
start_cursor: options?.cursor,
sorts: [
{
direction: "descending",
timestamp: sort,
timestamp: options?.sort ?? "last_edited_time",
},
],
filter: query
filter: options?.query
? {
and: [
{
property: "title",
title: {
contains: query,
},
},
],
property: "title",
title: { contains: options.query },
}
: undefined,
});

return database.results.map(pageMapper);
return {
pages: results.map(pageMapper),
hasMore: has_more,
nextCursor: next_cursor,
};
} catch (err) {
return handleError(err, "Failed to query database", []);
return handleError(err, "Failed to query database", {
pages: [],
hasMore: false,
nextCursor: null,
});
}
}

Expand Down

0 comments on commit 061a5cb

Please sign in to comment.