docs(react,0.9): fix quick-start example#1250
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the React renderer's README with a more comprehensive 'Quick Start' guide, including core concept definitions and an improved code example that handles multiple surfaces and various message types. A review comment suggests enhancing the example's useEffect hook to handle surface deletion events, ensuring the UI stays synchronized with the agent's state.
| useEffect(() => { | ||
| // 2. Listen for surface creation | ||
| const sub = processor.onSurfaceCreated(s => { | ||
| // You can filter by surfaceId or manage multiple surfaces | ||
| if (s.id === 'main-chat') setSurface(s); | ||
| }); | ||
|
|
||
| // 3. Process messages from your agent (e.g., via WebSocket or SSE) | ||
| // For demo purposes, we manually process a creation message: | ||
| processor.processMessages([{ | ||
| version: 'v0.9', | ||
| createSurface: { | ||
| surfaceId: 'main-chat', | ||
| catalogId: basicCatalog.id // 'https://a2ui.org/specification/v0_9/basic_catalog.json' | ||
| } | ||
| }]); | ||
|
|
||
| return () => sub.unsubscribe(); | ||
| const surfaceCreatedSub = processor.onSurfaceCreated(() => | ||
| setSurfaces(Array.from(processor.model.surfacesMap.values())) | ||
| ); | ||
| return () => surfaceCreatedSub.unsubscribe(); | ||
| }, [processor]); |
There was a problem hiding this comment.
The current useEffect only handles surface creation. To keep the UI in sync with the agent's state, it should also handle surface deletion events. This ensures that surfaces removed by the agent (via deleteSurface messages) are correctly removed from the React state and the UI.
| useEffect(() => { | |
| // 2. Listen for surface creation | |
| const sub = processor.onSurfaceCreated(s => { | |
| // You can filter by surfaceId or manage multiple surfaces | |
| if (s.id === 'main-chat') setSurface(s); | |
| }); | |
| // 3. Process messages from your agent (e.g., via WebSocket or SSE) | |
| // For demo purposes, we manually process a creation message: | |
| processor.processMessages([{ | |
| version: 'v0.9', | |
| createSurface: { | |
| surfaceId: 'main-chat', | |
| catalogId: basicCatalog.id // 'https://a2ui.org/specification/v0_9/basic_catalog.json' | |
| } | |
| }]); | |
| return () => sub.unsubscribe(); | |
| const surfaceCreatedSub = processor.onSurfaceCreated(() => | |
| setSurfaces(Array.from(processor.model.surfacesMap.values())) | |
| ); | |
| return () => surfaceCreatedSub.unsubscribe(); | |
| }, [processor]); | |
| useEffect(() => { | |
| const sync = () => setSurfaces(Array.from(processor.model.surfacesMap.values())); | |
| const subCreated = processor.onSurfaceCreated(sync); | |
| const subDeleted = processor.onSurfaceDeleted(sync); | |
| return () => { | |
| subCreated.unsubscribe(); | |
| subDeleted.unsubscribe(); | |
| }; | |
| }, [processor]); |
There was a problem hiding this comment.
I did consider this as I do want the quick start code to complete enough to convert into a functional app. I decided against as I wanted to balance that idea with keeping the code as simple as possible. However, I think that given that this is a "quick start" and not just a "hello world" or step 1 of a code lab, I think adding a listener for surface deletion makes sense.
There was a problem hiding this comment.
Addressed in docs(react): improve step 2 comment and grouping
|
@ditman @jacobsimionato This supersedes #1225. PTAL to keep me honest and make sure I've sufficiently addressed your feedback from that PR. |
jacobsimionato
left a comment
There was a problem hiding this comment.
This looks great - thank you for giving our users a smooth onboarding experience! I'm a React newbie, but based on what you were telling me about useState and useEffect, this seems correct and more elegant.
|
I've come across Footnotes |
Fixes #1223. Supersedes #1225, which attempted to fix the quick start by in part by making
processCreateSurfaceMessageidempotent in web_core. #1238 has since clarified that duplicatecreateSurfaceshould error. This PR doesn't modify web_core.To summarize, this PR fixes the quick-start code for the react renderer so that it actually runs while also trying to make things easier to follow for new users.
useStateis now used for hooking up the listener to the message processor. This avoids the fatal double message processing errors (useEffectdoesn't guarantee only being called once, and react strict mode intentionally runs these hooks twice).Textcomponent is used. Some breadcrumbs are also added to the readme as natural springboards for readers to use to learn more about specific conceepts (i.e. message types and paths).Testing/verification instructions
Create a fresh react project and follow the quick start from the readme. Be careful if you decide to use an agent, as they can sometimes silently modify code instead of directly copy/pasting from the readme.
Pre-launch Checklist
If you need help, consider asking for advice on the discussion board.