`eslint-plugin-react-hooks` was a `clients/web` devDependency only, and each client's `eslint .` is scoped to its own directory — so the whole hook rule set had never looked at `core/react/`, a directory of nothing but React hooks. "Neither surface has JSX" was the stated reason and was never a good one: the rules judge hooks, not JSX. Add the plugin at the root and apply it in a `core/react/**` block (scoped there rather than to all of `core/**`, since the rules key off the `use` prefix), with `exhaustive-deps` promoted to `error` as `clients/web` does. Resolve the 15 findings it then reports: - `useSettingsDraft` / `useClientSettingsDraft` seed their draft during render instead of in a `targetId`/`opened`-keyed effect, so the previous target's values never reach the DOM — the same rule `clients/web` spells `useValueChange`. Their "latest value" refs move from render-time writes into an effect; every reader (the debounce timer, `flush`, `onChange`) runs after a commit, so a post-commit write is what they need. - The three async loaders keep their effects behind justified inline disables: `set-state-in-effect` follows a call into an async function without modelling the `await`, verified against a minimal hook whose only setState is in a continuation. - `useInitialConfig`'s cleanup bump keeps reading `generation.current` at teardown — the rule's "copy it into a variable" advice would restore a stale request token and let a superseded response commit. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013hzwzS8UvBsr4yZm7o7sev Signed-off-by: cliffhall <cliff@futurescale.com>
111 lines
3.6 KiB
TypeScript
111 lines
3.6 KiB
TypeScript
/**
|
|
* Draft state for the install-level client settings modal (client.json).
|
|
*
|
|
* Same controlled-modal pattern as {@link useSettingsDraft}: local draft,
|
|
* debounced persist, synchronous `flush` on close. Keyed on modal
|
|
* open/close (`opened`) rather than a server id because client config is
|
|
* install-level, not per-server.
|
|
*/
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
|
|
export interface UseClientSettingsDraftOptions<T> {
|
|
opened: boolean;
|
|
resolveInitial: () => T;
|
|
onPersist: (value: T) => Promise<void>;
|
|
onError: (err: unknown) => void;
|
|
debounceMs?: number;
|
|
}
|
|
|
|
export interface UseClientSettingsDraftResult<T> {
|
|
draft: T | null;
|
|
onChange: (next: T | ((prev: T) => T)) => void;
|
|
flush: () => void;
|
|
}
|
|
|
|
export function useClientSettingsDraft<T>({
|
|
opened,
|
|
resolveInitial,
|
|
onPersist,
|
|
onError,
|
|
debounceMs = 300,
|
|
}: UseClientSettingsDraftOptions<T>): UseClientSettingsDraftResult<T> {
|
|
const [draft, setDraft] = useState<T | null>(null);
|
|
const timerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
const latestValuesRef = useRef<T | null>(null);
|
|
|
|
// Seeded during render rather than in an effect, for the reason spelled
|
|
// out on {@link useSettingsDraft}: an effect would paint the previous
|
|
// session's draft for one frame before correcting itself. `seeded` is
|
|
// tracked separately from `draft` so a `resolveInitial` that legitimately
|
|
// returns `null` is not re-seeded on every render.
|
|
const [seeded, setSeeded] = useState(false);
|
|
if (seeded !== opened) {
|
|
setSeeded(opened);
|
|
setDraft(opened ? resolveInitial() : null);
|
|
}
|
|
|
|
const onPersistRef = useRef(onPersist);
|
|
const onErrorRef = useRef(onError);
|
|
const openedRef = useRef(opened);
|
|
// Written in an effect rather than during render (`react-hooks/refs`).
|
|
// Every reader — the debounce timer, `flush`, and `onChange` — runs after
|
|
// a commit, so a post-commit write is what they need. `latestValuesRef`
|
|
// mirrors `draft` here so it is re-seeded (and cleared on close) without a
|
|
// second copy of the seeding rule; `onChange` still writes it
|
|
// synchronously, because a functional update in the same tick has to see
|
|
// the value the previous call produced.
|
|
useEffect(() => {
|
|
onPersistRef.current = onPersist;
|
|
onErrorRef.current = onError;
|
|
openedRef.current = opened;
|
|
latestValuesRef.current = draft;
|
|
});
|
|
|
|
useEffect(() => {
|
|
return () => {
|
|
if (timerRef.current) {
|
|
clearTimeout(timerRef.current);
|
|
timerRef.current = undefined;
|
|
}
|
|
};
|
|
}, []);
|
|
|
|
const persist = useCallback((value: T) => {
|
|
onPersistRef.current(value).catch((err) => {
|
|
onErrorRef.current(err);
|
|
});
|
|
}, []);
|
|
|
|
const onChange = useCallback(
|
|
(next: T | ((prev: T) => T)) => {
|
|
if (!opened) return;
|
|
const prev = latestValuesRef.current;
|
|
if (prev === null) return;
|
|
const resolved =
|
|
typeof next === "function" ? (next as (prev: T) => T)(prev) : next;
|
|
latestValuesRef.current = resolved;
|
|
setDraft(resolved);
|
|
if (timerRef.current) clearTimeout(timerRef.current);
|
|
timerRef.current = setTimeout(() => {
|
|
timerRef.current = undefined;
|
|
const value = latestValuesRef.current;
|
|
if (value !== null) {
|
|
persist(value);
|
|
}
|
|
}, debounceMs);
|
|
},
|
|
[opened, debounceMs, persist],
|
|
);
|
|
|
|
const flush = useCallback(() => {
|
|
if (!timerRef.current) return;
|
|
clearTimeout(timerRef.current);
|
|
timerRef.current = undefined;
|
|
const value = latestValuesRef.current;
|
|
if (openedRef.current && value !== null) {
|
|
persist(value);
|
|
}
|
|
}, [persist]);
|
|
|
|
return { draft, onChange, flush };
|
|
}
|