import React, { useState, useEffect, useCallback } from 'react';
import { View, TouchableOpacity, Text, StyleSheet, Share, Alert } from 'react-native';
import Clipboard from '@react-native-clipboard/clipboard';
import { CometChatCalls } from '@cometchat/calls-sdk-react-native';
interface ShareInviteProps {
sessionId: string;
}
function ShareInvite({ sessionId }: ShareInviteProps) {
const [showOptions, setShowOptions] = useState(false);
useEffect(() => {
const unsubscribe = CometChatCalls.addEventListener(
'onShareInviteButtonClicked',
() => {
setShowOptions(true);
}
);
return () => unsubscribe();
}, []);
const generateInviteLink = useCallback(() => {
return `https://yourapp.com/call?sessionId=${encodeURIComponent(sessionId)}`;
}, [sessionId]);
const handleShare = async () => {
const inviteLink = generateInviteLink();
try {
await Share.share({
message: `Join my video call!\n\n${inviteLink}`,
title: 'Join Call',
});
} catch (error) {
console.error('Error sharing:', error);
}
setShowOptions(false);
};
const handleCopyLink = () => {
const inviteLink = generateInviteLink();
Clipboard.setString(inviteLink);
Alert.alert('Link Copied', 'The invite link has been copied to your clipboard.');
setShowOptions(false);
};
if (!showOptions) {
return (
<TouchableOpacity
style={styles.shareButton}
onPress={() => setShowOptions(true)}
>
<Text style={styles.shareButtonText}>🔗 Share</Text>
</TouchableOpacity>
);
}
return (
<View style={styles.optionsContainer}>
<View style={styles.optionsCard}>
<Text style={styles.optionsTitle}>Share Invite</Text>
<TouchableOpacity style={styles.optionButton} onPress={handleShare}>
<Text style={styles.optionIcon}>📤</Text>
<Text style={styles.optionText}>Share via...</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.optionButton} onPress={handleCopyLink}>
<Text style={styles.optionIcon}>📋</Text>
<Text style={styles.optionText}>Copy Link</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.cancelButton}
onPress={() => setShowOptions(false)}
>
<Text style={styles.cancelText}>Cancel</Text>
</TouchableOpacity>
</View>
</View>
);
}
const styles = StyleSheet.create({
shareButton: {
backgroundColor: '#333',
paddingHorizontal: 16,
paddingVertical: 12,
borderRadius: 8,
},
shareButtonText: {
color: '#fff',
fontSize: 14,
fontWeight: '600',
},
optionsContainer: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: 'rgba(0, 0, 0, 0.5)',
justifyContent: 'center',
alignItems: 'center',
},
optionsCard: {
backgroundColor: '#1a1a1a',
borderRadius: 16,
padding: 20,
width: '80%',
maxWidth: 300,
},
optionsTitle: {
color: '#fff',
fontSize: 18,
fontWeight: '600',
textAlign: 'center',
marginBottom: 20,
},
optionButton: {
flexDirection: 'row',
alignItems: 'center',
backgroundColor: '#333',
padding: 16,
borderRadius: 8,
marginBottom: 12,
},
optionIcon: {
fontSize: 20,
marginRight: 12,
},
optionText: {
color: '#fff',
fontSize: 16,
},
cancelButton: {
padding: 16,
alignItems: 'center',
},
cancelText: {
color: '#6851D6',
fontSize: 16,
fontWeight: '600',
},
});
export default ShareInvite;