From 7345cd4678111594b27cd9276ceded6273b1936f Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sat, 23 Dec 2023 14:19:06 +0000 Subject: [PATCH] Chore: Refactor sync target code to allow labels and descriptions to be translated --- packages/lib/BaseSyncTarget.ts | 15 +++++++-- packages/lib/SyncTargetRegistry.ts | 49 +++++++++++++++--------------- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/packages/lib/BaseSyncTarget.ts b/packages/lib/BaseSyncTarget.ts index 95a42a0937..5ca6503e9d 100644 --- a/packages/lib/BaseSyncTarget.ts +++ b/packages/lib/BaseSyncTarget.ts @@ -5,6 +5,11 @@ import shim from './shim'; import ResourceService from './services/ResourceService'; import ShareService from './services/share/ShareService'; +interface CheckConfigResult { + ok: boolean; + errorMessage: string; +} + export default class BaseSyncTarget { // eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied @@ -73,17 +78,17 @@ export default class BaseSyncTarget { return null; } - public static id() { + public static id(): number { throw new Error('id() not implemented'); } // Note: it cannot be called just "name()" because that's a reserved keyword and // it would throw an obscure error in React Native. - public static targetName() { + public static targetName(): string { throw new Error('targetName() not implemented'); } - public static label() { + public static label(): string { throw new Error('label() not implemented'); } @@ -144,6 +149,10 @@ export default class BaseSyncTarget { } } + public static async checkConfig(_options: any): Promise { + throw new Error('Not implemented'); + } + public async syncStarted() { if (!this.synchronizer_) return false; if (!(await this.isAuthenticated())) return false; diff --git a/packages/lib/SyncTargetRegistry.ts b/packages/lib/SyncTargetRegistry.ts index d28351c62d..6af34c4c80 100644 --- a/packages/lib/SyncTargetRegistry.ts +++ b/packages/lib/SyncTargetRegistry.ts @@ -1,3 +1,5 @@ +import type BaseSyncTarget from './BaseSyncTarget'; + export interface SyncTargetInfo { id: number; name: string; @@ -6,12 +8,12 @@ export interface SyncTargetInfo { supportsConfigCheck: boolean; supportsRecursiveLinkedNotes: boolean; description: string; - classRef: any; + classRef: typeof BaseSyncTarget; } export default class SyncTargetRegistry { - private static reg_: Record = {}; + private static reg_: Record = {}; private static get reg() { return this.reg_; @@ -20,12 +22,24 @@ export default class SyncTargetRegistry { public static classById(syncTargetId: number) { const info = SyncTargetRegistry.reg[syncTargetId]; if (!info) throw new Error(`Invalid id: ${syncTargetId}`); - return info.classRef; + return info; } public static infoByName(name: string): SyncTargetInfo { - for (const [, info] of Object.entries(this.reg)) { - if (info.name === name) return info; + for (const [, SyncTargetClass] of Object.entries(this.reg)) { + if (SyncTargetClass.targetName() === name) { + const output: SyncTargetInfo = { + id: SyncTargetClass.id(), + name: SyncTargetClass.targetName(), + label: SyncTargetClass.label(), + classRef: SyncTargetClass, + description: SyncTargetClass.description(), + supportsSelfHosted: SyncTargetClass.supportsSelfHosted(), + supportsConfigCheck: SyncTargetClass.supportsConfigCheck(), + supportsRecursiveLinkedNotes: SyncTargetClass.supportsRecursiveLinkedNotes(), + }; + return output; + } } throw new Error(`Unknown name: ${name}`); } @@ -34,17 +48,8 @@ export default class SyncTargetRegistry { return this.infoByName(this.idToName(id)); } - public static addClass(SyncTargetClass: any) { - this.reg[SyncTargetClass.id()] = { - id: SyncTargetClass.id(), - name: SyncTargetClass.targetName(), - label: SyncTargetClass.label(), - classRef: SyncTargetClass, - description: SyncTargetClass.description(), - supportsSelfHosted: SyncTargetClass.supportsSelfHosted(), - supportsConfigCheck: SyncTargetClass.supportsConfigCheck(), - supportsRecursiveLinkedNotes: SyncTargetClass.supportsRecursiveLinkedNotes(), - }; + public static addClass(SyncTargetClass: typeof BaseSyncTarget) { + this.reg[SyncTargetClass.id()] = SyncTargetClass; } public static allIds() { @@ -54,28 +59,24 @@ export default class SyncTargetRegistry { public static nameToId(name: string) { for (const n in this.reg) { if (!this.reg.hasOwnProperty(n)) continue; - if (this.reg[n].name === name) return this.reg[n].id; + if (this.reg[n].targetName() === name) return this.reg[n].id(); } throw new Error(`Name not found: ${name}. Was the sync target registered?`); } public static idToMetadata(id: number) { - for (const n in this.reg) { - if (!this.reg.hasOwnProperty(n)) continue; - if (this.reg[n].id === id) return this.reg[n]; - } - throw new Error(`ID not found: ${id}`); + return this.infoById(id); } public static idToName(id: number) { - return this.idToMetadata(id).name; + return this.reg[id].targetName(); } public static idAndLabelPlainObject(os: string) { const output: Record = {}; for (const n in this.reg) { if (!this.reg.hasOwnProperty(n)) continue; - const info = this.reg[n]; + const info = this.infoById(this.reg[n].id()); if (info.classRef.unsupportedPlatforms().indexOf(os) >= 0) { continue; }