Skip to content

Commit

Permalink
Order chats in select chat modal by last updated (#4461)
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan authored Sep 28, 2023
1 parent c654dfd commit e88cea1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
44 changes: 27 additions & 17 deletions frontend/app/src/components/SelectChatModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import MessageOutline from "svelte-material-icons/MessageOutline.svelte";
import ForumOutline from "svelte-material-icons/ForumOutline.svelte";
import Search from "./Search.svelte";
import { compareBigints } from "../utils/bigints";
const client = getContext<OpenChat>("client");
const dispatch = createEventDispatcher();
Expand All @@ -42,13 +43,15 @@
avatarUrl: string;
description: string;
username: string | undefined;
lastUpdated: bigint;
};
type ShareCommunity = {
kind: "community";
id: CommunityIdentifier;
name: string;
avatarUrl: string;
description: string;
lastUpdated: bigint;
channels: ShareChat[];
};
Expand Down Expand Up @@ -90,25 +93,29 @@
}
function chatMatchesSearch(chats: ShareChat[], searchTerm: string): ShareChat[] {
return chats.filter(
(c) =>
searchTerm === "" ||
c.name.toLowerCase().includes(searchTerm) ||
c.username?.toLowerCase()?.includes(searchTerm)
);
return chats
.filter(
(c) =>
searchTerm === "" ||
c.name.toLowerCase().includes(searchTerm) ||
c.username?.toLowerCase()?.includes(searchTerm)
)
.sort((a, b) => compareBigints(b.lastUpdated, a.lastUpdated));
}
function communityMatchesSearch(communities: ShareCommunity[], searchTerm: string) {
return communities.reduce((agg, c) => {
const filtered = chatMatchesSearch(c.channels, searchTerm);
if (filtered.length > 0) {
agg.push({
...c,
channels: filtered,
});
}
return agg;
}, [] as ShareCommunity[]);
return communities
.reduce((agg, c) => {
const filtered = chatMatchesSearch(c.channels, searchTerm);
if (filtered.length > 0) {
agg.push({
...c,
channels: filtered,
});
}
return agg;
}, [] as ShareCommunity[])
.sort((a, b) => compareBigints(b.lastUpdated, a.lastUpdated));
}
async function buildListOfTargets(
Expand Down Expand Up @@ -148,7 +155,7 @@
async function normaliseCommunity(
now: number,
selectedChatId: ChatIdentifier | undefined,
{ id, name, avatar, description, channels }: CommunitySummary
{ id, name, avatar, description, channels, lastUpdated }: CommunitySummary
): Promise<ShareCommunity> {
const normalisedChannels = await Promise.all(
filterChatSelection(channels, selectedChatId).map((c) => normaliseChatSummary(now, c))
Expand All @@ -159,6 +166,7 @@
name,
avatarUrl: client.communityAvatarUrl(id.communityId, avatar),
description,
lastUpdated,
channels: normalisedChannels,
};
}
Expand All @@ -176,6 +184,7 @@
avatarUrl: client.userAvatarUrl(them),
description,
username: "@" + them.username,
lastUpdated: chatSummary.lastUpdated,
};
default:
Expand All @@ -187,6 +196,7 @@
avatarUrl: client.groupAvatarUrl(chatSummary),
description: buildGroupChatDescription(chatSummary),
username: undefined,
lastUpdated: chatSummary.lastUpdated,
};
}
}
Expand Down
3 changes: 3 additions & 0 deletions frontend/app/src/utils/bigints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function compareBigints(a: bigint, b: bigint): number {
return a < b ? -1 : a > b ? 1 : 0;
}

0 comments on commit e88cea1

Please sign in to comment.