Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit e654098

Browse files
Don't show the same user twice in Spotlight (#8978)
Co-authored-by: Janne Mareike Koschinski <[email protected]>
1 parent 7afc8c5 commit e654098

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

cypress/integration/12-spotlight/spotlight.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ declare global {
5555
roomHeaderName(
5656
options?: Partial<Loggable & Timeoutable & Withinable & Shadow>
5757
): Chainable<JQuery<HTMLElement>>;
58+
startDM(name: string): Chainable<void>;
5859
}
5960
}
6061
}
@@ -109,6 +110,20 @@ Cypress.Commands.add("roomHeaderName", (
109110
return cy.get(".mx_RoomHeader_nametext", options);
110111
});
111112

113+
Cypress.Commands.add("startDM", (name: string) => {
114+
cy.openSpotlightDialog().within(() => {
115+
cy.spotlightFilter(Filter.People);
116+
cy.spotlightSearch().clear().type(name);
117+
cy.get(".mx_Spinner").should("not.exist");
118+
cy.spotlightResults().should("have.length", 1);
119+
cy.spotlightResults().eq(0).should("contain", name);
120+
cy.spotlightResults().eq(0).click();
121+
}).then(() => {
122+
cy.roomHeaderName().should("contain", name);
123+
cy.get(".mx_RoomSublist[aria-label=People]").should("contain", name);
124+
});
125+
});
126+
112127
describe("Spotlight", () => {
113128
let synapse: SynapseInstance;
114129

@@ -319,6 +334,23 @@ describe("Spotlight", () => {
319334
});
320335
});
321336

337+
it("should close spotlight after starting a DM", () => {
338+
cy.startDM(bot1Name);
339+
cy.get(".mx_SpotlightDialog").should("have.length", 0);
340+
});
341+
342+
it("should show the same user only once", () => {
343+
cy.startDM(bot1Name);
344+
cy.visit("/#/home");
345+
346+
cy.openSpotlightDialog().within(() => {
347+
cy.spotlightFilter(Filter.People);
348+
cy.spotlightSearch().clear().type(bot1Name);
349+
cy.get(".mx_Spinner").should("not.exist");
350+
cy.spotlightResults().should("have.length", 1);
351+
});
352+
});
353+
322354
it("should be able to navigate results via keyboard", () => {
323355
cy.openSpotlightDialog().within(() => {
324356
cy.spotlightFilter(Filter.People);

src/components/views/dialogs/spotlight/SpotlightDialog.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,25 @@ const SpotlightDialog: React.FC<IProps> = ({ initialText = "", initialFilter = n
320320
);
321321
const possibleResults = useMemo<Result[]>(
322322
() => {
323-
const roomMembers = findVisibleRoomMembers(cli);
324-
const roomMemberIds = new Set(roomMembers.map(item => item.userId));
323+
const roomResults = findVisibleRooms(cli).map(toRoomResult);
324+
// If we already have a DM with the user we're looking for, we will
325+
// show that DM instead of the user themselves
326+
const alreadyAddedUserIds = roomResults.reduce((userIds, result) => {
327+
const userId = DMRoomMap.shared().getUserIdForRoomId(result.room.roomId);
328+
if (!userId) return userIds;
329+
if (result.room.getJoinedMemberCount() > 2) return userIds;
330+
userIds.add(userId);
331+
return userIds;
332+
}, new Set<string>());
333+
const userResults = [];
334+
for (const user of [...findVisibleRoomMembers(cli), ...users]) {
335+
// Make sure we don't have any user more than once
336+
if (alreadyAddedUserIds.has(user.userId)) continue;
337+
alreadyAddedUserIds.add(user.userId);
338+
339+
userResults.push(toMemberResult(user));
340+
}
341+
325342
return [
326343
...SpaceStore.instance.enabledMetaSpaces.map(spaceKey => ({
327344
section: Section.Spaces,
@@ -335,9 +352,8 @@ const SpotlightDialog: React.FC<IProps> = ({ initialText = "", initialFilter = n
335352
SpaceStore.instance.setActiveSpace(spaceKey);
336353
},
337354
})),
338-
...findVisibleRooms(cli).map(toRoomResult),
339-
...roomMembers.map(toMemberResult),
340-
...users.filter(item => !roomMemberIds.has(item.userId)).map(toMemberResult),
355+
...roomResults,
356+
...userResults,
341357
...(profile ? [new DirectoryMember(profile)] : []).map(toMemberResult),
342358
...publicRooms.map(toPublicRoomResult),
343359
].filter(result => filter === null || result.filter.includes(filter));

0 commit comments

Comments
 (0)