fix(settings): count only real changes as unsaved

Runtime settings and the subscription edit dialog compare the
normalized draft with the stored values, so an edit put back to its
value is no longer unsaved while save stays disabled. The
subscription dialog uses the same check for the guard and for save.
This commit is contained in:
Zakk committed 2026-10-02 18:13:22 +10:00
1 parent 0523195a8f
commit c488927580
5 files changed
+64 -46

No files matched your search

+4 -3
View File
@@ -13,9 +13,10 @@ This changelog follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
- Pressing Enter twice quickly on a drag handle no longer starts a second drag and raises an error; the second press is ignored until the first drag is ready for its keys. (#300)
- On the Config page, every module card shows one summary line under its header instead of status dots, a per-node edit link list, and the group policies. The Nodes page no longer accepts the `editNodeSource` link parameter that only those links used. (#298)
- An operation the backend accepted whose progress can no longer be read is reported as having an unknown result, and the page is read again, for every operation rather than only configuration writes. A group selection or runtime setting refused as temporarily unavailable reads the group or the settings back, since the change may have been stored, and the control stays busy until that read lands and the backend's Retry-After has passed.
- Applying held rules keeps going when the page that started it closes: rules whose file the backend accepted leave the held list, so the next apply does not write them a second time.
- An operation whose result is unknown is reported with a neutral notice instead of an error when it comes from applying rules, a group action, a geodata update, refreshing every subscription or saving runtime settings. A rule apply that fails after its dialog or page closed reports the failure as a notice.
- An operation the backend accepted whose progress can no longer be read is reported as having an unknown result, and the page is read again, for every operation rather than only configuration writes. A group selection or runtime setting refused as temporarily unavailable reads the group or the settings back, since the change may have been stored, and the control stays busy until that read lands and the backend's Retry-After has passed. (#303)
- Applying held rules keeps going when the page that started it closes: rules whose file the backend accepted leave the held list, so the next apply does not write them a second time. (#303)
- An operation whose result is unknown is reported with a neutral notice instead of an error when it comes from applying rules, a group action, a geodata update, refreshing every subscription or saving runtime settings. A rule apply that fails after its dialog or page closed reports the failure as a notice. (#303)
- A runtime setting or a subscription edit put back to its stored value no longer counts as unsaved, so leaving the form does not ask to discard it. (#303)
- On a phone, the About dialog stacks its links full width instead of leaving one alone, scrolls its content between the title and the Close button, and every dialog keeps its bottom margin above a mobile browser's toolbar. The desktop layout is unchanged.
## [0.1.0-beta.12] - 2026-09-30
+14 -29
View File
@@ -24,7 +24,7 @@ import {
nodeSource,
ownedNodes,
providerCreate,
providerEdited,
providerChanges,
providerRows,
renameReferences,
selectedProvider,
@@ -70,12 +70,14 @@ export function useNodesPage({go, query}: PageProps) {
// Why the last submit did not land; `id` changes with each refusal so the alert takes focus again.
const [problem, setProblem] = useState<{id: number; text: string} | null>(null);
const intervalSeconds = draftInterval(form.interval);
const edited =
dialog?.kind === 'editProvider'
? providerEdited(form, dialog.entry)
: dialog?.kind === 'editNode'
? form.name !== dialog.entry.name || form.value !== dialog.entry.link
: !!(form.name || form.value);
const resources = useCapabilities().data?.resources;
const createOptions = resources?.providers.create_options;
const changes = dialog?.kind === 'editProvider' ? providerChanges(form, dialog.entry, createOptions?.cache) : null;
const edited = changes
? Object.values(changes).some(Boolean)
: dialog?.kind === 'editNode'
? form.name !== dialog.entry.name || form.value !== dialog.entry.link
: !!(form.name || form.value);
const guard = useDraftGuard(!!dialog && edited, () => {
session.current++;
setDialog(null);
@@ -91,7 +93,6 @@ export function useNodesPage({go, query}: PageProps) {
setDialog(next);
}, []);
const lang = useLang();
const resources = useCapabilities().data?.resources;
const providers = useProviders(offered(resources, 'providers', {whileLoading: true}));
const nodes = useNodes(offered(resources, 'nodes', {whileLoading: true}));
const names = useOutboundNames();
@@ -294,11 +295,11 @@ export function useNodesPage({go, query}: PageProps) {
const written = writeSubscriptionEntry(text, from, {
tag,
url,
...(intervalChanged ? {interval: draftInterval(form.interval)!} : {}),
...(form.agent !== (dialog.entry.ua ?? '') ? {ua: form.agent.trim() || null} : {}),
...(editCache.changed ? {cache: editCache.value} : {}),
...(changes?.interval ? {interval: draftInterval(form.interval)!} : {}),
...(changes?.agent ? {ua: form.agent.trim() || null} : {}),
...(changes?.cache ? {cache: form.cache} : {}),
// Following the routing rules is honk's default, so choosing it removes the route.
...(routeChanged ? {route: editRoute === 'routing' ? null : editRoute} : {})
...(changes?.route ? {route: (form.route || 'routing') === 'routing' ? null : form.route} : {})
});
if (!follow) return written;
return citingGroups(written, from).reduce((out, group) => removeSubtagsFromGroup(addSubtagsToGroup(out, group, [tag]), group, [from]), written);
@@ -340,7 +341,6 @@ export function useNodesPage({go, query}: PageProps) {
: dialog.kind === 'editProvider'
? t('nodes.editProviderTitle', {name: dialog.entry.tag})
: t(dialog.kind === 'removeProvider' ? 'nodes.removeProviderTitle' : 'nodes.removeNodeTitle', {name: dialog.item.name});
const createOptions = resources?.providers.create_options;
const agentKey = agentProblem(form.agent, false);
const agentError = agentKey && t(agentKey);
// An entry's own User-Agent must also be written back as a quoted value; empty removes it, leaving the engine default.
@@ -348,7 +348,6 @@ export function useNodesPage({go, query}: PageProps) {
const editAgentError = editAgentKey && t(editAgentKey);
// The switch shows the entry's cache, or the default a new subscription gets when the entry sets none.
const writtenCache = dialog?.kind === 'editProvider' ? (dialog.entry.cache ?? createOptions?.cache) : undefined;
const editCache = {value: form.cache ?? writtenCache ?? null, changed: form.cache !== null && form.cache !== writtenCache};
// A kept name is valid as written; a new one is bare, as doona writes names, and free among the subscriptions.
const editName = form.name.trim();
const editNameError =
@@ -359,11 +358,8 @@ export function useNodesPage({go, query}: PageProps) {
: declared.has(editName)
? t('nodes.tagTaken')
: null;
const editRoute = form.route || 'routing';
const routeChanged = dialog?.kind === 'editProvider' && editRoute !== (dialog.entry.route || 'routing');
const intervalKey = intervalProblem(form.interval);
const intervalError = intervalKey && t(intervalKey);
const intervalChanged = dialog?.kind === 'editProvider' && intervalSeconds != null && intervalSeconds !== dialog.entry.interval;
const editUrlValid = isSubscriptionUrl(form.value) && isQuotable(form.value.trim());
const references =
dialog?.kind === 'editProvider' && editName !== dialog.entry.tag
@@ -391,18 +387,7 @@ export function useNodesPage({go, query}: PageProps) {
dialog?.kind === 'editNode'
? nodeState!.valid
: dialog?.kind === 'editProvider'
? !!editName &&
editNameError === null &&
intervalSeconds !== null &&
editUrlValid &&
editAgentError === null &&
!references.elsewhere.length &&
(editName !== dialog.entry.tag ||
form.value.trim() !== dialog.entry.url ||
(form.agent !== (dialog.entry.ua ?? '') && (form.agent.trim() || null) !== dialog.entry.ua) ||
editCache.changed ||
intervalChanged ||
routeChanged)
? !!editName && editNameError === null && intervalSeconds !== null && editUrlValid && editAgentError === null && !references.elsewhere.length && edited
: dialog?.kind === 'provider'
? isBareName(form.name.trim()) && isSubscriptionUrl(form.value) && !agentError && intervalSeconds !== null
: dialog?.kind === 'node'
+15 -1
View File
@@ -404,5 +404,19 @@ it.each([
['-1h', true]
] as const)('guards only changed subscription intervals (%s)', (interval, edited) => {
const entry = readSubscriptionEntries("subscription {\n harbor: 'https://example.org/sub' {\n interval: 1h\n }\n}")[0];
expect(providerEdited({name: entry.tag, value: entry.url, interval, agent: '', cache: null, route: ''}, entry)).toBe(edited);
expect(providerEdited({name: entry.tag, value: entry.url, interval, agent: '', cache: null, route: ''}, entry, undefined)).toBe(edited);
});
it.each([
['the cache switched back to the default', {cache: true}, true, false],
['the cache switched off the default', {cache: false}, true, true],
['the cache switched with no default', {cache: true}, undefined, true],
['the route put back to routing', {route: 'routing'}, undefined, false],
['another route', {route: 'direct'}, undefined, true],
['the name with spaces around it', {name: ' harbor '}, undefined, false],
['a blank User-Agent', {agent: ' '}, undefined, false]
] as const)('counts only a real change as unsaved: %s', (_, change, defaultCache, edited) => {
const entry = readSubscriptionEntries("subscription {\n harbor: 'https://example.org/sub'\n}")[0];
const form = {name: entry.tag, value: entry.url, interval: '', agent: '', cache: null, route: '', ...change};
expect(providerEdited(form, entry, defaultCache)).toBe(edited);
});
+15 -9
View File
@@ -47,18 +47,24 @@ const latencyOf = (node: Node) => healthMillis(preferredHealth(node));
export const editableSource = (daeText: boolean, canWrite: boolean, source: ConfigSource, complete: boolean | null | undefined) =>
daeText && canWrite && source.writable && complete === true;
export function providerEdited(form: ProviderForm, entry: SubscriptionText) {
// What the edit dialog changes in the entry, as the write reads it: names and URLs trimmed, a blank User-Agent as none,
// an empty route as `routing`, and the cache switch against the entry's value or the default it falls back to. An
// interval not yet valid counts as changed, so the draft is kept; the dialog refuses to save it.
export function providerChanges(form: ProviderForm, entry: SubscriptionText, defaultCache: boolean | undefined) {
const interval = draftInterval(form.interval);
return (
form.name !== entry.tag ||
form.value !== entry.url ||
(form.interval !== '' && (interval === null || interval !== entry.interval)) ||
form.agent !== (entry.ua ?? '') ||
form.cache !== null ||
form.route !== (entry.route ?? '')
);
return {
name: form.name.trim() !== entry.tag,
url: form.value.trim() !== entry.url,
interval: form.interval !== '' && (interval === null || interval !== entry.interval),
agent: form.agent !== (entry.ua ?? '') && (form.agent.trim() || null) !== (entry.ua ?? null),
cache: form.cache !== null && form.cache !== (entry.cache ?? defaultCache),
route: (form.route || 'routing') !== (entry.route || 'routing')
};
}
export const providerEdited = (form: ProviderForm, entry: SubscriptionText, defaultCache: boolean | undefined) =>
Object.values(providerChanges(form, entry, defaultCache)).some(Boolean);
export function subscriptionActionKind(unique: boolean, daeText: boolean, canWrite: boolean, source: ConfigSource, complete: boolean | undefined) {
if (unique && daeText && canWrite && source.writable && complete === undefined) return null;
return unique && editableSource(daeText, canWrite, source, complete) ? 'edit' : 'open';
@@ -44,8 +44,6 @@ export function useRuntimeSettingsForm() {
} | null>(null);
const edits = draft ?? {at: stamp, values: {}, modes: {}};
const level = edits.level ?? baseline?.log?.level ?? '';
const dirty = !!draft && (draft.level !== undefined || Object.keys(draft.values).length > 0 || Object.keys(draft.modes).length > 0);
const guard = useDraftGuard(dirty, () => setDraft(null));
const ceilings: Record<Numeric, number | undefined> = {
'log.buffered_records': capabilities?.logs.max_buffered_records,
'dns_log.max_records': capabilities?.dns_log.max_records,
@@ -84,6 +82,20 @@ export function useRuntimeSettingsForm() {
for (const field of numeric)
if (!field.invalid && Number(field.value) !== numericAccess[field.id].read(baseline)) numericAccess[field.id].write(patch, Number(field.value));
}
// An edit put back to the stored value leaves nothing to save, so only a value that differs, or one not yet valid,
// counts as unsaved.
const invalid = numeric.find(field => field.invalid)?.label ?? null;
// A draft value whose field the engine no longer offers cannot be saved, but it is still an unsaved edit.
const hiddenEdit =
!!draft &&
(numericFields.some(
id =>
!numeric.some(field => field.id === id) && draft.values[id] !== undefined && Number(draft.values[id]) !== (baseline && numericAccess[id].read(baseline))
) ||
recorderFields.some(id => !recorders.some(recorder => recorder.id === id) && draft.modes[id] !== undefined && draft.modes[id] !== modeOf(id)) ||
(!hasLevel && draft.level !== undefined && draft.level !== baseline?.log?.level));
const dirty = !!draft && (Object.keys(patch).length > 0 || invalid !== null || hiddenEdit);
const guard = useDraftGuard(dirty, () => setDraft(null));
const apply = () => {
if (settings.busy) return;
const submitted = draft;
@@ -127,8 +139,8 @@ export function useRuntimeSettingsForm() {
setDraft(null);
},
dirty,
blocked: !Object.keys(patch).length || numeric.some(field => field.invalid),
reason: runtimeApplyReason({busy: settings.busy, invalid: numeric.find(field => field.invalid)?.label ?? null}, t),
blocked: !Object.keys(patch).length || invalid !== null,
reason: runtimeApplyReason({busy: settings.busy, invalid}, t),
apply
};
}