1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-18 09:35:20 +02:00
joplin/packages/lib/services/synchronizer/gui/useSyncTargetUpgrade.ts

62 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-11-05 18:58:23 +02:00
import shim from '../../../shim';
import MigrationHandler from '../MigrationHandler';
import Setting from '../../../models/Setting';
import { reg } from '../../../registry';
import { appTypeToLockType } from '../LockHandler';
const { useEffect, useState } = shim.react();
export interface SyncTargetUpgradeResult {
done: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
error: any;
}
export default function useSyncTargetUpgrade(): SyncTargetUpgradeResult {
2020-11-05 18:58:23 +02:00
const [upgradeResult, setUpgradeResult] = useState({
done: false,
error: null,
});
async function upgradeSyncTarget() {
reg.logger().info('useSyncTargetUpgrade: Starting process...');
let error = null;
try {
reg.logger().info('useSyncTargetUpgrade: Acquire synchronizer...');
const synchronizer = await reg.syncTarget().synchronizer();
reg.logger().info('useSyncTargetUpgrade: Create migration handler...');
const migrationHandler = new MigrationHandler(
synchronizer.api(),
reg.db(),
synchronizer.lockHandler(),
appTypeToLockType(Setting.value('appType')),
Setting.value('clientId'),
);
reg.logger().info('useSyncTargetUpgrade: Start upgrade...');
await migrationHandler.upgrade();
} catch (e) {
error = e;
}
reg.logger().info('useSyncTargetUpgrade: Error:', error);
if (!error) {
Setting.setValue('sync.upgradeState', Setting.SYNC_UPGRADE_STATE_IDLE);
await Setting.saveAll();
}
setUpgradeResult({
done: true,
error: error,
});
}
useEffect(() => {
2020-11-25 16:40:25 +02:00
void upgradeSyncTarget();
}, []);
return upgradeResult;
}