[codex] cache profile analyses
What changed
- Added an SWR-backed fetch layer for profile analyses so repeated navigations reuse cached responses instead of refetching GitHub data every mount.
- Moved the API call into a shared helper that normalizes errors, handles the star-gate response, and supports force refreshes.
- Wired the existing refresh buttons through the cached mutate path so manual refreshes update the same cache entry.
Why
- This issue calls out repeated GitHub API requests. The profile page was doing a local fetch in the client component on remount, so the same username could trigger the analysis again even when the app already had the result.
Validation
- npm run lint
- npm run build (passed after supplying the repo's required local environment variables)
Summary by CodeRabbit
- Improvements
- Enhanced profile analysis fetching with better error handling and messaging
- More reliable profile refresh and analysis updates
๐ Walkthrough
Walkthrough
This PR integrates SWR for profile analysis fetching. A new profile-analysis.ts module exports ProfileAnalysisError, ProfileAnalysisOptions, and fetchProfileAnalysis() function. ProfileClient now uses useSWR instead of manual state, gated by username validation, with refresh callbacks and error/modal handling.
Changes
SWR Integration for Profile Analysis
| Layer / File(s) | Summary |
|---|---|
Profile Analysis Fetch Module src/lib/profile-analysis.ts |
New module exports ProfileAnalysisError class (carrying status and code), ProfileAnalysisOptions type with force/nosave flags, buildProfileUrl helper that constructs /api/analyze URLs with query parameters, and fetchProfileAnalysis function that performs no-store fetch, parses JSON defensively, throws ProfileAnalysisError for 403 "Star required" or any non-OK response, and returns parsed result. |
ProfileClient SWR Integration Setup package.json, src/app/[username]/ProfileClient.tsx |
Add swr dependency, import useSWR hook and new fetchProfileAnalysis/ProfileAnalysisError, establish state for error/showStarModal/isRefreshing, compute safeUsername with invalid-username gating, wire useSWR with fallbackData and custom options, attach effects to synchronize modal/error state from SWR errors and dismiss modal when fresh data arrives. |
ProfileClient Refresh and Error Handling src/app/[username]/ProfileClient.tsx |
Implement refreshAnalysis callback that calls fetchProfileAnalysis and updates SWR via mutate, with error handling that shows star-modal on STAR_REQUIRED code or sets error state otherwise, resets isRefreshing in finally block; add username validation effect that sets error when invalid. |
ProfileClient Refresh Event Handlers src/app/[username]/ProfileClient.tsx |
Update handleRecheckStar to call refreshAnalysis() after delay, update ownership/identity effect to use safeUsername, change "See Latest Analysis" button to refreshAnalysis({ force: true, nosave: true }), change "Force Refresh" button to refreshAnalysis({ force: true }). |
Estimated code review effort
๐ฏ 3 (Moderate) | โฑ๏ธ ~20 minutes
Poem
๐ฐ SWR swoops in with caching grace, The profile analysis finds its place, State and stale-while-revalidate dance, Fresh data flows in every glance, A rabbit's refactor, smooth and bright! โจ
๐ฅ Pre-merge checks | โ 4 | โ 1
โ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | โ ๏ธ Warning | Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. | Write docstrings for the functions missing them to satisfy the coverage threshold. |
โ Passed checks (4 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title check | โ Passed | The title '[codex] cache profile analyses' clearly summarizes the main change: implementing caching via SWR for profile analysis results to avoid repeated fetches. |
| Description check | โ Passed | The description covers what changed, why it was needed, and validation steps performed; required sections are addressed despite not following the exact template format. |
| Linked Issues check | โ Passed | Check skipped because no linked issues were found for this pull request. |
| Out of Scope Changes check | โ Passed | Check skipped because no linked issues were found for this pull request. |
โ๏ธ Tip: You can configure your own custom pre-merge checks in the settings.
โจ Finishing Touches
๐งช Generate unit tests (beta)
- [ ] Create PR with unit tests
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 @coderabbitai help to get the list of available commands and usage tips.