Skip to content

Commit 3495c4a

Browse files
authoredFeb 28, 2025··
fix(comments): set up listener with filter-only query (#8793)
1 parent c44a858 commit 3495c4a

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed
 

‎packages/sanity/src/core/comments/store/useCommentsStore.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,8 @@ const QUERY_SORT_ORDER = `order(${SORT_FIELD} ${SORT_ORDER})`
6363
export function useCommentsStore(opts: CommentsStoreOptions): CommentsStoreReturnType {
6464
const {client, documentId, onLatestTransactionIdReceived, transactionsIdMap, releaseId} = opts
6565

66-
const query = useMemo(() => {
67-
const filters = [...QUERY_FILTERS, releaseId ? VERSION_FILTER : NO_VERSION_FILTER]
68-
return `*[${filters.join(' && ')}] ${QUERY_PROJECTION} | ${QUERY_SORT_ORDER}`
69-
}, [releaseId])
66+
const filters = [...QUERY_FILTERS, releaseId ? VERSION_FILTER : NO_VERSION_FILTER].join(' && ')
67+
const query = useMemo(() => `*[${filters}] ${QUERY_PROJECTION} | ${QUERY_SORT_ORDER}`, [filters])
7068

7169
const [state, dispatch] = useReducer(commentsReducer, INITIAL_STATE)
7270
const [loading, setLoading] = useState<boolean>(client !== null)
@@ -170,15 +168,15 @@ export function useCommentsStore(opts: CommentsStoreOptions): CommentsStoreRetur
170168
const listener$ = useMemo(() => {
171169
if (!client) return of()
172170

173-
const events$ = client.observable.listen(query, params, LISTEN_OPTIONS).pipe(
171+
const events$ = client.observable.listen(`*[${filters}]`, params, LISTEN_OPTIONS).pipe(
174172
catchError((err) => {
175173
setError(err)
176174
return of(err)
177175
}),
178176
)
179177

180178
return events$
181-
}, [client, params, query])
179+
}, [client, filters, params])
182180

183181
useEffect(() => {
184182
const sub = listener$.subscribe(handleListenerEvent)

0 commit comments

Comments
 (0)
Please sign in to comment.