Restore classification suggestion pill to original position on API failure#772
Restore classification suggestion pill to original position on API failure#772Intenzi wants to merge 5 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #772 +/- ##
==========================================
Coverage 76.71% 76.71%
Complexity 1828 1828
==========================================
Files 87 87
Lines 7764 7764
==========================================
Hits 5956 5956
Misses 1808 1808
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
… sessions of content classification
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
I haven't implemented for one edge case, I'd like to discuss on clarification around its approach decisions and whether we want to address it at all: Edge case: Restoring pills when the API call fails after the component has unmounted. If a user clicks to accept a suggestion (starting the asynchronous Potential Solution & React Anti-PatternsTo solve this, my thought process was to simply track the mount status using a ref:
However, using Questions for DiscussionTo resolve this cleanly, we might need a more holistic reapproach to how API calls and state are managed, or is the module-level cache enough? Is it acceptable to leave it as-is for now, or is decoupling the state management the right next step for this plugin's architecture? I am thinking that we could use WordPress Data Store to register a redux store that manages the state of the suggestions per taxonomy. I'm hesitant to however introduce that level of architectural overhead for an edge case fix. Looking forward to your thoughts on the same. |
What?
Closes #771
This PR visually adds back the AI suggested/generated tags/categories pills which fail to be added to the post upon performing the click action that visually removes them. (Content Classification Experiment)
Why?
It is expected that when a tag/category is selected (by clicking on it), if it fails to be added, it should be visible again for re-attempting to add it.
It resolves the visual issue and provides better User Experience.
How?
I have implemented the PR in the following manner:
addTermToPostmethod to return a success boolean flag upon completion.handleAcceptcallback which callsaddTermToPostaddTermToPostreturnedfalsefor success, we restore the suggested tag/category back to the suggestions state list viasetSuggestions(). It also takes into account the index of the suggestion to try restore at the same position from which the pill was removed.I have then taken account of an additional edge case:
+Lengthy API calls can lead to the user performing further actions such as
Suggest againandDismiss all, in which case we should not be restoring the pill after API failure is received.This has been implemented by storing a
generationCountsvariable that stores an integer value for the current "session" of pills displayed.Session here implies the state after clicking on "Suggest again" or "Dismiss all" or the initial "Suggest Categories/Tags" button.
I track this state as an integer value which gets incremented upon the above mentioned actions.
The old session integer is captured in a closure by the
.then()callback asclickGenerationCountvariable.Once the
addTermToPostmethod fails it compares the presentgenerationCountsagainst the oldclickGenerationCountto determine whether we have a stale state for adding back the suggestion pill.I have refactored suggestion re-insertion logic into a separate method of
insertSuggestionAt()to reduce nested code.+I have implemented e2e tests that validate the resolution of the same.
/wp/v2/tags/API calls viapage.route()to return a simulated failure.Use of AI Tools
AI assistance: Yes
Tool(s): Antigravity
Model(s): Gemini 3.5 Flash (Low)
Used for: Identifying the relevant possible fixes. Implemented the E2E tests using the same. Final implementation decision and tests were reviewed and edited by me.
Testing Instructions
Screenshots or screencast
Screen.Recording.2026-06-25.at.7.27.14.PM.mov
Changelog Entry