import React, { useState, useEffect, useCallback, forwardRef, useImperativeHandle } from 'react'; import { View, StyleSheet, Alert } from 'react-native'; import { useTheme } from '~/features/theme/ThemeProvider'; import Text from '~/components/atoms/Text'; import Button from '~/components/atoms/Button'; import InviteUserModal from './InviteUserModal'; import InvitesList from './InvitesList'; import spaceService, { SpaceInvite } from '../services/spaceService'; interface SpaceInvitesManagerProps { spaceId: string; spaceName: string; isOwner: boolean; } export interface SpaceInvitesManagerHandle { openInviteModal: () => void; } /** * Component for managing space invites - allows sending invites and viewing pending invites */ const SpaceInvitesManager = forwardRef( ({ spaceId, spaceName, isOwner }, ref) => { const { isDark } = useTheme(); const [invites, setInvites] = useState([]); const [isLoading, setIsLoading] = useState(false); const [showInviteModal, setShowInviteModal] = useState(false); // Expose the openInviteModal function to parent components useImperativeHandle(ref, () => ({ openInviteModal: () => setShowInviteModal(true), })); // Fetch invites for the space const fetchInvites = useCallback(async () => { if (!spaceId) return; setIsLoading(true); try { const invitesData = await spaceService.getSpaceInvites(spaceId); setInvites(invitesData); } catch (error) { console.error('Failed to fetch invites:', error); const errorMessage = error instanceof Error ? error.message : 'Unknown error'; Alert.alert('Error', `Failed to load invites: ${errorMessage}`); } finally { setIsLoading(false); } }, [spaceId]); // Load invites when component mounts useEffect(() => { fetchInvites(); }, [fetchInvites]); // Handle sending an invitation const handleSendInvite = async (email: string, role: string) => { try { await spaceService.inviteUserToSpace(spaceId, email, role); // Refresh the invites list after sending an invitation fetchInvites(); } catch (error) { console.error('Failed to send invite:', error); throw error; } }; // Handle resending an invitation const handleResendInvite = async (invite: SpaceInvite) => { try { Alert.alert( 'Resend Invitation', `Are you sure you want to resend the invitation to ${invite.email}?`, [ { text: 'Cancel', style: 'cancel' }, { text: 'Resend', style: 'default', onPress: async () => { try { await spaceService.resendInvite(invite.id); Alert.alert('Success', `Invitation resent to ${invite.email}`); fetchInvites(); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; Alert.alert('Error', `Failed to resend invitation: ${errorMessage}`); } }, }, ] ); } catch (error) { console.error('Failed to resend invite:', error); } }; // Handle canceling an invitation const handleCancelInvite = async (invite: SpaceInvite) => { try { Alert.alert( 'Cancel Invitation', `Are you sure you want to cancel the invitation to ${invite.email}?`, [ { text: 'No', style: 'cancel' }, { text: 'Yes, Cancel', style: 'destructive', onPress: async () => { try { await spaceService.cancelInvite(invite.id); Alert.alert('Success', `Invitation to ${invite.email} has been canceled`); fetchInvites(); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; Alert.alert('Error', `Failed to cancel invitation: ${errorMessage}`); } }, }, ] ); } catch (error) { console.error('Failed to cancel invite:', error); } }; // Only show the invite button if the user is the owner of the space if (!isOwner) { return null; } return ( Space Members & Invitations