2026-01-20 12:26:27 +05:30
|
|
|
'use client';
|
|
|
|
|
|
2026-02-08 22:44:06 +05:30
|
|
|
import { useState, useRef, useEffect, useCallback } from 'react';
|
2026-01-20 12:26:27 +05:30
|
|
|
import Image from 'next/image';
|
2026-02-08 22:44:06 +05:30
|
|
|
import { useSession } from 'next-auth/react';
|
2026-01-20 12:26:27 +05:30
|
|
|
import { ChatHeader } from './ChatHeader';
|
|
|
|
|
import { MessageInput } from './MessageInput';
|
2026-03-09 23:47:28 +05:30
|
|
|
import { ChatDropdownMenu, type DropdownMenuItem } from './ChatDropdownMenu';
|
2026-03-15 00:29:25 +05:30
|
|
|
import { NewConversationModal, type ConnectedContact } from './NewConversationModal';
|
2026-03-19 05:24:41 +05:30
|
|
|
import { ReportUserModal } from './ReportUserModal';
|
2026-02-08 22:44:06 +05:30
|
|
|
import { useMessaging } from '@/hooks/useMessaging';
|
|
|
|
|
import { connectionRequestsService, type ConnectionRequest } from '@/services/connection-requests.service';
|
|
|
|
|
import { uploadService } from '@/services/upload.service';
|
2026-02-21 22:11:42 +05:30
|
|
|
import type { Conversation, Message, MessageType } from '@/types/messaging';
|
2026-02-08 22:44:06 +05:30
|
|
|
|
|
|
|
|
// Helper to get a valid avatar URL (must start with http://, https://, or /)
|
|
|
|
|
function getValidAvatarUrl(avatar: string | null | undefined): string {
|
|
|
|
|
const placeholder = '/assets/icons/user-placeholder-icon.svg';
|
|
|
|
|
if (!avatar) return placeholder;
|
|
|
|
|
// Check if it's a valid URL format
|
|
|
|
|
if (avatar.startsWith('http://') || avatar.startsWith('https://') || avatar.startsWith('/')) {
|
|
|
|
|
return avatar;
|
|
|
|
|
}
|
|
|
|
|
// Invalid URL format (like raw storage key), use placeholder
|
|
|
|
|
return placeholder;
|
|
|
|
|
}
|
2026-01-20 12:26:27 +05:30
|
|
|
|
|
|
|
|
// Custom scrollbar styles
|
|
|
|
|
const customScrollbarStyles = `
|
|
|
|
|
.custom-scrollbar::-webkit-scrollbar {
|
|
|
|
|
width: 8px;
|
|
|
|
|
}
|
|
|
|
|
.custom-scrollbar::-webkit-scrollbar-track {
|
|
|
|
|
background: transparent;
|
|
|
|
|
}
|
|
|
|
|
.custom-scrollbar::-webkit-scrollbar-thumb {
|
|
|
|
|
background: rgba(0, 41, 61, 0.5);
|
|
|
|
|
border-radius: 10px;
|
|
|
|
|
}
|
|
|
|
|
.custom-scrollbar::-webkit-scrollbar-thumb:hover {
|
|
|
|
|
background: rgba(0, 41, 61, 0.7);
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2026-02-08 22:44:06 +05:30
|
|
|
// Helper to format date for conversation list
|
|
|
|
|
function formatConversationDate(dateString: string | null): string {
|
|
|
|
|
if (!dateString) return '';
|
|
|
|
|
const date = new Date(dateString);
|
|
|
|
|
const now = new Date();
|
|
|
|
|
const diffDays = Math.floor((now.getTime() - date.getTime()) / (1000 * 60 * 60 * 24));
|
|
|
|
|
|
|
|
|
|
if (diffDays === 0) {
|
|
|
|
|
return date.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit' });
|
|
|
|
|
} else if (diffDays === 1) {
|
|
|
|
|
return 'Yesterday';
|
|
|
|
|
} else if (diffDays < 7) {
|
|
|
|
|
return date.toLocaleDateString('en-US', { weekday: 'short' });
|
|
|
|
|
} else {
|
|
|
|
|
return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
|
|
|
|
|
}
|
2026-01-20 12:26:27 +05:30
|
|
|
}
|
|
|
|
|
|
2026-02-08 22:44:06 +05:30
|
|
|
// Helper to format message timestamp
|
|
|
|
|
function formatMessageTime(dateString: string): string {
|
|
|
|
|
const date = new Date(dateString);
|
|
|
|
|
return date.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit' });
|
2026-01-20 12:26:27 +05:30
|
|
|
}
|
|
|
|
|
|
2026-02-08 22:44:06 +05:30
|
|
|
// Helper to get last seen text
|
|
|
|
|
function formatLastSeen(lastSeenAt: string | null | undefined, isOnline: boolean): string {
|
|
|
|
|
if (isOnline) return 'Online';
|
|
|
|
|
if (!lastSeenAt) return 'Offline';
|
|
|
|
|
|
|
|
|
|
const date = new Date(lastSeenAt);
|
|
|
|
|
const now = new Date();
|
|
|
|
|
const diffHours = Math.floor((now.getTime() - date.getTime()) / (1000 * 60 * 60));
|
|
|
|
|
|
|
|
|
|
if (diffHours < 1) return 'Last seen recently';
|
|
|
|
|
if (diffHours < 24) return `${diffHours}h ago`;
|
|
|
|
|
const diffDays = Math.floor(diffHours / 24);
|
|
|
|
|
if (diffDays === 1) return 'Last seen yesterday';
|
|
|
|
|
return `Last seen ${diffDays}d ago`;
|
2026-01-20 12:26:27 +05:30
|
|
|
}
|
|
|
|
|
|
2026-03-27 15:47:29 +05:30
|
|
|
// Avatar component with shimmer loading animation and initials fallback
|
|
|
|
|
function AvatarWithPlaceholder({ src, alt, size, name }: { src: string; alt: string; size: number; name?: string }) {
|
2026-03-13 22:45:11 +05:30
|
|
|
const [loaded, setLoaded] = useState(false);
|
2026-03-27 15:47:29 +05:30
|
|
|
const [imgError, setImgError] = useState(false);
|
2026-03-13 22:45:11 +05:30
|
|
|
const placeholder = '/assets/icons/user-placeholder-icon.svg';
|
|
|
|
|
const isPlaceholder = !src || src === placeholder;
|
2026-03-27 15:47:29 +05:30
|
|
|
const initial = name?.[0]?.toUpperCase() || '?';
|
2026-03-13 22:45:11 +05:30
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2026-03-28 17:01:37 +05:30
|
|
|
{/* Shimmer while loading, initials only on error/no image */}
|
|
|
|
|
{imgError || isPlaceholder ? (
|
|
|
|
|
<div className="absolute inset-0 rounded-full bg-[#c4d9d4] flex items-center justify-center">
|
|
|
|
|
<span className="font-bold text-[#00293d]" style={{ fontSize: size * 0.4 }}>{initial}</span>
|
|
|
|
|
</div>
|
|
|
|
|
) : !loaded ? (
|
|
|
|
|
<div className="absolute inset-0 rounded-full shimmer-loading" />
|
|
|
|
|
) : null}
|
2026-03-27 15:47:29 +05:30
|
|
|
{src && !isPlaceholder && !imgError && (
|
2026-03-13 22:45:11 +05:30
|
|
|
<img
|
2026-03-18 11:54:40 +05:30
|
|
|
ref={(el) => { if (el?.complete && el.naturalWidth > 0) setLoaded(true); }}
|
2026-03-13 22:45:11 +05:30
|
|
|
src={src}
|
|
|
|
|
alt={alt}
|
2026-03-14 14:37:19 +05:30
|
|
|
className={`absolute inset-0 w-full h-full object-cover transition-opacity duration-300 ${loaded ? 'opacity-100' : 'opacity-0'}`}
|
2026-03-13 22:45:11 +05:30
|
|
|
onLoad={() => setLoaded(true)}
|
2026-03-27 15:47:29 +05:30
|
|
|
onError={() => setImgError(true)}
|
2026-03-13 22:45:11 +05:30
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 00:29:25 +05:30
|
|
|
// Connected contact with avatar URL (used for both user-side agents and agent-side users)
|
2026-02-08 22:44:06 +05:30
|
|
|
interface ConnectedAgent {
|
|
|
|
|
agentProfileId: string;
|
2026-03-15 00:29:25 +05:30
|
|
|
userId?: string; // The user's ID (for agent-side contacts)
|
2026-02-08 22:44:06 +05:30
|
|
|
name: string;
|
|
|
|
|
avatar: string | null;
|
|
|
|
|
avatarUrl: string | null;
|
|
|
|
|
headline: string | null;
|
2026-03-15 00:29:25 +05:30
|
|
|
email: string | null;
|
2026-02-08 22:44:06 +05:30
|
|
|
city: string | null;
|
|
|
|
|
state: string | null;
|
2026-01-20 12:26:27 +05:30
|
|
|
}
|
|
|
|
|
|
2026-03-29 17:40:06 +05:30
|
|
|
export function MessagingPage({ initialConversationId, initialAgentProfileId }: { initialConversationId?: string | null; initialAgentProfileId?: string | null }) {
|
2026-02-08 22:44:06 +05:30
|
|
|
const { data: session } = useSession();
|
|
|
|
|
const {
|
|
|
|
|
conversations,
|
|
|
|
|
currentConversation,
|
|
|
|
|
messages,
|
|
|
|
|
isConnected,
|
|
|
|
|
isLoading,
|
|
|
|
|
isLoadingMessages,
|
|
|
|
|
typingUsers,
|
|
|
|
|
selectConversation,
|
|
|
|
|
sendMessage,
|
|
|
|
|
startTyping,
|
|
|
|
|
stopTyping,
|
|
|
|
|
loadMoreMessages,
|
|
|
|
|
hasMoreMessages,
|
|
|
|
|
startConversation,
|
2026-03-09 23:47:28 +05:30
|
|
|
markAllAsRead,
|
|
|
|
|
deleteConversation,
|
|
|
|
|
clearMessages,
|
2026-03-19 17:03:25 +05:30
|
|
|
toggleMute,
|
|
|
|
|
toggleFavorite,
|
2026-02-08 22:44:06 +05:30
|
|
|
} = useMessaging();
|
|
|
|
|
|
2026-01-20 12:26:27 +05:30
|
|
|
const [searchQuery, setSearchQuery] = useState('');
|
2026-03-09 23:47:28 +05:30
|
|
|
const [isPageMenuOpen, setIsPageMenuOpen] = useState(false);
|
|
|
|
|
const [isAllMuted, setIsAllMuted] = useState(false);
|
|
|
|
|
const [confirmDeleteId, setConfirmDeleteId] = useState<string | null>(null);
|
2026-02-21 22:11:42 +05:30
|
|
|
const [isUploading, setIsUploading] = useState(false);
|
2026-01-20 12:26:27 +05:30
|
|
|
const [showScrollButton, setShowScrollButton] = useState(false);
|
2026-02-08 22:44:06 +05:30
|
|
|
const [connectedAgents, setConnectedAgents] = useState<ConnectedAgent[]>([]);
|
|
|
|
|
const [isLoadingAgents, setIsLoadingAgents] = useState(false);
|
|
|
|
|
const [isStartingConversation, setIsStartingConversation] = useState<string | null>(null);
|
2026-03-15 00:29:25 +05:30
|
|
|
const [isComposeOpen, setIsComposeOpen] = useState(false);
|
2026-03-19 05:24:41 +05:30
|
|
|
const [isReportModalOpen, setIsReportModalOpen] = useState(false);
|
2026-03-20 02:41:07 +05:30
|
|
|
const [showMobileChat, setShowMobileChat] = useState(false);
|
2026-02-08 22:44:06 +05:30
|
|
|
// Store converted avatar URLs by conversation ID
|
|
|
|
|
const [conversationAvatarUrls, setConversationAvatarUrls] = useState<Record<string, string>>({});
|
2026-02-21 22:11:42 +05:30
|
|
|
// Store converted file URLs by message ID (for S3-stored images/files)
|
|
|
|
|
const [messageFileUrls, setMessageFileUrls] = useState<Record<string, string>>({});
|
2026-01-20 12:26:27 +05:30
|
|
|
const messagesContainerRef = useRef<HTMLDivElement>(null);
|
2026-02-08 22:44:06 +05:30
|
|
|
const messagesEndRef = useRef<HTMLDivElement>(null);
|
2026-02-11 07:36:40 +05:30
|
|
|
const justSwitchedConversationRef = useRef(false);
|
2026-01-20 12:26:27 +05:30
|
|
|
|
2026-02-08 22:44:06 +05:30
|
|
|
const currentUserId = session?.user?.id;
|
|
|
|
|
const userRole = session?.user?.role;
|
|
|
|
|
|
|
|
|
|
// Fetch connected users/agents (accepted connections) based on role
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const fetchConnectedContacts = async () => {
|
|
|
|
|
if (!session || !userRole) return;
|
|
|
|
|
|
|
|
|
|
setIsLoadingAgents(true);
|
|
|
|
|
try {
|
|
|
|
|
// USER sees agents, AGENT sees users
|
|
|
|
|
if (userRole === 'USER') {
|
|
|
|
|
const requests = await connectionRequestsService.getMyRequests('ACCEPTED');
|
|
|
|
|
|
|
|
|
|
// Map to connected agents with avatar URLs
|
|
|
|
|
const agents: ConnectedAgent[] = await Promise.all(
|
|
|
|
|
requests.map(async (req) => {
|
|
|
|
|
const agent = req.agentProfile;
|
|
|
|
|
let avatarUrl: string | null = null;
|
|
|
|
|
|
|
|
|
|
if (agent?.avatar) {
|
|
|
|
|
try {
|
|
|
|
|
avatarUrl = await uploadService.getPresignedDownloadUrl(agent.avatar);
|
|
|
|
|
} catch {
|
|
|
|
|
avatarUrl = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
agentProfileId: req.agentProfileId,
|
|
|
|
|
name: agent ? `${agent.firstName || ''} ${agent.lastName || ''}`.trim() || 'Agent' : 'Agent',
|
|
|
|
|
avatar: agent?.avatar || null,
|
|
|
|
|
avatarUrl,
|
|
|
|
|
headline: agent?.headline || null,
|
2026-03-15 00:29:25 +05:30
|
|
|
email: null,
|
2026-02-08 22:44:06 +05:30
|
|
|
city: agent?.city || null,
|
|
|
|
|
state: agent?.state || null,
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
setConnectedAgents(agents);
|
|
|
|
|
} else {
|
2026-03-15 00:29:25 +05:30
|
|
|
// AGENT role — fetch accepted connections (users who connected with this agent)
|
|
|
|
|
const requests = await connectionRequestsService.getReceivedRequests('ACCEPTED');
|
|
|
|
|
|
|
|
|
|
const users: ConnectedAgent[] = await Promise.all(
|
|
|
|
|
requests.map(async (req) => {
|
|
|
|
|
const user = req.user;
|
|
|
|
|
const profile = user?.userProfile;
|
|
|
|
|
let avatarUrl: string | null = null;
|
|
|
|
|
|
|
|
|
|
const avatarKey = profile?.avatar || user?.avatar;
|
|
|
|
|
if (avatarKey) {
|
|
|
|
|
try {
|
|
|
|
|
avatarUrl = await uploadService.getPresignedDownloadUrl(avatarKey);
|
|
|
|
|
} catch {
|
|
|
|
|
avatarUrl = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fullName = profile
|
|
|
|
|
? `${profile.firstName || ''} ${profile.lastName || ''}`.trim()
|
|
|
|
|
: '';
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
agentProfileId: req.agentProfileId,
|
|
|
|
|
userId: req.userId,
|
|
|
|
|
name: fullName || user?.email || 'User',
|
|
|
|
|
avatar: avatarKey || null,
|
|
|
|
|
avatarUrl,
|
|
|
|
|
headline: null,
|
|
|
|
|
email: user?.email || null,
|
|
|
|
|
city: null,
|
|
|
|
|
state: null,
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
setConnectedAgents(users);
|
2026-02-08 22:44:06 +05:30
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to fetch connected contacts:', error);
|
|
|
|
|
setConnectedAgents([]);
|
|
|
|
|
} finally {
|
|
|
|
|
setIsLoadingAgents(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fetchConnectedContacts();
|
|
|
|
|
}, [session, userRole]);
|
|
|
|
|
|
|
|
|
|
// Convert conversation avatar storage keys to presigned URLs
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const convertAvatarUrls = async () => {
|
|
|
|
|
const newAvatarUrls: Record<string, string> = {};
|
|
|
|
|
|
|
|
|
|
for (const conversation of conversations) {
|
|
|
|
|
const avatar = conversation.otherParty?.avatar;
|
|
|
|
|
if (!avatar) continue;
|
|
|
|
|
|
|
|
|
|
// Skip if already converted or if it's already a valid URL
|
|
|
|
|
if (conversationAvatarUrls[conversation.id]) {
|
|
|
|
|
newAvatarUrls[conversation.id] = conversationAvatarUrls[conversation.id];
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if it's already a valid URL format
|
|
|
|
|
if (avatar.startsWith('http://') || avatar.startsWith('https://') || avatar.startsWith('/')) {
|
|
|
|
|
newAvatarUrls[conversation.id] = avatar;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-01-20 12:26:27 +05:30
|
|
|
|
2026-02-08 22:44:06 +05:30
|
|
|
// Convert storage key to presigned URL
|
|
|
|
|
try {
|
|
|
|
|
const presignedUrl = await uploadService.getPresignedDownloadUrl(avatar);
|
|
|
|
|
newAvatarUrls[conversation.id] = presignedUrl;
|
|
|
|
|
} catch {
|
|
|
|
|
// Keep as null/undefined if conversion fails
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Object.keys(newAvatarUrls).length > 0) {
|
|
|
|
|
setConversationAvatarUrls(prev => ({ ...prev, ...newAvatarUrls }));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (conversations.length > 0) {
|
|
|
|
|
convertAvatarUrls();
|
|
|
|
|
}
|
|
|
|
|
}, [conversations]);
|
|
|
|
|
|
2026-03-15 01:03:44 +05:30
|
|
|
// Auto-select conversation when navigated with initialConversationId
|
2026-03-19 16:37:24 +05:30
|
|
|
const initialConvHandledRef = useRef<string | null>(null);
|
2026-03-15 01:03:44 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
if (
|
|
|
|
|
initialConversationId &&
|
2026-03-19 16:37:24 +05:30
|
|
|
initialConvHandledRef.current !== initialConversationId &&
|
2026-03-15 01:03:44 +05:30
|
|
|
conversations.length > 0 &&
|
|
|
|
|
!isLoading
|
|
|
|
|
) {
|
2026-03-19 16:37:24 +05:30
|
|
|
initialConvHandledRef.current = initialConversationId;
|
2026-03-15 01:03:44 +05:30
|
|
|
selectConversation(initialConversationId);
|
2026-03-20 02:41:07 +05:30
|
|
|
setShowMobileChat(true);
|
2026-03-15 01:03:44 +05:30
|
|
|
}
|
|
|
|
|
}, [initialConversationId, conversations, isLoading, selectConversation]);
|
|
|
|
|
|
2026-03-29 17:40:06 +05:30
|
|
|
// Auto-select or create conversation when navigated with initialAgentProfileId
|
|
|
|
|
const initialAgentHandledRef = useRef<string | null>(null);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (
|
|
|
|
|
initialAgentProfileId &&
|
|
|
|
|
initialAgentHandledRef.current !== initialAgentProfileId &&
|
|
|
|
|
conversations.length > 0 &&
|
|
|
|
|
!isLoading
|
|
|
|
|
) {
|
|
|
|
|
initialAgentHandledRef.current = initialAgentProfileId;
|
|
|
|
|
// Find existing conversation with this agent
|
|
|
|
|
const existing = conversations.find(
|
|
|
|
|
(c) => c.agentProfileId === initialAgentProfileId || c.otherParty?.id === initialAgentProfileId
|
|
|
|
|
);
|
|
|
|
|
if (existing) {
|
|
|
|
|
selectConversation(existing.id);
|
|
|
|
|
setShowMobileChat(true);
|
|
|
|
|
} else {
|
|
|
|
|
// Start a new conversation
|
|
|
|
|
startConversation({ agentProfileId: initialAgentProfileId });
|
|
|
|
|
setShowMobileChat(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [initialAgentProfileId, conversations, isLoading, selectConversation, startConversation]);
|
|
|
|
|
|
2026-02-08 22:44:06 +05:30
|
|
|
// Convert current conversation avatar when selected
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const convertCurrentAvatar = async () => {
|
|
|
|
|
if (!currentConversation) return;
|
|
|
|
|
|
|
|
|
|
const avatar = currentConversation.otherParty?.avatar;
|
|
|
|
|
if (!avatar) return;
|
|
|
|
|
|
|
|
|
|
// Skip if already converted
|
|
|
|
|
if (conversationAvatarUrls[currentConversation.id]) return;
|
|
|
|
|
|
|
|
|
|
// Check if it's already a valid URL format
|
|
|
|
|
if (avatar.startsWith('http://') || avatar.startsWith('https://') || avatar.startsWith('/')) {
|
|
|
|
|
setConversationAvatarUrls(prev => ({ ...prev, [currentConversation.id]: avatar }));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert storage key to presigned URL
|
|
|
|
|
try {
|
|
|
|
|
const presignedUrl = await uploadService.getPresignedDownloadUrl(avatar);
|
|
|
|
|
setConversationAvatarUrls(prev => ({ ...prev, [currentConversation.id]: presignedUrl }));
|
|
|
|
|
} catch {
|
|
|
|
|
// Keep as null/undefined if conversion fails
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
convertCurrentAvatar();
|
|
|
|
|
}, [currentConversation]);
|
|
|
|
|
|
2026-02-21 22:11:42 +05:30
|
|
|
// Convert S3 keys to presigned download URLs for message attachments
|
|
|
|
|
useEffect(() => {
|
2026-03-20 01:49:46 +05:30
|
|
|
const isS3Key = (val: string) => val && !val.startsWith('http://') && !val.startsWith('https://') && !val.startsWith('/') && !val.startsWith('data:');
|
|
|
|
|
const isUrl = (val: string) => val.startsWith('http://') || val.startsWith('https://');
|
|
|
|
|
|
2026-02-21 22:11:42 +05:30
|
|
|
const convertFileUrls = async () => {
|
|
|
|
|
for (const msg of messages) {
|
2026-03-20 01:49:46 +05:30
|
|
|
if (msg.messageType !== 'IMAGE' && msg.messageType !== 'FILE') continue;
|
|
|
|
|
if (messageFileUrls[msg.id]) continue;
|
|
|
|
|
|
|
|
|
|
// Determine the source: fileUrl or content (some messages store S3 key in content)
|
|
|
|
|
const source = msg.fileUrl || msg.content;
|
|
|
|
|
if (!source) continue;
|
|
|
|
|
|
|
|
|
|
if (isUrl(source)) {
|
|
|
|
|
setMessageFileUrls(prev => ({ ...prev, [msg.id]: source }));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isS3Key(source)) {
|
2026-02-21 22:11:42 +05:30
|
|
|
try {
|
2026-03-20 01:49:46 +05:30
|
|
|
const presignedUrl = await uploadService.getPresignedDownloadUrl(source);
|
2026-02-21 22:11:42 +05:30
|
|
|
setMessageFileUrls(prev => ({ ...prev, [msg.id]: presignedUrl }));
|
|
|
|
|
} catch {
|
|
|
|
|
// Keep original if conversion fails
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (messages.length > 0) {
|
|
|
|
|
convertFileUrls();
|
|
|
|
|
}
|
|
|
|
|
}, [messages]);
|
|
|
|
|
|
|
|
|
|
// Clear cached file URLs when switching conversations
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setMessageFileUrls({});
|
|
|
|
|
}, [currentConversation?.id]);
|
|
|
|
|
|
2026-03-15 00:29:25 +05:30
|
|
|
// Handle starting a new conversation — role-aware
|
|
|
|
|
const handleStartConversation = useCallback(async (contactId: string) => {
|
|
|
|
|
setIsStartingConversation(contactId);
|
2026-02-08 22:44:06 +05:30
|
|
|
try {
|
2026-03-15 00:29:25 +05:30
|
|
|
const body = userRole === 'AGENT'
|
|
|
|
|
? { userId: contactId }
|
|
|
|
|
: { agentProfileId: contactId };
|
|
|
|
|
const conversation = await startConversation(body);
|
2026-02-08 22:44:06 +05:30
|
|
|
await selectConversation(conversation.id);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to start conversation:', error);
|
|
|
|
|
} finally {
|
|
|
|
|
setIsStartingConversation(null);
|
|
|
|
|
}
|
2026-03-15 00:29:25 +05:30
|
|
|
}, [startConversation, selectConversation, userRole]);
|
2026-02-08 22:44:06 +05:30
|
|
|
|
|
|
|
|
// Handle sending a message
|
|
|
|
|
const handleSendMessage = useCallback(async (content: string) => {
|
|
|
|
|
if (!content.trim()) return;
|
|
|
|
|
await sendMessage(content);
|
|
|
|
|
}, [sendMessage]);
|
|
|
|
|
|
2026-02-21 22:11:42 +05:30
|
|
|
// Handle sending a GIF
|
|
|
|
|
const handleSendGif = useCallback(async (gifUrl: string) => {
|
|
|
|
|
await sendMessage(gifUrl, 'IMAGE' as MessageType, { fileUrl: gifUrl, mimeType: 'image/gif' });
|
|
|
|
|
}, [sendMessage]);
|
|
|
|
|
|
|
|
|
|
// Handle sending a file (image or document)
|
|
|
|
|
const handleSendFile = useCallback(async (file: File, messageType: 'IMAGE' | 'FILE') => {
|
|
|
|
|
setIsUploading(true);
|
|
|
|
|
try {
|
|
|
|
|
const uploaded = await uploadService.uploadMessageFile(file);
|
|
|
|
|
// uploaded.url is the S3 key (not a full URL)
|
|
|
|
|
const content = messageType === 'IMAGE' ? 'Sent an image' : 'Sent a file';
|
|
|
|
|
await sendMessage(
|
|
|
|
|
content,
|
|
|
|
|
messageType as MessageType,
|
|
|
|
|
{
|
|
|
|
|
fileUrl: uploaded.url,
|
|
|
|
|
mimeType: file.type,
|
|
|
|
|
fileName: file.name,
|
|
|
|
|
fileSize: file.size,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to upload file:', error);
|
|
|
|
|
} finally {
|
|
|
|
|
setIsUploading(false);
|
|
|
|
|
}
|
|
|
|
|
}, [sendMessage]);
|
|
|
|
|
|
2026-02-08 22:44:06 +05:30
|
|
|
// Handle typing events
|
|
|
|
|
const handleTypingStart = useCallback(() => {
|
|
|
|
|
startTyping();
|
|
|
|
|
}, [startTyping]);
|
|
|
|
|
|
|
|
|
|
const handleTypingStop = useCallback(() => {
|
|
|
|
|
stopTyping();
|
|
|
|
|
}, [stopTyping]);
|
|
|
|
|
|
2026-03-09 23:47:28 +05:30
|
|
|
// Handle delete conversation with confirmation
|
|
|
|
|
const handleDeleteConversation = useCallback(async () => {
|
|
|
|
|
if (!currentConversation) return;
|
|
|
|
|
setConfirmDeleteId(currentConversation.id);
|
|
|
|
|
}, [currentConversation]);
|
|
|
|
|
|
|
|
|
|
const confirmDelete = useCallback(async () => {
|
|
|
|
|
if (!confirmDeleteId) return;
|
|
|
|
|
try {
|
|
|
|
|
await deleteConversation(confirmDeleteId);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to delete conversation:', error);
|
|
|
|
|
}
|
|
|
|
|
setConfirmDeleteId(null);
|
|
|
|
|
}, [confirmDeleteId, deleteConversation]);
|
|
|
|
|
|
|
|
|
|
// Page header menu items
|
|
|
|
|
const pageMenuItems: DropdownMenuItem[] = [
|
|
|
|
|
{
|
|
|
|
|
label: 'Mark All as Read',
|
|
|
|
|
onClick: () => {
|
|
|
|
|
markAllAsRead().catch(console.error);
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: isAllMuted ? 'Unmute All Notifications' : 'Mute All Notifications',
|
|
|
|
|
onClick: () => setIsAllMuted((prev) => !prev),
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2026-02-08 22:44:06 +05:30
|
|
|
// Handle scroll events
|
2026-01-20 12:26:27 +05:30
|
|
|
const handleScroll = () => {
|
|
|
|
|
if (messagesContainerRef.current) {
|
|
|
|
|
const { scrollTop, scrollHeight, clientHeight } = messagesContainerRef.current;
|
|
|
|
|
// Show button if not at bottom (with 100px threshold)
|
|
|
|
|
setShowScrollButton(scrollHeight - scrollTop - clientHeight > 100);
|
2026-02-08 22:44:06 +05:30
|
|
|
|
|
|
|
|
// Load more messages when scrolled to top
|
|
|
|
|
if (scrollTop === 0 && hasMoreMessages && !isLoadingMessages) {
|
|
|
|
|
loadMoreMessages();
|
|
|
|
|
}
|
2026-01-20 12:26:27 +05:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const scrollToBottom = () => {
|
|
|
|
|
if (messagesContainerRef.current) {
|
|
|
|
|
messagesContainerRef.current.scrollTo({
|
|
|
|
|
top: messagesContainerRef.current.scrollHeight,
|
|
|
|
|
behavior: 'smooth'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-11 07:36:40 +05:30
|
|
|
// Mark that we switched conversations so we scroll after messages load
|
2026-01-20 12:26:27 +05:30
|
|
|
useEffect(() => {
|
2026-02-11 07:36:40 +05:30
|
|
|
justSwitchedConversationRef.current = true;
|
2026-02-08 22:44:06 +05:30
|
|
|
}, [currentConversation?.id]);
|
|
|
|
|
|
2026-02-11 07:36:40 +05:30
|
|
|
// Scroll to bottom when messages load after opening a chat, or when a new message arrives
|
2026-02-08 22:44:06 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
if (messages.length > 0) {
|
2026-02-11 07:36:40 +05:30
|
|
|
// After opening/switching a conversation — scroll once messages are rendered
|
|
|
|
|
if (justSwitchedConversationRef.current) {
|
|
|
|
|
justSwitchedConversationRef.current = false;
|
|
|
|
|
setTimeout(scrollToBottom, 50);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// New incoming message — scroll if it's very recent
|
2026-02-08 22:44:06 +05:30
|
|
|
const lastMessage = messages[messages.length - 1];
|
|
|
|
|
const isVeryRecent = new Date().getTime() - new Date(lastMessage.createdAt).getTime() < 2000;
|
|
|
|
|
if (isVeryRecent) {
|
|
|
|
|
scrollToBottom();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [messages]);
|
2026-01-20 12:26:27 +05:30
|
|
|
|
2026-02-11 07:36:40 +05:30
|
|
|
// Scroll to bottom when typing indicator appears
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (typingUsers.size > 0) {
|
|
|
|
|
scrollToBottom();
|
|
|
|
|
}
|
|
|
|
|
}, [typingUsers]);
|
|
|
|
|
|
2026-02-08 22:44:06 +05:30
|
|
|
// Filter conversations by search
|
2026-01-20 12:26:27 +05:30
|
|
|
const filteredConversations = conversations.filter((conv) =>
|
2026-02-08 22:44:06 +05:30
|
|
|
conv.otherParty?.name?.toLowerCase().includes(searchQuery.toLowerCase())
|
2026-01-20 12:26:27 +05:30
|
|
|
);
|
|
|
|
|
|
2026-02-08 22:44:06 +05:30
|
|
|
// Build selected user info for ChatHeader
|
|
|
|
|
const selectedUserInfo = currentConversation ? {
|
|
|
|
|
name: currentConversation.otherParty?.name || 'Unknown',
|
|
|
|
|
role: currentConversation.otherParty?.headline || 'Agent',
|
|
|
|
|
lastSeen: formatLastSeen(currentConversation.otherParty?.lastSeenAt, currentConversation.otherParty?.isOnline),
|
|
|
|
|
avatar: getValidAvatarUrl(conversationAvatarUrls[currentConversation.id]),
|
|
|
|
|
isOnline: currentConversation.otherParty?.isOnline || false,
|
|
|
|
|
} : null;
|
|
|
|
|
|
|
|
|
|
// Get typing user names
|
|
|
|
|
const typingUserNames = Array.from(typingUsers)
|
|
|
|
|
.filter(userId => userId !== currentUserId)
|
|
|
|
|
.map(userId => {
|
|
|
|
|
// The other party is typing
|
|
|
|
|
if (currentConversation?.otherParty.userId === userId || currentConversation?.otherParty.id === userId) {
|
|
|
|
|
return currentConversation.otherParty.name.split(' ')[0];
|
|
|
|
|
}
|
|
|
|
|
return 'Someone';
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-20 12:26:27 +05:30
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<style>{customScrollbarStyles}</style>
|
2026-03-09 23:47:28 +05:30
|
|
|
|
|
|
|
|
{/* Delete confirmation dialog */}
|
|
|
|
|
{confirmDeleteId && (
|
|
|
|
|
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
|
|
|
|
|
<div className="bg-white rounded-[15px] p-6 max-w-[400px] w-full mx-4 shadow-xl">
|
|
|
|
|
<h3 className="font-fractul font-bold text-[16px] text-[#00293D] mb-2">
|
|
|
|
|
Delete Conversation
|
|
|
|
|
</h3>
|
|
|
|
|
<p className="font-serif font-normal text-[14px] text-[#00293D]/70 mb-6">
|
|
|
|
|
Are you sure you want to delete this conversation? This action cannot be undone and all messages will be permanently removed.
|
|
|
|
|
</p>
|
|
|
|
|
<div className="flex items-center justify-end gap-3">
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => setConfirmDeleteId(null)}
|
|
|
|
|
className="px-4 py-2 font-serif font-normal text-[14px] text-[#00293D] rounded-[10px] border border-[#00293d]/10 hover:bg-gray-50 transition-colors cursor-pointer"
|
|
|
|
|
>
|
|
|
|
|
Cancel
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
onClick={confirmDelete}
|
|
|
|
|
className="px-4 py-2 font-serif font-normal text-[14px] text-white bg-red-600 rounded-[10px] hover:bg-red-700 transition-colors cursor-pointer"
|
|
|
|
|
>
|
|
|
|
|
Delete
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
2026-03-15 00:29:25 +05:30
|
|
|
{/* New conversation modal */}
|
|
|
|
|
<NewConversationModal
|
|
|
|
|
isOpen={isComposeOpen}
|
|
|
|
|
onClose={() => setIsComposeOpen(false)}
|
|
|
|
|
contacts={connectedAgents.map((agent) => ({
|
|
|
|
|
id: userRole === 'AGENT' ? agent.userId! : agent.agentProfileId,
|
|
|
|
|
name: agent.name,
|
|
|
|
|
avatar: agent.avatar,
|
|
|
|
|
avatarUrl: agent.avatarUrl,
|
|
|
|
|
headline: agent.headline,
|
|
|
|
|
email: agent.email,
|
|
|
|
|
city: agent.city,
|
|
|
|
|
state: agent.state,
|
|
|
|
|
}))}
|
|
|
|
|
isLoading={isLoadingAgents}
|
|
|
|
|
onStartConversation={async (contactId) => {
|
|
|
|
|
await handleStartConversation(contactId);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
|
2026-03-19 05:24:41 +05:30
|
|
|
{/* Report user modal */}
|
|
|
|
|
{currentConversation && (
|
|
|
|
|
<ReportUserModal
|
|
|
|
|
isOpen={isReportModalOpen}
|
|
|
|
|
onClose={() => setIsReportModalOpen(false)}
|
|
|
|
|
reportedUserId={currentConversation.otherParty?.userId || currentConversation.otherParty?.id || ''}
|
|
|
|
|
reportedUserName={currentConversation.otherParty?.name || 'User'}
|
|
|
|
|
conversationId={currentConversation.id}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
2026-01-20 12:26:27 +05:30
|
|
|
<div className="border border-[#00293d]/10 rounded-[20px] bg-white overflow-hidden">
|
|
|
|
|
{/* Header */}
|
|
|
|
|
<div className="flex items-center gap-4 p-4 border-b border-[#00293d]/10">
|
|
|
|
|
<h1 className="font-fractul font-bold text-[18px] leading-[22px] text-[#00293D]">
|
|
|
|
|
Messaging
|
|
|
|
|
</h1>
|
|
|
|
|
|
2026-02-08 22:44:06 +05:30
|
|
|
{/* Connection status indicator */}
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<span
|
|
|
|
|
className={`w-2 h-2 rounded-full ${isConnected ? 'bg-green-500' : 'bg-gray-400'}`}
|
|
|
|
|
title={isConnected ? 'Connected' : 'Disconnected'}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-01-20 12:26:27 +05:30
|
|
|
{/* Search bar */}
|
|
|
|
|
<div className="flex-1 max-w-[600px]">
|
|
|
|
|
<div className="flex items-center gap-2 border border-[#00293d]/10 rounded-[15px] px-4 py-2">
|
|
|
|
|
<Image
|
|
|
|
|
src="/assets/icons/search-icon.svg"
|
|
|
|
|
alt="Search"
|
|
|
|
|
width={20}
|
|
|
|
|
height={20}
|
|
|
|
|
/>
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
value={searchQuery}
|
|
|
|
|
onChange={(e) => setSearchQuery(e.target.value)}
|
|
|
|
|
placeholder="Search Message"
|
|
|
|
|
className="flex-1 font-serif font-normal text-[18px] leading-[24px] text-[#00293D] placeholder-[#00293D]/50 focus:outline-none"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Header actions */}
|
|
|
|
|
<div className="flex items-center gap-2 ml-auto">
|
2026-03-09 23:47:28 +05:30
|
|
|
<div className="relative">
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => setIsPageMenuOpen((prev) => !prev)}
|
|
|
|
|
className="p-2 hover:bg-gray-100 rounded-lg transition-colors cursor-pointer"
|
|
|
|
|
>
|
|
|
|
|
<Image
|
|
|
|
|
src="/assets/icons/three-dots-horizontal-icon.svg"
|
|
|
|
|
alt="More options"
|
|
|
|
|
width={24}
|
|
|
|
|
height={24}
|
|
|
|
|
/>
|
|
|
|
|
</button>
|
|
|
|
|
<ChatDropdownMenu
|
|
|
|
|
items={pageMenuItems}
|
|
|
|
|
isOpen={isPageMenuOpen}
|
|
|
|
|
onClose={() => setIsPageMenuOpen(false)}
|
2026-01-20 12:26:27 +05:30
|
|
|
/>
|
2026-03-09 23:47:28 +05:30
|
|
|
</div>
|
2026-03-15 00:29:25 +05:30
|
|
|
<button
|
|
|
|
|
onClick={() => setIsComposeOpen(true)}
|
|
|
|
|
className="p-2 hover:bg-gray-100 rounded-lg transition-colors cursor-pointer"
|
|
|
|
|
>
|
2026-01-20 12:26:27 +05:30
|
|
|
<Image
|
|
|
|
|
src="/assets/icons/compose-icon.svg"
|
|
|
|
|
alt="Compose"
|
|
|
|
|
width={24}
|
|
|
|
|
height={24}
|
|
|
|
|
/>
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Main content */}
|
|
|
|
|
<div className="flex h-[700px]">
|
2026-03-20 02:41:07 +05:30
|
|
|
{/* Left sidebar - Conversation list (hidden on mobile when chat is open) */}
|
|
|
|
|
<div className={`w-full md:w-[380px] border-r border-[#00293d]/10 flex-shrink-0 overflow-hidden flex flex-col ${showMobileChat ? 'hidden md:flex' : 'flex'}`}>
|
2026-02-08 22:44:06 +05:30
|
|
|
{isLoading || isLoadingAgents ? (
|
|
|
|
|
<div className="flex-1 flex items-center justify-center">
|
|
|
|
|
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-[#00293d]" />
|
|
|
|
|
</div>
|
|
|
|
|
) : filteredConversations.length === 0 ? (
|
|
|
|
|
/* Show connected agents when no conversations */
|
|
|
|
|
<div className="flex-1 flex flex-col">
|
|
|
|
|
{/* Header for connected agents */}
|
|
|
|
|
<div className="p-4 border-b border-[#00293d]/10">
|
|
|
|
|
<p className="font-serif font-normal text-[14px] text-[#00293D]/50 text-center">
|
2026-03-15 00:29:25 +05:30
|
|
|
{searchQuery ? 'No conversations found' : `No conversations yet. Start a conversation with ${userRole === 'AGENT' ? 'a user' : 'an agent'}!`}
|
2026-02-08 22:44:06 +05:30
|
|
|
</p>
|
|
|
|
|
</div>
|
2026-01-20 12:26:27 +05:30
|
|
|
|
2026-02-08 22:44:06 +05:30
|
|
|
{/* List of connected agents */}
|
|
|
|
|
{connectedAgents.length > 0 ? (
|
|
|
|
|
<div className="flex-1 overflow-y-auto p-2 space-y-2 custom-scrollbar">
|
|
|
|
|
<p className="px-2 py-1 font-fractul font-medium text-[12px] text-[#00293D]/70 uppercase tracking-wide">
|
2026-03-15 00:29:25 +05:30
|
|
|
{userRole === 'AGENT' ? 'Your Connected Users' : 'Your Connected Agents'}
|
2026-01-20 12:26:27 +05:30
|
|
|
</p>
|
2026-02-08 22:44:06 +05:30
|
|
|
{connectedAgents
|
|
|
|
|
.filter((agent) =>
|
|
|
|
|
agent.name.toLowerCase().includes(searchQuery.toLowerCase())
|
|
|
|
|
)
|
|
|
|
|
.map((agent) => (
|
|
|
|
|
<button
|
|
|
|
|
key={agent.agentProfileId}
|
2026-03-15 00:29:25 +05:30
|
|
|
onClick={() => handleStartConversation(userRole === 'AGENT' ? agent.userId! : agent.agentProfileId)}
|
2026-02-08 22:44:06 +05:30
|
|
|
disabled={isStartingConversation === agent.agentProfileId}
|
|
|
|
|
className="w-full flex items-center gap-3 p-4 rounded-[15px] cursor-pointer transition-colors hover:bg-[#00293d]/5 disabled:opacity-50"
|
|
|
|
|
style={{ border: '1px solid rgba(0, 41, 61, 0.1)' }}
|
|
|
|
|
>
|
|
|
|
|
{/* Avatar */}
|
|
|
|
|
<div className="relative flex-shrink-0">
|
2026-03-13 22:45:11 +05:30
|
|
|
<div className="w-[50px] h-[50px] rounded-full overflow-hidden border border-[#00293D]/20 relative">
|
|
|
|
|
<AvatarWithPlaceholder
|
2026-02-08 22:44:06 +05:30
|
|
|
src={getValidAvatarUrl(agent.avatarUrl)}
|
|
|
|
|
alt={agent.name}
|
2026-03-13 22:45:11 +05:30
|
|
|
size={50}
|
2026-03-27 15:47:29 +05:30
|
|
|
name={agent.name}
|
2026-02-08 22:44:06 +05:30
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Agent Info */}
|
|
|
|
|
<div className="flex-1 min-w-0 text-left">
|
|
|
|
|
<p className="font-fractul font-medium text-[14px] leading-[17px] text-[#00293D]">
|
|
|
|
|
{agent.name}
|
|
|
|
|
</p>
|
|
|
|
|
{agent.headline && (
|
|
|
|
|
<p className="font-serif font-normal text-[12px] leading-[16px] text-[#00293D]/70 truncate">
|
|
|
|
|
{agent.headline}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
{(agent.city || agent.state) && (
|
|
|
|
|
<p className="font-serif font-normal text-[12px] leading-[16px] text-[#00293D]/50">
|
|
|
|
|
{[agent.city, agent.state].filter(Boolean).join(', ')}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Start conversation indicator */}
|
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
|
{isStartingConversation === agent.agentProfileId ? (
|
|
|
|
|
<div className="animate-spin rounded-full h-5 w-5 border-b-2 border-[#e58625]" />
|
|
|
|
|
) : (
|
|
|
|
|
<div className="w-8 h-8 rounded-full bg-[#e58625]/10 flex items-center justify-center">
|
|
|
|
|
<Image
|
|
|
|
|
src="/assets/icons/message-icon.svg"
|
|
|
|
|
alt="Start chat"
|
|
|
|
|
width={16}
|
|
|
|
|
height={16}
|
|
|
|
|
className="opacity-70"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</button>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="flex-1 flex items-center justify-center text-[#00293D]/50 font-serif p-4 text-center">
|
2026-03-15 00:29:25 +05:30
|
|
|
<p>{userRole === 'AGENT' ? 'No connected users yet.' : 'No connected agents yet. Connect with agents to start messaging!'}</p>
|
2026-01-20 12:26:27 +05:30
|
|
|
</div>
|
2026-02-08 22:44:06 +05:30
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="flex-1 overflow-y-auto p-2 space-y-2 custom-scrollbar">
|
|
|
|
|
{filteredConversations.map((conversation) => (
|
|
|
|
|
<div
|
|
|
|
|
key={conversation.id}
|
2026-03-20 02:41:07 +05:30
|
|
|
onClick={() => { selectConversation(conversation.id); setShowMobileChat(true); }}
|
2026-02-08 22:44:06 +05:30
|
|
|
className="relative flex items-start gap-3 p-4 rounded-[15px] cursor-pointer transition-colors hover:bg-[#00293d]/5"
|
|
|
|
|
style={{ border: '1px solid rgba(0, 41, 61, 0.1)' }}
|
|
|
|
|
>
|
|
|
|
|
{/* Active indicator line */}
|
|
|
|
|
{currentConversation?.id === conversation.id && (
|
|
|
|
|
<div className="absolute left-0 top-1/2 -translate-y-1/2 w-[4px] h-[70px] bg-[#E58625] rounded-r-[4px]" />
|
|
|
|
|
)}
|
2026-01-20 12:26:27 +05:30
|
|
|
|
2026-02-08 22:44:06 +05:30
|
|
|
{/* Avatar with online indicator */}
|
|
|
|
|
<div className="relative flex-shrink-0">
|
2026-03-13 22:45:11 +05:30
|
|
|
<div className="w-[70px] h-[70px] rounded-full overflow-hidden border border-[#00293D]/20 relative">
|
|
|
|
|
<AvatarWithPlaceholder
|
2026-02-08 22:44:06 +05:30
|
|
|
src={getValidAvatarUrl(conversationAvatarUrls[conversation.id])}
|
|
|
|
|
alt={conversation.otherParty?.name || 'User'}
|
2026-03-13 22:45:11 +05:30
|
|
|
size={70}
|
2026-03-27 15:47:29 +05:30
|
|
|
name={conversation.otherParty?.name}
|
2026-02-08 22:44:06 +05:30
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
{conversation.otherParty?.isOnline && (
|
|
|
|
|
<div className="absolute bottom-1 right-1 w-3 h-3 bg-green-500 rounded-full border-2 border-white" />
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Content */}
|
|
|
|
|
<div className="flex-1 min-w-0">
|
|
|
|
|
<div className="flex items-center gap-2 mb-1">
|
2026-03-19 17:03:25 +05:30
|
|
|
{(userRole === 'AGENT' ? conversation.agentFavorited : conversation.userFavorited) && (
|
|
|
|
|
<Image src="/assets/icons/star-filled-icon.svg" alt="Starred" width={14} height={14} className="flex-shrink-0" />
|
|
|
|
|
)}
|
2026-02-08 22:44:06 +05:30
|
|
|
<p className="font-fractul font-medium text-[14px] leading-[17px] text-[#00293D]">
|
|
|
|
|
{conversation.otherParty?.name || 'Unknown'}
|
|
|
|
|
</p>
|
2026-03-19 17:03:25 +05:30
|
|
|
{(userRole === 'AGENT' ? conversation.agentMuted : conversation.userMuted) && (
|
|
|
|
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" className="flex-shrink-0 text-[#00293D]/40">
|
|
|
|
|
<path d="M11 5L6 9H2v6h4l5 4V5zM23 9l-6 6M17 9l6 6" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
|
|
|
|
|
</svg>
|
|
|
|
|
)}
|
2026-02-08 22:44:06 +05:30
|
|
|
{conversation.unreadCount > 0 && (
|
|
|
|
|
<span className="bg-[#e58625] text-white text-xs font-bold px-2 py-0.5 rounded-full min-w-[20px] text-center">
|
|
|
|
|
{conversation.unreadCount}
|
|
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
<p className="font-serif font-normal text-[14px] leading-[19px] text-[#00293D] line-clamp-2">
|
2026-03-27 06:23:28 +05:30
|
|
|
{conversation.lastMessageText
|
|
|
|
|
? /giphy\.com|\.gif(\?|$)/i.test(conversation.lastMessageText)
|
|
|
|
|
? 'GIF 🎬'
|
|
|
|
|
: /\.(jpg|jpeg|png|webp|svg)(\?|$)/i.test(conversation.lastMessageText) && conversation.lastMessageText.startsWith('http')
|
|
|
|
|
? 'Image 📷'
|
|
|
|
|
: conversation.lastMessageText
|
|
|
|
|
: 'No messages yet'}
|
2026-02-08 22:44:06 +05:30
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Date */}
|
|
|
|
|
<p className="font-serif font-normal text-[14px] leading-[19px] text-[#00293D] flex-shrink-0">
|
|
|
|
|
{formatConversationDate(conversation.lastMessageAt)}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2026-01-20 12:26:27 +05:30
|
|
|
</div>
|
|
|
|
|
|
2026-03-20 02:41:07 +05:30
|
|
|
{/* Right panel - Chat area (full width on mobile, hidden when no conversation on mobile) */}
|
|
|
|
|
<div className={`flex-1 flex flex-col p-4 ${showMobileChat ? 'flex' : 'hidden md:flex'}`}>
|
2026-02-08 22:44:06 +05:30
|
|
|
{currentConversation && selectedUserInfo ? (
|
2026-01-20 12:26:27 +05:30
|
|
|
<>
|
2026-03-20 02:41:07 +05:30
|
|
|
{/* Mobile back button */}
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => setShowMobileChat(false)}
|
|
|
|
|
className="md:hidden flex items-center gap-2 mb-3 hover:opacity-70 transition-opacity cursor-pointer"
|
|
|
|
|
>
|
|
|
|
|
<svg width="20" height="20" viewBox="0 0 24 24" fill="none">
|
|
|
|
|
<path d="M19 12H5M5 12L12 19M5 12L12 5" stroke="#00293D" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
|
|
|
|
|
</svg>
|
|
|
|
|
<span className="font-fractul font-medium text-[14px] text-[#00293D]">Back</span>
|
|
|
|
|
</button>
|
2026-01-20 12:26:27 +05:30
|
|
|
{/* Chat header */}
|
2026-02-08 22:44:06 +05:30
|
|
|
<ChatHeader
|
|
|
|
|
{...selectedUserInfo}
|
|
|
|
|
isTyping={typingUserNames.length > 0}
|
|
|
|
|
typingText={typingUserNames.length > 0 ? `${typingUserNames.join(', ')} is typing...` : undefined}
|
2026-03-19 17:03:25 +05:30
|
|
|
isMuted={userRole === 'AGENT' ? (currentConversation?.agentMuted || false) : (currentConversation?.userMuted || false)}
|
|
|
|
|
isStarred={userRole === 'AGENT' ? (currentConversation?.agentFavorited || false) : (currentConversation?.userFavorited || false)}
|
2026-03-09 23:47:28 +05:30
|
|
|
onClearChat={clearMessages}
|
|
|
|
|
onDeleteConversation={handleDeleteConversation}
|
2026-03-19 05:24:41 +05:30
|
|
|
onReportUser={() => setIsReportModalOpen(true)}
|
2026-03-19 17:03:25 +05:30
|
|
|
onToggleMute={(muted) => currentConversation && toggleMute(currentConversation.id, muted)}
|
|
|
|
|
onToggleStar={(starred) => currentConversation && toggleFavorite(currentConversation.id, starred)}
|
2026-02-08 22:44:06 +05:30
|
|
|
/>
|
2026-01-20 12:26:27 +05:30
|
|
|
|
|
|
|
|
{/* Messages area wrapper */}
|
|
|
|
|
<div className="flex-1 relative">
|
2026-02-08 22:44:06 +05:30
|
|
|
{isLoadingMessages && messages.length === 0 ? (
|
|
|
|
|
<div className="absolute inset-0 flex items-center justify-center">
|
|
|
|
|
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-[#00293d]" />
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div
|
|
|
|
|
ref={messagesContainerRef}
|
|
|
|
|
onScroll={handleScroll}
|
|
|
|
|
className="absolute inset-0 overflow-y-auto py-4 custom-scrollbar"
|
|
|
|
|
>
|
|
|
|
|
{/* Load more indicator */}
|
|
|
|
|
{isLoadingMessages && messages.length > 0 && (
|
|
|
|
|
<div className="flex justify-center py-2">
|
|
|
|
|
<div className="animate-spin rounded-full h-5 w-5 border-b-2 border-[#00293d]" />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<div className="space-y-4 px-2">
|
|
|
|
|
{messages.map((message) => {
|
|
|
|
|
const isOwn = message.senderId === currentUserId;
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
key={message.id}
|
|
|
|
|
className={`flex ${isOwn ? 'justify-end' : 'justify-start'}`}
|
2026-01-20 12:26:27 +05:30
|
|
|
>
|
2026-02-08 22:44:06 +05:30
|
|
|
<div
|
2026-02-21 22:11:42 +05:30
|
|
|
className={`max-w-[70%] rounded-[15px] ${
|
|
|
|
|
message.messageType === 'IMAGE' ? 'p-1' : 'px-4 py-3'
|
|
|
|
|
} ${
|
2026-02-08 22:44:06 +05:30
|
|
|
isOwn
|
|
|
|
|
? 'bg-[#e58625] text-[#00293D]'
|
|
|
|
|
: 'bg-[#00293d]/10 text-[#00293D]'
|
|
|
|
|
}`}
|
|
|
|
|
>
|
2026-02-21 22:11:42 +05:30
|
|
|
{message.messageType === 'IMAGE' ? (
|
|
|
|
|
messageFileUrls[message.id] ? (
|
2026-03-20 01:49:46 +05:30
|
|
|
<>
|
|
|
|
|
<img
|
|
|
|
|
src={messageFileUrls[message.id]}
|
|
|
|
|
alt={message.fileName || 'Image'}
|
|
|
|
|
className="rounded-[12px] max-w-full cursor-pointer"
|
|
|
|
|
style={{ maxHeight: '250px' }}
|
|
|
|
|
onClick={() => window.open(messageFileUrls[message.id], '_blank')}
|
|
|
|
|
onError={(e) => {
|
|
|
|
|
const target = e.target as HTMLImageElement;
|
|
|
|
|
target.style.display = 'none';
|
|
|
|
|
const fallback = target.nextElementSibling;
|
|
|
|
|
if (fallback) (fallback as HTMLElement).style.display = 'flex';
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<div className="hidden items-center gap-2 px-3 py-2">
|
|
|
|
|
<Image src="/assets/icons/attachment-icon.svg" alt="Image" width={16} height={9} />
|
|
|
|
|
<span className="font-serif text-[13px]">{message.fileName || 'Image'}</span>
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => window.open(messageFileUrls[message.id], '_blank')}
|
|
|
|
|
className="font-serif text-[12px] text-[#e58625] underline ml-1"
|
|
|
|
|
>
|
|
|
|
|
Download
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
2026-02-21 22:11:42 +05:30
|
|
|
) : (
|
2026-03-14 14:37:19 +05:30
|
|
|
<div className="w-[200px] h-[150px] rounded-[12px] overflow-hidden">
|
|
|
|
|
<div className="w-full h-full shimmer-loading rounded-[12px]" />
|
2026-02-21 22:11:42 +05:30
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
) : message.messageType === 'FILE' ? (
|
|
|
|
|
<a
|
|
|
|
|
href={messageFileUrls[message.id] || '#'}
|
|
|
|
|
target="_blank"
|
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
|
className="flex items-center gap-3 no-underline"
|
|
|
|
|
>
|
|
|
|
|
<div className={`w-[40px] h-[40px] rounded-[8px] flex items-center justify-center flex-shrink-0 ${isOwn ? 'bg-[#00293D]/10' : 'bg-[#e58625]/10'}`}>
|
|
|
|
|
<Image
|
|
|
|
|
src="/assets/icons/attachment-icon.svg"
|
|
|
|
|
alt="File"
|
|
|
|
|
width={20}
|
|
|
|
|
height={11}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="min-w-0">
|
|
|
|
|
<p className="font-serif font-medium text-[14px] leading-[19px] truncate">
|
|
|
|
|
{message.fileName || message.content}
|
|
|
|
|
</p>
|
|
|
|
|
{message.fileSize && (
|
|
|
|
|
<p className={`font-serif font-normal text-[12px] leading-[16px] ${isOwn ? 'text-[#00293D]/70' : 'text-[#00293D]/50'}`}>
|
|
|
|
|
{message.fileSize < 1024
|
|
|
|
|
? `${message.fileSize} B`
|
|
|
|
|
: message.fileSize < 1024 * 1024
|
|
|
|
|
? `${(message.fileSize / 1024).toFixed(1)} KB`
|
|
|
|
|
: `${(message.fileSize / (1024 * 1024)).toFixed(1)} MB`}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</a>
|
|
|
|
|
) : (
|
|
|
|
|
<p className="font-serif font-normal text-[14px] leading-[19px]">
|
|
|
|
|
{message.content}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
<div className={`flex items-center gap-2 mt-1 ${message.messageType === 'IMAGE' ? 'px-2 pb-1' : ''} ${isOwn ? 'justify-end' : 'justify-start'}`}>
|
2026-02-08 22:44:06 +05:30
|
|
|
<p
|
|
|
|
|
className={`font-serif font-normal text-[12px] leading-[16px] ${
|
|
|
|
|
isOwn ? 'text-[#00293D]/70' : 'text-[#00293D]/50'
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
{formatMessageTime(message.createdAt)}
|
|
|
|
|
</p>
|
|
|
|
|
{isOwn && (
|
2026-02-21 22:11:42 +05:30
|
|
|
<span className="inline-flex items-center">
|
|
|
|
|
{message.status === 'READ' ? (
|
|
|
|
|
<Image src="/assets/icons/tick-double-blue.svg" alt="Read" width={16} height={11} />
|
|
|
|
|
) : message.status === 'DELIVERED' ? (
|
|
|
|
|
<Image src="/assets/icons/tick-double-gray.svg" alt="Delivered" width={16} height={11} />
|
|
|
|
|
) : (
|
|
|
|
|
<Image src="/assets/icons/tick-single-gray.svg" alt="Sent" width={11} height={11} />
|
|
|
|
|
)}
|
2026-02-08 22:44:06 +05:30
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
|
|
|
|
|
{/* Typing indicator in messages */}
|
|
|
|
|
{typingUserNames.length > 0 && (
|
|
|
|
|
<div className="flex justify-start">
|
|
|
|
|
<div className="bg-[#00293d]/10 text-[#00293D] rounded-[15px] px-4 py-3">
|
2026-02-11 07:36:40 +05:30
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<div className="flex items-center gap-1">
|
|
|
|
|
<span className="w-2 h-2 bg-[#00293D]/50 rounded-full animate-bounce" style={{ animationDelay: '0ms' }} />
|
|
|
|
|
<span className="w-2 h-2 bg-[#00293D]/50 rounded-full animate-bounce" style={{ animationDelay: '150ms' }} />
|
|
|
|
|
<span className="w-2 h-2 bg-[#00293D]/50 rounded-full animate-bounce" style={{ animationDelay: '300ms' }} />
|
|
|
|
|
</div>
|
|
|
|
|
<span className="font-serif font-normal text-[12px] leading-[16px] text-[#00293D]/50">
|
|
|
|
|
{formatMessageTime(new Date().toISOString())}
|
|
|
|
|
</span>
|
2026-02-08 22:44:06 +05:30
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-01-20 12:26:27 +05:30
|
|
|
</div>
|
2026-02-08 22:44:06 +05:30
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<div ref={messagesEndRef} />
|
|
|
|
|
</div>
|
2026-01-20 12:26:27 +05:30
|
|
|
</div>
|
2026-02-08 22:44:06 +05:30
|
|
|
)}
|
2026-01-20 12:26:27 +05:30
|
|
|
|
2026-02-08 22:44:06 +05:30
|
|
|
{/* Scroll to bottom button */}
|
2026-01-20 12:26:27 +05:30
|
|
|
{showScrollButton && (
|
|
|
|
|
<button
|
|
|
|
|
onClick={scrollToBottom}
|
|
|
|
|
className="absolute bottom-4 right-4 w-[50px] h-[50px] bg-[#00293d]/10 rounded-full flex items-center justify-center hover:bg-[#00293d]/20 transition-all cursor-pointer shadow-lg z-10"
|
|
|
|
|
>
|
|
|
|
|
<Image
|
|
|
|
|
src="/assets/icons/arrow-down-icon.svg"
|
|
|
|
|
alt="Scroll to bottom"
|
|
|
|
|
width={20}
|
|
|
|
|
height={20}
|
|
|
|
|
/>
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Message input */}
|
2026-02-08 22:44:06 +05:30
|
|
|
<MessageInput
|
|
|
|
|
onSend={handleSendMessage}
|
2026-02-21 22:11:42 +05:30
|
|
|
onSendGif={handleSendGif}
|
|
|
|
|
onSendFile={handleSendFile}
|
2026-02-08 22:44:06 +05:30
|
|
|
onTypingStart={handleTypingStart}
|
|
|
|
|
onTypingStop={handleTypingStop}
|
2026-02-21 22:11:42 +05:30
|
|
|
isUploading={isUploading}
|
2026-02-08 22:44:06 +05:30
|
|
|
/>
|
2026-01-20 12:26:27 +05:30
|
|
|
</>
|
|
|
|
|
) : (
|
2026-02-08 22:44:06 +05:30
|
|
|
<div className="flex items-center justify-center h-full text-[#00293D]/50 font-serif flex-col gap-4">
|
|
|
|
|
<Image
|
|
|
|
|
src="/assets/icons/message-icon.svg"
|
|
|
|
|
alt="Messages"
|
|
|
|
|
width={64}
|
|
|
|
|
height={64}
|
|
|
|
|
className="opacity-30"
|
|
|
|
|
/>
|
|
|
|
|
<p>Select a conversation to start messaging</p>
|
2026-01-20 12:26:27 +05:30
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|