-
Notifications
You must be signed in to change notification settings - Fork 12
feat: add LocationSensor helper component for next AppRouter and PagesRouter #789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
thomaswhyyou
merged 6 commits into
main
from
thomas-kno-10209-sdk-activation-rules-are-not-being-re-evaluated-on-location
Oct 28, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a358770
add LocationSensor for guide
thomaswhyyou f395c33
format and lint fixes
thomaswhyyou e55a61a
fix build error
thomaswhyyou a513b03
set initial location in LocationSensorNextPagesRouter
thomaswhyyou 2e1568a
account for query params and simplify the setLocation logic
thomaswhyyou d795cc6
changeset
thomaswhyyou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@knocklabs/client": patch | ||
| "@knocklabs/react": patch | ||
| --- | ||
|
|
||
| [guides] add dedicated nextjs helper components for detecting location changes |
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
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
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
28 changes: 28 additions & 0 deletions
28
packages/react/src/modules/guide/components/LocationSensor/NextAppRouter.tsx
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| "use client"; | ||
|
|
||
| import { useGuideContext } from "@knocklabs/react-core"; | ||
| import { usePathname, useSearchParams } from "next/navigation"; | ||
| import { useEffect } from "react"; | ||
|
|
||
| import { checkForWindow } from "../../../core/utils"; | ||
|
|
||
| export const LocationSensorNextAppRouter = () => { | ||
| const pathname = usePathname(); | ||
| const searchParams = useSearchParams(); | ||
| const queryStr = searchParams.toString(); | ||
|
|
||
| const { client } = useGuideContext(); | ||
|
|
||
| useEffect(() => { | ||
| client.removeLocationChangeEventListeners(); | ||
| }, [client]); | ||
|
|
||
| useEffect(() => { | ||
| const win = checkForWindow(); | ||
| if (!win) return; | ||
|
|
||
| client.setLocation(win.location.href); | ||
| }, [client, pathname, queryStr]); | ||
|
|
||
| return null; | ||
| }; | ||
41 changes: 41 additions & 0 deletions
41
packages/react/src/modules/guide/components/LocationSensor/NextPagesRouter.tsx
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { useGuideContext } from "@knocklabs/react-core"; | ||
| import { useRouter } from "next/router"; | ||
| import { useEffect } from "react"; | ||
|
|
||
| import { checkForWindow } from "../../../core/utils"; | ||
|
|
||
| export const LocationSensorNextPagesRouter = () => { | ||
| const router = useRouter(); | ||
| const { client } = useGuideContext(); | ||
|
|
||
| useEffect(() => { | ||
| const win = checkForWindow(); | ||
| if (!win) return; | ||
|
|
||
| // Set the initial location if not yet set. | ||
| if (!client.store.state.location) { | ||
| client.setLocation(win.location.href); | ||
| } | ||
|
|
||
| // Remove any location chagne event listeners on the window object in case | ||
| // they are attached. | ||
| client.removeLocationChangeEventListeners(); | ||
|
|
||
| // Attach a route change event listener to the nextjs router. Note, here url | ||
| // is the pathname and any query parameters of the new route but does not | ||
| // include the domain or origin. | ||
| const handleRouteChangeComplete = (url: string) => { | ||
| client.setLocation(win.location.origin + url); | ||
| }; | ||
| router.events.on("routeChangeComplete", handleRouteChangeComplete); | ||
|
|
||
| return () => { | ||
| router.events.off("routeChangeComplete", handleRouteChangeComplete); | ||
| }; | ||
| // We want to run this effect once per client instance and `router` is not | ||
| // guaranteed to be referentially stable. | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [client]); | ||
|
|
||
| return null; | ||
| }; |
2 changes: 2 additions & 0 deletions
2
packages/react/src/modules/guide/components/LocationSensor/index.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { LocationSensorNextAppRouter } from "./NextAppRouter"; | ||
| export { LocationSensorNextPagesRouter } from "./NextPagesRouter"; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| export { Banner, BannerView } from "./Banner"; | ||
| export { Card, CardView } from "./Card"; | ||
| export { GuideToolbar } from "./GuideToolbar"; | ||
| export { | ||
| LocationSensorNextAppRouter, | ||
| LocationSensorNextPagesRouter, | ||
| } from "./LocationSensor"; | ||
| export { Modal, ModalView } from "./Modal"; |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.