mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-19 15:26:59 +08:00
perf(core): only full sync before exporting (#10408)
This commit is contained in:
@@ -8,8 +8,8 @@ import type { Workspace } from '@affine/core/modules/workspace';
|
||||
import { useI18n } from '@affine/i18n';
|
||||
import { universalId } from '@affine/nbstore';
|
||||
import track from '@affine/track';
|
||||
import { useService } from '@toeverything/infra';
|
||||
import { useState } from 'react';
|
||||
import { LiveData, useLiveData, useService } from '@toeverything/infra';
|
||||
import { useMemo, useState } from 'react';
|
||||
|
||||
interface ExportPanelProps {
|
||||
workspace: Workspace;
|
||||
@@ -20,9 +20,44 @@ export const DesktopExportPanel = ({ workspace }: ExportPanelProps) => {
|
||||
const [saving, setSaving] = useState(false);
|
||||
const isOnline = useSystemOnline();
|
||||
const desktopApi = useService(DesktopApiService);
|
||||
const isLocalWorkspace = workspace.flavour === 'local';
|
||||
|
||||
const docSyncState = useLiveData(
|
||||
useMemo(() => {
|
||||
return workspace
|
||||
? LiveData.from(workspace.engine.doc.state$, null).throttleTime(500)
|
||||
: null;
|
||||
}, [workspace])
|
||||
);
|
||||
|
||||
const blobSyncState = useLiveData(
|
||||
useMemo(() => {
|
||||
return workspace
|
||||
? LiveData.from(workspace.engine.blob.state$, null).throttleTime(500)
|
||||
: null;
|
||||
}, [workspace])
|
||||
);
|
||||
|
||||
const docSynced = !docSyncState?.syncing;
|
||||
const blobSynced =
|
||||
!blobSyncState || blobSyncState.synced === blobSyncState.total;
|
||||
const [fullSynced, setFullSynced] = useState(false);
|
||||
|
||||
const shouldWaitForFullSync =
|
||||
isLocalWorkspace || !isOnline || (fullSynced && docSynced && blobSynced);
|
||||
const fullSyncing = fullSynced && (!docSynced || !blobSynced);
|
||||
|
||||
const fullSync = useAsyncCallback(async () => {
|
||||
// NOTE: doc full sync is always started by default
|
||||
// await workspace.engine.doc.waitForSynced();
|
||||
workspace.engine.blob.fullDownload().catch(() => {
|
||||
/* noop */
|
||||
});
|
||||
setFullSynced(true);
|
||||
}, [workspace.engine.blob]);
|
||||
|
||||
const onExport = useAsyncCallback(async () => {
|
||||
if (saving || !workspace) {
|
||||
if (saving) {
|
||||
return;
|
||||
}
|
||||
setSaving(true);
|
||||
@@ -30,10 +65,6 @@ export const DesktopExportPanel = ({ workspace }: ExportPanelProps) => {
|
||||
track.$.settingsPanel.workspace.export({
|
||||
type: 'workspace',
|
||||
});
|
||||
if (isOnline) {
|
||||
await workspace.engine.doc.waitForSynced();
|
||||
await workspace.engine.blob.fullSync();
|
||||
}
|
||||
|
||||
const result = await desktopApi.handler?.dialog.saveDBFileAs(
|
||||
universalId({
|
||||
@@ -53,16 +84,37 @@ export const DesktopExportPanel = ({ workspace }: ExportPanelProps) => {
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}, [desktopApi, isOnline, saving, t, workspace]);
|
||||
}, [desktopApi, saving, t, workspace]);
|
||||
|
||||
if (!shouldWaitForFullSync) {
|
||||
return (
|
||||
<SettingRow name={t['Export']()} desc={t['Full Sync Description']()}>
|
||||
<Button
|
||||
data-testid="export-affine-full-sync"
|
||||
onClick={fullSync}
|
||||
loading={fullSyncing}
|
||||
>
|
||||
{t['Full Sync']()}
|
||||
</Button>
|
||||
</SettingRow>
|
||||
);
|
||||
}
|
||||
|
||||
const button =
|
||||
isLocalWorkspace || isOnline ? t['Export']() : t['Export(Offline)']();
|
||||
const desc =
|
||||
isLocalWorkspace || isOnline
|
||||
? t['Export Description']()
|
||||
: t['Export Description(Offline)']();
|
||||
|
||||
return (
|
||||
<SettingRow name={t['Export']()} desc={t['Export Description']()}>
|
||||
<SettingRow name={t['Export']()} desc={desc}>
|
||||
<Button
|
||||
data-testid="export-affine-backup"
|
||||
onClick={onExport}
|
||||
disabled={saving}
|
||||
>
|
||||
{t['Export']()}
|
||||
{button}
|
||||
</Button>
|
||||
</SettingRow>
|
||||
);
|
||||
|
||||
@@ -195,10 +195,26 @@ export function useAFFiNEI18N(): {
|
||||
* `Export`
|
||||
*/
|
||||
Export(): string;
|
||||
/**
|
||||
* `Export (Offline)`
|
||||
*/
|
||||
["Export(Offline)"](): string;
|
||||
/**
|
||||
* `Full Sync`
|
||||
*/
|
||||
["Full Sync"](): string;
|
||||
/**
|
||||
* `You can export the entire Workspace data for backup, and the exported data can be re-imported.`
|
||||
*/
|
||||
["Export Description"](): string;
|
||||
/**
|
||||
* `You can export the entire Workspace data for backup, and the exported data can be re-imported, but you are offline now which will cause the exported data not up to date.`
|
||||
*/
|
||||
["Export Description(Offline)"](): string;
|
||||
/**
|
||||
* `You can export the entire Workspace data for backup, and the exported data can be re-imported, but you must sync all cloud data first to keep your exported data up to date.`
|
||||
*/
|
||||
["Full Sync Description"](): string;
|
||||
/**
|
||||
* `Export failed`
|
||||
*/
|
||||
@@ -2675,6 +2691,10 @@ export function useAFFiNEI18N(): {
|
||||
* `Workspace name`
|
||||
*/
|
||||
["com.affine.nameWorkspace.subtitle.workspace-name"](): string;
|
||||
/**
|
||||
* `Workspace type`
|
||||
*/
|
||||
["com.affine.nameWorkspace.subtitle.workspace-type"](): string;
|
||||
/**
|
||||
* `Name your workspace`
|
||||
*/
|
||||
@@ -3513,11 +3533,11 @@ export function useAFFiNEI18N(): {
|
||||
*/
|
||||
["com.affine.payment.cloud.free.benefit.g2-5"](): string;
|
||||
/**
|
||||
* `Open-source under MIT license.`
|
||||
* `Local Editor under MIT license.`
|
||||
*/
|
||||
["com.affine.payment.cloud.free.description"](): string;
|
||||
/**
|
||||
* `FOSS + Basic`
|
||||
* `Local FOSS + Cloud Basic`
|
||||
*/
|
||||
["com.affine.payment.cloud.free.name"](): string;
|
||||
/**
|
||||
|
||||
@@ -39,7 +39,11 @@
|
||||
"Enable AFFiNE Cloud Description": "If enabled, the data in this workspace will be backed up and synchronised via AFFiNE Cloud.",
|
||||
"Enable cloud hint": "The following functions rely on AFFiNE Cloud. All data is stored on the current device. You can enable AFFiNE Cloud for this workspace to keep data in sync with the cloud.",
|
||||
"Export": "Export",
|
||||
"Export(Offline)": "Export (Offline)",
|
||||
"Full Sync": "Full Sync",
|
||||
"Export Description": "You can export the entire Workspace data for backup, and the exported data can be re-imported.",
|
||||
"Export Description(Offline)": "You can export the entire Workspace data for backup, and the exported data can be re-imported. But you are offline now which will cause the exported data not up to date.",
|
||||
"Full Sync Description": "You can export the entire Workspace data for backup, and the exported data can be re-imported. But you must sync all cloud data first to keep your exported data up to date.",
|
||||
"Export failed": "Export failed",
|
||||
"Export success": "Export success",
|
||||
"Export to HTML": "Export to HTML",
|
||||
|
||||
Reference in New Issue
Block a user