File size: 12,453 Bytes
cc276cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256

"use client";

import { useState, useCallback, useEffect } from "react";
import { useSearchParams, useRouter } from 'next/navigation';
import type { ChatRecipient, User, Group, CallType } from "@/lib/types";

import { useAuth } from "@/contexts/auth-context";
import { useSettings } from "@/contexts/settings-context";
import { useCalls } from "@/contexts/calls-context";
import { useContacts } from "@/contexts/contacts-context";
import { UserList } from "@/components/user-list";
import { ChatWindow } from "@/app/chat-window";
import { CreateGroupModal } from "@/components/create-group-modal";
import { ViewProfileModal } from "@/components/view-profile-modal";
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog";
import { ImagePreviewModal } from "@/components/image-preview-modal";
import { AddGroupMembersModal } from "@/components/add-group-members-modal";
import { CallModal } from "@/components/call-modal";
import { SettingsPage } from "@/components/settings-page";
import { MobileBottomNav } from "@/components/mobile-bottom-nav";
import { Input } from "@/components/ui/input";
import { ChangeNameModal } from "@/components/change-name-modal";
import { ChangeStatusModal } from "@/components/change-status-modal";
import { ChangeBioModal } from "@/components/change-bio-modal";
import { cn } from "@/lib/utils";
import { NetworkStatusIndicator } from "@/components/network-status-indicator";
import { Capacitor } from '@capacitor/core';
import { PushNotifications } from '@capacitor/push-notifications';
import { WelcomeScreen } from "./welcome-screen";


