From e88cea1ef51bf6e212b42776120b5c9a5a1bf8c1 Mon Sep 17 00:00:00 2001 From: Matt Grogan Date: Thu, 28 Sep 2023 10:58:49 -0500 Subject: [PATCH] Order chats in select chat modal by last updated (#4461) --- .../app/src/components/SelectChatModal.svelte | 44 ++++++++++++------- frontend/app/src/utils/bigints.ts | 3 ++ 2 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 frontend/app/src/utils/bigints.ts diff --git a/frontend/app/src/components/SelectChatModal.svelte b/frontend/app/src/components/SelectChatModal.svelte index 4b8f10932b..39e6a16cc7 100644 --- a/frontend/app/src/components/SelectChatModal.svelte +++ b/frontend/app/src/components/SelectChatModal.svelte @@ -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("client"); const dispatch = createEventDispatcher(); @@ -42,6 +43,7 @@ avatarUrl: string; description: string; username: string | undefined; + lastUpdated: bigint; }; type ShareCommunity = { kind: "community"; @@ -49,6 +51,7 @@ name: string; avatarUrl: string; description: string; + lastUpdated: bigint; channels: ShareChat[]; }; @@ -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( @@ -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 { const normalisedChannels = await Promise.all( filterChatSelection(channels, selectedChatId).map((c) => normaliseChatSummary(now, c)) @@ -159,6 +166,7 @@ name, avatarUrl: client.communityAvatarUrl(id.communityId, avatar), description, + lastUpdated, channels: normalisedChannels, }; } @@ -176,6 +184,7 @@ avatarUrl: client.userAvatarUrl(them), description, username: "@" + them.username, + lastUpdated: chatSummary.lastUpdated, }; default: @@ -187,6 +196,7 @@ avatarUrl: client.groupAvatarUrl(chatSummary), description: buildGroupChatDescription(chatSummary), username: undefined, + lastUpdated: chatSummary.lastUpdated, }; } } diff --git a/frontend/app/src/utils/bigints.ts b/frontend/app/src/utils/bigints.ts new file mode 100644 index 0000000000..d13ee15f59 --- /dev/null +++ b/frontend/app/src/utils/bigints.ts @@ -0,0 +1,3 @@ +export function compareBigints(a: bigint, b: bigint): number { + return a < b ? -1 : a > b ? 1 : 0; +} \ No newline at end of file