The modal held the state of seven independent features next to the staged settings form. Each feature now has its own file with a hook for its state and the rows that show it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
65 lines
2.9 KiB
TypeScript
65 lines
2.9 KiB
TypeScript
import React, { Suspense, lazy, useEffect, useRef, useState } from 'react'
|
|
import { parseAppError } from '../../i18n'
|
|
import { Locale, PluginInfo } from '../../types'
|
|
import { api } from '../../wails'
|
|
|
|
const SchemaForm = lazy(() => import('../SchemaForm').then((m) => ({ default: m.SchemaForm })))
|
|
|
|
/** Per-VM settings of the active plugins. Changes save at once, in order. */
|
|
export function useVMPluginSettings(vmKey: string, onError: (message: string) => void) {
|
|
const [vmPlugins, setVmPlugins] = useState<PluginInfo[]>([])
|
|
const [vmPluginVals, setVmPluginVals] = useState<Record<string, Record<string, unknown>>>({})
|
|
const vmPluginSettingsSaveQueues = useRef(new Map<string, Promise<void>>())
|
|
|
|
useEffect(() => {
|
|
let live = true
|
|
Promise.all([api.ListPlugins(), api.GetVMPluginSettings(vmKey)])
|
|
.then(([list, vals]) => {
|
|
if (!live) return
|
|
const activeWithVM = (list ?? []).filter((p) => (p.state === 'active' || p.state === 'degraded') && !!p.settings)
|
|
setVmPlugins(activeWithVM)
|
|
setVmPluginVals((vals ?? {}) as Record<string, Record<string, unknown>>)
|
|
})
|
|
.catch((e) => console.warn('[settings] load VM plugin settings failed:', e))
|
|
return () => { live = false }
|
|
}, [vmKey])
|
|
|
|
const updateVMPluginSetting = (pluginID: string, patch: Record<string, unknown>) => {
|
|
setVmPluginVals((prev) => ({ ...prev, [pluginID]: { ...prev[pluginID], ...patch } }))
|
|
const queueKey = JSON.stringify([pluginID, vmKey])
|
|
const previous = vmPluginSettingsSaveQueues.current.get(queueKey) ?? Promise.resolve()
|
|
const save = previous.catch((error) => {
|
|
console.warn('[settings] previous plugin save failed:', error)
|
|
}).then(() => api.SetPluginSettings(pluginID, vmKey, patch))
|
|
vmPluginSettingsSaveQueues.current.set(queueKey, save)
|
|
void save.catch((e) => onError(parseAppError(e).summary)).finally(() => {
|
|
if (vmPluginSettingsSaveQueues.current.get(queueKey) === save) {
|
|
vmPluginSettingsSaveQueues.current.delete(queueKey)
|
|
}
|
|
})
|
|
}
|
|
|
|
return { vmPlugins, vmPluginVals, updateVMPluginSetting }
|
|
}
|
|
|
|
export const VMPluginGroup: React.FC<{ locale: Locale; group: string; state: ReturnType<typeof useVMPluginSettings> }> = ({ locale, group, state }) => {
|
|
const { vmPlugins, vmPluginVals, updateVMPluginSetting } = state
|
|
const matched = vmPlugins.filter((p) => p.settings && (p.settings['x-group'] === group || Object.values(p.settings.properties ?? {}).some((prop) => prop['x-group'] === group)))
|
|
if (matched.length === 0) return null
|
|
return (
|
|
<Suspense fallback={null}>
|
|
{matched.map((p) => (
|
|
<SchemaForm
|
|
key={p.id}
|
|
locale={locale}
|
|
schema={p.settings!}
|
|
values={vmPluginVals[p.id] ?? {}}
|
|
scopeFilter="vm"
|
|
groupFilter={group}
|
|
onChange={(_, patch) => updateVMPluginSetting(p.id, patch)}
|
|
/>
|
|
))}
|
|
</Suspense>
|
|
)
|
|
}
|