import { defineStore } from 'pinia' import { computed, ref } from 'vue' import type { ServerConfig, ServerConfigValue } from '@/constants/serverConfig' type ServerConfigWithValue = ServerConfig & { /** * Current value. */ value: T /** * Initial value loaded from settings. */ initialValue: T } export const useServerConfigStore = defineStore('serverConfig', () => { const serverConfigById = ref< Record> >({}) const serverConfigs = computed(() => { return Object.values(serverConfigById.value) }) const modifiedConfigs = computed[]>( () => { return serverConfigs.value.filter((config) => { return config.initialValue !== config.value }) } ) const revertChanges = () => { for (const config of modifiedConfigs.value) { config.value = config.initialValue } } const serverConfigsByCategory = computed< Record[]> >(() => { return serverConfigs.value.reduce( (acc, config) => { const category = config.category?.[0] ?? 'General' acc[category] = acc[category] || [] acc[category].push(config) return acc }, {} as Record[]> ) }) const serverConfigValues = computed>(() => { return Object.fromEntries( serverConfigs.value.map((config) => { return [ config.id, config.value === config.defaultValue || config.value === null || config.value === undefined ? undefined : config.value ] }) ) }) const launchArgs = computed>(() => { const args: Record< string, Omit > = Object.assign( {}, ...serverConfigs.value.map((config) => { // Filter out configs that have the default value or undefined | null value if ( config.value === config.defaultValue || config.value === null || config.value === undefined ) { return {} } return config.getValue ? config.getValue(config.value) : { [config.id]: config.value } }) ) // Convert true to empty string // Convert number to string return Object.fromEntries( Object.entries(args).map(([key, value]) => { if (value === true) { return [key, ''] } return [key, value.toString()] }) ) as Record }) const commandLineArgs = computed(() => { return Object.entries(launchArgs.value) .map(([key, value]) => [`--${key}`, value]) .flat() .filter((arg: string) => arg !== '') .join(' ') }) function loadServerConfig( configs: ServerConfig[], values: Record ) { for (const config of configs) { const value = values[config.id] ?? config.defaultValue serverConfigById.value[config.id] = { ...config, value, initialValue: value } } } return { serverConfigById, serverConfigs, modifiedConfigs, serverConfigsByCategory, serverConfigValues, launchArgs, commandLineArgs, revertChanges, loadServerConfig } })