docs(Autocomplete): add Live Wikipedia story demonstrating a real remote API - #382
Merged
Conversation
…ote API Fetches Wikipedia's CORS-enabled opensearch endpoint with the production recipe: 250ms debounce + AbortController so stale responses never land, loading state via emptyMessage. Cross-referenced from the wiring-patterns docs.
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new Storybook documentation example for Autocomplete that demonstrates the “remote/async items” wiring pattern against a real third-party API (Wikipedia opensearch), and cross-references it from the existing wiring guide section.
Changes:
- Updates the “Remote / async source” docs section to reference the new “Live Wikipedia” story.
- Adds a new
LiveWikipediastory that performs debounced fetches to Wikipedia and renders a selected result as an external link.
Suppressed comments (3)
src/components/Autocomplete/Autocomplete.stories.tsx:316
latestQueryshould be updated on every keystroke (including when clearing the input) so pending timers/fetches can reliably detect they’re stale and bail out.
const search = (q: string) => {
clearTimeout(debounceTimer.current);
abortRef.current?.abort();
if (!q) {
setItems([]);
src/components/Autocomplete/Autocomplete.stories.tsx:336
- Even with aborting, a stale timer/fetch can still reach this point; guard before applying results so the list never updates for an older query.
];
setItems(
titles.map((title, i) => ({
src/components/Autocomplete/Autocomplete.stories.tsx:345
- On non-abort failures, the story currently keeps showing the previous query’s results while the input contains the new query. Clearing
items(and optionally showing an error message) avoids misleading/stale results.
} catch (err) {
if ((err as Error).name !== 'AbortError') setLoading(false);
}
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Deploying ui with
|
| Latest commit: |
3ede059
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://06104f03.ui-6d0.pages.dev |
| Branch Preview URL: | https://docs-autocomplete-live-wiki.ui-6d0.pages.dev |
Guard against stale responses updating state, check res.ok before parsing, and clean up the debounce timer and in-flight request on unmount. Addresses Copilot review on #382.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #381. The Async Source story demonstrates the remote wiring pattern with a simulated
setTimeoutbackend — this adds a Live Wikipedia story that hits a real third-party API, proving the pattern works cross-origin against production infrastructure and demonstrating the full production recipe.What changed
Docs-only change to
src/components/Autocomplete/Autocomplete.stories.tsx(+110):Live Wikipediastory — type-ahead against Wikipedia's opensearch endpoint (CORS-enabled viaorigin=*):AbortControllercancels the in-flight request per keystroke, so stale responses never landemptyMessage("Searching Wikipedia…")clearOnSelect={false}keeps the queryNotes
tests/visual/components.spec.ts), so no snapshot flakinessVerification
pnpm typecheck✅ / eslint ✅ / prettier ✅ /pnpm vitest run src/components/Autocomplete7/7 ✅