2026-03-18 11:54:40 +05:30
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { createContext, useContext, useState, useEffect, useRef, useCallback } from 'react';
|
|
|
|
|
import { useSession } from 'next-auth/react';
|
|
|
|
|
import { agentsService } from '@/services/agents.service';
|
|
|
|
|
import { usersService } from '@/services/users.service';
|
|
|
|
|
import { uploadService } from '@/services/upload.service';
|
|
|
|
|
import { notificationsApiService } from '@/services/notifications-api.service';
|
2026-04-10 17:44:36 +05:30
|
|
|
import { messagesService } from '@/services/messages.service';
|
2026-03-28 15:34:09 +05:30
|
|
|
import { socketService } from '@/services';
|
2026-03-18 11:54:40 +05:30
|
|
|
|
|
|
|
|
interface HeaderData {
|
|
|
|
|
profileImage: string | null;
|
|
|
|
|
profileName: string | null;
|
2026-03-19 13:56:09 +05:30
|
|
|
profileTitle: string | null;
|
2026-03-18 11:54:40 +05:30
|
|
|
avatarLoaded: boolean;
|
|
|
|
|
setAvatarLoaded: (loaded: boolean) => void;
|
|
|
|
|
notificationCount: number;
|
2026-04-10 17:44:36 +05:30
|
|
|
messageCount: number;
|
2026-03-18 11:54:40 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const HeaderContext = createContext<HeaderData>({
|
|
|
|
|
profileImage: null,
|
|
|
|
|
profileName: null,
|
2026-03-19 13:56:09 +05:30
|
|
|
profileTitle: null,
|
2026-03-18 11:54:40 +05:30
|
|
|
avatarLoaded: false,
|
|
|
|
|
setAvatarLoaded: () => {},
|
|
|
|
|
notificationCount: 0,
|
2026-04-10 17:44:36 +05:30
|
|
|
messageCount: 0,
|
2026-03-18 11:54:40 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export function useHeaderData() {
|
|
|
|
|
return useContext(HeaderContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function HeaderProvider({ children }: { children: React.ReactNode }) {
|
|
|
|
|
const { data: session, status } = useSession();
|
|
|
|
|
const [profileImage, setProfileImage] = useState<string | null>(null);
|
|
|
|
|
const [profileName, setProfileName] = useState<string | null>(null);
|
2026-03-19 13:56:09 +05:30
|
|
|
const [profileTitle, setProfileTitle] = useState<string | null>(null);
|
2026-03-18 11:54:40 +05:30
|
|
|
const [avatarLoaded, setAvatarLoaded] = useState(false);
|
|
|
|
|
const [notificationCount, setNotificationCount] = useState(0);
|
2026-04-10 17:44:36 +05:30
|
|
|
const [messageCount, setMessageCount] = useState(0);
|
2026-03-18 11:54:40 +05:30
|
|
|
const lastAvatarKeyRef = useRef<string | null>(null);
|
|
|
|
|
const hasFetchedRef = useRef(false);
|
|
|
|
|
|
|
|
|
|
const fetchProfileData = useCallback(async (forceRefresh = false) => {
|
|
|
|
|
try {
|
|
|
|
|
const role = (session?.user as any)?.role;
|
|
|
|
|
let avatar: string | null = null;
|
|
|
|
|
let name: string | null = null;
|
2026-03-19 13:56:09 +05:30
|
|
|
let title: string | null = null;
|
2026-03-18 11:54:40 +05:30
|
|
|
|
|
|
|
|
if (role === 'AGENT') {
|
|
|
|
|
const profile = await agentsService.getMyProfile();
|
|
|
|
|
avatar = profile.avatar;
|
|
|
|
|
name = profile.firstName ? `${profile.firstName} ${profile.lastName || ''}`.trim() : null;
|
2026-03-19 13:56:09 +05:30
|
|
|
title = profile.agentType?.name || 'Real Estate Agent';
|
2026-03-18 11:54:40 +05:30
|
|
|
} else if (role === 'USER') {
|
|
|
|
|
const profile = await usersService.getMyProfile();
|
|
|
|
|
avatar = profile.avatar;
|
|
|
|
|
name = profile.firstName ? `${profile.firstName} ${profile.lastName || ''}`.trim() : null;
|
2026-03-19 13:56:09 +05:30
|
|
|
title = 'Member';
|
2026-03-18 11:54:40 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (name) setProfileName(name);
|
2026-03-19 13:56:09 +05:30
|
|
|
if (title) setProfileTitle(title);
|
2026-03-18 11:54:40 +05:30
|
|
|
|
|
|
|
|
if (avatar && (avatar !== lastAvatarKeyRef.current || forceRefresh)) {
|
|
|
|
|
lastAvatarKeyRef.current = avatar;
|
|
|
|
|
setAvatarLoaded(false);
|
|
|
|
|
try {
|
|
|
|
|
const avatarUrl = await uploadService.getPresignedDownloadUrl(avatar);
|
|
|
|
|
setProfileImage(avatarUrl);
|
|
|
|
|
} catch {
|
|
|
|
|
setProfileImage(avatar);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Failed to fetch profile data:', err);
|
|
|
|
|
}
|
|
|
|
|
}, [session]);
|
|
|
|
|
|
|
|
|
|
// Fetch once on auth
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (status === 'authenticated' && session?.user && !hasFetchedRef.current) {
|
|
|
|
|
hasFetchedRef.current = true;
|
|
|
|
|
fetchProfileData();
|
|
|
|
|
}
|
|
|
|
|
if (status === 'unauthenticated') {
|
|
|
|
|
hasFetchedRef.current = false;
|
|
|
|
|
setProfileImage(null);
|
|
|
|
|
setProfileName(null);
|
2026-03-19 13:56:09 +05:30
|
|
|
setProfileTitle(null);
|
2026-03-18 11:54:40 +05:30
|
|
|
setAvatarLoaded(false);
|
|
|
|
|
lastAvatarKeyRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
}, [status, session, fetchProfileData]);
|
|
|
|
|
|
|
|
|
|
// Listen for profile-updated events
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const handleProfileUpdate = () => {
|
|
|
|
|
lastAvatarKeyRef.current = null;
|
|
|
|
|
fetchProfileData(true);
|
|
|
|
|
};
|
|
|
|
|
window.addEventListener('profile-updated', handleProfileUpdate);
|
|
|
|
|
return () => window.removeEventListener('profile-updated', handleProfileUpdate);
|
|
|
|
|
}, [fetchProfileData]);
|
|
|
|
|
|
2026-03-28 15:34:09 +05:30
|
|
|
// Notification count polling + real-time socket updates
|
2026-03-18 11:54:40 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
if (status !== 'authenticated') return;
|
|
|
|
|
|
|
|
|
|
const fetchCount = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const count = await notificationsApiService.getUnreadCount();
|
|
|
|
|
setNotificationCount(count);
|
|
|
|
|
} catch {
|
|
|
|
|
// Silently fail
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-10 17:44:36 +05:30
|
|
|
const fetchMessageCount = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const count = await messagesService.getUnreadCount();
|
|
|
|
|
setMessageCount(count);
|
|
|
|
|
} catch {
|
|
|
|
|
// Silently fail
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-18 11:54:40 +05:30
|
|
|
fetchCount();
|
2026-04-10 17:44:36 +05:30
|
|
|
fetchMessageCount();
|
2026-03-18 11:54:40 +05:30
|
|
|
const interval = setInterval(fetchCount, 30000);
|
2026-04-10 17:44:36 +05:30
|
|
|
const messageInterval = setInterval(fetchMessageCount, 30000);
|
2026-03-18 11:54:40 +05:30
|
|
|
const handleNotification = () => fetchCount();
|
|
|
|
|
window.addEventListener('notification-received', handleNotification);
|
|
|
|
|
|
2026-03-28 15:34:09 +05:30
|
|
|
// Listen to socket events for immediate notification count refresh
|
|
|
|
|
const unsubConnectionReq = socketService.onConnectionRequest(() => fetchCount());
|
|
|
|
|
const unsubConnectionRes = socketService.onConnectionResponse(() => fetchCount());
|
2026-04-10 17:44:36 +05:30
|
|
|
const unsubNewMessage = socketService.onNewMessage(() => { fetchCount(); fetchMessageCount(); });
|
2026-03-28 15:34:09 +05:30
|
|
|
|
2026-03-18 11:54:40 +05:30
|
|
|
return () => {
|
|
|
|
|
clearInterval(interval);
|
2026-04-10 17:44:36 +05:30
|
|
|
clearInterval(messageInterval);
|
2026-03-18 11:54:40 +05:30
|
|
|
window.removeEventListener('notification-received', handleNotification);
|
2026-03-28 15:34:09 +05:30
|
|
|
unsubConnectionReq();
|
|
|
|
|
unsubConnectionRes();
|
|
|
|
|
unsubNewMessage();
|
2026-03-18 11:54:40 +05:30
|
|
|
};
|
|
|
|
|
}, [status]);
|
|
|
|
|
|
|
|
|
|
return (
|
2026-04-10 17:44:36 +05:30
|
|
|
<HeaderContext.Provider value={{ profileImage, profileName, profileTitle, avatarLoaded, setAvatarLoaded, notificationCount, messageCount }}>
|
2026-03-18 11:54:40 +05:30
|
|
|
{children}
|
|
|
|
|
</HeaderContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|