Skip to content

Commit 2ce9d37

Browse files
committed
fix(core): circular dependencies (#8215)
1 parent a387e4a commit 2ce9d37

File tree

7 files changed

+90
-66
lines changed

7 files changed

+90
-66
lines changed

packages/frontend/core/src/modules/cloud/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { SubscriptionPrices } from './entities/subscription-prices';
3535
import { UserCopilotQuota } from './entities/user-copilot-quota';
3636
import { UserFeature } from './entities/user-feature';
3737
import { UserQuota } from './entities/user-quota';
38+
import { AIService } from './services/ai';
3839
import { AuthService } from './services/auth';
3940
import { CloudDocMetaService } from './services/cloud-doc-meta';
4041
import { FetchService } from './services/fetch';
@@ -68,6 +69,7 @@ export function configureCloudModule(framework: Framework) {
6869
.entity(AuthSession, [AuthStore])
6970
.service(SubscriptionService, [SubscriptionStore])
7071
.store(SubscriptionStore, [GraphQLService, GlobalCache])
72+
.service(AIService, [AuthService])
7173
.entity(Subscription, [AuthService, ServerConfigService, SubscriptionStore])
7274
.entity(SubscriptionPrices, [ServerConfigService, SubscriptionStore])
7375
.service(UserQuotaService)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { AIProvider } from '@affine/core/blocksuite/presets/ai';
2+
import { Service } from '@toeverything/infra';
3+
import { distinctUntilChanged, map, skip } from 'rxjs';
4+
5+
import { type AuthAccountInfo } from '../entities/session';
6+
import type { AuthService } from './auth';
7+
function toAIUserInfo(account: AuthAccountInfo | null) {
8+
if (!account) return null;
9+
return {
10+
avatarUrl: account.avatar ?? '',
11+
email: account.email ?? '',
12+
id: account.id,
13+
name: account.label,
14+
};
15+
}
16+
17+
export class AIService extends Service {
18+
constructor(private readonly auth: AuthService) {
19+
super();
20+
21+
AIProvider.provide('userInfo', () => {
22+
return toAIUserInfo(this.auth.session.account$.value);
23+
});
24+
25+
this.auth.session.account$
26+
.pipe(
27+
map(a => ({
28+
id: a?.id,
29+
account: a,
30+
})),
31+
distinctUntilChanged((a, b) => a.id === b.id), // only emit when the value changes
32+
skip(1) // skip the initial value
33+
)
34+
.subscribe(({ account }) => {
35+
AIProvider.slots.userInfo.emit(toAIUserInfo(account));
36+
});
37+
}
38+
}

packages/frontend/core/src/modules/cloud/services/auth.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { AIProvider } from '@affine/core/blocksuite/presets/ai';
21
import { track } from '@affine/core/mixpanel';
32
import { appInfo } from '@affine/electron-api';
43
import type { OAuthProviderType } from '@affine/graphql';
@@ -25,16 +24,6 @@ export const AccountLoggedIn = createEvent<AuthAccountInfo>('AccountLoggedIn');
2524
export const AccountLoggedOut =
2625
createEvent<AuthAccountInfo>('AccountLoggedOut');
2726

28-
function toAIUserInfo(account: AuthAccountInfo | null) {
29-
if (!account) return null;
30-
return {
31-
avatarUrl: account.avatar ?? '',
32-
email: account.email ?? '',
33-
id: account.id,
34-
name: account.label,
35-
};
36-
}
37-
3827
@OnEvent(ApplicationStarted, e => e.onApplicationStart)
3928
@OnEvent(ApplicationFocused, e => e.onApplicationFocused)
4029
export class AuthService extends Service {
@@ -46,10 +35,6 @@ export class AuthService extends Service {
4635
) {
4736
super();
4837

49-
AIProvider.provide('userInfo', () => {
50-
return toAIUserInfo(this.session.account$.value);
51-
});
52-
5338
this.session.account$
5439
.pipe(
5540
map(a => ({
@@ -66,7 +51,6 @@ export class AuthService extends Service {
6651
this.eventBus.emit(AccountLoggedIn, account);
6752
}
6853
this.eventBus.emit(AccountChanged, account);
69-
AIProvider.slots.userInfo.emit(toAIUserInfo(account));
7054
});
7155
}
7256

packages/frontend/core/src/modules/workbench/view/workbench-root.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { ResizePanel } from '@affine/component/resize-panel';
22
import { rightSidebarWidthAtom } from '@affine/core/atoms';
3-
import { viewRoutes } from '@affine/core/router';
3+
import { workspaceRoutes } from '@affine/core/workspace-router';
44
import {
55
appSettingAtom,
66
FrameworkScope,
77
useLiveData,
88
useService,
99
} from '@toeverything/infra';
1010
import { useAtom, useAtomValue } from 'jotai';
11-
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
11+
import { memo, useCallback, useEffect, useRef, useState } from 'react';
1212
import { type RouteObject, useLocation } from 'react-router-dom';
1313

1414
import type { View } from '../entities/view';
@@ -26,6 +26,13 @@ const useAdapter = BUILD_CONFIG.isElectron
2626
? useBindWorkbenchToDesktopRouter
2727
: useBindWorkbenchToBrowserRouter;
2828

29+
const routes: RouteObject[] = [
30+
{
31+
element: <RouteContainer />,
32+
children: workspaceRoutes,
33+
},
34+
];
35+
2936
export const WorkbenchRoot = memo(() => {
3037
const workbench = useService(WorkbenchService).workbench;
3138

@@ -93,15 +100,6 @@ const WorkbenchView = ({ view, index }: { view: View; index: number }) => {
93100
return;
94101
}, [handleOnFocus]);
95102

96-
const routes: RouteObject[] = useMemo(() => {
97-
return [
98-
{
99-
element: <RouteContainer />,
100-
children: viewRoutes,
101-
},
102-
] satisfies RouteObject[];
103-
}, []);
104-
105103
return (
106104
<div className={styles.workbenchViewContainer} ref={containerRef}>
107105
<ViewRoot routes={routes} key={view.id} view={view} />

packages/frontend/core/src/pages/workspace/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AffineOtherPageLayout } from '@affine/component/affine-other-page-layout';
22
import { AppFallback } from '@affine/core/components/affine/app-container';
3-
import { viewRoutes } from '@affine/core/router';
3+
import { workspaceRoutes } from '@affine/core/workspace-router';
44
import { ZipTransformer } from '@blocksuite/blocks';
55
import type { Workspace, WorkspaceMetadata } from '@toeverything/infra';
66
import {
@@ -55,9 +55,10 @@ export const Component = (): ReactElement => {
5555
match &&
5656
match.params.docId &&
5757
match.params.workspaceId &&
58-
// TODO(eyhn): need a better way to check if it's a docId
59-
viewRoutes.find(route => matchPath(route.path, '/' + match.params.docId))
60-
?.path === '/:pageId'
58+
// // TODO(eyhn): need a better way to check if it's a docId
59+
workspaceRoutes.find(route =>
60+
matchPath(route.path, '/' + match.params.docId)
61+
)?.path === '/:pageId'
6162
) {
6263
return {
6364
docId: match.params.docId,

packages/frontend/core/src/router.tsx

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -163,41 +163,6 @@ export const topLevelRoutes = [
163163
},
164164
] satisfies [RouteObject, ...RouteObject[]];
165165

166-
export const viewRoutes = [
167-
{
168-
path: '/all',
169-
lazy: () => import('./pages/workspace/all-page/all-page'),
170-
},
171-
{
172-
path: '/collection',
173-
lazy: () => import('./pages/workspace/all-collection'),
174-
},
175-
{
176-
path: '/collection/:collectionId',
177-
lazy: () => import('./pages/workspace/collection/index'),
178-
},
179-
{
180-
path: '/tag',
181-
lazy: () => import('./pages/workspace/all-tag'),
182-
},
183-
{
184-
path: '/tag/:tagId',
185-
lazy: () => import('./pages/workspace/tag'),
186-
},
187-
{
188-
path: '/trash',
189-
lazy: () => import('./pages/workspace/trash-page'),
190-
},
191-
{
192-
path: '/:pageId',
193-
lazy: () => import('./pages/workspace/detail-page/detail-page'),
194-
},
195-
{
196-
path: '*',
197-
lazy: () => import('./pages/404'),
198-
},
199-
] satisfies [RouteObject, ...RouteObject[]];
200-
201166
const createBrowserRouter = wrapCreateBrowserRouter(
202167
reactRouterCreateBrowserRouter
203168
);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { RouteObject } from 'react-router-dom';
2+
3+
export const workspaceRoutes = [
4+
{
5+
path: '/all',
6+
lazy: () => import('./pages/workspace/all-page/all-page'),
7+
},
8+
{
9+
path: '/collection',
10+
lazy: () => import('./pages/workspace/all-collection'),
11+
},
12+
{
13+
path: '/collection/:collectionId',
14+
lazy: () => import('./pages/workspace/collection/index'),
15+
},
16+
{
17+
path: '/tag',
18+
lazy: () => import('./pages/workspace/all-tag'),
19+
},
20+
{
21+
path: '/tag/:tagId',
22+
lazy: () => import('./pages/workspace/tag'),
23+
},
24+
{
25+
path: '/trash',
26+
lazy: () => import('./pages/workspace/trash-page'),
27+
},
28+
{
29+
path: '/:pageId',
30+
lazy: () => import('./pages/workspace/detail-page/detail-page'),
31+
},
32+
{
33+
path: '*',
34+
lazy: () => import('./pages/404'),
35+
},
36+
] satisfies RouteObject[];

0 commit comments

Comments
 (0)