Repository navigation
fix: Adjust links and search path with locale - #3214
Conversation
- Add useSearchBase hook using useLink resolveLink for /s base path - Use useSearchBase in SearchTop, SearchInput, and SearchDropdown - Search links to respect locale and custom path (/emea-eur/s and /pt-BR/s)
when search state is applied (filters, sort) - updates the browser URL through resolveLink before writing it to handle localization paths
helps internal link decides if use custom path prefix or next.js locale prefix
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 WalkthroughReplaced direct search-path formatting with a hook-based approach: added Changes
Sequence DiagramsequenceDiagram
participant SC as Search Component
participant UFS as useFormatSearchPath
participant USB as useSearchBase
participant UL as useLink/resolveLink
participant Router as Router
SC->>UFS: call useFormatSearchPath()
activate UFS
UFS->>USB: call useSearchBase()
activate USB
USB->>UL: resolveLink("/s")
activate UL
UL->>Router: inspect asPath, locale, custom prefixes
Router-->>UL: resolved base path
deactivate UL
USB-->>UFS: return resolved base
deactivate USB
UFS->>UFS: bind formatSearchPath(params, base)
UFS-->>SC: return bound format function
deactivate UFS
SC->>SC: compute href via bound format function and use in link/navigation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 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: 2
🧹 Nitpick comments (1)
packages/core/src/components/search/SearchDropdown/SearchDropdown.tsx (1)
64-95:formatSearchPathcalled with identical args on lines 70 and 74 — minor redundancy.The
hrefcomputed on line 70 (for the anchor'shrefprop) and on line 74 (insideonClick) use the same{ term: suggestion, sort }arguments. You could reuse the anchor's href or lift the computation:Suggested simplification
{terms?.map(({ value: suggestion }) => { + const suggestionPath = formatSearchPath({ term: suggestion, sort }) + return ( <UISearchAutoCompleteTerm key={suggestion} term={term} suggestion={suggestion} linkProps={{ - href: formatSearchPath({ term: suggestion, sort }), + href: suggestionPath, onClick: async (event: React.MouseEvent<HTMLAnchorElement>) => { event.preventDefault() - const href = formatSearchPath({ term: suggestion, sort }) - // Execute search selection callback onSearchSelection?.( term, formatSearchPath({ term: term, sort }) ) // Wait for analytics event to complete try { await sendAutocompleteClickEvent({ term: term, url: window.location.href, }) } catch (_) {} // Navigate after events are completed - window.location.href = href + window.location.href = suggestionPath }, }} /> + ) })}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/core/src/components/search/SearchDropdown/SearchDropdown.tsx` around lines 64 - 95, Compute the suggestion URL once inside the map and reuse it instead of calling formatSearchPath twice: inside the terms?.map block create const href = formatSearchPath({ term: suggestion, sort }) and pass that href to UISearchAutoCompleteTerm's linkProps.href and use the same href inside the onClick handler for window.location.href (and for any analytics/navigation uses), leaving sendAutocompleteClickEvent, onSearchSelection, UISearchAutoCompleteTerm, term, suggestion, and sort intact.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/core/src/components/search/SearchTop/SearchTop.tsx`:
- Line 12: The import useSearchBase is unused in SearchTop.tsx
(useFormatSearchPath already consumes it), so remove the unused import statement
"useSearchBase" from the top of the file to clean up imports and avoid linter
warnings; ensure only the necessary imports (including useFormatSearchPath)
remain.
In `@packages/core/src/sdk/search/state.ts`:
- Around line 8-26: The useApplySearchState callback redundantly calls
resolveLink on newUrl which has already been locale/custom-path resolved by
useSearchBase/formatSearchState, causing double-prefixing; remove the
resolveLink(newUrl) call and use newUrl directly when setting
window.history.replaceState (keep spreading window.history.state and the as/url
fields), ensuring the function signatures and dependency on resolveLink remain
consistent in the closure.
---
Nitpick comments:
In `@packages/core/src/components/search/SearchDropdown/SearchDropdown.tsx`:
- Around line 64-95: Compute the suggestion URL once inside the map and reuse it
instead of calling formatSearchPath twice: inside the terms?.map block create
const href = formatSearchPath({ term: suggestion, sort }) and pass that href to
UISearchAutoCompleteTerm's linkProps.href and use the same href inside the
onClick handler for window.location.href (and for any analytics/navigation
uses), leaving sendAutocompleteClickEvent, onSearchSelection,
UISearchAutoCompleteTerm, term, suggestion, and sort intact.
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/core/src/components/search/SearchTop/SearchTop.tsx (1)
52-68: Per-term locale-aware path looks correct; optional: memoize the hook's return value.The inline
formatSearchPath({term, sort})call inside.map()is fine for this bounded list. One thing worth noting:useFormatSearchPathreturns a brand-new closure on every render (nouseCallbackinside the hook). It's benign here because the function is called inline and never passed as a prop, but memoizing it inside the hook would be the cleaner approach if this pattern is reused in components where the function is propagated.The fix lives in the hook itself (
formatSearchPath.ts), not in this component — but flagging the pattern:// packages/core/src/sdk/search/formatSearchPath.ts +import { useCallback } from 'react' export function useFormatSearchPath(): (params: FormatSearchPathParams) => string { const searchBase = useSearchBase() - return (params: FormatSearchPathParams) => - formatSearchPath(params, searchBase) + return useCallback( + (params: FormatSearchPathParams) => formatSearchPath(params, searchBase), + [searchBase] + ) }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/core/src/components/search/SearchTop/SearchTop.tsx` around lines 52 - 68, The hook useFormatSearchPath (formatSearchPath) currently returns a new function on every render; update the hook in formatSearchPath.ts to memoize the returned closure with React.useCallback so callers (e.g., SearchTop's formatSearchPath) receive a stable function instance — include all relevant dependencies (locale, base path, any translation or routing values used inside the closure) in the useCallback dependency array to preserve correctness.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/core/src/components/search/SearchTop/SearchTop.tsx`:
- Around line 52-68: The hook useFormatSearchPath (formatSearchPath) currently
returns a new function on every render; update the hook in formatSearchPath.ts
to memoize the returned closure with React.useCallback so callers (e.g.,
SearchTop's formatSearchPath) receive a stable function instance — include all
relevant dependencies (locale, base path, any translation or routing values used
inside the closure) in the useCallback dependency array to preserve correctness.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/core/src/sdk/ui/useLink.ts`:
- Around line 28-35: The locale-prefix branch in useLink (inside the useLink
hook) lacks the same storeConfig.localization?.enabled guard used by
addCustomPathPrefix and other localization utilities, so it may incorrectly
prepend router.locale when localization is defined but disabled; update the
conditional that builds `/${router.locale}${href}` to include
`storeConfig.localization?.enabled` (alongside the existing checks for
router.locale, defaultLocale, and !onCustomPath) so the locale prefix is only
applied when localization is enabled.
What's the purpose of this pull request?
Centralize link resolution so internal links always get the correct locale/context: either a custom path prefix (e.g. /europe/it/...) when on a custom path page, or the Next.js locale prefix (e.g. /pt-BR/...) when localization is enabled and the current locale is not the default.
Keep search URLs aligned with the rest of the application: when search state is applied, the URL written is resolved through the same logic so it keeps the current locale or custom path instead of always using
/s.Problematic behaviour:
busca_verde.mov
How it works?
useLinkOnCustomPathcheck when the current page is under a custom path, so we don’t add the Next.js locale segment on those pages (locale is already in the path).Search state
useApplySearchStateruns the new search URL through resolveLink() before calling replaceState, so the URL in the url and history keeps the correct locale or custom path (e.g. /europe/it/s?q=shoes or /pt-BR/s?q=shoes instead of /s?q=shoes when that would be wrong).How to test it?
Use the instructions here to run the project locally and set the binding and enable localization.
Scenario 1:
search_link_locale.mov
Scenario 2: With custom paths
search-link-custom-path.mov
note: we still need to adjust the search history scenario, it will redirect to the "correct" url, but we are currently saving all the search history terms using the same locale key. (task to address it)

References
Jira Tasks:
Summary by CodeRabbit
Refactor
New Features
Chores