Skip to content

Commit 797bcb2

Browse files
committed
ignore stale listing thread loads
1 parent 6f64d50 commit 797bcb2

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

src/components/ListingRead/ListingRead.tsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use client";
2-
import { Fragment, useState, memo, useEffect, useMemo, useRef } from "react";
2+
import { Fragment, useState, memo, useEffect, useMemo } from "react";
33
import type { ReactNode } from "react";
44
import 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;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- The composite (initiator_id, created_at desc) index covers initiator_id-only
2+
-- lookups via its leftmost prefix, so the older single-column index is redundant.
3+
4+
drop index if exists public.chat_threads_initiator_id_idx;

0 commit comments

Comments
 (0)