From ccb0df10e4dfdc33f1f9b544e6b57d58c02b802a Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Sun, 23 Jul 2023 18:45:01 +0800 Subject: [PATCH] fix: temp workaround for missing blobs in export (#3347) --- .../new-workspace-setting-detail/export.tsx | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/apps/core/src/components/affine/new-workspace-setting-detail/export.tsx b/apps/core/src/components/affine/new-workspace-setting-detail/export.tsx index 8e365352c4..fa31d40832 100644 --- a/apps/core/src/components/affine/new-workspace-setting-detail/export.tsx +++ b/apps/core/src/components/affine/new-workspace-setting-detail/export.tsx @@ -1,30 +1,51 @@ import { Button, toast } from '@affine/component'; import { SettingRow } from '@affine/component/setting-components'; +import { isDesktop } from '@affine/env/constant'; import { useAFFiNEI18N } from '@affine/i18n/hooks'; -import type { FC } from 'react'; +import { type FC, useCallback } from 'react'; import type { AffineOfficialWorkspace } from '../../../shared'; +async function syncBlobsToSqliteDb(workspace: AffineOfficialWorkspace) { + if (window.apis && isDesktop) { + const bs = workspace.blockSuiteWorkspace.blobs; + const blobsInDb = await window.apis.db.getBlobKeys(workspace.id); + const blobsInStorage = await bs.list(); + const blobsToSync = blobsInStorage.filter( + blob => !blobsInDb.includes(blob) + ); + + await Promise.all( + blobsToSync.map(async blobKey => { + const blob = await bs.get(blobKey); + if (blob) { + const bin = new Uint8Array(await blob.arrayBuffer()); + await window.apis.db.addBlob(workspace.id, blobKey, bin); + } + }) + ); + } +} + export const ExportPanel: FC<{ workspace: AffineOfficialWorkspace; }> = ({ workspace }) => { const workspaceId = workspace.id; const t = useAFFiNEI18N(); + const onExport = useCallback(async () => { + await syncBlobsToSqliteDb(workspace); + const result = await window.apis?.dialog.saveDBFileAs(workspaceId); + if (result?.error) { + // @ts-expect-error: result.error is dynamic + toast(t[result.error]()); + } else if (!result?.canceled) { + toast(t['Export success']()); + } + }, [t, workspace, workspaceId]); return ( <> -