-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathGroupListItem.tsx
192 lines (173 loc) · 5.19 KB
/
GroupListItem.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import {
confirm,
CopyIcon,
ExternalIcon,
GlobeAltIcon,
GlobeAltLockIcon,
LeaveIcon,
LockIcon,
} from '@hypothesis/frontend-shared';
import classnames from 'classnames';
import type { Group, GroupType } from '../../../types/api';
import { orgName } from '../../helpers/group-list-item-common';
import { withServices } from '../../service-context';
import type { GroupsService } from '../../services/groups';
import type { ToastMessengerService } from '../../services/toast-messenger';
import { useSidebarStore } from '../../store';
import { copyPlainText } from '../../util/copy-to-clipboard';
import MenuItem from '../MenuItem';
function GroupIcon({ type }: { type: GroupType }) {
const title =
type === 'open'
? 'Public group'
: type === 'restricted'
? 'Restricted group'
: 'Private group';
return (
<div
className="text-color-text-light"
title={title}
data-testid="group-icon"
>
{type === 'open' && <GlobeAltIcon />}
{type === 'restricted' && <GlobeAltLockIcon />}
{type === 'private' && <LockIcon />}
</div>
);
}
export type GroupListItemProps = {
group: Group;
/** Whether the submenu for this group is expanded. */
isExpanded?: boolean;
/** Callback invoked to expand or collapse the current group. */
onExpand: (expand: boolean) => void;
groups: GroupsService;
toastMessenger: ToastMessengerService;
};
/**
* An item in the groups selection menu.
*
* The item has a primary action which selects the group, along with a set of
* secondary actions accessible via a toggle menu.
*/
function GroupListItem({
isExpanded,
group,
groups: groupsService,
onExpand,
toastMessenger,
}: GroupListItemProps) {
const activityUrl = group.links.html;
const hasActionMenu = activityUrl || group.canLeave;
const isSelectable =
(group.scopes && !group.scopes.enforced) || group.isScopedToUri;
const store = useSidebarStore();
const focusedGroupId = store.focusedGroupId();
const isSelected = group.id === focusedGroupId;
const focusGroup = () => {
store.clearDirectLinkedGroupFetchFailed();
store.clearDirectLinkedIds();
groupsService.focus(group.id);
};
const leaveGroup = async () => {
const message = `Are you sure you want to leave the group "${group.name}"?`;
if (
await confirm({
title: 'Leave group?',
message,
confirmAction: 'Leave',
})
) {
groupsService.leave(group.id);
}
};
const toggleSubmenu = (event: Event) => {
event.stopPropagation();
// Prevents group items opening a new window when clicked.
// TODO - Fix this more cleanly in `MenuItem`.
event.preventDefault();
onExpand(!isExpanded);
};
const copyLink = async (url: string) => {
try {
await copyPlainText(url);
toastMessenger.success(`Copied link for "${group.name}"`);
} catch {
toastMessenger.error('Unable to copy link');
}
};
const copyLinkLabel =
group.type === 'private' ? 'Copy invite link' : 'Copy activity link';
const leftChannelContent = group.logo ? (
<img className="w-4 h-4" alt={orgName(group)} src={group.logo} />
) : (
<span className="sr-only">{orgName(group)}</span>
);
return (
<MenuItem
isDisabled={!isSelectable}
isExpanded={hasActionMenu ? isExpanded : false}
isSelected={isSelected}
isSubmenuVisible={hasActionMenu ? isExpanded : undefined}
label={group.name}
richLabel={
<div className="grow flex items-center justify-between gap-x-2">
{group.name}
<GroupIcon type={group.type} />
</div>
}
leftChannelContent={leftChannelContent}
onClick={isSelectable ? focusGroup : toggleSubmenu}
onToggleSubmenu={toggleSubmenu}
submenu={
<>
<ul>
{activityUrl && (
<li>
<MenuItem
href={activityUrl}
icon={ExternalIcon}
isSubmenuItem={true}
label="View group activity"
/>
</li>
)}
{activityUrl && (
<li>
<MenuItem
onClick={() => copyLink(activityUrl)}
icon={CopyIcon}
isSubmenuItem={true}
label={copyLinkLabel}
/>
</li>
)}
{group.canLeave && (
<li>
<MenuItem
icon={LeaveIcon}
isSubmenuItem={true}
label="Leave group"
onClick={leaveGroup}
/>
</li>
)}
</ul>
{!isSelectable && (
<p
className={classnames(
// Left padding to match submenu items above. Turn off hyphenation
// as it causes this content to hyphenate awkwardly.
'p-2 pl-9 bg-grey-1 hyphens-none',
)}
data-testid="unselectable-group-note"
>
This group is restricted to specific URLs.
</p>
)}
</>
}
/>
);
}
export default withServices(GroupListItem, ['groups', 'toastMessenger']);