diff --git a/src/app/(user)/user/message/page.tsx b/src/app/(user)/user/message/page.tsx index ff2aad8..07ea0e3 100644 --- a/src/app/(user)/user/message/page.tsx +++ b/src/app/(user)/user/message/page.tsx @@ -6,10 +6,11 @@ import { MessagingPage } from '@/components/message'; export default function UserMessagePage() { const searchParams = useSearchParams(); const conversationId = searchParams.get('conversationId'); + const agentProfileId = searchParams.get('agentProfileId'); return (
- +
); } diff --git a/src/app/(user)/user/profile/[id]/page.tsx b/src/app/(user)/user/profile/[id]/page.tsx index 8aa80ef..d4f0b25 100644 --- a/src/app/(user)/user/profile/[id]/page.tsx +++ b/src/app/(user)/user/profile/[id]/page.tsx @@ -342,7 +342,7 @@ export default function AgentProfileView() { bio={profileCardData.bio || agentProfile.bio || ''} expertise={profileCardData.expertise.length > 0 ? profileCardData.expertise : (agentProfile.specializations || [])} showEditButton={false} - messageHref="/user/message" + messageHref={`/user/message?agentProfileId=${agentProfile.id}`} connectionStatus={connectionStatus} onConnectClick={() => setShowConnectModal(true)} onUnlinkClick={handleUnlink} diff --git a/src/components/message/MessagingPage.tsx b/src/components/message/MessagingPage.tsx index 53d7f8e..0dcfc5d 100644 --- a/src/components/message/MessagingPage.tsx +++ b/src/components/message/MessagingPage.tsx @@ -127,7 +127,7 @@ interface ConnectedAgent { state: string | null; } -export function MessagingPage({ initialConversationId }: { initialConversationId?: string | null }) { +export function MessagingPage({ initialConversationId, initialAgentProfileId }: { initialConversationId?: string | null; initialAgentProfileId?: string | null }) { const { data: session } = useSession(); const { conversations, @@ -318,6 +318,31 @@ export function MessagingPage({ initialConversationId }: { initialConversationId } }, [initialConversationId, conversations, isLoading, selectConversation]); + // Auto-select or create conversation when navigated with initialAgentProfileId + const initialAgentHandledRef = useRef(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]); + // Convert current conversation avatar when selected useEffect(() => { const convertCurrentAvatar = async () => {