fix(webui): save settings when editing completes (NAN-323) (#6384)
* fix(webui): save settings when editing completes (NAN-323) * fix(webui): close settings completion and verification gaps * refactor(webui): simplify settings edit ownership
This commit is contained in:
1 parent
26e1f41a00
commit
c6babb20e8
34 files changed
+1363
-388
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import type { ReactNode } from "react";
|
||||
import { useState, type ReactNode } from "react";
|
||||
import { CircleAlert, Loader2, RotateCcw, X } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
@@ -411,16 +411,20 @@ export function NumberInput({
|
||||
onChange: (value: number) => void;
|
||||
suffix?: string;
|
||||
}) {
|
||||
const [draft, setDraft] = useState<{ text: string; value: number } | null>(null);
|
||||
return (
|
||||
<div className="relative w-full">
|
||||
<Input
|
||||
type="number"
|
||||
min={min}
|
||||
max={max}
|
||||
value={value}
|
||||
aria-invalid={!Number.isFinite(value) || value < min || value > max}
|
||||
value={draft && Object.is(draft.value, value) ? draft.text : Number.isFinite(value) ? value : ""}
|
||||
onChange={(event) => {
|
||||
const parsed = Number(event.target.value);
|
||||
if (Number.isFinite(parsed)) onChange(parsed);
|
||||
const text = event.target.value;
|
||||
const parsed = text.trim() ? Number(text) : Number.NaN;
|
||||
setDraft({ text, value: parsed });
|
||||
onChange(parsed);
|
||||
}}
|
||||
className={cn("h-9 w-full rounded-full text-[13px]", suffix && "pr-12")}
|
||||
/>
|
||||
|
||||
@@ -22,7 +22,7 @@ export function SettingsAdvancedOptions({ children }: { children: ReactNode }) {
|
||||
}
|
||||
|
||||
export function SettingsFeature({
|
||||
title, enabled, onChange, disabled, initialOpen = false, error, children,
|
||||
title, enabled, onChange, disabled, initialOpen = false, error, children, onBeforeClose,
|
||||
}: {
|
||||
title: string;
|
||||
enabled: boolean;
|
||||
@@ -31,6 +31,7 @@ export function SettingsFeature({
|
||||
initialOpen?: boolean;
|
||||
error?: string;
|
||||
children?: ReactNode;
|
||||
onBeforeClose?: () => Promise<boolean>;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const [open, setOpen] = useState(initialOpen && enabled);
|
||||
@@ -43,7 +44,11 @@ export function SettingsFeature({
|
||||
}, [initialOpen, enabled]);
|
||||
return (
|
||||
<section ref={section} aria-label={title}>
|
||||
<Dialog open={Boolean(children) && open} onOpenChange={setOpen}>
|
||||
<Dialog open={Boolean(children) && open} onOpenChange={(next) => {
|
||||
if (!next && onBeforeClose) {
|
||||
void onBeforeClose().then((saved) => { if (saved) setOpen(false); });
|
||||
} else setOpen(next);
|
||||
}}>
|
||||
<SettingsGroup>
|
||||
<SettingsRow title={children ? (
|
||||
<DialogTrigger asChild>
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
|
||||
/** Save a valid draft once per edit; failures wait for another edit or explicit retry. */
|
||||
export function useAutoSave(value: unknown, dirty: boolean, saving: boolean, onSave: () => void, enabled = true) {
|
||||
const latest = useRef(onSave);
|
||||
latest.current = onSave;
|
||||
const attempted = useRef<string | null>(null);
|
||||
const signature = JSON.stringify(value);
|
||||
useEffect(() => {
|
||||
if (!dirty) { attempted.current = null; return; }
|
||||
if (!enabled || saving || attempted.current === signature) return;
|
||||
const timer = window.setTimeout(() => {
|
||||
attempted.current = signature;
|
||||
latest.current();
|
||||
}, 600);
|
||||
return () => window.clearTimeout(timer);
|
||||
}, [signature, dirty, saving, enabled]);
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import { useEffect, useRef, type FocusEvent, type FormEvent } from "react";
|
||||
|
||||
function isDraftInput(target: EventTarget) {
|
||||
return target instanceof HTMLTextAreaElement ||
|
||||
(target instanceof HTMLInputElement && !["checkbox", "radio", "button", "submit"].includes(target.type));
|
||||
}
|
||||
|
||||
type Draft = {
|
||||
signature: string;
|
||||
identity: string;
|
||||
revision: number;
|
||||
dirty: boolean;
|
||||
enabled: boolean;
|
||||
onSave: () => void | Promise<unknown>;
|
||||
};
|
||||
|
||||
/** Completion belongs to the draft owner; queued saves retain that version and target. */
|
||||
export function useEditCompletion(value: unknown, dirty: boolean, onSave: Draft["onSave"], enabled = true, identity = "") {
|
||||
const signature = JSON.stringify(value);
|
||||
const version = useRef({ signature, identity, revision: 0 });
|
||||
if (version.current.signature !== signature || version.current.identity !== identity) {
|
||||
version.current = { signature, identity, revision: version.current.revision + 1 };
|
||||
}
|
||||
const revision = version.current.revision;
|
||||
const latest = useRef<Draft>({ signature, dirty, enabled, onSave, identity, revision });
|
||||
latest.current = { signature, dirty, enabled, onSave, identity, revision };
|
||||
const focused = useRef(false);
|
||||
const completed = useRef(false);
|
||||
const previousIdentity = useRef(identity);
|
||||
const attempts = useRef(new Map<string, Promise<boolean>>());
|
||||
const tail = useRef(Promise.resolve(true));
|
||||
const outstanding = useRef(0);
|
||||
const queuedDraft = useRef<Draft | null>(null);
|
||||
const commitDraft = (draft: Draft): Promise<boolean> => {
|
||||
const queued = queuedDraft.current;
|
||||
const changedDuringSave = outstanding.current > 0 && queued &&
|
||||
queued.identity === draft.identity && queued.signature !== draft.signature;
|
||||
if (!draft.dirty && !changedDuringSave) return outstanding.current > 0 ? tail.current : Promise.resolve(true);
|
||||
if (!draft.enabled) return Promise.resolve(false);
|
||||
const version = `${draft.identity}:${draft.revision}`;
|
||||
const existing = attempts.current.get(version);
|
||||
if (existing) return existing;
|
||||
const request = tail.current.then(async () => {
|
||||
try {
|
||||
return (await draft.onSave()) !== false;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
outstanding.current += 1;
|
||||
queuedDraft.current = draft;
|
||||
attempts.current.set(version, request);
|
||||
tail.current = request;
|
||||
void request.then((saved) => {
|
||||
outstanding.current -= 1;
|
||||
if (!saved || latest.current.revision !== draft.revision) attempts.current.delete(version);
|
||||
});
|
||||
return request;
|
||||
};
|
||||
const commit = async (): Promise<boolean> => {
|
||||
focused.current = false;
|
||||
const draft = latest.current;
|
||||
const saved = await commitDraft(draft);
|
||||
if (!saved) return false;
|
||||
const current = latest.current;
|
||||
if (current.identity === draft.identity && current.signature !== draft.signature) return commit();
|
||||
return true;
|
||||
};
|
||||
const commitRef = useRef(commit);
|
||||
commitRef.current = commit;
|
||||
const commitDraftRef = useRef(commitDraft);
|
||||
commitDraftRef.current = commitDraft;
|
||||
useEffect(() => {
|
||||
if (!dirty && outstanding.current === 0) attempts.current.clear();
|
||||
if (previousIdentity.current !== identity) {
|
||||
previousIdentity.current = identity;
|
||||
focused.current = false;
|
||||
return;
|
||||
}
|
||||
if ((!focused.current || completed.current) && (dirty || outstanding.current > 0)) void commitDraftRef.current(latest.current);
|
||||
completed.current = false;
|
||||
}, [signature, dirty, enabled, identity]);
|
||||
useEffect(() => () => { void commitRef.current(); }, []);
|
||||
return {
|
||||
identity,
|
||||
commit,
|
||||
complete: () => { completed.current = true; },
|
||||
onChangeCapture: (event: FormEvent<HTMLElement>) => {
|
||||
if (isDraftInput(event.target)) {
|
||||
focused.current = true;
|
||||
completed.current = false;
|
||||
} else completed.current = true;
|
||||
},
|
||||
needsCompletion: () => latest.current.dirty || outstanding.current > 0,
|
||||
onFocusCapture: (event: FocusEvent<HTMLElement>) => {
|
||||
if (isDraftInput(event.target)) focused.current = true;
|
||||
},
|
||||
onBlurCapture: (event: FocusEvent<HTMLElement>) => {
|
||||
if (!isDraftInput(event.target)) return;
|
||||
focused.current = false;
|
||||
void commitDraft(latest.current);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export type EditCompletion = ReturnType<typeof useEditCompletion>;
|
||||
Reference in new issue
Block a user