import React, { useState, useEffect } from 'react';
import { View, TouchableOpacity, Text, StyleSheet, Alert } from 'react-native';
import { CometChatCalls } from '@cometchat/calls-sdk-react-native';
function ScreenShareControls() {
const [isSharing, setIsSharing] = useState(false);
const [remoteSharer, setRemoteSharer] = useState<string | null>(null);
useEffect(() => {
const unsubscribeStart = CometChatCalls.addEventListener(
'onScreenShareStarted',
() => {
setIsSharing(true);
}
);
const unsubscribeStop = CometChatCalls.addEventListener(
'onScreenShareStopped',
() => {
setIsSharing(false);
}
);
const unsubscribeRemoteStart = CometChatCalls.addEventListener(
'onParticipantStartedScreenShare',
(participant) => {
setRemoteSharer(participant.name);
}
);
const unsubscribeRemoteStop = CometChatCalls.addEventListener(
'onParticipantStoppedScreenShare',
() => {
setRemoteSharer(null);
}
);
return () => {
unsubscribeStart();
unsubscribeStop();
unsubscribeRemoteStart();
unsubscribeRemoteStop();
};
}, []);
const toggleScreenShare = () => {
if (isSharing) {
CometChatCalls.stopScreenSharing();
} else {
if (remoteSharer) {
Alert.alert(
'Screen Share Active',
`${remoteSharer} is currently sharing their screen.`
);
return;
}
CometChatCalls.startScreenSharing();
}
};
return (
<View style={styles.container}>
{remoteSharer && (
<Text style={styles.sharingText}>
{remoteSharer} is sharing their screen
</Text>
)}
<TouchableOpacity
style={[styles.button, isSharing && styles.activeButton]}
onPress={toggleScreenShare}
>
<Text style={styles.buttonText}>
{isSharing ? 'Stop Sharing' : 'Share Screen'}
</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
padding: 16,
alignItems: 'center',
},
sharingText: {
color: '#6851D6',
fontSize: 14,
marginBottom: 8,
},
button: {
backgroundColor: '#333',
paddingHorizontal: 20,
paddingVertical: 12,
borderRadius: 8,
},
activeButton: {
backgroundColor: '#6851D6',
},
buttonText: {
color: '#fff',
fontSize: 14,
fontWeight: '600',
},
});
export default ScreenShareControls;