diff --git a/packages/frontend/core/src/modules/cloud/index.ts b/packages/frontend/core/src/modules/cloud/index.ts index b0225dc0fc..85dbae9973 100644 --- a/packages/frontend/core/src/modules/cloud/index.ts +++ b/packages/frontend/core/src/modules/cloud/index.ts @@ -35,6 +35,7 @@ import { SubscriptionPrices } from './entities/subscription-prices'; import { UserCopilotQuota } from './entities/user-copilot-quota'; import { UserFeature } from './entities/user-feature'; import { UserQuota } from './entities/user-quota'; +import { AIService } from './services/ai'; import { AuthService } from './services/auth'; import { CloudDocMetaService } from './services/cloud-doc-meta'; import { FetchService } from './services/fetch'; @@ -68,6 +69,7 @@ export function configureCloudModule(framework: Framework) { .entity(AuthSession, [AuthStore]) .service(SubscriptionService, [SubscriptionStore]) .store(SubscriptionStore, [GraphQLService, GlobalCache]) + .service(AIService, [AuthService]) .entity(Subscription, [AuthService, ServerConfigService, SubscriptionStore]) .entity(SubscriptionPrices, [ServerConfigService, SubscriptionStore]) .service(UserQuotaService) diff --git a/packages/frontend/core/src/modules/cloud/services/ai.ts b/packages/frontend/core/src/modules/cloud/services/ai.ts new file mode 100644 index 0000000000..85f9634eee --- /dev/null +++ b/packages/frontend/core/src/modules/cloud/services/ai.ts @@ -0,0 +1,38 @@ +import { AIProvider } from '@affine/core/blocksuite/presets/ai'; +import { Service } from '@toeverything/infra'; +import { distinctUntilChanged, map, skip } from 'rxjs'; + +import { type AuthAccountInfo } from '../entities/session'; +import type { AuthService } from './auth'; +function toAIUserInfo(account: AuthAccountInfo | null) { + if (!account) return null; + return { + avatarUrl: account.avatar ?? '', + email: account.email ?? '', + id: account.id, + name: account.label, + }; +} + +export class AIService extends Service { + constructor(private readonly auth: AuthService) { + super(); + + AIProvider.provide('userInfo', () => { + return toAIUserInfo(this.auth.session.account$.value); + }); + + this.auth.session.account$ + .pipe( + map(a => ({ + id: a?.id, + account: a, + })), + distinctUntilChanged((a, b) => a.id === b.id), // only emit when the value changes + skip(1) // skip the initial value + ) + .subscribe(({ account }) => { + AIProvider.slots.userInfo.emit(toAIUserInfo(account)); + }); + } +} diff --git a/packages/frontend/core/src/modules/cloud/services/auth.ts b/packages/frontend/core/src/modules/cloud/services/auth.ts index 82d5a2bbe8..db987423e0 100644 --- a/packages/frontend/core/src/modules/cloud/services/auth.ts +++ b/packages/frontend/core/src/modules/cloud/services/auth.ts @@ -1,4 +1,3 @@ -import { AIProvider } from '@affine/core/blocksuite/presets/ai'; import { track } from '@affine/core/mixpanel'; import { appInfo } from '@affine/electron-api'; import type { OAuthProviderType } from '@affine/graphql'; @@ -25,16 +24,6 @@ export const AccountLoggedIn = createEvent('AccountLoggedIn'); export const AccountLoggedOut = createEvent('AccountLoggedOut'); -function toAIUserInfo(account: AuthAccountInfo | null) { - if (!account) return null; - return { - avatarUrl: account.avatar ?? '', - email: account.email ?? '', - id: account.id, - name: account.label, - }; -} - @OnEvent(ApplicationStarted, e => e.onApplicationStart) @OnEvent(ApplicationFocused, e => e.onApplicationFocused) export class AuthService extends Service { @@ -46,10 +35,6 @@ export class AuthService extends Service { ) { super(); - AIProvider.provide('userInfo', () => { - return toAIUserInfo(this.session.account$.value); - }); - this.session.account$ .pipe( map(a => ({ @@ -66,7 +51,6 @@ export class AuthService extends Service { this.eventBus.emit(AccountLoggedIn, account); } this.eventBus.emit(AccountChanged, account); - AIProvider.slots.userInfo.emit(toAIUserInfo(account)); }); } diff --git a/packages/frontend/core/src/modules/workbench/view/workbench-root.tsx b/packages/frontend/core/src/modules/workbench/view/workbench-root.tsx index 5db0940d45..89fd9fdd86 100644 --- a/packages/frontend/core/src/modules/workbench/view/workbench-root.tsx +++ b/packages/frontend/core/src/modules/workbench/view/workbench-root.tsx @@ -1,6 +1,6 @@ import { ResizePanel } from '@affine/component/resize-panel'; import { rightSidebarWidthAtom } from '@affine/core/atoms'; -import { viewRoutes } from '@affine/core/router'; +import { workspaceRoutes } from '@affine/core/workspace-router'; import { appSettingAtom, FrameworkScope, @@ -8,7 +8,7 @@ import { useService, } from '@toeverything/infra'; import { useAtom, useAtomValue } from 'jotai'; -import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import { memo, useCallback, useEffect, useRef, useState } from 'react'; import { type RouteObject, useLocation } from 'react-router-dom'; import type { View } from '../entities/view'; @@ -26,6 +26,13 @@ const useAdapter = BUILD_CONFIG.isElectron ? useBindWorkbenchToDesktopRouter : useBindWorkbenchToBrowserRouter; +const routes: RouteObject[] = [ + { + element: , + children: workspaceRoutes, + }, +]; + export const WorkbenchRoot = memo(() => { const workbench = useService(WorkbenchService).workbench; @@ -93,15 +100,6 @@ const WorkbenchView = ({ view, index }: { view: View; index: number }) => { return; }, [handleOnFocus]); - const routes: RouteObject[] = useMemo(() => { - return [ - { - element: , - children: viewRoutes, - }, - ] satisfies RouteObject[]; - }, []); - return (
diff --git a/packages/frontend/core/src/pages/workspace/index.tsx b/packages/frontend/core/src/pages/workspace/index.tsx index f872c8dabf..126abc98ab 100644 --- a/packages/frontend/core/src/pages/workspace/index.tsx +++ b/packages/frontend/core/src/pages/workspace/index.tsx @@ -1,6 +1,6 @@ import { AffineOtherPageLayout } from '@affine/component/affine-other-page-layout'; import { AppFallback } from '@affine/core/components/affine/app-container'; -import { viewRoutes } from '@affine/core/router'; +import { workspaceRoutes } from '@affine/core/workspace-router'; import { ZipTransformer } from '@blocksuite/blocks'; import type { Workspace, WorkspaceMetadata } from '@toeverything/infra'; import { @@ -55,9 +55,10 @@ export const Component = (): ReactElement => { match && match.params.docId && match.params.workspaceId && - // TODO(eyhn): need a better way to check if it's a docId - viewRoutes.find(route => matchPath(route.path, '/' + match.params.docId)) - ?.path === '/:pageId' + // // TODO(eyhn): need a better way to check if it's a docId + workspaceRoutes.find(route => + matchPath(route.path, '/' + match.params.docId) + )?.path === '/:pageId' ) { return { docId: match.params.docId, diff --git a/packages/frontend/core/src/router.tsx b/packages/frontend/core/src/router.tsx index f9f988f792..dcaf04565e 100644 --- a/packages/frontend/core/src/router.tsx +++ b/packages/frontend/core/src/router.tsx @@ -163,41 +163,6 @@ export const topLevelRoutes = [ }, ] satisfies [RouteObject, ...RouteObject[]]; -export const viewRoutes = [ - { - path: '/all', - lazy: () => import('./pages/workspace/all-page/all-page'), - }, - { - path: '/collection', - lazy: () => import('./pages/workspace/all-collection'), - }, - { - path: '/collection/:collectionId', - lazy: () => import('./pages/workspace/collection/index'), - }, - { - path: '/tag', - lazy: () => import('./pages/workspace/all-tag'), - }, - { - path: '/tag/:tagId', - lazy: () => import('./pages/workspace/tag'), - }, - { - path: '/trash', - lazy: () => import('./pages/workspace/trash-page'), - }, - { - path: '/:pageId', - lazy: () => import('./pages/workspace/detail-page/detail-page'), - }, - { - path: '*', - lazy: () => import('./pages/404'), - }, -] satisfies [RouteObject, ...RouteObject[]]; - const createBrowserRouter = wrapCreateBrowserRouter( reactRouterCreateBrowserRouter ); diff --git a/packages/frontend/core/src/workspace-router.ts b/packages/frontend/core/src/workspace-router.ts new file mode 100644 index 0000000000..7531c7e2dc --- /dev/null +++ b/packages/frontend/core/src/workspace-router.ts @@ -0,0 +1,36 @@ +import type { RouteObject } from 'react-router-dom'; + +export const workspaceRoutes = [ + { + path: '/all', + lazy: () => import('./pages/workspace/all-page/all-page'), + }, + { + path: '/collection', + lazy: () => import('./pages/workspace/all-collection'), + }, + { + path: '/collection/:collectionId', + lazy: () => import('./pages/workspace/collection/index'), + }, + { + path: '/tag', + lazy: () => import('./pages/workspace/all-tag'), + }, + { + path: '/tag/:tagId', + lazy: () => import('./pages/workspace/tag'), + }, + { + path: '/trash', + lazy: () => import('./pages/workspace/trash-page'), + }, + { + path: '/:pageId', + lazy: () => import('./pages/workspace/detail-page/detail-page'), + }, + { + path: '*', + lazy: () => import('./pages/404'), + }, +] satisfies RouteObject[];