From 3129fcf164369ecd1ed7fd26e17e0806ada4e435 Mon Sep 17 00:00:00 2001
From: syuilo <4439005+syuilo@users.noreply.github.com>
Date: Wed, 12 Mar 2025 20:17:54 +0900
Subject: [PATCH] fix(frontend): fix type errors

---
 packages/frontend/src/preferences.ts          |  4 +--
 packages/frontend/src/preferences/def.ts      |  9 ++----
 .../preferences/{profile.ts => manager.ts}    | 30 ++++++++++++++-----
 packages/frontend/src/preferences/utility.ts  |  2 +-
 4 files changed, 28 insertions(+), 17 deletions(-)
 rename packages/frontend/src/preferences/{profile.ts => manager.ts} (92%)

diff --git a/packages/frontend/src/preferences.ts b/packages/frontend/src/preferences.ts
index ab73fe2b77..7d1821b72b 100644
--- a/packages/frontend/src/preferences.ts
+++ b/packages/frontend/src/preferences.ts
@@ -4,10 +4,10 @@
  */
 
 import { v4 as uuid } from 'uuid';
-import type { PreferencesProfile, StorageProvider } from '@/preferences/profile.js';
+import type { PreferencesProfile, StorageProvider } from '@/preferences/manager.js';
 import { cloudBackup } from '@/preferences/utility.js';
 import { miLocalStorage } from '@/local-storage.js';
-import { isSameCond, ProfileManager } from '@/preferences/profile.js';
+import { isSameCond, ProfileManager } from '@/preferences/manager.js';
 import { store } from '@/store.js';
 import { $i } from '@/account.js';
 import { misskeyApi } from '@/utility/misskey-api.js';
diff --git a/packages/frontend/src/preferences/def.ts b/packages/frontend/src/preferences/def.ts
index b75b99d6b5..47d0ab5cbc 100644
--- a/packages/frontend/src/preferences/def.ts
+++ b/packages/frontend/src/preferences/def.ts
@@ -9,7 +9,8 @@ import type { Theme } from '@/theme.js';
 import type { SoundType } from '@/utility/sound.js';
 import type { Plugin } from '@/plugin.js';
 import type { DeviceKind } from '@/utility/device-kind.js';
-import type { Column, DeckProfile } from '@/deck.js';
+import type { DeckProfile } from '@/deck.js';
+import type { PreferencesDefinition } from './manager.js';
 import { DEFAULT_DEVICE_KIND } from '@/utility/device-kind.js';
 
 /** サウンド設定 */
@@ -324,8 +325,4 @@ export const PREF_DEF = {
 			sfxVolume: 1,
 		},
 	},
-} satisfies Record<string, {
-	default: any;
-	accountDependent?: boolean;
-	serverDependent?: boolean;
-}>;
+} satisfies PreferencesDefinition;
diff --git a/packages/frontend/src/preferences/profile.ts b/packages/frontend/src/preferences/manager.ts
similarity index 92%
rename from packages/frontend/src/preferences/profile.ts
rename to packages/frontend/src/preferences/manager.ts
index 2ac4e58d14..9866227d93 100644
--- a/packages/frontend/src/preferences/profile.ts
+++ b/packages/frontend/src/preferences/manager.ts
@@ -84,6 +84,12 @@ export type StorageProvider = {
 	cloudSet: <K extends keyof PREF>(ctx: { key: K; cond: Cond; value: ValueOf<K>; }) => Promise<void>;
 };
 
