@@ -113,13 +113,8 @@ const ListingRead = memo(function Listing({
113113 async function loadExistingThread ( ) {
114114 if ( ! supabase ) return ;
115115 const { data : thread , error } = await supabase
116- . from ( "chat_threads_with_participants" )
117- . select (
118- `
119- *,
120- chat_messages_with_senders (*)
121- `
122- )
116+ . from ( "chat_threads" )
117+ . select ( "id, created_at, listing_id, initiator_id, owner_id" )
123118 . match ( {
124119 listing_id : listingId ,
125120 initiator_id : userId ,
@@ -132,11 +127,31 @@ const ListingRead = memo(function Listing({
132127 return ;
133128 }
134129
135- setExistingThread ( thread ) ;
130+ if ( ! thread ) {
131+ setExistingThread ( null ) ;
132+ return ;
133+ }
134+
135+ const { data : messages , error : messagesError } = await supabase
136+ . from ( "chat_messages" )
137+ . select ( "id, content, created_at, read_at, sender_id, thread_id" )
138+ . eq ( "thread_id" , thread . id )
139+ . order ( "created_at" , { ascending : true } ) ;
140+
141+ if ( messagesError ) {
142+ console . error ( "Error loading thread messages:" , messagesError ) ;
143+ return ;
144+ }
145+
146+ setExistingThread ( {
147+ ...thread ,
148+ listing : realListing ,
149+ messages : messages ?? [ ] ,
150+ } ) ;
136151 }
137152
138153 loadExistingThread ( ) ;
139- } , [ listingId , listingOwnerId , userId , isDemo , supabase ] ) ;
154+ } , [ listingId , listingOwnerId , userId , isDemo , supabase , realListing ] ) ;
140155
141156 const initialZoomLevel = 14 ;
142157 const listingDisplayNameCopy = useMemo (
0 commit comments