ai-assistant: appbar: Register events after render - #1247
Conversation
9e9e8e9 to
399fe74
Compare
There was a problem hiding this comment.
Pull request overview
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
This PR moves Headlamp event callback registration out of render and into a post-commit effect to avoid React’s “setState during render” warning, while keeping the callback reading the latest plugin state via a ref.
Changes:
- Register
registerHeadlampEventCallbackinsideuseEffectinstead of during render. - Use a
useRefto keep the event handler wired to the latestuseGlobalState()value without re-registering. - Update tests to render the component and assert single registration across rerenders.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| ai-assistant/src/components/appbar/HeadlampEventHandler.tsx | Moves callback registration into useEffect and uses a ref to avoid stale state closures. |
| ai-assistant/src/components/appbar/HeadlampEventHandler.test.tsx | Renders the component in tests and adds coverage for single registration on rerender. |
Suppressed comments (1)
ai-assistant/src/components/appbar/HeadlampEventHandler.test.tsx:40
- After moving registration into
useEffect, the callback is registered post-commit rather than during render. This helper readsregisterHeadlampEventCallback.mock.callsimmediately afterrender, which can become timing-sensitive depending on React/test environment. To avoid flakes, wait until the mock has been called (e.g.,waitForan expected call count) before accessing.mock.calls.at(-1).
async function getEventCallback() {
const { default: HeadlampEventHandler } = await import('./HeadlampEventHandler');
render(<HeadlampEventHandler />);
return registerHeadlampEventCallback.mock.calls.at(-1)![0] as (event: {
type: string;
data?: unknown;
}) => null;
}
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
399fe74 to
0ebf5b2
Compare
Register the Headlamp callback after commit to avoid dispatching to the store during React render. Signed-off-by: René Dudfield <renedudfield@microsoft.com>
0ebf5b2 to
3f846b2
Compare
Summary
Was seeing this
Testing
CI=true npm run check