From f1c3d575ad49b2887253f2241a32f338c01e84ef Mon Sep 17 00:00:00 2001 From: Himself65 Date: Mon, 8 May 2023 14:47:47 +0800 Subject: [PATCH] fix: dispatch workspace change event (#2261) --- apps/web/src/atoms/root.ts | 18 ++++++++++++++++++ apps/web/src/layouts/workspace-layout.tsx | 11 ----------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/apps/web/src/atoms/root.ts b/apps/web/src/atoms/root.ts index e14f8ed864..0ba1f94017 100644 --- a/apps/web/src/atoms/root.ts +++ b/apps/web/src/atoms/root.ts @@ -110,10 +110,28 @@ export const rootCurrentWorkspaceAtom = atom>( // we will wait for the necessary providers to be ready await provider.whenReady; } + logger.info('current workspace', workspace); + globalThis.currentWorkspace = workspace; + globalThis.dispatchEvent( + new CustomEvent('affine:workspace:change', { + detail: { id: workspace.id }, + }) + ); return workspace; } ); +declare global { + /** + * @internal debug only + */ + // eslint-disable-next-line no-var + var currentWorkspace: AllWorkspace | undefined; + interface WindowEventMap { + 'affine:workspace:change': CustomEvent<{ id: string }>; + } +} + // Do not add `rootCurrentWorkspacePageAtom`, this is not needed. // It can be derived from `rootCurrentWorkspaceAtom` and `rootCurrentPageIdAtom` diff --git a/apps/web/src/layouts/workspace-layout.tsx b/apps/web/src/layouts/workspace-layout.tsx index 57f794462f..60a43da0aa 100644 --- a/apps/web/src/layouts/workspace-layout.tsx +++ b/apps/web/src/layouts/workspace-layout.tsx @@ -37,7 +37,6 @@ import { useSyncRouterWithCurrentWorkspaceId } from '../hooks/use-sync-router-wi import { useWorkspaces } from '../hooks/use-workspaces'; import { WorkspacePlugins } from '../plugins'; import { ModalProvider } from '../providers/ModalProvider'; -import type { AllWorkspace } from '../shared'; import { pathGenerator, publicPathGenerator } from '../shared'; import { MainContainer, @@ -46,11 +45,6 @@ import { StyledToolWrapper, } from './styles'; -declare global { - // eslint-disable-next-line no-var - var currentWorkspace: AllWorkspace; -} - const QuickSearchModal = lazy(() => import('../components/pure/quick-search-modal').then(module => ({ default: module.QuickSearchModal, @@ -260,11 +254,6 @@ export const WorkspaceLayoutInner: FC = ({ children }) => { const { jumpToPage } = useRouterHelper(router); const t = useAFFiNEI18N(); - useEffect(() => { - logger.info('currentWorkspace: ', currentWorkspace); - globalThis.currentWorkspace = currentWorkspace; - }, [currentWorkspace]); - //#region init workspace if (currentWorkspace.blockSuiteWorkspace.isEmpty) { // this is a new workspace, so we should redirect to the new page