feat(electron): backup panel (#9738)

fix PD-2071, PD-2059, PD-2069, PD-2068
This commit is contained in:
pengx17
2025-01-22 03:11:28 +00:00
committed by Peng Xiao
parent 862a9d0bc4
commit 088ae0ac0a
22 changed files with 754 additions and 30 deletions

View File

@@ -0,0 +1,9 @@
import { type Framework } from '@toeverything/infra';
import { DesktopApiService } from '../desktop-api';
import { WorkspacesService } from '../workspace';
import { BackupService } from './services';
export function configureDesktopBackupModule(framework: Framework) {
framework.service(BackupService, [DesktopApiService, WorkspacesService]);
}

View File

@@ -0,0 +1,71 @@
import {
catchErrorInto,
effect,
fromPromise,
LiveData,
onComplete,
onStart,
Service,
} from '@toeverything/infra';
import { EMPTY, mergeMap, switchMap } from 'rxjs';
import type { DesktopApiService } from '../../desktop-api';
import type { WorkspacesService } from '../../workspace';
import { _addLocalWorkspace } from '../../workspace-engine';
type BackupWorkspaceResult = Awaited<
ReturnType<DesktopApiService['handler']['workspace']['getBackupWorkspaces']>
>;
export class BackupService extends Service {
constructor(
private readonly desktopApiService: DesktopApiService,
private readonly workspacesService: WorkspacesService
) {
super();
}
isLoading$ = new LiveData(false);
error$ = new LiveData<any>(null);
pageBackupWorkspaces$ = new LiveData<BackupWorkspaceResult | undefined>(
undefined
);
readonly revalidate = effect(
switchMap(() =>
fromPromise(async () => {
return this.desktopApiService.handler.workspace.getBackupWorkspaces();
}).pipe(
mergeMap(data => {
this.pageBackupWorkspaces$.setValue(data);
return EMPTY;
}),
catchErrorInto(this.error$),
onStart(() => this.isLoading$.setValue(true)),
onComplete(() => this.isLoading$.setValue(false))
)
)
);
async recoverBackupWorkspace(dbPath: string) {
const result =
await this.desktopApiService.handler.dialog.loadDBFile(dbPath);
if (result.workspaceId) {
_addLocalWorkspace(result.workspaceId);
this.workspacesService.list.revalidate();
}
return result.workspaceId;
}
async deleteBackupWorkspace(backupWorkspaceId: string) {
await this.desktopApiService.handler.workspace.deleteBackupWorkspace(
backupWorkspaceId
);
this.revalidate();
}
override dispose(): void {
this.revalidate.unsubscribe();
}
}

View File

@@ -8,6 +8,7 @@ export type SettingTab =
| 'about'
| 'plans'
| 'billing'
| 'backup' // electron only
| 'experimental-features'
| 'editor'
| 'account'