refactor: migrate search to react-query#92
Conversation
Replace manual fetch/abort/debounce with useQuery and keepPreviousData. Lodash debounce drives debouncedSearch state for the query key. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Suggestions cached before dialog opens for instant display. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughSummary by CodeRabbit
WalkthroughThis PR refactors the Search component to use ChangesSearch Component React Query Migration and Prefetch
Sequence DiagramsequenceDiagram
participant User
participant SearchComponent
participant ReactQuery
participant SearchAPI
User->>SearchComponent: types query
SearchComponent->>SearchComponent: debounce(input) -> debouncedSearch
SearchComponent->>ReactQuery: useQuery(['search', debouncedSearch, tag], enabled=open)
ReactQuery->>SearchAPI: GET /api/search?q=debouncedSearch
SearchAPI-->>ReactQuery: results
ReactQuery-->>SearchComponent: data (keepPreviousData)
SearchComponent->>SearchComponent: deduplicate data
SearchComponent-->>User: render suggestions/results
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint skipped: no ESLint configuration detected in root package.json. To enable, add Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/chronicle/src/lib/preload.ts (1)
44-53: 💤 Low valuePrefetch only covers unversioned documentation.
The hardcoded query key
['search', '', undefined]won't match versioned searches wheretagequalsversion.dir(e.g.,['search', '', 'v1']). Users on versioned URLs won't benefit from this prefetch.Consider accepting an optional
tagparameter to support versioned prefetch:-export function prefetchSearchSuggestions() { +export function prefetchSearchSuggestions(tag?: string) { queryClient.prefetchQuery({ - queryKey: ['search', '', undefined], + queryKey: ['search', '', tag], queryFn: async () => { - const res = await fetch('/api/search'); + const params = tag ? `?tag=${encodeURIComponent(tag)}` : ''; + const res = await fetch(`/api/search${params}`); if (!res.ok) throw new Error(String(res.status)); return res.json(); }, }); }Then in
entry-client.tsx, resolve the version first and passrouteVersion.dir.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/chronicle/src/lib/preload.ts` around lines 44 - 53, The prefetchSearchSuggestions function currently only prefetches the unversioned key ['search', '', undefined], so versioned searches (tag = version.dir) miss the prefetch; change prefetchSearchSuggestions to accept an optional tag parameter (e.g., prefetchSearchSuggestions(tag?: string)), include that tag in the queryKey (['search','', tag]) and use it when calling queryClient.prefetchQuery, and update the call site in entry-client.tsx to resolve the route version and pass routeVersion.dir (or undefined) into prefetchSearchSuggestions so versioned pages receive the correct prefetched data.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/chronicle/src/lib/preload.ts`:
- Around line 44-53: The prefetchSearchSuggestions function currently only
prefetches the unversioned key ['search', '', undefined], so versioned searches
(tag = version.dir) miss the prefetch; change prefetchSearchSuggestions to
accept an optional tag parameter (e.g., prefetchSearchSuggestions(tag?:
string)), include that tag in the queryKey (['search','', tag]) and use it when
calling queryClient.prefetchQuery, and update the call site in entry-client.tsx
to resolve the route version and pass routeVersion.dir (or undefined) into
prefetchSearchSuggestions so versioned pages receive the correct prefetched
data.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 37eb0353-98a5-4922-95d8-86c1f534576f
📒 Files selected for processing (3)
packages/chronicle/src/components/ui/search.tsxpackages/chronicle/src/lib/preload.tspackages/chronicle/src/server/entry-client.tsx
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
useMemo is semantically correct for memoizing a computed value. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Move debouncedSearch update to onChange handler directly, removing unnecessary useEffect sync. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
useQuery+keepPreviousDatadebouncedSearchstate for query key (150ms)Test plan
🤖 Generated with Claude Code