import React, { useCallback, useEffect } from 'react'; import { Image, StyleSheet, TouchableOpacity, ViewStyle, ImageStyle, } from 'react-native'; import { IGif } from '@livelike/javascript'; import { BasePickerData, LLBasePicker, LLBasePickerProps, } from '../LLBasePicker'; import { ComponentStyleProp, LLComponentStyleFn } from '../../types'; import { useGifPicker, useStyles } from '../../hooks'; import { LLGifPickerHeader, LLGifPickerHeaderProps } from './LLGifPickerHeader'; export type LLGifPickerStyles = { gifImage: ImageStyle; gifImageContainer: ViewStyle; }; export type LLGifPickerProps = ComponentStyleProp & { visible?: boolean; onPanelOpen?: () => void; onPanelClose?: () => void; closeGifPicker?: () => void; onSelectGif: (gifImage: IGif) => void; PickerComponentStyles?: LLBasePickerProps['styles']; GifPickerHeaderComponentStyles?: LLGifPickerHeaderProps['styles']; GifPickerHeaderComponent?: typeof LLGifPickerHeader; }; /** * @description Provides stock UI for the GIF picker component. * @param {Function} onPanelOpen - A callback function to be called when the panel opens. It is used for tracking analytics events when the component becomes visible. * @param {Function} onPanelClose - A callback function to be called when the panel closes. It is used for tracking analytics events when the component is unmounted as it becomes invisible. * @returns {JSX.Element} - Stock UI for the GIF picker. */ export function LLGifPicker({ visible, onPanelOpen, onPanelClose, closeGifPicker, onSelectGif, styles: stylesProp, PickerComponentStyles, GifPickerHeaderComponentStyles, GifPickerHeaderComponent = LLGifPickerHeader, }: LLGifPickerProps) { const gifPickerStyles = useStyles({ componentStylesFn: getGifPickerStyles, stylesProp, }); const { isLoading, gifImages, onGifSearchInputChange } = useGifPicker(); useEffect(() => { onGifSearchInputChange('', { debounce: false }); }, []); useEffect(() => { onPanelOpen?.(); return () => { onPanelClose?.(); }; }, []); const renderGifPickerHeaderComponent = useCallback(() => { return ( ); }, [onGifSearchInputChange, closeGifPicker]); const renderGifPickerItemComponent = useCallback( ({ item }) => { return ( <> {item.images?.preview_gif?.url && ( onSelectGif(item)} > )} ); }, [gifPickerStyles, onSelectGif] ); return ( ); } const getGifPickerStyles: LLComponentStyleFn = ({ theme }) => StyleSheet.create({ gifImage: { width: 80, height: 80, }, gifImageContainer: { margin: 5, }, });