Files
TREK/client/src/components/Plugins/settingsForm.ts
T
jubnl 1e6ab069cd Fix/sdk default (#2199)
* feat(plugin-sdk): settings-field default + unknown-attribute warning

* feat(shared): default on the plugin settings-field contract

* feat(server): persist and serve plugin settings-field defaults

* feat(client): pre-fill plugin settings defaults in the admin and user forms

* feat(server): enforce required plugin settings on both save paths

* feat(client): block plugin-settings save on an empty required field

* feat(server): resolve plugin settings defaults at runtime

A settings-field `default` was stored and served to the form but never
reached the plugin: the child's init envelope, `ctx.settings.get()`, the
channel dispatch's readAll and the OAuth provider config all read the
stored blob only. Fold `stored ?? default` in on every path that hands
config to a plugin (settings-defaults.ts), and let a default satisfy
`required` on both save gates — refusing a save the runtime would have
accepted was a contradiction.

Also: one `isFilled` rule shared by the save gate and `hasRequired`
(checkbox exempt, whitespace empty); the install parser drops a
non-boolean checkbox default or a select default outside its options,
matching what the SDK already refuses; export SETTING_FIELD_KEYS so the
SDK's mirror can be parity-tested.

* feat(client): share the plugin settings form rules across all three forms

The mobile user settings screen was a third consumer of the fields/config
contract that never got `default` pre-fill or the required check, and
the seed/patch loops lived in five hand copies. Extract
seedSettingsValues / findMissingRequired / settingsPatch into one module
and use it from the admin instance form, the desktop user tab and the
mobile screen. The user forms now surface a 4xx `{ error }` from the
server (the required-field refusal) instead of the generic toast.

* feat(plugin-sdk): seed dev fixtures from manifest defaults

`trek-plugin dev` handed the plugin only the fixture values, so a plugin
relying on its manifest defaults read undefined in dev and only in dev.
Seed ctx.config / ctx.settings from the declared defaults (a fixture
value still wins) via a new exported settingDefaults(manifest, scope).
SETTING_FIELD_KEYS is now parity-tested against the host's list.

* docs(plugins): document settings defaults as runtime fallbacks

A `default` is the field's effective value wherever nobody set one, not
just a form pre-fill; `required` is enforced on both ends; unknown
attributes are dropped at install and warned on by validate. Add a
Settings section with the attribute table to the SDK README and update
the ctx type comments, the shared schema, and the wiki guide.

* add missing exports
2026-09-02 18:42:16 +02:00

59 lines
2.4 KiB
TypeScript

/**
* Shared rules for the plugin-settings forms — the admin instance form
* (useInstanceSettings), the desktop user tab (PluginSettingsTab) and the mobile user
* screen (MSettingsPlugins) all render the same `fields` + `config` contract and used to
* each hand-copy these loops, which is how the mobile screen missed `default` and the
* required check.
*/
import type { PluginUserSettingField } from '../../api/client';
/** What the server echoes in place of a stored secret; sending it back means "unchanged". */
export const SECRET_MASK = '••••••••';
export type SettingsValues = Record<string, string | boolean>;
/**
* Form draft from the stored config, falling back to each field's manifest `default`
* when nothing is stored (the same `stored ?? default` rule the runtime applies, so the
* form shows the value the plugin actually gets). Checkboxes become booleans, everything
* else a string.
*/
export function seedSettingsValues(fields: PluginUserSettingField[], config: Record<string, unknown>): SettingsValues {
const values: SettingsValues = {};
for (const f of fields) {
const stored = config[f.key];
const seeded = stored === undefined || stored === null ? f.default : stored;
values[f.key] = f.input_type === 'checkbox' ? seeded === true : seeded == null ? '' : String(seeded);
}
return values;
}
/**
* The first `required` field the draft leaves blank, or undefined when Save may proceed.
* Mirrors the server gate: a checkbox is exempt (required would mean "must be true", a
* consent flow, not a setting), whitespace is blank, an untouched secret (still the mask)
* is filled.
*/
export function findMissingRequired(
fields: PluginUserSettingField[],
values: SettingsValues
): PluginUserSettingField | undefined {
return fields.find((f) => {
if (!f.required || f.input_type === 'checkbox') return false;
const v = values[f.key];
if (f.secret && v === SECRET_MASK) return false;
return String(v ?? '').trim() === '';
});
}
/** The save body: every field's draft value, minus an untouched secret so the mask never overwrites the stored ciphertext. */
export function settingsPatch(fields: PluginUserSettingField[], values: SettingsValues): Record<string, unknown> {
const patch: Record<string, unknown> = {};
for (const f of fields) {
const v = values[f.key];
if (f.secret && v === SECRET_MASK) continue;
patch[f.key] = v;
}
return patch;
}