Skip to content

Commit

Permalink
Merge pull request #87 from Nosto/hooks-folder
Browse files Browse the repository at this point in the history
Move hooks to hooks folder
  • Loading branch information
timowestnosto authored Jul 22, 2024
2 parents 2712282 + 1324171 commit 44b892c
Show file tree
Hide file tree
Showing 18 changed files with 80 additions and 79 deletions.
3 changes: 1 addition & 2 deletions src/components/Nosto404.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useNostoContext } from "./context"
import { useNostoApi } from "../utils/hooks"
import { useNostoContext, useNostoApi } from "../hooks"

/**
* You can personalise your cart and checkout pages by using the `Nosto404` component.
Expand Down
3 changes: 1 addition & 2 deletions src/components/NostoCategory.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useNostoContext } from "./context"
import { useNostoApi } from "../utils/hooks"
import { useNostoContext, useNostoApi } from "../hooks"

/**
* You can personalise your category and collection pages by using the NostoCategory component.
Expand Down
3 changes: 1 addition & 2 deletions src/components/NostoCheckout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useNostoContext } from "./context"
import { useNostoApi } from "../utils/hooks"
import { useNostoContext, useNostoApi } from "../hooks"

/**
* You can personalise your cart and checkout pages by using the NostoCheckout component.
Expand Down
3 changes: 1 addition & 2 deletions src/components/NostoHome.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useNostoContext } from "./context"
import { useNostoApi } from "../utils/hooks"
import { useNostoContext, useNostoApi } from "../hooks"

/**
* The `NostoHome` component must be used to personalise the home page. The component does not require any props.
Expand Down
3 changes: 1 addition & 2 deletions src/components/NostoOrder.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Order } from "../types"
import { useNostoContext } from "./context"
import { useNostoContext, useNostoApi } from "../hooks"
import { snakeize } from "../utils/snakeize"
import { useNostoApi } from "../utils/hooks"

/**
* You can personalise your order-confirmation/thank-you page by using the `NostoOrder` component.
Expand Down
3 changes: 1 addition & 2 deletions src/components/NostoOther.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useNostoContext } from "./context"
import { useNostoApi } from "../utils/hooks"
import { useNostoContext, useNostoApi } from "../hooks"

/**
* You can personalise your miscellaneous pages by using the NostoOther component.
Expand Down
3 changes: 1 addition & 2 deletions src/components/NostoProduct.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useNostoContext } from "./context"
import { useNostoApi } from "../utils/hooks"
import { useNostoContext, useNostoApi } from "../hooks"
import { Product } from "../types"

/**
Expand Down
4 changes: 3 additions & 1 deletion src/components/NostoProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, isValidElement, useState, useRef } from "react"
import { NostoContext } from "./context"
import { NostoContext } from "../context"
import { createRoot, Root } from "react-dom/client"
import { NostoClient, Recommendation } from "../types"

Expand Down Expand Up @@ -88,6 +88,7 @@ export default function NostoProvider(props: NostoProviderProps) {

// custom hook for rendering campaigns (CSR/SSR):
const [pageType, setPageType] = useState("")

function useRenderCampaigns(type: string = "") {
const placementRefs = useRef<Record<string, Root>>({})
useEffect(() => {
Expand Down Expand Up @@ -130,6 +131,7 @@ export default function NostoProvider(props: NostoProviderProps) {
}
}
}

return { renderCampaigns, pageTypeUpdated }
}

Expand Down
3 changes: 1 addition & 2 deletions src/components/NostoSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useNostoContext } from "./context"
import { useNostoApi } from "../utils/hooks"
import { useNostoContext, useNostoApi } from "../hooks"

/**
* You can personalise your search pages by using the NostoSearch component.
Expand Down
3 changes: 1 addition & 2 deletions src/components/NostoSession.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useNostoContext } from "./context"
import { useNostoContext, useDeepCompareEffect } from "../hooks"
import { Cart, Customer } from "../types"
import { snakeize } from "../utils/snakeize"
import { useDeepCompareEffect } from "../utils/hooks"

/**
* Nosto React requires that you pass it the details of current cart contents and the details of the currently logged-in customer, if any, on every route change.
Expand Down
3 changes: 0 additions & 3 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@ export { default as NostoOrder } from "./NostoOrder"
export { default as NostoHome } from "./NostoHome"
export { default as NostoPlacement } from "./NostoPlacement"
export { default as NostoProvider } from "./NostoProvider"

export { NostoContext, useNostoContext } from "./context"
export type { NostoContextType } from "./context"
export { default as NostoSession } from "./NostoSession"
18 changes: 2 additions & 16 deletions src/components/context.ts → src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createContext, useContext } from "react"
import { NostoClient, Recommendation, RenderMode } from "../types"
import { createContext } from "react"
import { NostoClient, Recommendation, RenderMode } from "./types"

type AnyFunction = (...args: unknown[]) => unknown

Expand Down Expand Up @@ -39,17 +39,3 @@ export const NostoContext = createContext<NostoContextType>({
},
})

/**
* 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
}
3 changes: 3 additions & 0 deletions src/hooks/index.ts
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"
21 changes: 21 additions & 0 deletions src/hooks/useDeepCompareEffect.ts
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])
}
22 changes: 22 additions & 0 deletions src/hooks/useNostoApi.ts
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 ?? [])])
}
18 changes: 18 additions & 0 deletions src/hooks/useNostoContext.ts
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
}

2 changes: 2 additions & 0 deletions src/index.ts
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"
41 changes: 0 additions & 41 deletions src/utils/hooks.ts

This file was deleted.

0 comments on commit 44b892c

Please sign in to comment.