diff --git a/packages/frontend/core/src/components/page-list/docs/page-list-header.tsx b/packages/frontend/core/src/components/page-list/docs/page-list-header.tsx index 0028e37c67..375b4fe945 100644 --- a/packages/frontend/core/src/components/page-list/docs/page-list-header.tsx +++ b/packages/frontend/core/src/components/page-list/docs/page-list-header.tsx @@ -6,11 +6,11 @@ import { Scrollable, useConfirmModal, } from '@affine/component'; -import { useAsyncCallback } from '@affine/core/components/hooks/affine-async-hooks'; import { useNavigateHelper } from '@affine/core/components/hooks/use-navigate-helper'; import { WorkspaceDialogService } from '@affine/core/modules/dialogs'; import type { Tag } from '@affine/core/modules/tag'; import { TagService } from '@affine/core/modules/tag'; +import { WorkbenchService } from '@affine/core/modules/workbench'; import { isNewTabTrigger } from '@affine/core/utils'; import type { Collection } from '@affine/env/filter'; import { useI18n } from '@affine/i18n'; @@ -41,31 +41,54 @@ import { PageListNewPageButton } from './page-list-new-page-button'; export const PageListHeader = () => { const t = useI18n(); - const { workspaceService } = useServices({ - WorkspaceService, - }); + const { workspaceService, workspaceDialogService, workbenchService } = + useServices({ + WorkspaceService, + WorkspaceDialogService, + WorkbenchService, + }); + const workbench = workbenchService.workbench; const workspace = workspaceService.workspace; - const { importFile, createEdgeless, createPage } = usePageHelper( - workspace.docCollection - ); + const { createEdgeless, createPage } = usePageHelper(workspace.docCollection); const title = useMemo(() => { return t['com.affine.all-pages.header'](); }, [t]); - const onImportFile = useAsyncCallback(async () => { - const options = await importFile(); - if (options.isWorkspaceFile) { - track.allDocs.header.actions.createWorkspace({ - control: 'import', - }); - } else { - track.allDocs.header.actions.createDoc({ - control: 'import', - }); - } - }, [importFile]); + const handleOpenDocs = useCallback( + (result: { + docIds: string[]; + entryId?: string; + isWorkspaceFile?: boolean; + }) => { + const { docIds, entryId, isWorkspaceFile } = result; + // If the imported file is a workspace file, open the entry page. + if (isWorkspaceFile && entryId) { + workbench.openDoc(entryId); + } else if (!docIds.length) { + return; + } + // Open all the docs when there are multiple docs imported. + if (docIds.length > 1) { + workbench.openAll(); + } else { + // Otherwise, open the only doc. + workbench.openDoc(docIds[0]); + } + }, + [workbench] + ); + + const onImportFile = useCallback(() => { + track.$.header.importModal.open(); + workspaceDialogService.open('import', undefined, payload => { + if (!payload) { + return; + } + handleOpenDocs(payload); + }); + }, [workspaceDialogService, handleOpenDocs]); return (
diff --git a/packages/frontend/core/src/components/root-app-sidebar/import-page.tsx b/packages/frontend/core/src/components/root-app-sidebar/import-page.tsx deleted file mode 100644 index c810156401..0000000000 --- a/packages/frontend/core/src/components/root-app-sidebar/import-page.tsx +++ /dev/null @@ -1,30 +0,0 @@ -import { useAsyncCallback } from '@affine/core/components/hooks/affine-async-hooks'; -import { MenuItem } from '@affine/core/modules/app-sidebar/views'; -import { useI18n } from '@affine/i18n'; -import { track } from '@affine/track'; -import type { DocCollection } from '@blocksuite/affine/store'; -import { ImportIcon } from '@blocksuite/icons/rc'; - -import { usePageHelper } from '../blocksuite/block-suite-page-list/utils'; - -const ImportPage = ({ docCollection }: { docCollection: DocCollection }) => { - const t = useI18n(); - const { importFile } = usePageHelper(docCollection); - - const onImportFile = useAsyncCallback(async () => { - const options = await importFile(); - track.$.navigationPanel.workspaceList[ - options.isWorkspaceFile ? 'createWorkspace' : 'createDoc' - ]({ - control: 'import', - }); - }, [importFile]); - - return ( - } onClick={onImportFile}> - {t['Import']()} - - ); -}; - -export default ImportPage; diff --git a/packages/frontend/core/src/desktop/pages/workspace/all-page/all-page-header.tsx b/packages/frontend/core/src/desktop/pages/workspace/all-page/all-page-header.tsx index c67ac82122..8896f1ec40 100644 --- a/packages/frontend/core/src/desktop/pages/workspace/all-page/all-page-header.tsx +++ b/packages/frontend/core/src/desktop/pages/workspace/all-page/all-page-header.tsx @@ -1,5 +1,4 @@ import { usePageHelper } from '@affine/core/components/blocksuite/block-suite-page-list/utils'; -import { useAsyncCallback } from '@affine/core/components/hooks/affine-async-hooks'; import { AllPageListOperationsMenu, PageDisplayMenu, @@ -7,12 +6,15 @@ import { } from '@affine/core/components/page-list'; import { Header } from '@affine/core/components/pure/header'; import { WorkspaceModeFilterTab } from '@affine/core/components/pure/workspace-mode-filter-tab'; +import { WorkspaceDialogService } from '@affine/core/modules/dialogs'; +import { WorkbenchService } from '@affine/core/modules/workbench'; import { isNewTabTrigger } from '@affine/core/utils'; import type { Filter } from '@affine/env/filter'; import { track } from '@affine/track'; import { PlusIcon } from '@blocksuite/icons/rc'; import { useServices, WorkspaceService } from '@toeverything/infra'; import clsx from 'clsx'; +import { useCallback } from 'react'; import * as styles from './all-page.css'; @@ -25,26 +27,49 @@ export const AllPageHeader = ({ filters: Filter[]; onChangeFilters: (filters: Filter[]) => void; }) => { - const { workspaceService } = useServices({ - WorkspaceService, - }); + const { workspaceService, workspaceDialogService, workbenchService } = + useServices({ + WorkspaceService, + WorkspaceDialogService, + WorkbenchService, + }); + const workbench = workbenchService.workbench; const workspace = workspaceService.workspace; - const { importFile, createEdgeless, createPage } = usePageHelper( - workspace.docCollection + const { createEdgeless, createPage } = usePageHelper(workspace.docCollection); + + const handleOpenDocs = useCallback( + (result: { + docIds: string[]; + entryId?: string; + isWorkspaceFile?: boolean; + }) => { + const { docIds, entryId, isWorkspaceFile } = result; + // If the imported file is a workspace file, open the entry page. + if (isWorkspaceFile && entryId) { + workbench.openDoc(entryId); + } else if (!docIds.length) { + return; + } + // Open all the docs when there are multiple docs imported. + if (docIds.length > 1) { + workbench.openAll(); + } else { + // Otherwise, open the only doc. + workbench.openDoc(docIds[0]); + } + }, + [workbench] ); - const onImportFile = useAsyncCallback(async () => { - const options = await importFile(); - if (options.isWorkspaceFile) { - track.allDocs.header.actions.createWorkspace({ - control: 'import', - }); - } else { - track.allDocs.header.actions.createDoc({ - control: 'import', - }); - } - }, [importFile]); + const onImportFile = useCallback(() => { + track.$.header.importModal.open(); + workspaceDialogService.open('import', undefined, payload => { + if (!payload) { + return; + } + handleOpenDocs(payload); + }); + }, [workspaceDialogService, handleOpenDocs]); return (