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 CometChatConversationStarterProps { getConversationStarters: () => Promise; onSuggestionClicked?: (reply: string) => void; } type State = 'loading' | 'loaded' | 'empty' | 'error'; const CometChatConversationStarter: React.FC = ({ getConversationStarters, onSuggestionClicked, }) => { const theme = useTheme(); const { t } = useCometChatTranslation(); const [state, setState] = useState('loading'); const [starters, setStarters] = 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(); getConversationStarters() .then((res) => { if (res && res.length > 0) { setStarters(res); setState('loaded'); } else { setState('empty'); } }) .catch(() => setState('error')) .finally(() => loop.stop()); return () => loop.stop(); }, []); const s = styles(theme); if (state === 'loading') { return ( {[120, 90, 150].map((w, i) => ( ))} ); } if (state === 'error') { return ( {t('ai_conversation_starter_error')} ); } if (state === 'empty') { return null; } return ( {starters.map((starter, i) => ( onSuggestionClicked?.(starter)} activeOpacity={0.7} accessibilityRole="button" accessibilityLabel={starter} > {starter} ))} ); }; const styles = (theme: ReturnType) => StyleSheet.create({ container: { paddingHorizontal: 12, paddingVertical: 8, gap: 8, }, scrollContent: { gap: 8, paddingRight: 8, }, shimmerChip: { height: 36, borderRadius: 18, backgroundColor: theme.color.background4, marginRight: 8, }, chip: { paddingHorizontal: 16, paddingVertical: 8, borderRadius: 18, borderWidth: 1, borderColor: theme.color.primary, backgroundColor: theme.color.background1, maxWidth: 240, }, chipText: { color: theme.color.primary, 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: 4, }, }); export default CometChatConversationStarter;