chore(frontend): improve type def
This commit is contained in:
parent
777ca15083
commit
c5d33661b2
|
@ -6,12 +6,12 @@
|
||||||
import * as Misskey from 'misskey-js';
|
import * as Misskey from 'misskey-js';
|
||||||
import { hemisphere } from '@@/js/intl-const.js';
|
import { hemisphere } from '@@/js/intl-const.js';
|
||||||
import { v4 as uuid } from 'uuid';
|
import { v4 as uuid } from 'uuid';
|
||||||
|
import { definePreferences } from './manager.js';
|
||||||
import type { Theme } from '@/theme.js';
|
import type { Theme } from '@/theme.js';
|
||||||
import type { SoundType } from '@/utility/sound.js';
|
import type { SoundType } from '@/utility/sound.js';
|
||||||
import type { Plugin } from '@/plugin.js';
|
import type { Plugin } from '@/plugin.js';
|
||||||
import type { DeviceKind } from '@/utility/device-kind.js';
|
import type { DeviceKind } from '@/utility/device-kind.js';
|
||||||
import type { 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';
|
import { DEFAULT_DEVICE_KIND } from '@/utility/device-kind.js';
|
||||||
import { deepEqual } from '@/utility/deep-equal.js';
|
import { deepEqual } from '@/utility/deep-equal.js';
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ export type SoundStore = {
|
||||||
|
|
||||||
// NOTE: デフォルト値は他の設定の状態に依存してはならない(依存していた場合、ユーザーがその設定項目単体で「初期値にリセット」した場合不具合の原因になる)
|
// NOTE: デフォルト値は他の設定の状態に依存してはならない(依存していた場合、ユーザーがその設定項目単体で「初期値にリセット」した場合不具合の原因になる)
|
||||||
|
|
||||||
export const PREF_DEF = {
|
export const PREF_DEF = definePreferences({
|
||||||
accounts: {
|
accounts: {
|
||||||
default: [] as [host: string, user: {
|
default: [] as [host: string, user: {
|
||||||
id: string;
|
id: string;
|
||||||
|
@ -88,7 +88,7 @@ export const PREF_DEF = {
|
||||||
emojis: string[];
|
emojis: string[];
|
||||||
}[],
|
}[],
|
||||||
mergeStrategy: (a, b) => {
|
mergeStrategy: (a, b) => {
|
||||||
const mergedItems = [] as (typeof a)[];
|
const mergedItems = [] as typeof a;
|
||||||
for (const x of a.concat(b)) {
|
for (const x of a.concat(b)) {
|
||||||
const sameIdItem = mergedItems.find(y => y.id === x.id);
|
const sameIdItem = mergedItems.find(y => y.id === x.id);
|
||||||
if (sameIdItem != null) {
|
if (sameIdItem != null) {
|
||||||
|
@ -119,7 +119,7 @@ export const PREF_DEF = {
|
||||||
themes: {
|
themes: {
|
||||||
default: [] as Theme[],
|
default: [] as Theme[],
|
||||||
mergeStrategy: (a, b) => {
|
mergeStrategy: (a, b) => {
|
||||||
const mergedItems = [] as (typeof a)[];
|
const mergedItems = [] as typeof a;
|
||||||
for (const x of a.concat(b)) {
|
for (const x of a.concat(b)) {
|
||||||
const sameIdItem = mergedItems.find(y => y.id === x.id);
|
const sameIdItem = mergedItems.find(y => y.id === x.id);
|
||||||
if (sameIdItem != null) {
|
if (sameIdItem != null) {
|
||||||
|
@ -464,4 +464,4 @@ export const PREF_DEF = {
|
||||||
'experimental.enableFolderPageView': {
|
'experimental.enableFolderPageView': {
|
||||||
default: false,
|
default: false,
|
||||||
},
|
},
|
||||||
} satisfies PreferencesDefinition;
|
});
|
||||||
|
|
|
@ -96,6 +96,14 @@ type PreferencesDefinitionRecord<Default, T = Default extends (...args: any) =>
|
||||||
|
|
||||||
export type PreferencesDefinition = Record<string, PreferencesDefinitionRecord<any>>;
|
export type PreferencesDefinition = Record<string, PreferencesDefinitionRecord<any>>;
|
||||||
|
|
||||||
|
export function definePreferences<T extends Record<string, unknown>>(x: {
|
||||||
|
[K in keyof T]: PreferencesDefinitionRecord<T[K]>
|
||||||
|
}): {
|
||||||
|
[K in keyof T]: PreferencesDefinitionRecord<T[K]>
|
||||||
|
} {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
export function getInitialPrefValue<K extends keyof PREF>(k: K): ValueOf<K> {
|
export function getInitialPrefValue<K extends keyof PREF>(k: K): ValueOf<K> {
|
||||||
if (typeof PREF_DEF[k].default === 'function') { // factory
|
if (typeof PREF_DEF[k].default === 'function') { // factory
|
||||||
return PREF_DEF[k].default();
|
return PREF_DEF[k].default();
|
||||||
|
|
Loading…
Reference in New Issue