@@ -10,6 +10,10 @@ import {
1010
1111import type { Channel } from "@/shared/api/types" ;
1212import { scoreChannelMatch } from "@/features/channels/lib/channelSearchScore" ;
13+ import {
14+ type ChannelSortMode ,
15+ sortChannelsForSidebar ,
16+ } from "@/features/sidebar/lib/channelSortPreference" ;
1317import { ListSortDescending } from "@/shared/ui/icons" ;
1418import {
1519 Dialog ,
@@ -45,10 +49,11 @@ import {
4549} from "@/features/sidebar/ui/CreateChannelFormFields" ;
4650
4751type BrowserTab = "all" | "joined" | "archived" ;
48- type ChannelSort = "alphabetical" | "members" ;
52+ type ChannelSort = ChannelSortMode | "members" ;
4953
5054const 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"
0 commit comments