import React, { useEffect, useRef, useState } from 'react'; import { Animated, ScrollView, StyleSheet, Text, TouchableOpacity, View, } from 'react-native'; import { useTheme } from '../../../theme'; import { useCometChatTranslation } from '../../resources/CometChatLocalizeNew'; export interface CometChatSmartRepliesProps { getSmartReplies: () => Promise; onSuggestionClicked?: (reply: string) => void; closeCallback?: () => void; } type State = 'loading' | 'loaded' | 'empty' | 'error'; const CometChatSmartReplies: React.FC = ({ getSmartReplies, onSuggestionClicked, closeCallback, }) => { const theme = useTheme(); const { t } = useCometChatTranslation(); const [state, setState] = useState('loading'); const [replies, setReplies] = useState([]); const shimmer = useRef(new Animated.Value(0.4)).current; useEffect(() => { const loop = Animated.loop( Animated.sequence([ Animated.timing(shimmer, { toValue: 1, duration: 800, useNativeDriver: true }), Animated.timing(shimmer, { toValue: 0.4, duration: 800, useNativeDriver: true }), ]) ); loop.start(); getSmartReplies() .then((res) => { if (res && res.length > 0) { setReplies(res); setState('loaded'); } else { setState('empty'); } }) .catch(() => setState('error')) .finally(() => loop.stop()); return () => loop.stop(); }, []); const s = styles(theme); const renderBody = () => { if (state === 'loading') { return ( {[80, 110, 70].map((w, i) => ( ))} ); } if (state === 'error') { return {t('ai_smart_replies_error')}; } if (state === 'empty') { return {t('ai_smart_replies_empty')}; } return ( {replies.map((reply, i) => ( onSuggestionClicked?.(reply)} activeOpacity={0.7} accessibilityRole="button" accessibilityLabel={reply} > {reply} ))} ); }; return ( {t('ai_smart_replies_title')} {renderBody()} ); }; const styles = (theme: ReturnType) => StyleSheet.create({ wrapper: { backgroundColor: theme.color.background1, borderTopWidth: 1, borderTopColor: theme.color.borderDefault, paddingBottom: 4, }, header: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', paddingHorizontal: 16, paddingTop: 10, paddingBottom: 6, }, title: { color: theme.color.textSecondary, fontSize: theme.typography.caption1.regular.fontSize, fontFamily: theme.typography.caption1.medium?.fontFamily || theme.typography.caption1.regular.fontFamily, textTransform: 'uppercase', letterSpacing: 0.5, }, closeIcon: { color: theme.color.textSecondary, fontSize: 14, }, body: { paddingBottom: 8, }, scrollContent: { paddingHorizontal: 12, gap: 8, }, shimmerChip: { height: 34, borderRadius: 17, backgroundColor: theme.color.background4, marginRight: 8, }, chip: { paddingHorizontal: 16, paddingVertical: 8, borderRadius: 17, borderWidth: 1, borderColor: theme.color.borderDefault, backgroundColor: theme.color.background2, }, chipText: { color: theme.color.textPrimary, fontSize: theme.typography.body.regular.fontSize, fontFamily: theme.typography.body.regular.fontFamily, }, stateText: { color: theme.color.textSecondary, fontSize: theme.typography.caption1.regular.fontSize, fontFamily: theme.typography.caption1.regular.fontFamily, paddingHorizontal: 16, paddingVertical: 8, }, }); export default CometChatSmartReplies;