diff --git a/packages/frontend/src/preferences/manager.ts b/packages/frontend/src/preferences/manager.ts index 51272f45f4..0389cf612a 100644 --- a/packages/frontend/src/preferences/manager.ts +++ b/packages/frontend/src/preferences/manager.ts @@ -155,7 +155,7 @@ function normalizePreferences(preferences: PossiblyNonNormalizedPreferencesProfi } continue; } else { - if (account && isAccountDependentKey(key as keyof typeof PREF_DEF) && !records.some(([scope]) => parseScope(scope).server === host && parseScope(scope).account === account!.id)) { + if (account && isAccountDependentKey(key as keyof typeof PREF_DEF) && !records.some(([scope]) => parseScope(scope).server === host && parseScope(scope).account === account.id)) { data[key] = records.concat([[makeScope({ server: host, account: account.id, @@ -233,6 +233,7 @@ export class PreferencesManager { // TODO: desync対策 cloudの値のfetchが正常に完了していない状態でcommitすると多分値が上書きされる public commit(key: K, value: ValueOf) { + const currentAccount = this.currentAccount; // TSを黙らせるため const v = JSON.parse(JSON.stringify(value)); // deep copy 兼 vueのプロキシ解除 if (deepEqual(this.s[key], v)) { @@ -246,10 +247,10 @@ export class PreferencesManager { const record = this.getMatchedRecordOf(key); - if (parseScope(record[0]).account == null && isAccountDependentKey(key)) { + if (parseScope(record[0]).account == null && isAccountDependentKey(key) && currentAccount != null) { this.profile.preferences[key].push([makeScope({ server: host, - account: this.currentAccount!.id, + account: currentAccount.id, }), v, {}]); this.save(); return; @@ -360,11 +361,20 @@ export class PreferencesManager { } public getMatchedRecordOf(key: K): PrefRecord { + const currentAccount = this.currentAccount; // TSを黙らせるため + const records = this.profile.preferences[key]; - if (this.currentAccount == null) return records.find(([scope, v]) => parseScope(scope).account == null)!; + if (currentAccount == null) { + const record = records.find(([scope, v]) => parseScope(scope).account == null); - const accountOverrideRecord = records.find(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account === this.currentAccount!.id); + // 設計上あり得ないけどTSに怒られるため + if (record == null) throw new Error(`no record found for key: ${key}`); + + return record; + } + + const accountOverrideRecord = records.find(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account === currentAccount.id); if (accountOverrideRecord) return accountOverrideRecord; const serverOverrideRecord = records.find(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account == null); @@ -372,39 +382,41 @@ export class PreferencesManager { const record = records.find(([scope, v]) => parseScope(scope).account == null); - if (record == null) { // 設計上あり得ないけどTSに怒られるため - throw new Error(`no record found for key: ${key}`); - } + // 設計上あり得ないけどTSに怒られるため + if (record == null) throw new Error(`no record found for key: ${key}`); return record; } public isAccountOverrided(key: K): boolean { - if (this.currentAccount == null) return false; - return this.profile.preferences[key].some(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account === this.currentAccount!.id); + const currentAccount = this.currentAccount; // TSを黙らせるため + if (currentAccount == null) return false; + return this.profile.preferences[key].some(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account === currentAccount.id); } public setAccountOverride(key: K) { - if (this.currentAccount == null) return; + const currentAccount = this.currentAccount; // TSを黙らせるため + if (currentAccount == null) return; if (isAccountDependentKey(key)) throw new Error('already account-dependent'); if (this.isAccountOverrided(key)) return; const records = this.profile.preferences[key]; records.push([makeScope({ server: host, - account: this.currentAccount!.id, + account: currentAccount.id, }), this.s[key], {}]); this.save(); } public clearAccountOverride(key: K) { - if (this.currentAccount == null) return; + const currentAccount = this.currentAccount; // TSを黙らせるため + if (currentAccount == null) return; if (isAccountDependentKey(key)) throw new Error('cannot clear override for this account-dependent property'); const records = this.profile.preferences[key]; - const index = records.findIndex(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account === this.currentAccount!.id); + const index = records.findIndex(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account === currentAccount.id); if (index === -1) return; records.splice(index, 1);