Repository navigation
refactor(core): extract recommendation data layer into src/sdk/recommendations - #3442
Conversation
…endations Move the VRN parsing, anonymous user id and recommendations query out of the RecommendationShelf section and into a headless SDK module, orchestrated by a new useRecommendationShelf() hook. The section becomes a presentation-only component that consumes the hook. No behavior change: the campaign rules, the CMS opt-in gate and the error handling are the same code, only relocated.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughRecommendation data handling moved from ChangesRecommendations SDK migration
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant RecommendationShelf
participant useRecommendationShelf
participant useRecommendationUserId
participant useRecommendations
RecommendationShelf->>useRecommendationShelf: pass campaign VRN and item context
useRecommendationShelf->>useRecommendationUserId: resolve anonymous user ID
useRecommendationShelf->>useRecommendations: fetch campaign recommendations
useRecommendations-->>useRecommendationShelf: return products and metadata
useRecommendationShelf-->>RecommendationShelf: return items, state, campaign, and correlation ID
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
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 |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
packages/core/src/components/sections/RecommendationShelf/RecommendationShelf.tsx (1)
3-3: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAlias composed UI components with the
UIprefix.Rename
CarouselandProductShelfimports toUICarouselandUIProductShelf. Update their JSX references.As per coding guidelines, “Import from
@faststore/uiand alias withUIprefix to avoid local name collisions when composing library components.”🤖 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/core/src/components/sections/RecommendationShelf/RecommendationShelf.tsx` at line 3, Alias the Carousel and ProductShelf imports from `@faststore/ui` as UICarousel and UIProductShelf, then update all corresponding JSX references in RecommendationShelf to use the aliased names.Source: Coding guidelines
packages/core/src/sdk/recommendations/useRecommendations.ts (1)
42-48: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winRemove the synthetic request variables.
RecommendationInputhas required fields, so{}is not a valid request. Passargsdirectly and declare the variables asRecommendationInput | null.useQueryreturns anullkey before request execution whendoNotRunistrue.🤖 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/core/src/sdk/recommendations/useRecommendations.ts` around lines 42 - 48, Update useRecommendations to pass args directly to useQuery instead of using the synthetic empty RecommendationInput cast, and change the useQuery variables type to RecommendationInput | null. Preserve doNotRun: args === null so the query remains unexecuted with a null key.Source: Path instructions
🤖 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.
Inline comments:
In `@packages/core/src/sdk/recommendations/useRecommendationShelf.ts`:
- Line 87: Update the recommendation shelf flow around useRecommendationUserId
to receive the enableRecommendations opt-in state, and make its effect return
before invoking retry or reading the cookie when recommendations are disabled.
Preserve the existing enabled behavior, and add a test verifying disabled
shelves do not read the personalization cookie.
---
Nitpick comments:
In
`@packages/core/src/components/sections/RecommendationShelf/RecommendationShelf.tsx`:
- Line 3: Alias the Carousel and ProductShelf imports from `@faststore/ui` as
UICarousel and UIProductShelf, then update all corresponding JSX references in
RecommendationShelf to use the aliased names.
In `@packages/core/src/sdk/recommendations/useRecommendations.ts`:
- Around line 42-48: Update useRecommendations to pass args directly to useQuery
instead of using the synthetic empty RecommendationInput cast, and change the
useQuery variables type to RecommendationInput | null. Preserve doNotRun: args
=== null so the query remains unexecuted with a null key.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: ef8c7407-54dd-4ec1-9d5e-a8f7c0f3253a
📒 Files selected for processing (14)
packages/core/src/components/sections/RecommendationShelf/README.mdpackages/core/src/components/sections/RecommendationShelf/RecommendationShelf.tsxpackages/core/src/components/sections/RecommendationShelf/RecommendationShelf.types.tspackages/core/src/sdk/recommendations/index.tspackages/core/src/sdk/recommendations/types.tspackages/core/src/sdk/recommendations/useRecommendationShelf.tspackages/core/src/sdk/recommendations/useRecommendationUserId.tspackages/core/src/sdk/recommendations/useRecommendations.tspackages/core/src/sdk/recommendations/vrn.tspackages/core/test/components/RecommendationShelf/RecommendationShelf.browser.test.tsxpackages/core/test/sdk/recommendations/useRecommendationShelf.browser.test.tsxpackages/core/test/sdk/recommendations/useRecommendationUserId.test.tspackages/core/test/sdk/recommendations/useRecommendations.test.tspackages/core/test/sdk/recommendations/vrn.test.ts
The previous wording claimed no personalization cookie is read when the opt-in is off. The cookie lookup is not gated (and was not before this refactor), so describe what actually happens: the campaign is never requested and the resolved user id goes unused.
|
On the two nitpicks — declining both, since they target pre-existing code that this PR only relocated.
Synthetic Both are reasonable improvements on their own, just not in a PR whose entire value proposition is being a verifiable pure move. |
@faststore/api
@faststore/cli
@faststore/components
@faststore/core
@faststore/diagnostics
@faststore/lighthouse
@faststore/sdk
@faststore/ui
commit: |
|

0 New Issues
0 Fixed Issues
0 Accepted Issues
What is the purpose of this pull request?
Extracts the data layer behind
RecommendationShelfinto a headless SDK module atsrc/sdk/recommendations, leaving the section as a presentation-only component.vrn.ts,useRecommendations.tsanduseRecommendationUserId.tsmove out ofsrc/components/sections/RecommendationShelf/unchanged (Git tracks them as renames).types.tscentralizesItemContextandRecommendationType.RecommendationShelf.types.tsre-exports both, so existing imports and store customizations keep working.useRecommendationShelf()hook orchestrates the pieces (user id, product context, campaign gating, fetch) and returnsitems,isLoading,error,campaignandcorrelationId.RecommendationShelf.tsxdrops ~100 lines and now just consumes the hook.No behavior change. The campaign rules, the CMS opt-in gate and the error handling are the same code, only relocated. The one cosmetic difference is that the empty-items fallback is now a stable constant instead of a new array per render.
What problem is this solving?
Part of RR-568 (mini cart recommendation shelf). A second surface will render recommendations inside the cart drawer with a different layout but identical campaign rules. Without this extraction, that surface would have to duplicate VRN resolution, the anonymous user id cookie handling and the context-products logic — three places to keep in sync every time a campaign type is added.
This PR is the enabler and ships on its own so the refactor can be reviewed as a pure move, separately from the new feature.
How to test it?
No functional change is expected, so the check is that the existing shelf keeps behaving identically.
Locally on this branch:
pnpm vitest run test/sdk/recommendations test/components/RecommendationShelf test/sdk/analyticsinpackages/core— 43 tests passing across 8 files.pnpm lintandtsc --noEmitclean (no new errors in the touched files).No workspace link is attached, since this branch has no user-facing change to preview.
Screenshots or example usage
Not applicable — refactor with no visual or behavioral change.
Usage of the extracted hook:
How does this PR make you feel? 🔗
Like tidying a drawer before buying more stuff to put in it. (No gif picked.)
Types of changes
Summary by CodeRabbit
New Features
Documentation
Tests