@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.
@@ -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
- }
@@ -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
- }