refactor(ui): the popover engine and every render-phase ref write compile under the React Compiler
E32 S3. The compiler skipped 93 functions; it now skips 29 (try/finally shapes, two suppressions,
VirtualGrid). react-hooks/refs hits 245 -> 31.
- Popover engine: `ref={pop.refs.setReference}` made the compiler treat the whole popover handle as
a ref, so every component holding one was skipped (21 call sites, the engine and its surface
among them). The setter is destructured now; the same for useScrollFade and useReturnFocus.
- Render-phase `ref.current = x` writes moved to `useEffectEvent` where only effects read them,
else to the new `useLatest` (written in a layout effect) / `useStableCallback`.
- FocusSafeButton (under every Button) read the focused element during render; it reads it through
useSyncExternalStore, which React asks on every render instead of a cached memo.
Bugs this surfaced, fixed:
- Combobox: a chip could keep showing its raw id. The label cache was a ref an effect filled after
the render that needed it; it is state merged during render now (test seen RED).
- useUndoable rendered from a ref plus a forced re-render; compiled, that memoizes a stale value.
The history is state now, mirrored in a ref for the stable callbacks.
- ConfigPanel and the saved-views editor re-seeded their drafts in an effect, painting the stale
draft for a frame; both adjust during render against the remembered key.
- Toaster's handover is written after commit instead of during render; a new test pins that the
inherited set is taken once, at the takeover (seen RED on a per-render re-read).
- IssueComposer mutated a ref prop as a one-shot focus token; it is plain state.
- PlexBrowse wrote its info-scroll memory from an effect declared before the ref, past its own
"one writer" rule; the writer comes first now and the effect goes through it.
The compiler ratchet drops to the measured 29 and now also requires FocusSafeButton and the popover
engine to compile.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { BookmarkPlus, Check, Play, Save, Star, Trash2 } from "lucide-react";
|
||||
import {
|
||||
@@ -80,6 +80,34 @@ function ViewRow({
|
||||
);
|
||||
}
|
||||
|
||||
/** The SELECTED view's two drafts: its name (the rename field) and a local, undoable draft of its
|
||||
* captured filters. Removing a chip prunes the filter draft and — via onApply — writes the pruned set
|
||||
* straight back to the stored view; Undo/Redo re-persist the same way. The draft is the single
|
||||
* source of truth for the chip row, so a chip-prune refetch (which returns the value we just wrote)
|
||||
* never clobbers the undo history: both re-seed only when the SELECTION changes (and on Save-current,
|
||||
* by the caller). Adjusted DURING render against the remembered selection — an effect painted the
|
||||
* previous view's filters and name for a frame first. `selectedId` is the trigger, `selected` only
|
||||
* the data. */
|
||||
function useSelectedViewDrafts(
|
||||
selected: SavedView | null,
|
||||
selectedId: number | null,
|
||||
updateFilters: (id: number, filters: Record<string, unknown>) => unknown,
|
||||
) {
|
||||
const [nameDraft, setNameDraft] = useState("");
|
||||
const filtersDraft = useUndoable<Record<string, unknown>>(selected?.filters ?? {}, {
|
||||
onApply: (f) => {
|
||||
if (selected) void updateFilters(selected.id, f);
|
||||
},
|
||||
});
|
||||
const [seededFor, setSeededFor] = useState<number | null | undefined>(undefined);
|
||||
if (seededFor !== selectedId) {
|
||||
setSeededFor(selectedId);
|
||||
filtersDraft.reset(selected?.filters ?? {});
|
||||
setNameDraft(selected?.name ?? "");
|
||||
}
|
||||
return { filtersDraft, nameDraft, setNameDraft };
|
||||
}
|
||||
|
||||
export default function PersonalViewsModal() {
|
||||
const { t } = useTranslation();
|
||||
const {
|
||||
@@ -107,29 +135,11 @@ export default function PersonalViewsModal() {
|
||||
const selected = views.find((v) => v.id === selectedId) ?? null;
|
||||
// Two independent name fields: the left one names a NEW view; the right one renames the selected one.
|
||||
const [createName, setCreateName] = useState("");
|
||||
const [nameDraft, setNameDraft] = useState("");
|
||||
|
||||
// A local, undoable draft of the SELECTED view's captured filters. Removing a chip prunes the draft
|
||||
// and — via onApply — writes the pruned set straight back to the stored view; Undo/Redo re-persist
|
||||
// the same way. The draft is the single source of truth for the chip row, so a chip-prune refetch
|
||||
// (which returns the value we just wrote) never clobbers the undo history. We only re-seed it on an
|
||||
// external change of the visible filters: switching the selection, or Save-current below.
|
||||
const filtersDraft = useUndoable<Record<string, unknown>>(selected?.filters ?? {}, {
|
||||
onApply: (f) => {
|
||||
if (selected) void updateFilters(selected.id, f);
|
||||
},
|
||||
});
|
||||
const resetDraft = filtersDraft.reset;
|
||||
// Re-seed only when the SELECTION changes, not on every same-view refetch — a refetch returns the
|
||||
// value we just wrote, and re-seeding on it would throw away the prune's undo history. The chosen
|
||||
// view is therefore read through a ref: `selectedId` is the trigger, `selected` is only the data.
|
||||
const selectedRef = useRef(selected);
|
||||
selectedRef.current = selected;
|
||||
useEffect(() => {
|
||||
const view = selectedRef.current;
|
||||
resetDraft(view?.filters ?? {});
|
||||
setNameDraft(view?.name ?? "");
|
||||
}, [selectedId, resetDraft]);
|
||||
const { filtersDraft, nameDraft, setNameDraft } = useSelectedViewDrafts(
|
||||
selected,
|
||||
selectedId,
|
||||
updateFilters,
|
||||
);
|
||||
|
||||
// The selected view's captured filters, as the same human labels its module's chip row shows, read
|
||||
// from the undoable draft. The feed hook runs unconditionally (fed empty filters off-feed); Plex
|
||||
@@ -322,7 +332,7 @@ export default function PersonalViewsModal() {
|
||||
const saved = await saveCurrent(selected.id);
|
||||
// Re-seed the draft to what we just stored (the live filters) so the chip row
|
||||
// reflects the save; clears the now-stale undo history.
|
||||
if (saved) resetDraft(saved);
|
||||
if (saved) filtersDraft.reset(saved);
|
||||
}}
|
||||
disabled={saveCurrentDisabled}
|
||||
title={
|
||||
|
||||
Reference in New Issue
Block a user