From 713551fbf1fc06e051c2dd0420f3961a49019cde Mon Sep 17 00:00:00 2001 From: donteatfriedrice Date: Tue, 12 Nov 2024 02:40:57 +0000 Subject: [PATCH] feat: add import snapshot option to import dialog (#8778) --- .../core/src/desktop/dialogs/import/index.tsx | 55 +++++++++++++++++-- packages/frontend/i18n/src/resources/en.json | 1 + .../frontend/i18n/src/resources/zh-Hans.json | 1 + .../frontend/i18n/src/resources/zh-Hant.json | 1 + 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/packages/frontend/core/src/desktop/dialogs/import/index.tsx b/packages/frontend/core/src/desktop/dialogs/import/index.tsx index 855aa5e00f..2311061b52 100644 --- a/packages/frontend/core/src/desktop/dialogs/import/index.tsx +++ b/packages/frontend/core/src/desktop/dialogs/import/index.tsx @@ -11,6 +11,7 @@ import { MarkdownTransformer, NotionHtmlTransformer, openFileOrFiles, + ZipTransformer, } from '@blocksuite/affine/blocks'; import type { DocCollection } from '@blocksuite/affine/store'; import { @@ -18,14 +19,19 @@ import { HelpIcon, NotionIcon, } from '@blocksuite/icons/rc'; -import { useService, WorkspaceService } from '@toeverything/infra'; +import { + FeatureFlagService, + useLiveData, + useService, + WorkspaceService, +} from '@toeverything/infra'; import { cssVar } from '@toeverything/theme'; import { cssVarV2 } from '@toeverything/theme/v2'; import { type ReactElement, useCallback, useState } from 'react'; import * as style from './styles.css'; -type ImportType = 'markdown' | 'markdownZip' | 'notion'; +type ImportType = 'markdown' | 'markdownZip' | 'notion' | 'snapshot'; type AcceptType = 'Markdown' | 'Zip'; type Status = 'idle' | 'importing' | 'success' | 'error'; type ImportResult = { @@ -46,6 +52,7 @@ const DISCORD_URL = 'https://discord.gg/whd5mjYqVw'; const importOptions = [ { + key: 'markdown', label: 'com.affine.import.markdown-files', prefixIcon: ( , suffixIcon: ( @@ -137,6 +146,21 @@ const importConfigs: Record = { }; }, }, + snapshot: { + fileOptions: { acceptType: 'Zip', multiple: false }, + importFunction: async (docCollection, file) => { + if (Array.isArray(file)) { + throw new Error('Expected a single zip file for snapshot import'); + } + const docIds = (await ZipTransformer.importDocs(docCollection, file)) + .filter(doc => doc !== undefined) + .map(doc => doc.id); + + return { + docIds, + }; + }, + }, }; const ImportOptionItem = ({ @@ -176,14 +200,27 @@ const ImportOptions = ({ onImport: (type: ImportType) => void; }) => { const t = useI18n(); + const featureFlagService = useService(FeatureFlagService); + const enableSnapshotImportExport = useLiveData( + featureFlagService.flags.enable_snapshot_import_export.$ + ); + return ( <>
{t['Import']()}
{importOptions.map( - ({ label, prefixIcon, suffixIcon, suffixTooltip, testId, type }) => ( + ({ + key, + label, + prefixIcon, + suffixIcon, + suffixTooltip, + testId, + type, + }) => ( + {enableSnapshotImportExport && ( +
+ {t['Import']()}{' '} + onImport('snapshot')}> + {t['Snapshot']()}. + +
+ )}
{t['com.affine.import.modal.tip']()}{' '} Discord - {' '} + .
diff --git a/packages/frontend/i18n/src/resources/en.json b/packages/frontend/i18n/src/resources/en.json index f6afd8c7f2..df725c912b 100644 --- a/packages/frontend/i18n/src/resources/en.json +++ b/packages/frontend/i18n/src/resources/en.json @@ -522,6 +522,7 @@ "com.affine.import.modal.tip": "If you'd like to request support for additional file types, feel free to let us know on", "com.affine.import.notion": "Notion", "com.affine.import.notion.tooltip": "Import your Notion data. Supported import formats: HTML with subpages.", + "com.affine.import.snapshot": "Snapshot", "com.affine.import.status.failed.message": "Import failed, please try again.", "com.affine.import.status.failed.message.no-file-selected": "No file selected", "com.affine.import.status.failed.title": "Import failure", diff --git a/packages/frontend/i18n/src/resources/zh-Hans.json b/packages/frontend/i18n/src/resources/zh-Hans.json index af10717d8d..d0e4227297 100644 --- a/packages/frontend/i18n/src/resources/zh-Hans.json +++ b/packages/frontend/i18n/src/resources/zh-Hans.json @@ -517,6 +517,7 @@ "com.affine.import.markdown-with-media-files": "Markdown 文件(带媒体文件) (.zip)", "com.affine.import.modal.tip": "如果您希望请求支持其他文件类型,请随时告诉我们", "com.affine.import.notion": "Notion", + "com.affine.import.snapshot": "快照", "com.affine.import.notion.tooltip": "导入您的 Notion 数据。支持导入格式:HTML 带子页面。", "com.affine.import.status.failed.message": "导入失败,请重试。", "com.affine.import.status.failed.message.no-file-selected": "未选择文件", diff --git a/packages/frontend/i18n/src/resources/zh-Hant.json b/packages/frontend/i18n/src/resources/zh-Hant.json index 9b16bd1bcc..42f55eb4b0 100644 --- a/packages/frontend/i18n/src/resources/zh-Hant.json +++ b/packages/frontend/i18n/src/resources/zh-Hant.json @@ -514,6 +514,7 @@ "com.affine.import.markdown-with-media-files": "Markdown 文件(帶媒體文件) (.zip)", "com.affine.import.modal.tip": "如果您希望請求支持其他文件類型,請隨時告訴我們", "com.affine.import.notion": "Notion", + "com.affine.import.snapshot": "快照", "com.affine.import.notion.tooltip": "導入您的 Notion 數據。支持導入格式:HTML 帶子頁面。", "com.affine.import.status.failed.message": "導入失敗,請重試。", "com.affine.import.status.failed.message.no-file-selected": "未選擇文件",