UNPKG

@frak-labs/components

Version:

Frak Wallet components, helping any person to interact with the Frak wallet.

45 lines (44 loc) 1.65 kB
import { n as W } from "./rewards-CDS1CYlm.js"; import { getMerchantInformation } from "@frak-labs/core-sdk/actions"; import { useEffect, useState } from "preact/hooks"; //#region src/hooks/useReward.ts /** * Hook to fetch and format the best reward for a given interaction type. * * Calls `getMerchantInformation`, picks the highest-value reward across all * matching live campaigns for the requested `audience` side, and returns it as * a formatted string. * * @param shouldUseReward - Whether to fetch the reward at all * @param targetInteraction - Optional filter by interaction type (e.g. "purchase") * @param audience - Reward side to display: `"referrer"` (default) or `"referee"` * @param products - The products currently in view, when known. Purely * advisory (see `matchesProductScope`): deprioritizes campaigns matching none * of them, and changes nothing when omitted. * @returns Object containing the formatted reward string, or undefined if unavailable */ function useReward(shouldUseReward, targetInteraction, audience, products) { const [reward, setReward] = useState(void 0); useEffect(() => { if (!shouldUseReward) return; const client = window.FrakSetup?.client; if (!client) return; getMerchantInformation(client).then((merchantInfo) => { const best = W(merchantInfo.rewards, { currency: client.config.metadata?.currency, targetInteraction, audience, products }); if (best && best.payoutType !== "percentage") setReward(best.formatted); }).catch(() => {}); }, [ shouldUseReward, targetInteraction, audience, products ]); return { reward }; } //#endregion export { useReward as t };