import { View, Text, Animated, ActivityIndicator, Linking } from 'react-native' import React, { useState, useRef, useEffect } from 'react' import { TouchableOpacity } from 'react-native' import { ChevronDownIcon, ChevronUpIcon, GlobeAltIcon } from 'react-native-heroicons/outline' import Icon from 'react-native-vector-icons/FontAwesome6' import { getContacts } from '../api' export default function ContactUsCenter() { const [contacts, setContacts] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [expandedItems, setExpandedItems] = useState({}) const animations = useRef({}) useEffect(() => { fetchContacts() }, []) const fetchContacts = async () => { try { setLoading(true) const data = await getContacts() setContacts(data) // Initialize expanded state and animations const initialExpandedState = {} data.forEach(contact => { initialExpandedState[contact.id] = contact.contact_type === 'whatsapp' animations.current[contact.id] = new Animated.Value(contact.contact_type === 'whatsapp' ? 1 : 0) }) setExpandedItems(initialExpandedState) } catch (err) { setError('Failed to load contacts') console.error('Error fetching contacts:', err) } finally { setLoading(false) } } const toggleItem = (itemId) => { const isExpanded = !expandedItems[itemId] setExpandedItems(prev => ({ ...prev, [itemId]: isExpanded })) Animated.timing(animations.current[itemId], { toValue: isExpanded ? 1 : 0, duration: 300, useNativeDriver: false, }).start() } const handleContactPress = async (contact) => { try { let url = ''; switch (contact.contact_type) { case 'whatsapp': // Remove any non-numeric characters from the phone number const phoneNumber = contact.value.replace(/\D/g, ''); url = `https://wa.me/${phoneNumber}`; break; case 'website': url = contact.value.startsWith('http') ? contact.value : `https://${contact.value}`; break; case 'facebook': url = `https://facebook.com/${contact.value.replace('@', '')}`; break; case 'instagram': url = `https://instagram.com/${contact.value.replace('@', '')}`; break; case 'twitter': url = `https://twitter.com/${contact.value.replace('@', '')}`; break; case 'customer_service': // For phone numbers, use tel: protocol const telNumber = contact.value.replace(/\D/g, ''); url = `tel:${telNumber}`; break; default: return; } const supported = await Linking.canOpenURL(url); if (supported) { await Linking.openURL(url); } else { console.error(`Don't know how to open URL: ${url}`); } } catch (error) { console.error('Error opening URL:', error); } } const getIcon = (contactType) => { switch (contactType) { case 'customer_service': return case 'whatsapp': return case 'website': return case 'facebook': return case 'instagram': return case 'twitter': return default: return null } } if (loading) { return ( ) } if (error) { return ( {error} ) } return ( {contacts.map((contact) => ( toggleItem(contact.id)} className="flex-row items-center justify-between p-4"> {getIcon(contact.contact_type)} {contact.title} {expandedItems[contact.id] ? ( ) : ( )} handleContactPress(contact)}> {contact.value} ))} ) }