-
Notifications
You must be signed in to change notification settings - Fork 2
Nt 3493 add dedicated react sdk hooks for common optimization state and actions #323
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
Open
Lotfi Anwar L Arif (Lotfi-Arif)
wants to merge
2
commits into
NT-3492-improve-react-sdk-documentation-for-use-optimization-and-entry-interaction-setup
Choose a base branch
from
NT-3493-add-dedicated-react-sdk-hooks-for-common-optimization-state-and-actions
base: NT-3492-improve-react-sdk-documentation-for-use-optimization-and-entry-interaction-setup
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
48 changes: 48 additions & 0 deletions
48
packages/web/frameworks/react-web-sdk/src/hooks/useOptimizationActions.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,48 @@ | ||
| import { useMemo } from 'react' | ||
|
|
||
| import type { OptimizationSdk } from '../context/OptimizationContext' | ||
| import { useOptimization } from './useOptimization' | ||
|
|
||
| /** | ||
| * Bound Optimization SDK actions safe to destructure in React components. | ||
| * | ||
| * @public | ||
| */ | ||
| export interface UseOptimizationActionsResult { | ||
| readonly consent: OptimizationSdk['consent'] | ||
| readonly identify: OptimizationSdk['identify'] | ||
| readonly page: OptimizationSdk['page'] | ||
| readonly track: OptimizationSdk['track'] | ||
| } | ||
|
|
||
| /** | ||
| * Returns bound Optimization SDK actions that are safe to destructure. | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * const { track, consent } = useOptimizationActions() | ||
| * await track({ event: 'purchase' }) | ||
| * consent(true) | ||
| * ``` | ||
| * | ||
| * @remarks | ||
| * This hook does not create a new SDK instance. It binds the most common | ||
| * actions from the existing SDK instance returned by `useOptimization()`. | ||
| * | ||
| * @public | ||
| */ | ||
| export function useOptimizationActions(): UseOptimizationActionsResult { | ||
| const sdk = useOptimization() | ||
|
|
||
| return useMemo<UseOptimizationActionsResult>( | ||
| () => ({ | ||
| consent: (value) => { | ||
| sdk.consent(value) | ||
| }, | ||
| identify: async (payload) => await sdk.identify(payload), | ||
| page: async (payload) => await sdk.page(payload), | ||
| track: async (payload) => await sdk.track(payload), | ||
| }), | ||
| [sdk], | ||
| ) | ||
| } |
94 changes: 94 additions & 0 deletions
94
packages/web/frameworks/react-web-sdk/src/hooks/useOptimizationState.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,94 @@ | ||
| import { useCallback, useRef, useSyncExternalStore } from 'react' | ||
|
|
||
| import type { OptimizationSdk } from '../context/OptimizationContext' | ||
| import { useOptimization } from './useOptimization' | ||
|
|
||
| type OptimizationStates = OptimizationSdk['states'] | ||
| type ObservableValue<T> = T extends { readonly current: infer V } ? V : never | ||
|
|
||
| interface ObservableLike<T> { | ||
| readonly current: T | ||
| readonly subscribe: (next: (value: T) => void) => { unsubscribe: () => void } | ||
| } | ||
|
|
||
| function useObservableState<T>(observable: ObservableLike<T>): T { | ||
| const snapshotRef = useRef<T>(observable.current) | ||
| const observableRef = useRef(observable) | ||
|
|
||
| if (observableRef.current !== observable) { | ||
| const { current } = observable | ||
| observableRef.current = observable | ||
| snapshotRef.current = current | ||
| } | ||
|
|
||
| const subscribe = useCallback( | ||
| (onStoreChange: () => void) => { | ||
| const subscription = observable.subscribe((value) => { | ||
| snapshotRef.current = value | ||
| onStoreChange() | ||
| }) | ||
|
|
||
| return () => { | ||
| const { unsubscribe } = subscription | ||
| unsubscribe() | ||
| } | ||
| }, | ||
| [observable], | ||
| ) | ||
|
|
||
| const getSnapshot = useCallback(() => snapshotRef.current, []) | ||
|
|
||
| return useSyncExternalStore(subscribe, getSnapshot, getSnapshot) | ||
| } | ||
|
|
||
| /** | ||
| * Returns the current consent state. | ||
| * | ||
| * @public | ||
| */ | ||
| export function useConsentState(): ObservableValue<OptimizationStates['consent']> { | ||
| const sdk = useOptimization() | ||
| return useObservableState(sdk.states.consent) | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether optimization data is currently available. | ||
| * | ||
| * @public | ||
| */ | ||
| export function useCanOptimizeState(): ObservableValue<OptimizationStates['canOptimize']> { | ||
| const sdk = useOptimization() | ||
| return useObservableState(sdk.states.canOptimize) | ||
| } | ||
|
|
||
| /** | ||
| * Returns the latest emitted event payload. | ||
| * | ||
| * @public | ||
| */ | ||
| export function useEventStreamState(): ObservableValue<OptimizationStates['eventStream']> { | ||
| const sdk = useOptimization() | ||
| return useObservableState(sdk.states.eventStream) | ||
| } | ||
|
|
||
| /** | ||
| * Returns the current profile state. | ||
| * | ||
| * @public | ||
| */ | ||
| export function useProfileState(): ObservableValue<OptimizationStates['profile']> { | ||
| const sdk = useOptimization() | ||
| return useObservableState(sdk.states.profile) | ||
| } | ||
|
|
||
| /** | ||
| * Returns the current selected optimizations state. | ||
| * | ||
| * @public | ||
| */ | ||
| export function useSelectedOptimizationsState(): ObservableValue< | ||
| OptimizationStates['selectedOptimizations'] | ||
| > { | ||
| const sdk = useOptimization() | ||
| return useObservableState(sdk.states.selectedOptimizations) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of the recent changes to React hooks, this was not sufficient, so I increased it slightly. I'm not sure if that is okay; otherwise, I can find another solution.