Skip to content

Commit 2cb7753

Browse files
authored
fix(desktop): restore channel browsing and add recent sorting (#2065)
Signed-off-by: Pavlenex <[email protected]>
1 parent 66a0f7b commit 2cb7753

6 files changed

Lines changed: 92 additions & 29 deletions

File tree

‎desktop/src/features/channels/ui/ChannelBrowserDialog.tsx‎

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import {
1010

1111
import type { Channel } from "@/shared/api/types";
1212
import { scoreChannelMatch } from "@/features/channels/lib/channelSearchScore";
13+
import {
14+
type ChannelSortMode,
15+
sortChannelsForSidebar,
16+
} from "@/features/sidebar/lib/channelSortPreference";
1317
import { ListSortDescending } from "@/shared/ui/icons";
1418
import {
1519
Dialog,
@@ -45,10 +49,11 @@ import {
4549
} from "@/features/sidebar/ui/CreateChannelFormFields";
4650

4751
type BrowserTab = "all" | "joined" | "archived";
48-
type ChannelSort = "alphabetical" | "members";
52+
type ChannelSort = ChannelSortMode | "members";
4953

5054
const CHANNEL_SORT_OPTIONS: { label: string; value: ChannelSort }[] = [
51-
{ label: "Alphabetical", value: "alphabetical" },
55+
{ label: "Alphabetical", value: "alpha" },
56+
{ label: "Recent", value: "recent" },
5257
{ label: "Most members", value: "members" },
5358
];
5459

@@ -102,7 +107,7 @@ export function ChannelBrowserDialog({
102107
}: ChannelBrowserDialogProps) {
103108
const [query, setQuery] = React.useState("");
104109
const [activeTab, setActiveTab] = React.useState<BrowserTab>("all");
105-
const [sort, setSort] = React.useState<ChannelSort>("alphabetical");
110+
const [sort, setSort] = React.useState<ChannelSort>("alpha");
106111
const [selectedIndex, setSelectedIndex] = React.useState<number | null>(null);
107112
const [joiningChannelId, setJoiningChannelId] = React.useState<string | null>(
108113
null,
@@ -132,7 +137,7 @@ export function ChannelBrowserDialog({
132137
const isForumMode = channelTypeFilter === "forum";
133138
const canCreate = Boolean(onCreateChannel);
134139
const createKind = isForumMode ? "forum" : "stream";
135-
const browseTitle = isForumMode ? "Add a forum" : "Add a channel";
140+
const browseTitle = isForumMode ? "Add a forum" : "Browse channels";
136141
const searchPlaceholder = canCreate
137142
? isForumMode
138143
? "Search or create a forum"
@@ -206,23 +211,30 @@ export function ChannelBrowserDialog({
206211
const isSearching = deferredQuery.length > 0;
207212

208213
const orderedVisibleChannels = React.useMemo(() => {
209-
return [...visibleChannels].sort((a, b) => {
210-
// While searching, best match wins so the channel you meant floats to
211-
// the top; ties fall back to the user's chosen sort below.
212-
if (isSearching) {
213-
const scoreA = matchScoreById.get(a.id) ?? Number.POSITIVE_INFINITY;
214-
const scoreB = matchScoreById.get(b.id) ?? Number.POSITIVE_INFINITY;
215-
if (scoreA !== scoreB) return scoreA - scoreB;
216-
}
217-
218-
if (sort === "members" && b.memberCount !== a.memberCount) {
219-
return b.memberCount - a.memberCount;
220-
}
221-
222-
return a.name.localeCompare(b.name, undefined, { sensitivity: "base" });
223-
});
214+
const sorted =
215+
sort === "members"
216+
? [...visibleChannels].sort(
217+
(a, b) =>
218+
b.memberCount - a.memberCount ||
219+
a.name.localeCompare(b.name, undefined, {
220+
sensitivity: "base",
221+
}),
222+
)
223+
: sortChannelsForSidebar(visibleChannels, sort);
224+
225+
if (!isSearching) return sorted;
226+
227+
return sorted.sort(
228+
(a, b) =>
229+
(matchScoreById.get(a.id) ?? Number.POSITIVE_INFINITY) -
230+
(matchScoreById.get(b.id) ?? Number.POSITIVE_INFINITY),
231+
);
224232
}, [isSearching, matchScoreById, sort, visibleChannels]);
225233

234+
const selectedSortLabel =
235+
CHANNEL_SORT_OPTIONS.find((option) => option.value === sort)?.label ??
236+
"Alphabetical";
237+
226238
const allTabLabel = isForumMode ? "All forums" : "All channels";
227239

228240
// Whether an exact name match already exists — if so we don't offer to
@@ -320,7 +332,7 @@ export function ChannelBrowserDialog({
320332
if (!open) {
321333
setQuery("");
322334
setActiveTab("all");
323-
setSort("alphabetical");
335+
setSort("alpha");
324336
setSelectedIndex(null);
325337
setJoiningChannelId(null);
326338
setMode("browse");
@@ -500,11 +512,7 @@ export function ChannelBrowserDialog({
500512
<DropdownMenu>
501513
<DropdownMenuTrigger asChild>
502514
<Button
503-
aria-label={`Sort ${entityLabel}s: ${
504-
sort === "alphabetical"
505-
? "Alphabetical"
506-
: "Most members"
507-
}`}
515+
aria-label={`Sort ${entityLabel}s: ${selectedSortLabel}`}
508516
data-testid="channel-browser-sort"
509517
size="icon-xs"
510518
type="button"

‎desktop/src/features/search/ui/SearchResultItem.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
type LucideIcon,
1111
} from "lucide-react";
1212

13+
import { HashSearch } from "@/shared/ui/icons";
1314
import {
1415
resolveUserLabel,
1516
resolveUserSecondaryLabel,
@@ -24,7 +25,7 @@ export type SearchResult =
2425
kind: "action";
2526
action: {
2627
description?: string;
27-
id: "create-agent" | "create-channel";
28+
id: "browse-channels" | "create-agent" | "create-channel";
2829
title: string;
2930
};
3031
}
@@ -69,6 +70,9 @@ export function resultIcon(
6970
channelLookup: ReadonlyMap<string, Channel>,
7071
) {
7172
if (result.kind === "action") {
73+
if (result.action.id === "browse-channels") {
74+
return HashSearch;
75+
}
7276
return result.action.id === "create-agent" ? Bot : Plus;
7377
}
7478

‎desktop/src/features/search/ui/TopbarSearch.tsx‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type TopbarSearchProps = {
3434
onOpenChannel: (channelId: string) => void;
3535
onOpenResult: (hit: SearchHit) => void;
3636
onOpenUser?: (user: UserSearchResult) => void | Promise<void>;
37+
onBrowseChannels?: () => void | Promise<void>;
3738
onCreateAgent?: () => void | Promise<void>;
3839
onCreateChannel?: () => void | Promise<void>;
3940
suggestionChannels?: Channel[];
@@ -393,6 +394,7 @@ export function TopbarSearch({
393394
onOpenChannel,
394395
onOpenResult,
395396
onOpenUser,
397+
onBrowseChannels,
396398
onCreateAgent,
397399
onCreateChannel,
398400
suggestionChannels,
@@ -426,6 +428,16 @@ export function TopbarSearch({
426428
const suggestionActionResults = React.useMemo(() => {
427429
const actions: SearchResult[] = [];
428430

431+
if (onBrowseChannels) {
432+
actions.push({
433+
kind: "action",
434+
action: {
435+
id: "browse-channels",
436+
title: "Browse channels",
437+
},
438+
});
439+
}
440+
429441
if (onCreateChannel) {
430442
actions.push({
431443
kind: "action",
@@ -447,7 +459,7 @@ export function TopbarSearch({
447459
}
448460

449461
return actions;
450-
}, [onCreateAgent, onCreateChannel]);
462+
}, [onBrowseChannels, onCreateAgent, onCreateChannel]);
451463
const suggestionResults = React.useMemo(
452464
() => [...suggestedResults, ...suggestionActionResults],
453465
[suggestedResults, suggestionActionResults],
@@ -513,7 +525,11 @@ export function TopbarSearch({
513525

514526
if (result.kind === "action") {
515527
setSelectedMenuIndex(0);
516-
if (result.action.id === "create-channel") {
528+
if (result.action.id === "browse-channels") {
529+
openAfterExit(() => {
530+
void onBrowseChannels?.();
531+
});
532+
} else if (result.action.id === "create-channel") {
517533
openAfterExit(() => {
518534
void onCreateChannel?.();
519535
});
@@ -528,6 +544,7 @@ export function TopbarSearch({
528544
onOpenResult(result.hit);
529545
},
530546
[
547+
onBrowseChannels,
531548
onCreateAgent,
532549
onCreateChannel,
533550
onOpenChannel,

‎desktop/src/features/sidebar/ui/AppSidebar.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ export function AppSidebar({
554554
<AppSidebarPinnedHeader
555555
channelLabels={dmChannelLabels}
556556
currentPubkey={currentPubkey}
557+
onBrowseChannels={onBrowseChannels}
557558
onCreateAgent={onCreateAgent}
558559
onCreateChannel={handleOpenCreateChannel}
559560
onOpenDm={onOpenDm}
@@ -720,7 +721,7 @@ export function AppSidebar({
720721
}
721722
actionsTestId="section-actions-channels"
722723
listTestId="stream-list"
723-
quickCreateLabel="Add channel"
724+
quickCreateLabel="Browse channels"
724725
onQuickCreateClick={onBrowseChannels}
725726
showQuickCreate
726727
onMarkAllRead={onMarkAllChannelsRead}

‎desktop/src/features/sidebar/ui/AppSidebarPinnedHeader.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type SidebarSelectedView =
2424
type AppSidebarPinnedHeaderProps = {
2525
channelLabels: Record<string, string>;
2626
currentPubkey?: string;
27+
onBrowseChannels?: () => void;
2728
onCreateAgent: () => void;
2829
onCreateChannel: () => void;
2930
onOpenDm: (input: { pubkeys: string[] }) => Promise<void>;
@@ -47,6 +48,7 @@ type AppSidebarPrimaryMenuProps = {
4748
export function AppSidebarPinnedHeader({
4849
channelLabels,
4950
currentPubkey,
51+
onBrowseChannels,
5052
onCreateAgent,
5153
onCreateChannel,
5254
onOpenDm,
@@ -69,6 +71,7 @@ export function AppSidebarPinnedHeader({
6971
onOpenChannel={onSelectChannel}
7072
onOpenResult={onOpenSearchResult}
7173
onOpenUser={(user) => onOpenDm({ pubkeys: [user.pubkey] })}
74+
onBrowseChannels={onBrowseChannels}
7275
onCreateAgent={onCreateAgent}
7376
onCreateChannel={onCreateChannel}
7477
suggestionChannels={suggestionChannels}

‎desktop/tests/e2e/channel-browser.spec.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ test("channel browser shows channels not yet joined", async ({ page }) => {
5757

5858
await openChannelBrowser(page);
5959
await expect(page.getByTestId("channel-browser-dialog")).toBeVisible();
60+
await expect(
61+
page.getByRole("heading", { name: "Browse channels" }),
62+
).toBeVisible();
6063

6164
// "design" and "sales" are open channels the mock user is NOT a member of
6265
await expect(page.getByTestId("browse-channel-design")).toBeVisible();
@@ -108,6 +111,33 @@ test("channel browser sorts alphabetically or by member count", async ({
108111
);
109112
});
110113

114+
test("channel browser sorts by recent activity", async ({ page }) => {
115+
await page.goto("/");
116+
117+
await openChannelBrowser(page);
118+
const rows = page.locator('[data-testid^="browse-channel-"]');
119+
120+
await page.getByTestId("channel-browser-sort").click();
121+
await page.getByTestId("channel-browser-sort-recent").click();
122+
123+
await expect(rows).toHaveText([
124+
/#all-replies/,
125+
/#deep-history/,
126+
/#general/,
127+
/#agents/,
128+
/#sales/,
129+
/#engineering/,
130+
/#design/,
131+
/#random/,
132+
/#secret-projects/,
133+
/#welcome-everyone/,
134+
]);
135+
await expect(page.getByTestId("channel-browser-sort")).toHaveAttribute(
136+
"aria-label",
137+
"Sort channels: Recent",
138+
);
139+
});
140+
111141
test("channel browser search filters by name", async ({ page }) => {
112142
await page.goto("/");
113143

0 commit comments

Comments
 (0)