UNPKG

@bzbs/react-providers

Version:

A collection of React Context Provider for Buzzebees apps

464 lines (452 loc) 17.5 kB
import * as emittery from 'emittery'; import emittery__default from 'emittery'; import { AxiosInstance } from 'axios'; import { LoginResponse, Version, ErrorResponse, ServiceResponse, ForgetPasswordResponse, StatusResponse, OtpResponse, ValidateOtpResponse, ConfirmOtpResponse, ResumeResponse, Purchase } from '@bzbs/react-api-client'; import React from 'react'; import * as zustand from 'zustand'; import MatomoTracker, { UserInfo } from 'matomo-tracker-react-native'; type LoginResponseHandler = (response: LoginResponse) => void; type AuthContextType = { isInitialized: boolean; versionData?: Version | null; isLoading: boolean; token: string | null; isLoadingToken: boolean; isLoggedIn: boolean; error: ErrorResponse | null; actions: { init(uuid: string, appId: string, deviceToken: string): void; setUUID(value: string): void; setAppId(value: string): void; setDeviceToken(value: string): void; setClientVersion(value: string): void; setOs(value: string): void; setPlatform(value: string): void; setError(value: unknown): void; setLoginResponseHandler(fn: LoginResponseHandler): void; clearData: () => void; loginWithUsernamePassword: (username: string, password: string) => Promise<ServiceResponse<LoginResponse>>; loginWithGoogle: (token: string) => Promise<ServiceResponse<LoginResponse>>; loginWithFacebook: (token: string) => Promise<ServiceResponse<LoginResponse>>; loginWithApple: (token: string, refreshToken: string) => Promise<ServiceResponse<LoginResponse>>; loginWithUUID: () => Promise<ServiceResponse<LoginResponse>>; logout: () => Promise<ServiceResponse<unknown>>; forgetPassword: (contact: string, type: 'email' | 'contact_number') => Promise<ServiceResponse<ForgetPasswordResponse>>; resetPassword: (contact: string, otp: string, refCode: string, password: string) => Promise<ServiceResponse<StatusResponse>>; sendOtp: (contact: string) => Promise<ServiceResponse<OtpResponse>>; validateOtp: (otp: string, refCode: string, contact: string, channel: string) => Promise<ServiceResponse<ValidateOtpResponse>>; confirmOtp: (otp: string, refCode: string, contact: string) => Promise<ServiceResponse<ConfirmOtpResponse>>; resume: (clientVersion: string) => Promise<ServiceResponse<ResumeResponse>>; version: (clientVersion: string) => Promise<ServiceResponse<Version>>; }; }; declare const AuthContext: React.Context<AuthContextType>; declare function useAuthContext(): AuthContextType; type getTokenFunction = () => Promise<string | null>; type setTokenFunction = (token: string) => void; type removeTokenFunction = () => void; type TokenFunctions = { getToken: getTokenFunction; setToken: setTokenFunction; removeToken: removeTokenFunction; }; declare function AuthProvider({ tokenFunctions, children, }: { tokenFunctions: TokenFunctions; children: React.ReactNode; }): React.JSX.Element; type AnalyticsEventParams$1 = { action: string; name?: string; category?: string; value?: number; campaign?: string; userInfo?: UserInfo; }; type AnalyticsPageViewParams$1 = { name: string; url?: string; userInfo?: UserInfo; }; type AnalyticsContextType = { isInitialized: boolean; isEnabled: boolean; userId?: string; presetUserInfo?: UserInfo; actions: { setEnabled: (enabled: boolean) => void; setPresetUserInfo: (userInfo: UserInfo) => void; clearPresetUserInfo: () => void; trackAppStart: (userInfo?: UserInfo) => Promise<void>; trackEvent: (params: AnalyticsEventParams$1) => Promise<void>; trackScreenView: (params: AnalyticsPageViewParams$1) => Promise<void>; trackAction: (params: AnalyticsPageViewParams$1) => Promise<void>; trackSiteSearch: (params: { keyword: string; category?: string; count?: number; userInfo?: UserInfo; }) => Promise<void>; trackLink: (params: { link: string; userInfo?: UserInfo; }) => Promise<void>; trackDownload: (params: { download: string; userInfo?: UserInfo; }) => Promise<void>; setUserId: (userId: string | null) => void; setCustomDimension: (id: number, value: string) => void; reset: () => void; }; }; declare const AnalyticsContext: React.Context<AnalyticsContextType>; declare function useAnalyticsContext(): AnalyticsContextType; type AnalyticsProviderProps = { children: React.ReactNode; disabled?: boolean; instance?: MatomoTracker; userId?: string; enableLogging?: boolean; }; declare function AnalyticsProvider({ children, disabled, instance, userId, enableLogging, }: AnalyticsProviderProps): React.JSX.Element; declare const campaignType: { draw: number; free: number; deal: number; buy: number; bid: number; ads: number; install: number; booking: number; interface: number; event: number; media: number; ewalletTopUp: number; ewalletRedeem: number; ewalletTransfer: number; ewalletBanking: number; autofeed: number; news: number; reservation: number; ewalletBuy: number; pointRedemption: number; donate: number; pointFree: number; voucher: number; encrypt: number; encryptRedeem: number; giftCard: number; verifyCode: number; subscription: number; fillCode: number; subscriptionFood: number; buyEVoucher: number; uploadReceipt: number; payWithPoints: number; marketPlacePrivilege: number; topup2C2P: number; directTopup2C2P: number; }; declare const campaignPointType: { use: string; get: string; }; declare const campaignInterfaceType: { web: string; survey: string; surveyApprove: string; }; declare const eventEmitter: emittery__default<Record<PropertyKey, any>, Record<PropertyKey, any> & emittery.OmnipresentEventData, emittery.DatalessEventNames<Record<PropertyKey, any>>>; declare function addDefaultHeaderInterceptor(axiosInstance: AxiosInstance, appId: string, subscriptionKey: string, tokenFunctions: TokenFunctions, apiVersion: string, corelationIdGenerator?: () => string): void; declare function generateShortLivedId(): string; declare function avatarUrl(baseUrl: string, appId: string, userId: string, token: string | null): string; declare function largeImage(url: string): string; declare function fetchImage(axiosClient: AxiosInstance, imageUrl: string): Promise<any>; declare function createInterfaceWebsiteUrl(url: string, token?: string, returnUrl?: string, params?: { [key: string]: any; }): string; declare function createInterfaceToken(token: string): string; declare function createCartUrl(cartUrl: string, appName: string, accessKey: string, params?: { [key: string]: any; }): string; declare function generateBlake2bSignatureHex(data: string, key: string, encoder: TextEncoder): string; declare function getOtpSignatureHeaders({ appId, contactNumber, channel, encoder, now, }: { appId: string; contactNumber: string; channel: string; encoder: TextEncoder; now?: Date; }): { 'OTP-Signature': string; Timestamp: number; }; declare function addBlake2OtpSignatureInterceptor(axiosInstance: AxiosInstance, paths: string[], encoder: TextEncoder, getCurrentDateTime?: () => Date): void; type AnalyticsEventParams = { action: string; name?: string; category?: string; value?: number; campaign?: string; userInfo?: UserInfo; }; type AnalyticsPageViewParams = { name: string; url?: string; userInfo?: UserInfo; }; type AnalyticsState = { isInitialized: boolean; isEnabled: boolean; siteId?: number; urlBase?: string; trackerUrl?: string; userId?: string; disabled?: boolean; log?: boolean; matomoInstance?: MatomoTracker; presetUserInfo?: UserInfo; }; type AnalyticsActions = { initialize: (config: { siteId: number; urlBase: string; trackerUrl?: string; userId?: string; disabled?: boolean; log?: boolean; }) => void; setEnabled: (enabled: boolean) => void; setPresetUserInfo: (userInfo: UserInfo) => void; clearPresetUserInfo: () => void; trackAppStart: (userInfo?: UserInfo) => Promise<void>; trackEvent: (params: AnalyticsEventParams) => Promise<void>; trackScreenView: (params: AnalyticsPageViewParams) => Promise<void>; trackAction: (params: AnalyticsPageViewParams) => Promise<void>; trackSiteSearch: (params: { keyword: string; category?: string; count?: number; userInfo?: UserInfo; }) => Promise<void>; trackLink: (params: { link: string; userInfo?: UserInfo; }) => Promise<void>; trackDownload: (params: { download: string; userInfo?: UserInfo; }) => Promise<void>; setUserId: (userId: string | null) => void; setCustomDimension: (id: number, value: string) => void; reset: () => void; }; type AnalyticsStore = AnalyticsState & AnalyticsActions; declare const useAnalyticsStore: zustand.UseBoundStore<zustand.StoreApi<AnalyticsStore>>; /** * Hook to automatically track page views when component mounts or dependencies change */ declare const usePageTracking: (pageName?: string, dependencies?: unknown[], customUserInfo?: UserInfo) => void; /** * Hook that returns analytics tracking functions */ declare const useAnalytics: () => { isInitialized: boolean; isEnabled: boolean; trackEvent: (params: AnalyticsEventParams$1) => Promise<void>; trackScreenView: (params: AnalyticsPageViewParams$1) => Promise<void>; trackAction: (params: AnalyticsPageViewParams$1) => Promise<void>; trackAppStart: (userInfo?: UserInfo) => Promise<void>; trackSiteSearch: (params: { keyword: string; category?: string; count?: number; userInfo?: UserInfo; }) => Promise<void>; trackLink: (params: { link: string; userInfo?: UserInfo; }) => Promise<void>; trackDownload: (params: { download: string; userInfo?: UserInfo; }) => Promise<void>; setUserId: (userId: string | null) => void; setCustomDimension: (id: number, value: string) => void; setEnabled: (enabled: boolean) => void; setPresetUserInfo: (userInfo: UserInfo) => void; clearPresetUserInfo: () => void; }; /** * Hook that provides access to the Zustand analytics store */ declare const useAnalyticsState: () => AnalyticsStore; /** * Utility function to track common events with immediate execution */ declare const trackCommonEvents: { /** * Track register success event * Screen: register, Category: register, Event: register_success */ registerSuccess: (userInfo?: UserInfo) => Promise<void>; /** * Track login success event * Screen: login, Category: login, Event: login_success */ loginSuccess: (userInfo?: UserInfo) => Promise<void>; /** * Track view campaign event * Screen: campaign, Category: campaign_list, Event: view_campaign */ viewCampaign: (userInfo?: UserInfo) => Promise<void>; /** * Track redeem success event * Screen: campaign, Category: campaign_redeem, Event: redeem_success */ redeemSuccess: (campaignId: string, userInfo?: UserInfo) => Promise<void>; /** * Track unconsent success event * Screen: marketing, Category: marketing_unconsent, Event: unconsent_success */ unconsentSuccess: (userInfo?: UserInfo) => Promise<void>; /** * Track button click event */ buttonClick: (buttonName: string, category?: string, userInfo?: UserInfo) => Promise<void>; /** * Track form submit event */ formSubmit: (formName: string, success?: boolean, userInfo?: UserInfo) => Promise<void>; /** * Track search event */ search: (keyword: string, resultCount?: number, category?: string, userInfo?: UserInfo) => Promise<void>; /** * Track purchase event */ purchase: (amount: number, currency?: string, productName?: string, userInfo?: UserInfo) => Promise<void>; /** * Track user registration event */ userRegistration: (method?: string, userInfo?: UserInfo) => Promise<void>; /** * Track user login event */ userLogin: (method?: string, userInfo?: UserInfo) => Promise<void>; /** * Track error event */ error: (errorType: string, errorMessage?: string, userInfo?: UserInfo) => Promise<void>; /** * Track custom event */ custom: (name: string, category: string, label?: string, value?: number, userInfo?: UserInfo) => Promise<void>; }; /** * Higher-order component to automatically track page views */ declare const withPageTracking: <P extends object>(Component: React.ComponentType<P>, pageName?: string, customUserInfo?: UserInfo) => React.ComponentType<P>; /** * Initialize analytics from anywhere in the app (useful for Zustand-only usage) */ declare const initializeAnalytics: (config: { siteId: number; urlBase: string; trackerUrl?: string; userId?: string; disabled?: boolean; log?: boolean; }) => void; /** * Global analytics instance for direct usage without React hooks */ declare const analytics: { /** * Initialize analytics (can be called outside React components) */ init: (config: { siteId: number; urlBase: string; trackerUrl?: string; userId?: string; disabled?: boolean; log?: boolean; }) => void; /** * Track event (can be called outside React components) */ trackEvent: (params: { action: string; name?: string; category?: string; value?: number; campaign?: string; userInfo?: UserInfo; }) => Promise<void>; /** * Track screen view (can be called outside React components) */ trackScreenView: (name: string, userInfo?: UserInfo) => Promise<void>; /** * Track app start (can be called outside React components) */ trackAppStart: (userInfo?: UserInfo) => Promise<void>; /** * Track site search (can be called outside React components) */ trackSiteSearch: (keyword: string, category?: string, count?: number, userInfo?: UserInfo) => Promise<void>; /** * Track link click (can be called outside React components) */ trackLink: (link: string, userInfo?: UserInfo) => Promise<void>; /** * Track download (can be called outside React components) */ trackDownload: (download: string, userInfo?: UserInfo) => Promise<void>; /** * Set user ID and update preset userInfo (can be called outside React components) */ setUserId: (userId: string | null) => void; /** * Set custom dimension (can be called outside React components) */ setCustomDimension: (id: number, value: string) => void; /** * Set preset user info (can be called outside React components) */ setPresetUserInfo: (userInfo: UserInfo) => void; /** * Clear preset user info (can be called outside React components) */ clearPresetUserInfo: () => void; /** * Enable/disable analytics (can be called outside React components) */ setEnabled: (enabled: boolean) => void; /** * Reset analytics (can be called outside React components) */ reset: () => void; }; declare function isValidEmail(email: string): boolean; type PurchaseItemInfo = { title: string; pointPerUnit: number; expireDate: Date; expiredIn?: number; image: string; showTracking: boolean; showCode: boolean; expired: boolean; status: PurchaseItemStatus; }; type PurchaseItemStatus = 'redeemed' | 'used' | 'expired' | 'draw_win' | 'draw_lose' | 'draw_waiting' | 'prepared_for_shipping' | 'shipped'; declare function getPurchaseItemStatus(item: Purchase): PurchaseItemInfo; declare const localeToLCIDMap: { [key: string]: number; }; declare function findBestLocaleFromString(value: string): { locale: string; localeId: number; }; export { type AnalyticsEventParams as A, analytics as B, isValidEmail as C, type PurchaseItemStatus as D, getPurchaseItemStatus as E, localeToLCIDMap as F, findBestLocaleFromString as G, AuthContext as H, AuthProvider as I, useAuthContext as J, type AuthContextType as K, type AnalyticsEventParams$1 as L, type AnalyticsPageViewParams$1 as M, type AnalyticsContextType as N, AnalyticsContext as O, type PurchaseItemInfo as P, useAnalyticsContext as Q, type AnalyticsProviderProps as R, AnalyticsProvider as S, type TokenFunctions as T, type AnalyticsPageViewParams as a, type AnalyticsState as b, type AnalyticsActions as c, type AnalyticsStore as d, campaignType as e, campaignPointType as f, campaignInterfaceType as g, addBlake2OtpSignatureInterceptor as h, addDefaultHeaderInterceptor as i, avatarUrl as j, createCartUrl as k, createInterfaceToken as l, eventEmitter as m, fetchImage as n, generateBlake2bSignatureHex as o, generateShortLivedId as p, getOtpSignatureHeaders as q, createInterfaceWebsiteUrl as r, largeImage as s, usePageTracking as t, useAnalyticsStore as u, useAnalytics as v, useAnalyticsState as w, trackCommonEvents as x, withPageTracking as y, initializeAnalytics as z };