Skip to content

Editor: move AuthorsSelect init side effects to useEffect#175

Open
dknauss wants to merge 2 commits intohumanmade:developfrom
dknauss:codex/hm-editor-useeffect-fix
Open

Editor: move AuthorsSelect init side effects to useEffect#175
dknauss wants to merge 2 commits intohumanmade:developfrom
dknauss:codex/hm-editor-useeffect-fix

Conversation

@dknauss
Copy link

@dknauss dknauss commented Mar 13, 2026

What this does

Moves initial author-selection setup out of render and into useEffect.

What this fixes

Prevents render-time state updates and render-time async fetch work in AuthorsSelect, which can lead to repeated initialization and unstable editor behavior.

What this changes

  • moves initial selected-author hydration into useEffect
  • adds a one-time initialization guard
  • moves async preload fetch into the effect lifecycle
  • adds a cancellation guard so stale async completion does not update state

Why

The current component mutates state and starts async work while rendering. React components should keep render pure and perform this setup in effects.

Scope

This PR is intentionally narrow with only front-end concerns.

Included:

  • src/components/AuthorsSelect.tsx

Not included:

  • broader frontend modernization
  • react-select upgrade work
  • hook migration
  • accessibility/UI follow-up changes

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e8b7b9179e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

};
}

hasInitializedSelection.current = true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Delay initialization until author IDs are available

This unconditional hasInitializedSelection.current = true runs even when currentAuthorIDs is not yet available (for example, initially undefined/empty before editor data finishes loading), so later effect executions immediately return at the top guard and never hydrate selection from preload or API. In that delayed-load scenario the selector remains empty despite the post having authors, which is a regression from the previous logic that retried initialization once IDs appeared.

Useful? React with 👍 / 👎.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting catch! A little over-zealous since setting hasInitializedSelection.current = true at line 105 (the final fallback at the bottom of the effect) means that if currentAuthorIDs is initially undefined/empty, the guard at the top will prevent later hydration when IDs arrive.

But — there's a possible edge case: if currentAuthorIDs is initially [] (empty array, not undefined), the effect falls through to line 105, sets the ref to true, and then when real IDs arrive, the guard blocks re-initialization. That's a real (if unlikely) race condition depending on how the block editor hydrates the store.

The fix is setting the ref to true inside the two branches that actually initialize (preload match and API fetch), removing line 105 entirely.

Fixed in c7a772e. The fallback guard is removed so the effect re-runs until real data arrives.

The fallback `hasInitializedSelection.current = true` at the bottom of
the useEffect fired when currentAuthorIDs was initially empty, blocking
later re-runs from hydrating the selection once real data arrived.

The ref is now only set to true inside the two branches that actually
perform initialization (preload match and API fetch), so the effect
safely re-runs until data is available.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant