/** * Provides context and state management for NativeAdView and its child asset views. * Shares asset view refs and native ad data between internal components and the native SDK. */ import * as React from 'react'; import { useState, createContext, useRef } from 'react'; import type { ReactNode } from 'react'; import type { Image, NativeMethods, Text, View } from 'react-native'; import type { NativeAd } from '../types/NativeAd'; import type { NativeProps } from '../specs/AppLovinMAXNativeAdViewNativeComponent'; /** * Native component type for the rendered NativeAdView. */ export type NativeAdViewType = React.Component & NativeMethods; // Ref types for native views type TextRef = React.ElementRef; type ImageRef = React.ElementRef; type ViewRef = React.ElementRef; /** * Context type used internally by NativeAdView. * Stores references to asset views and the current native ad data. */ export type NativeAdViewContextType = { titleRef: React.RefObject; advertiserRef: React.RefObject; bodyRef: React.RefObject; callToActionRef: React.RefObject; imageRef: React.RefObject; optionViewRef: React.RefObject; mediaViewRef: React.RefObject; nativeAd: NativeAd; setNativeAd: React.Dispatch>; }; // Default for an uninitialized native ad const defaultNativeAd: NativeAd = { isOptionsViewAvailable: false, isMediaViewAvailable: false, }; /** * Internal context used by NativeAdView to provide access to asset view refs and native ad data. */ export const NativeAdViewContext = createContext({ titleRef: { current: null }, advertiserRef: { current: null }, bodyRef: { current: null }, callToActionRef: { current: null }, imageRef: { current: null }, optionViewRef: { current: null }, mediaViewRef: { current: null }, nativeAd: defaultNativeAd, setNativeAd: () => {}, }); /** * React provider that wraps components requiring access to NativeAdViewContext. */ export const NativeAdViewProvider: React.FC<{ children: ReactNode }> = ({ children }) => { const titleRef = useRef(null); const advertiserRef = useRef(null); const bodyRef = useRef(null); const callToActionRef = useRef(null); const imageRef = useRef(null); const optionViewRef = useRef(null); const mediaViewRef = useRef(null); const [nativeAd, setNativeAd] = useState(defaultNativeAd); /** * Memoized context value to avoid unnecessary renders. */ const providerValue = React.useMemo( () => ({ titleRef, advertiserRef, bodyRef, callToActionRef, imageRef, optionViewRef, mediaViewRef, nativeAd, setNativeAd, }), [nativeAd] ); return {children}; };