/
RoomList.tsx
132 lines (102 loc) · 3.45 KB
/
RoomList.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
import type { IRoom } from '@rocket.chat/core-typings';
import { css } from '@rocket.chat/css-in-js';
import { Box } from '@rocket.chat/fuselage';
import { useResizeObserver } from '@rocket.chat/fuselage-hooks';
import { useSession, useUserPreference, useUserId, useTranslation } from '@rocket.chat/ui-contexts';
import type { ReactElement } from 'react';
import React, { useMemo } from 'react';
import { Virtuoso } from 'react-virtuoso';
import { useAvatarTemplate } from '../hooks/useAvatarTemplate';
import { usePreventDefault } from '../hooks/usePreventDefault';
import { useRoomList } from '../hooks/useRoomList';
import { useShortcutOpenMenu } from '../hooks/useShortcutOpenMenu';
import { useTemplateByViewMode } from '../hooks/useTemplateByViewMode';
import RoomListRow from './RoomListRow';
import ScrollerWithCustomProps from './ScrollerWithCustomProps';
const computeItemKey = (index: number, room: IRoom): IRoom['_id'] | number => room._id || index;
const RoomList = (): ReactElement => {
const t = useTranslation();
const isAnonymous = !useUserId();
const roomsList = useRoomList();
const avatarTemplate = useAvatarTemplate();
const sideBarItemTemplate = useTemplateByViewMode();
const { ref } = useResizeObserver({ debounceDelay: 100 });
const openedRoom = (useSession('openedRoom') as string) || '';
const sidebarViewMode = useUserPreference<'extended' | 'medium' | 'condensed'>('sidebarViewMode') || 'extended';
const extended = sidebarViewMode === 'extended';
const itemData = useMemo(
() => ({
extended,
t,
SideBarItemTemplate: sideBarItemTemplate,
AvatarTemplate: avatarTemplate,
openedRoom,
sidebarViewMode,
isAnonymous,
}),
[avatarTemplate, extended, isAnonymous, openedRoom, sideBarItemTemplate, sidebarViewMode, t],
);
usePreventDefault(ref);
useShortcutOpenMenu(ref);
const roomsListStyle = css`
position: relative;
display: flex;
overflow-x: hidden;
overflow-y: hidden;
flex: 1 1 auto;
height: 100%;
&--embedded {
margin-top: 2rem;
}
&__list:not(:last-child) {
margin-bottom: 22px;
}
&__type {
display: flex;
flex-direction: row;
padding: 0 var(--sidebar-default-padding) 1rem var(--sidebar-default-padding);
color: var(--rooms-list-title-color);
font-size: var(--rooms-list-title-text-size);
align-items: center;
justify-content: space-between;
&-text--livechat {
flex: 1;
}
}
&__empty-room {
padding: 0 var(--sidebar-default-padding);
color: var(--rooms-list-empty-text-color);
font-size: var(--rooms-list-empty-text-size);
}
&__toolbar-search {
position: absolute;
z-index: 10;
left: 0;
overflow-y: scroll;
height: 100%;
background-color: var(--sidebar-background);
padding-block-start: 12px;
}
@media (max-width: 400px) {
padding: 0 calc(var(--sidebar-small-default-padding) - 4px);
&__type,
&__empty-room {
padding: 0 calc(var(--sidebar-small-default-padding) - 4px) 0.5rem calc(var(--sidebar-small-default-padding) - 4px);
}
}
`;
return (
<Box className={[roomsListStyle, 'sidebar--custom-colors'].filter(Boolean)} aria-label={t('Channels')} role='region'>
<Box h='full' w='full' ref={ref}>
<Virtuoso
totalCount={roomsList.length}
data={roomsList}
components={{ Scroller: ScrollerWithCustomProps }}
computeItemKey={computeItemKey}
itemContent={(_, data): ReactElement => <RoomListRow data={itemData} item={data} />}
/>
</Box>
</Box>
);
};
export default RoomList;