From c9086f25e861fcef3ad11885cdc4e3c7b187a7b0 Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Thu, 9 Jan 2025 13:09:27 +0800 Subject: [PATCH] feat(electron): close inactive views when switching workspace --- .../src/components/workspace-selector/index.tsx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/frontend/core/src/components/workspace-selector/index.tsx b/packages/frontend/core/src/components/workspace-selector/index.tsx index 1f77cc8795721..c20b3cb91b88d 100644 --- a/packages/frontend/core/src/components/workspace-selector/index.tsx +++ b/packages/frontend/core/src/components/workspace-selector/index.tsx @@ -1,12 +1,13 @@ import { Menu, type MenuProps } from '@affine/component'; import { useNavigateHelper } from '@affine/core/components/hooks/use-navigate-helper'; import { GlobalContextService } from '@affine/core/modules/global-context'; +import { WorkbenchService } from '@affine/core/modules/workbench'; import { type WorkspaceMetadata, WorkspacesService, } from '@affine/core/modules/workspace'; import { track } from '@affine/track'; -import { useLiveData, useServices } from '@toeverything/infra'; +import { useLiveData, useService, useServices } from '@toeverything/infra'; import { useCallback, useEffect, useState } from 'react'; import { UserWithWorkspaceList } from './user-with-workspace-list'; @@ -124,22 +125,33 @@ export const WorkspaceNavigator = ({ ...props }: WorkspaceSelectorProps) => { const { jumpToPage } = useNavigateHelper(); + const workbench = useService(WorkbenchService).workbench; const handleClickWorkspace = useCallback( (workspaceMetadata: WorkspaceMetadata) => { onSelectWorkspace?.(workspaceMetadata); + + const closeInactiveViews = () => + workbench.views$.value.forEach(view => { + if (workbench.activeView$.value !== view) { + workbench.close(view); + } + }); + if (document.startViewTransition) { document.startViewTransition(() => { + closeInactiveViews(); jumpToPage(workspaceMetadata.id, 'all'); return new Promise(resolve => setTimeout(resolve, 150) ); /* start transition after 150ms */ }); } else { + closeInactiveViews(); jumpToPage(workspaceMetadata.id, 'all'); } }, - [onSelectWorkspace, jumpToPage] + [jumpToPage, onSelectWorkspace, workbench] ); const handleCreatedWorkspace = useCallback( (payload: { metadata: WorkspaceMetadata; defaultDocId?: string }) => {