-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathChannelList.tsx
143 lines (132 loc) · 4.59 KB
/
ChannelList.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import cn from 'classnames';
import React from 'react';
import * as DropdownMenu from '@radix-ui/react-dropdown-menu';
import useAllBriefs from '@/logic/useAllBriefs';
import {
channelHref,
nestToFlag,
isChannelJoined,
canReadChannel,
} from '@/logic/utils';
import { useIsMobile } from '@/logic/useMedia';
import { useGroup, useVessel } from '@/state/groups';
import CaretDown16Icon from '@/components/icons/CaretDownIcon';
import SortIcon from '@/components/icons/SortIcon';
import SidebarItem from '@/components/Sidebar/SidebarItem';
import useChannelSort from '@/logic/useChannelSort';
import { DEFAULT } from '@/logic/useSidebarSort';
import useChannelSections from '@/logic/useChannelSections';
import { GroupChannel } from '@/types/groups';
import Divider from '@/components/Divider';
import ChannelIcon from '@/channels/ChannelIcon';
import useIsChannelUnread from '@/logic/useIsChannelUnread';
import UnreadIndicator from '@/components/Sidebar/UnreadIndicator';
import usePrefetchChannels from '@/logic/usePrefetchChannels';
import ChannelSortOptions from './ChannelSortOptions';
const UNZONED = 'default';
type ChannelSorterProps = {
isMobile?: boolean;
};
interface ChannelListProps {
flag: string;
className?: string;
}
export function ChannelSorter({ isMobile }: ChannelSorterProps) {
const { sortFn, sortOptions, setSortFn } = useChannelSort();
return (
<DropdownMenu.Root>
{isMobile ? (
<DropdownMenu.Trigger
className="default-focus flex items-center rounded-lg p-0 text-base font-semibold"
aria-label="Groups Sort Options"
>
<SortIcon className="h-6 w-6 text-gray-400" />
</DropdownMenu.Trigger>
) : (
<div className="p-2">
<DropdownMenu.Trigger
className="default-focus flex w-full items-center justify-between rounded-lg bg-gray-50 py-1 px-2 text-sm font-semibold"
aria-label="Channels Sort Options"
>
<span className="flex items-center">
<SortIcon className="h-4 w-4 text-gray-400" />
<span className="mr-2 pl-1">{`Sort: ${sortFn}`}</span>
</span>
<CaretDown16Icon className="h-4 w-4 text-gray-400" />
</DropdownMenu.Trigger>
</div>
)}
<ChannelSortOptions sortOptions={sortOptions} setSortFn={setSortFn} />
</DropdownMenu.Root>
);
}
export default function ChannelList({ flag, className }: ChannelListProps) {
const group = useGroup(flag);
const briefs = useAllBriefs();
const { sortFn, sortChannels } = useChannelSort();
const isDefaultSort = sortFn === DEFAULT;
const { sectionedChannels, sections } = useChannelSections(flag);
const isMobile = useIsMobile();
const { isChannelUnread } = useIsChannelUnread();
const vessel = useVessel(flag, window.our);
if (!group) {
return null;
}
const renderChannels = (channels: [string, GroupChannel][]) =>
channels
.filter(
([nest, chan]) =>
isChannelJoined(nest, briefs) && canReadChannel(chan, vessel)
)
.map(([nest, channel]) => {
const icon = (active: boolean) =>
isMobile ? (
<span
className={cn(
'flex h-12 w-12 items-center justify-center rounded-md',
!active && 'bg-gray-50',
active && 'bg-white'
)}
>
<ChannelIcon nest={nest} className="h-6 w-6" />
</span>
) : (
<ChannelIcon nest={nest} className="h-6 w-6" />
);
return (
<SidebarItem
inexact
key={nest}
icon={icon}
to={channelHref(flag, nest)}
actions={isChannelUnread(nest) ? <UnreadIndicator /> : null}
>
{channel.meta.title || nest}
</SidebarItem>
);
});
const unsectionedChannels = sortChannels(group.channels).filter(
([n]) => n in briefs
);
return (
<div className={className}>
{!isMobile && <ChannelSorter isMobile={false} />}
<ul className={cn('space-y-1', isMobile && 'flex-none space-y-3')}>
{isDefaultSort
? sections.map((s) => (
<div className="space-y-1" key={s}>
{s !== UNZONED ? (
<Divider>
{s in group.zones ? group.zones[s].meta.title : ''}
</Divider>
) : null}
{s in sectionedChannels
? renderChannels(sectionedChannels[s])
: null}
</div>
))
: renderChannels(unsectionedChannels)}
</ul>
</div>
);
}