Skip to content

Commit

Permalink
feat(electron): close inactive views when switching workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 committed Jan 9, 2025
1 parent 7c50e3d commit c9086f2
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/frontend/core/src/components/workspace-selector/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 }) => {
Expand Down

0 comments on commit c9086f2

Please sign in to comment.