diff --git a/app/soapbox/queries/chats.ts b/app/soapbox/queries/chats.ts index 96ad97ce8..924563192 100644 --- a/app/soapbox/queries/chats.ts +++ b/app/soapbox/queries/chats.ts @@ -136,9 +136,9 @@ const useChats = (search?: string) => { const nextPageLink = pageParam?.link; const uri = nextPageLink || endpoint; const response = await api.get(uri, { - params: { + params: search ? { search, - }, + } : undefined, }); const { data } = response; diff --git a/app/soapbox/utils/queries.ts b/app/soapbox/utils/queries.ts index 61df573f4..3016b0546 100644 --- a/app/soapbox/utils/queries.ts +++ b/app/soapbox/utils/queries.ts @@ -8,12 +8,27 @@ export interface PaginatedResult { link?: string, } +/** Deduplicate an array of entities by their ID. */ +const deduplicate = (entities: T[]): T[] => { + const map = entities.reduce>((result, entity) => { + // @ts-expect-error Entity might not have an ID... but it probably does. + return result.set(entity.id, entity); + }, new Map()); + + return Array.from(map.values()); +}; + /** Flatten paginated results into a single array. */ const flattenPages = (queryData: InfiniteData> | undefined) => { - return queryData?.pages.reduce( + const data = queryData?.pages.reduce( + // FIXME: Pleroma wants these to be reversed for Chats. (prev: T[], curr) => [...curr.result, ...prev], [], ); + + if (data) { + return deduplicate(data); + } }; /** Traverse pages and update the item inside if found. */