export function ChatInterface() {
    const { currentUser, authStatus, signOutUser } = useAuth();
    const { t } = useSettings();
    const { callState, startVideoCall, answerCall, startAudioCall, endCall } = useCalls();
    const { findUserByPublicId } = useContacts();
    const searchParams = useSearchParams();
    const router = useRouter();
    
    const [recipient, setRecipient] = useState<ChatRecipient | null>(null);
    const [isCreateGroupModalOpen, setCreateGroupModalOpen] = useState(false);
    const [isSettingsOpen, setSettingsOpen] = useState(false);
    const [settingsSection, setSettingsSection] = useState<string | undefined>(undefined);
    const [viewingProfile, setViewingProfile] = useState<User | null>(null);
    const [isSignOutModalOpen, setSignOutModalOpen] = useState(false);
    const [signOutConfirmText, setSignOutConfirmText] = useState('');
    const [imageToPreview, setImageToPreview] = useState<string | null>(null);
    const [groupToAddMembers, setGroupToAddMembers] = useState<Group | null>(null);

    // State for granular modals
    const [isChangeNameModalOpen, setChangeNameModalOpen] = useState(false);
    const [isChangeStatusModalOpen, setChangeStatusModalOpen] = useState(false);
    const [isChangeBioModalOpen, setChangeBioModalOpen] = useState(false);

    
    const handleStartCall = useCallback((peer: User, type: CallType) => {
        if (type === 'video') {
            startVideoCall(peer);
        } else {
            startAudioCall(peer);
        }
    }, [startAudioCall, startVideoCall]);
    
    const handleUserSelection = useCallback((userOrGroup: User | Group) => {
        if (!currentUser) return;

        if ('members' in userOrGroup) { // It's a Group
             if (userOrGroup.members[currentUser.uid]) {
                setRecipient({ ...userOrGroup.info, uid: userOrGroup.id, isGroup: true, displayName: userOrGroup.info.name, photoURL: userOrGroup.info.photoURL, publicId: userOrGroup.id });
            }
        } else { // It's a User
             if (userOrGroup.uid !== currentUser.uid) {
                setRecipient({ ...userOrGroup, isGroup: false, uid: userOrGroup.uid, displayName: userOrGroup.displayName });
            }
        }
    }, [currentUser]);


    useEffect(() => {
        const senderId = searchParams.get('senderId');
        if (senderId && currentUser) {
            findUserByPublicId(senderId).then(user => {
                if (user) {
                    handleUserSelection(user);
                }
            });
        }
    }, [searchParams, currentUser, findUserByPublicId, handleUserSelection]);
    
    useEffect(() => {
        if (Capacitor.getPlatform() === 'web') return;
    
        // Clear any existing listeners to avoid duplicates when the component re-renders
        PushNotifications.removeAllListeners().then(() => {
            PushNotifications.addListener('pushNotificationActionPerformed', (action) => {
                const { actionId, notification } = action;
                const data = notification.data || {};
                
                console.log(`[Push Action] Received action: ${actionId}`, data);
    
                if (data && data.type === 'incoming-call') {
                    // Important: Check if this action corresponds to the current incoming call
                    if (callState.status === 'incoming' && callState.channelName === data.channelName) {
                         if (actionId === 'answer') {
                            console.log('[Push Action] Answering call...');
                            answerCall();
                        } else if (actionId === 'decline') {
                             console.log('[Push Action] Declining call...');
                            endCall();
                        }
                    } else {
                        console.warn(`[Push Action] Received action for call ${data.channelName}, but current call state is ${callState.status} for channel ${callState.channelName}. Action ignored.`);
                    }
                }
            });
        });
    
        // Cleanup on component unmount
        return () => {
             PushNotifications.removeAllListeners();
        }
    }, [callState.status, callState.channelName, answerCall, endCall]); // Dependencies ensure the listener always has the latest state handlers

    const openSettings = useCallback((section?: string) => {
        setSettingsSection(section);
        setSettingsOpen(true);
        setRecipient(null); // Close any open chat window
    }, []);

    const openChangeNameModal = useCallback(() => setChangeNameModalOpen(true), []);
    const openChangeStatusModal = useCallback(() => setChangeStatusModalOpen(true), []);
    const openChangeBioModal = useCallback(() => setChangeBioModalOpen(true), []);

    if (authStatus !== 'authenticated' || !currentUser) {
        return null;
    }

    const handleSignOut = async () => {
        await signOutUser();
        setSignOutModalOpen(false);
        setSignOutConfirmText('');
    };

    const isMobile = typeof window !== 'undefined' && window.innerWidth < 768;
    const showMobileNav = isMobile && !recipient && !isSettingsOpen;
    const isSignOutConfirmValid = signOutConfirmText.toLowerCase() === t('leaveWord') || signOutConfirmText.toLowerCase() === 'leave';
    const numericUid = parseInt(currentUser.uid.replace(/[^0-9]/g, '').substring(0, 8), 10) || Math.floor(Math.random() * 100000);

    return (
        <>
            <NetworkStatusIndicator />
            <div className="relative h-full w-full flex overflow-hidden">
                {/* Pane 1: User List (always visible on desktop, conditionally on mobile) */}
                <div className={cn(
                    "h-full flex-col md:flex md:w-80 lg:w-96 md:flex-shrink-0 md:border-r",
                    (recipient || isSettingsOpen) ? "hidden" : "flex w-full"
                )}>
                    <UserList 
                        onSelectRecipient={(r) => { setRecipient(r); setSettingsOpen(false); }}
                        onOpenCreateGroup={() => setCreateGroupModalOpen(true)}
                        onOpenSettings={openSettings}
                        onSignOut={() => setSignOutModalOpen(true)}
                        onViewProfile={setViewingProfile}
                    />
                </div>

                {/* Pane 2: Main Content (Chat, Settings, or Welcome) */}
                <div className={cn(
                    "h-full flex-1 flex-col",
                    // On mobile, this pane is only visible if a chat or settings is open
                    (!recipient && !isSettingsOpen) ? "hidden md:flex" : "flex"
                )}>
                    {isSettingsOpen ? (
                        <SettingsPage 
                            onClose={() => setSettingsOpen(false)} 
                            initialSection={settingsSection}
                        />
                    ) : recipient ? (
                        <ChatWindow
                            recipient={recipient}
                            onClose={() => setRecipient(null)}
                            onViewProfile={setViewingProfile}
                            onPreviewImage={setImageToPreview}
                            onAddMembers={(group) => setGroupToAddMembers(group)}
                            onStartCall={handleStartCall}
                            onOpenSettings={openSettings}
                            openChangeNameModal={openChangeNameModal}
                            openChangeStatusModal={openChangeStatusModal}
                            openChangeBioModal={openChangeBioModal}
                        />
                    ) : (
                        // This only shows on desktop when no chat/settings are open
                        <WelcomeScreen />
                    )}
                </div>

                {/* Mobile nav is only shown on the UserList screen */}
                {showMobileNav && <MobileBottomNav onOpenSettings={openSettings} />}
            </div>

            <CreateGroupModal isOpen={isCreateGroupModalOpen} onClose={() => setCreateGroupModalOpen(false)} />
            <ViewProfileModal 
                user={viewingProfile} 
                isOpen={!!viewingProfile} 
                onClose={() => setViewingProfile(null)} 
                onStartChat={handleUserSelection} 
            />
            
            {groupToAddMembers && (
                <AddGroupMembersModal 
                    isOpen={!!groupToAddMembers} 
                    onClose={() => setGroupToAddMembers(null)} 
                    group={groupToAddMembers}
                />
            )}
            <ImagePreviewModal imageUrl={imageToPreview} isOpen={!!imageToPreview} onClose={() => setImageToPreview(null)} />
            
            <ChangeNameModal isOpen={isChangeNameModalOpen} onClose={() => setChangeNameModalOpen(false)} />
            <ChangeStatusModal isOpen={isChangeStatusModalOpen} onClose={() => setChangeStatusModalOpen(false)} />
            <ChangeBioModal isOpen={isChangeBioModalOpen} onClose={() => setChangeBioModalOpen(false)} />

            <AlertDialog open={isSignOutModalOpen} onOpenChange={setSignOutModalOpen}>
                <AlertDialogContent>
                    <AlertDialogHeader>
                        <AlertDialogTitle>{t('signOutConfirmationTitle')}</AlertDialogTitle>
                        <AlertDialogDescription>
                            {t('signOutConfirmationDescription', { word: t('leaveWord') })}
                        </AlertDialogDescription>
                    </AlertDialogHeader>
                    <Input 
                        value={signOutConfirmText}
                        onChange={(e) => setSignOutConfirmText(e.target.value)}
                        placeholder={t('leaveWord')}
                        autoFocus
                    />
                    <AlertDialogFooter>
                        <AlertDialogCancel onClick={() => setSignOutConfirmText('')}>{t('cancel')}</AlertDialogCancel>
                        <AlertDialogAction onClick={handleSignOut} disabled={!isSignOutConfirmValid} className="bg-destructive text-destructive-foreground hover:bg-destructive/90">
                            {t('signOut')}
                        </AlertDialogAction>
                    </AlertDialogFooter>
                </AlertDialogContent>
            </AlertDialog>

            <CallModal
                callState={callState}
                onAnswerCall={answerCall}
                onEndCall={endCall}
                uid={numericUid}
                userName={currentUser.displayName}
            />
        </>
    );
}