Skip to content

Commit 430ac84

Browse files
committed
feat(notion): refactor space handling and improve error reporting
This commit refactors how Notion spaces are handled by: 1. Moving space retrieval to a dedicated `getSpaces` method 2. Adding a `getSpaceId` helper method 3. Using the new methods consistently across the service Additionally, it improves error reporting in the frontend by showing error stacks in error messages. This helps with debugging when issues occur. The changes make the code more maintainable by centralizing space-related logic and provide better visibility into errors when they happen.
1 parent 2816b82 commit 430ac84

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/common/backend/services/notion/service.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,8 @@ export default class NotionDocumentService implements DocumentService {
7676
this.userContent = await this.getUserContent();
7777
}
7878

79-
if (!this.userContent.recordMap.space) {
80-
return [];
81-
}
82-
83-
const spaces = this.userContent.recordMap.space;
84-
8579
const userId = Object.keys(this.userContent.recordMap.notion_user)[0] as string;
86-
80+
const spaces = (await this.getSpaces(userId)) as any;
8781
const result: Array<NotionRepository[]> = await Promise.all(
8882
Object.keys(spaces).map(async (p) => {
8983
const space = spaces[p];
@@ -96,6 +90,17 @@ export default class NotionDocumentService implements DocumentService {
9690
return this.repositories;
9791
};
9892

93+
getSpaces = async (userId: string) => {
94+
const response = await this.requestWithCookie.post<{
95+
users: {
96+
[id: string]: {
97+
space: any;
98+
};
99+
};
100+
}>('/api/v3/getSpacesInitial');
101+
return response.data.users[userId].space;
102+
};
103+
99104
createDocument = async ({
100105
repositoryId,
101106
title,
@@ -118,7 +123,7 @@ export default class NotionDocumentService implements DocumentService {
118123
if (!this.userContent) {
119124
this.userContent = await this.getUserContent();
120125
}
121-
const spaceId = Object.values(this.userContent.recordMap.space)[0].value.id;
126+
const spaceId = await this.getSpaceId();
122127
await this.requestWithCookie.post('api/v3/enqueueTask', {
123128
task: {
124129
eventName: 'importFile',
@@ -140,11 +145,21 @@ export default class NotionDocumentService implements DocumentService {
140145
};
141146
};
142147

148+
getSpaceId = async () => {
149+
if (!this.userContent) {
150+
this.userContent = await this.getUserContent();
151+
}
152+
153+
const userId = Object.keys(this.userContent.recordMap.notion_user)[0] as string;
154+
const spaces = (await this.getSpaces(userId)) as any;
155+
return Object.keys(spaces)[0];
156+
};
157+
143158
createEmptyFile = async (repository: NotionRepository, title: string) => {
144159
if (!this.userContent) {
145160
this.userContent = await this.getUserContent();
146161
}
147-
const spaceId = Object.values(this.userContent.recordMap.space)[0].value.id;
162+
const spaceId = await this.getSpaceId();
148163
const documentId = generateUuid();
149164
const parentId = repository.id;
150165
const userId = Object.values(this.userContent.recordMap.notion_user)[0].value.id;

src/pages/app.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@ export default async () => {
4949
const app = dva({
5050
namespacePrefixWarning: false,
5151
history: createHashHistory(),
52-
onError: e => {
52+
onError: (e) => {
5353
(e as any).preventDefault();
5454
message.destroy();
5555
message.error(e.message);
56+
message.error(e.stack);
5657
},
5758
});
5859
app.use(createLoading());
5960

60-
app.router(router => {
61+
app.router((router) => {
6162
return (
6263
<LocalWrapper>
6364
<Router history={router!.history}>

0 commit comments

Comments
 (0)