@nosto/nosto-react 1.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +103 -755
- package/dist/index.es.js +563 -598
- package/dist/index.umd.js +9 -9
- package/package.json +10 -19
- package/src/components/Nosto404.tsx +4 -9
- package/src/components/NostoCategory.tsx +4 -7
- package/src/components/NostoCheckout.tsx +4 -9
- package/src/components/NostoHome.tsx +4 -8
- package/src/components/NostoOrder.tsx +5 -7
- package/src/components/NostoOther.tsx +4 -9
- package/src/components/NostoPlacement.tsx +0 -2
- package/src/components/NostoProduct.tsx +4 -8
- package/src/components/NostoProvider.tsx +26 -138
- package/src/components/NostoSearch.tsx +4 -7
- package/src/components/NostoSession.tsx +1 -2
- package/src/components/helpers.ts +3 -0
- package/src/components/index.ts +1 -4
- package/src/context.ts +31 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/scriptLoader.ts +30 -0
- package/src/hooks/useDeepCompareEffect.ts +21 -0
- package/src/hooks/useLoadClientScript.ts +84 -0
- package/src/hooks/useNostoApi.ts +22 -0
- package/src/hooks/useNostoContext.ts +12 -0
- package/src/hooks/useRenderCampaigns.tsx +59 -0
- package/src/index.ts +3 -0
- package/src/types.ts +73 -1
- package/src/components/context.ts +0 -55
- package/src/utils/hooks.ts +0 -41
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { createContext, useContext } from "react"
|
|
2
|
-
import { NostoClient, Recommendation, RenderMode } from "../types"
|
|
3
|
-
|
|
4
|
-
type AnyFunction = (...args: unknown[]) => unknown
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @group Types
|
|
8
|
-
*/
|
|
9
|
-
export interface NostoContextType {
|
|
10
|
-
account: string
|
|
11
|
-
clientScriptLoaded: boolean
|
|
12
|
-
currentVariation?: string
|
|
13
|
-
renderFunction?: AnyFunction
|
|
14
|
-
responseMode: RenderMode
|
|
15
|
-
recommendationComponent?: React.ReactElement<{
|
|
16
|
-
nostoRecommendation: Recommendation
|
|
17
|
-
}>
|
|
18
|
-
useRenderCampaigns(type: string): {
|
|
19
|
-
renderCampaigns(data: unknown, api: NostoClient): void
|
|
20
|
-
pageTypeUpdated: boolean
|
|
21
|
-
}
|
|
22
|
-
pageType: string
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* @group Essential Functions
|
|
27
|
-
*/
|
|
28
|
-
export const NostoContext = createContext<NostoContextType>({
|
|
29
|
-
account: "",
|
|
30
|
-
currentVariation: "",
|
|
31
|
-
pageType: "",
|
|
32
|
-
responseMode: "HTML",
|
|
33
|
-
clientScriptLoaded: false,
|
|
34
|
-
useRenderCampaigns: () => {
|
|
35
|
-
return {
|
|
36
|
-
renderCampaigns: () => {},
|
|
37
|
-
pageTypeUpdated: false,
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* A hook that allows you to access the NostoContext and retrieve Nosto-related data from it in React components.
|
|
44
|
-
*
|
|
45
|
-
* @group Essential Functions
|
|
46
|
-
*/
|
|
47
|
-
export function useNostoContext(): NostoContextType {
|
|
48
|
-
const context = useContext(NostoContext)
|
|
49
|
-
|
|
50
|
-
if (!context) {
|
|
51
|
-
throw new Error("No nosto context found")
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return context
|
|
55
|
-
}
|
package/src/utils/hooks.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef, useMemo } from "react"
|
|
2
|
-
import { useNostoContext } from "../components/context"
|
|
3
|
-
import { deepCompare } from "./compare"
|
|
4
|
-
import { NostoClient } from "../types"
|
|
5
|
-
|
|
6
|
-
export function useDeepCompareEffect(
|
|
7
|
-
callback: Parameters<typeof useEffect>[0],
|
|
8
|
-
dependencies: Parameters<typeof useEffect>[1]
|
|
9
|
-
): ReturnType<typeof useEffect> {
|
|
10
|
-
return useEffect(callback, useDeepCompareMemoize(dependencies))
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function useDeepCompareMemoize<T>(value: T) {
|
|
14
|
-
const ref = useRef<T>(value)
|
|
15
|
-
const signalRef = useRef<number>(0)
|
|
16
|
-
|
|
17
|
-
if (!deepCompare(value, ref.current)) {
|
|
18
|
-
ref.current = value
|
|
19
|
-
signalRef.current += 1
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return useMemo(() => ref.current, [signalRef.current])
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function useNostoApi(
|
|
26
|
-
cb: (api: NostoClient) => void,
|
|
27
|
-
deps?: React.DependencyList,
|
|
28
|
-
flags?: { deep?: boolean }
|
|
29
|
-
): void {
|
|
30
|
-
const { clientScriptLoaded, currentVariation, responseMode } = useNostoContext()
|
|
31
|
-
const useEffectFn = flags?.deep ? useDeepCompareEffect : useEffect
|
|
32
|
-
|
|
33
|
-
useEffectFn(() => {
|
|
34
|
-
if (clientScriptLoaded) {
|
|
35
|
-
window.nostojs(api => {
|
|
36
|
-
api.defaultSession().setVariation(currentVariation!).setResponseMode(responseMode)
|
|
37
|
-
cb(api)
|
|
38
|
-
})
|
|
39
|
-
}
|
|
40
|
-
}, [clientScriptLoaded, currentVariation, responseMode, ...(deps ?? [])])
|
|
41
|
-
}
|