11"use client" ;
2- import { Fragment , useState , memo , useEffect , useMemo , useRef } from "react" ;
2+ import { Fragment , useState , memo , useEffect , useMemo } from "react" ;
33import type { ReactNode } from "react" ;
44import type { User } from "@supabase/supabase-js" ;
55
@@ -102,8 +102,6 @@ const ListingRead = memo(function Listing({
102102 : null ,
103103 [ rawRealListing , user ]
104104 ) ;
105- const realListingRef = useRef ( realListing ) ;
106- realListingRef . current = realListing ;
107105 const listingForDisplay = demoListing ?? realListing ;
108106
109107 // Load existing thread if any (only if not in demo mode). Depend on the
@@ -113,7 +111,19 @@ const ListingRead = memo(function Listing({
113111 const listingOwnerId = realListing ?. owner_id ;
114112 const userId = user ?. id ;
115113 useEffect ( ( ) => {
116- if ( isDemo || ! supabase || ! userId || ! listingId || ! listingOwnerId ) return ;
114+ const listingSnapshot = realListing ;
115+ if (
116+ isDemo ||
117+ ! supabase ||
118+ ! userId ||
119+ ! listingId ||
120+ ! listingOwnerId ||
121+ ! listingSnapshot
122+ ) {
123+ return ;
124+ }
125+
126+ let isCurrentRequest = true ;
117127
118128 // TODO: Should this only be called when the actual ListingChatDrawer is loaded?
119129 async function loadExistingThread ( ) {
@@ -134,6 +144,7 @@ const ListingRead = memo(function Listing({
134144 }
135145
136146 if ( ! thread ) {
147+ if ( ! isCurrentRequest ) return ;
137148 setExistingThread ( null ) ;
138149 return ;
139150 }
@@ -149,14 +160,18 @@ const ListingRead = memo(function Listing({
149160 return ;
150161 }
151162
163+ if ( ! isCurrentRequest ) return ;
152164 setExistingThread ( {
153165 ...thread ,
154- listing : realListingRef . current ,
166+ listing : listingSnapshot ,
155167 messages : messages ?? [ ] ,
156168 } ) ;
157169 }
158170
159171 loadExistingThread ( ) ;
172+ return ( ) => {
173+ isCurrentRequest = false ;
174+ } ;
160175 } , [ listingId , listingOwnerId , userId , isDemo , supabase ] ) ;
161176
162177 const initialZoomLevel = 14 ;
0 commit comments