UNPKG

2.2 kBJavaScriptView Raw
1import GroupIcon from '@jetbrains/icons/group.svg';
2
3import List from '../list/list';
4import HubSourceUsersGroups from '../hub-source/hub-source__users-groups';
5
6const defaultOptions = {
7 GroupsTitle: 'Groups',
8 NoGroupsTitle: 'No groups',
9
10 UsersTitle: 'Users',
11 NoUsersTitle: 'No users',
12
13 getPluralForUserCount: count => {
14 // eslint-disable-next-line no-magic-numbers
15 const plural = count % 10 !== 1 || count % 100 === 11;
16 return `${count} member${plural ? 's' : ''}`;
17 }
18};
19
20const Filter = {
21 ALL: 0,
22 USERS: 1,
23 GROUPS: 2
24};
25
26export default class ListUsersGroupsSource extends HubSourceUsersGroups {
27 static Filter = Filter
28
29 constructor(auth, options) {
30 super(auth, options);
31
32 this.listSourceOptions = Object.assign({}, defaultOptions, options);
33 }
34
35 getGroupsSectionTitle(groups) {
36 return groups.length
37 ? this.listSourceOptions.GroupsTitle
38 : this.listSourceOptions.NoGroupsTitle;
39 }
40
41 getUsersSectionTitle(users) {
42 return users.length ? this.listSourceOptions.UsersTitle : this.listSourceOptions.NoUsersTitle;
43 }
44
45 async getForList(query, filter = Filter.ALL) {
46 const [users, groups] = await this.getUserAndGroups(query);
47 const items = [];
48
49 if (filter === Filter.ALL) {
50 items.push({
51 rgItemType: List.ListProps.Type.SEPARATOR,
52 key: 2,
53 description: this.getUsersSectionTitle(users)
54 });
55 }
56
57 if (filter !== Filter.GROUPS) {
58 users.forEach(user => items.push({
59 ...user,
60 key: user.id,
61 label: user.name,
62 avatar: user.profile ? user.profile.avatar.url : null,
63 description: user.login
64 }));
65 }
66
67 if (filter === Filter.ALL) {
68 items.push({
69 rgItemType: List.ListProps.Type.SEPARATOR,
70 key: 1,
71 description: this.getGroupsSectionTitle(groups)
72 });
73 }
74
75 if (filter !== Filter.USERS) {
76 groups.forEach(group => items.push({
77 ...group,
78 key: group.id,
79 label: group.name,
80 avatar: group.iconUrl,
81 glyph: group.iconUrl ? null : GroupIcon,
82 description: this.listSourceOptions.getPluralForUserCount(group.userCount)
83 }));
84 }
85
86 return items;
87 }
88}