+export type PreferencesDefinition = Record<string, {
+	default: any;
+	accountDependent?: boolean;
+	serverDependent?: boolean;
+}>;
+
 export class ProfileManager {
 	private storageProvider: StorageProvider;
 	public profile: PreferencesProfile;
@@ -118,6 +124,10 @@ export class ProfileManager {
 		// TODO: 定期的にクラウドの値をフェッチ
 	}
 
+	private isAccountDependentKey<K extends keyof PREF>(key: K): boolean {
+		return (PREF_DEF as PreferencesDefinition)[key].accountDependent === true;
+	}
+
 	private rewriteRawState<K extends keyof PREF>(key: K, value: ValueOf<K>) {
 		const v = JSON.parse(JSON.stringify(value)); // deep copy 兼 vueのプロキシ解除
 		this.r[key].value = this.s[key] = v;
@@ -129,7 +139,7 @@ export class ProfileManager {
 		this.rewriteRawState(key, value);
 
 		const record = this.getMatchedRecordOf(key);
-		if (parseCond(record[0]).account == null && PREF_DEF[key].accountDependent) {
+		if (parseCond(record[0]).account == null && this.isAccountDependentKey(key)) {
 			this.profile.preferences[key].push([makeCond({
 				account: `${host}/${$i!.id}`,
 			}), value, {}]);
@@ -186,9 +196,10 @@ export class ProfileManager {
 
 	private genStates() {
 		const states = {} as { [K in keyof PREF]: ValueOf<K> };
-		for (const key in PREF_DEF) {
+		for (const _key in PREF_DEF) {
+			const key = _key as keyof PREF;
 			const record = this.getMatchedRecordOf(key);
-			states[key] = record[1];
+			(states[key] as any) = record[1];
 		}
 
 		return states;
@@ -196,7 +207,8 @@ export class ProfileManager {
 
 	private async fetchCloudValues() {
 		const needs = [] as { key: keyof PREF; cond: Cond; }[];
-		for (const key in PREF_DEF) {
+		for (const _key in PREF_DEF) {
+			const key = _key as keyof PREF;
 			const record = this.getMatchedRecordOf(key);
 			if (record[2].sync) {
 				needs.push({
@@ -208,7 +220,8 @@ export class ProfileManager {
 
 		const cloudValues = await this.storageProvider.cloudGets({ needs });
 
-		for (const key in PREF_DEF) {
+		for (const _key in PREF_DEF) {
+			const key = _key as keyof PREF;
 			const record = this.getMatchedRecordOf(key);
 			if (record[2].sync && Object.hasOwn(cloudValues, key) && cloudValues[key] !== undefined) {
 				const cloudValue = cloudValues[key];
@@ -290,7 +303,7 @@ export class ProfileManager {
 
 	public setAccountOverride<K extends keyof PREF>(key: K) {
 		if ($i == null) return;
-		if (PREF_DEF[key].accountDependent) throw new Error('already account-dependent');
+		if (this.isAccountDependentKey(key)) throw new Error('already account-dependent');
 		if (this.isAccountOverrided(key)) return;
 
 		const records = this.profile.preferences[key];
@@ -303,7 +316,7 @@ export class ProfileManager {
 
 	public clearAccountOverride<K extends keyof PREF>(key: K) {
 		if ($i == null) return;
-		if (PREF_DEF[key].accountDependent) throw new Error('cannot clear override for this account-dependent property');
+		if (this.isAccountDependentKey(key)) throw new Error('cannot clear override for this account-dependent property');
 
 		const records = this.profile.preferences[key];
 
@@ -377,7 +390,8 @@ export class ProfileManager {
 	public rewriteProfile(profile: PreferencesProfile) {
 		this.profile = profile;
 		const states = this.genStates();
-		for (const key in states) {
+		for (const _key in states) {
+			const key = _key as keyof PREF;
 			this.rewriteRawState(key, states[key]);
 		}
 
diff --git a/packages/frontend/src/preferences/utility.ts b/packages/frontend/src/preferences/utility.ts
index fc6eff5f49..c37dbcf96b 100644
--- a/packages/frontend/src/preferences/utility.ts
+++ b/packages/frontend/src/preferences/utility.ts
@@ -4,7 +4,7 @@
  */
 
 import { ref, watch } from 'vue';
-import type { PreferencesProfile } from './profile.js';
+import type { PreferencesProfile } from './manager.js';
 import type { MenuItem } from '@/types/menu.js';
 import { copyToClipboard } from '@/utility/copy-to-clipboard.js';
 import { i18n } from '@/i18n.js';