-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from Nosto/hooks-folder
Move hooks to hooks folder
- Loading branch information
Showing
18 changed files
with
80 additions
and
79 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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,3 @@ | ||
export { useDeepCompareEffect } from "./useDeepCompareEffect" | ||
export { useNostoApi } from "./useNostoApi" | ||
export { useNostoContext } from "./useNostoContext" |
This file contains 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,21 @@ | ||
import { useEffect, useRef, useMemo } from "react" | ||
import { deepCompare } from "../utils/compare" | ||
|
||
export function useDeepCompareEffect( | ||
callback: Parameters<typeof useEffect>[0], | ||
dependencies: Parameters<typeof useEffect>[1] | ||
): ReturnType<typeof useEffect> { | ||
return useEffect(callback, useDeepCompareMemoize(dependencies)) | ||
} | ||
|
||
function useDeepCompareMemoize<T>(value: T) { | ||
const ref = useRef<T>(value); | ||
const signalRef = useRef<number>(0) | ||
|
||
if (!deepCompare(value, ref.current)) { | ||
ref.current = value | ||
signalRef.current += 1 | ||
} | ||
|
||
return useMemo(() => ref.current, [signalRef.current]) | ||
} |
This file contains 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,22 @@ | ||
import { useEffect } from "react" | ||
import { useNostoContext } from "./useNostoContext" | ||
import { NostoClient } from "../types" | ||
import { useDeepCompareEffect } from "./useDeepCompareEffect" | ||
|
||
export function useNostoApi( | ||
cb: (api: NostoClient) => void, | ||
deps?: React.DependencyList, | ||
flags?: { deep?: boolean } | ||
): void { | ||
const { clientScriptLoaded, currentVariation, responseMode } = useNostoContext() | ||
const useEffectFn = flags?.deep ? useDeepCompareEffect : useEffect | ||
|
||
useEffectFn(() => { | ||
if (clientScriptLoaded) { | ||
window.nostojs(api => { | ||
api.defaultSession().setVariation(currentVariation!).setResponseMode(responseMode) | ||
cb(api) | ||
}) | ||
} | ||
}, [clientScriptLoaded, currentVariation, responseMode, ...(deps ?? [])]) | ||
} |
This file contains 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,18 @@ | ||
import { useContext } from "react" | ||
import { NostoContext, NostoContextType } from "../context" | ||
|
||
/** | ||
* A hook that allows you to access the NostoContext and retrieve Nosto-related data from it in React components. | ||
* | ||
* @group Essential Functions | ||
*/ | ||
export function useNostoContext(): NostoContextType { | ||
const context = useContext(NostoContext) | ||
|
||
if (!context) { | ||
throw new Error("No nosto context found") | ||
} | ||
|
||
return context | ||
} | ||
|
This file contains 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,2 +1,4 @@ | ||
export type { Cart, Customer, Product, Order, Recommendation } from "./types" | ||
export * from "./components" | ||
export { NostoContext, type NostoContextType } from "./context" | ||
export { useNostoContext } from "./hooks/useNostoContext" |
This file was deleted.
Oops, something went wrong.