Skip to content

Commit 6b359ed

Browse files
committed
feat(content-sharing): Create contact service
1 parent b39a482 commit 6b359ed

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

src/elements/content-sharing/ContentSharingV2.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import API from '../../api';
88
import Internationalize from '../common/Internationalize';
99
import Providers from '../common/Providers';
1010
import { fetchAvatars, fetchCollaborators, fetchCurrentUser, fetchItem } from './apis';
11-
import { useSharingService } from './hooks/useSharingService';
11+
import { useContactService, useSharingService } from './hooks';
1212
import { convertCollabsResponse, convertItemResponse } from './utils';
1313

1414
import type { Collaborations, ItemType, StringMap } from '../../common/types/core';
@@ -60,6 +60,7 @@ function ContentSharingV2({
6060
setItem,
6161
setSharedLink,
6262
});
63+
const { contactService } = useContactService(api, itemID, currentUser?.id);
6364

6465
// Handle successful GET requests to /files or /folders
6566
const handleGetItemSuccess = React.useCallback(itemData => {
@@ -164,6 +165,7 @@ function ContentSharingV2({
164165
config={config}
165166
collaborationRoles={collaborationRoles}
166167
collaborators={collaborators}
168+
contactService={contactService}
167169
currentUser={currentUser}
168170
item={item}
169171
sharedLink={sharedLink}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { useContactService } from './useContactService';
2+
export { useSharingService } from './useSharingService';
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import * as React from 'react';
2+
3+
import { STATUS_INACTIVE } from '../../../constants';
4+
import useContacts from './useContacts';
5+
import useContactsByEmail from './useContactsByEmail';
6+
import { convertUserContactsByEmailResponse } from '../../../features/unified-share-modal/utils/convertData';
7+
8+
const APP_USERS_DOMAIN_REGEXP = /boxdevedition.com/;
9+
const sortByName = ({ name: nameA = '' }, { name: nameB = '' }) => nameA.localeCompare(nameB);
10+
11+
/**
12+
* Convert an enterprise users API response into an array of internal USM contacts.
13+
*/
14+
export const convertUserContactsResponse = (contactsAPIData, currentUserID) => {
15+
const { entries = [] } = contactsAPIData;
16+
17+
// Return all active users except for the current user and app users
18+
return entries
19+
.filter(
20+
({ id, login: email, status }) =>
21+
id !== currentUserID &&
22+
email &&
23+
!APP_USERS_DOMAIN_REGEXP.test(email) &&
24+
status &&
25+
status !== STATUS_INACTIVE,
26+
)
27+
.map(contact => {
28+
const { id, login: email, name, type } = contact;
29+
return {
30+
id,
31+
email,
32+
name,
33+
type,
34+
value: email,
35+
};
36+
})
37+
.sort(sortByName);
38+
};
39+
40+
/**
41+
* Convert an enterprise groups API response into an array of internal USM contacts.
42+
*/
43+
export const convertGroupContactsResponse = contactsAPIData => {
44+
const { entries = [] } = contactsAPIData;
45+
46+
// Only return groups with the correct permissions
47+
return entries
48+
.filter(({ permissions }) => {
49+
return permissions && permissions.can_invite_as_collaborator;
50+
})
51+
.map(contact => {
52+
const { id, name, type } = contact;
53+
return {
54+
id,
55+
email: 'Group', // Need this for the avatar to work for isUserContactType
56+
name,
57+
type,
58+
value: 'Group',
59+
};
60+
})
61+
.sort(sortByName);
62+
};
63+
64+
export const useContactService = (api, itemID, currentUserID) => {
65+
const getContacts = useContacts(api, itemID, {
66+
transformUsers: data => convertUserContactsResponse(data, currentUserID),
67+
transformGroups: data => convertGroupContactsResponse(data),
68+
});
69+
70+
const getContactsByEmail = useContactsByEmail(api, itemID, {
71+
transformUsers: data => convertUserContactsByEmailResponse(data),
72+
});
73+
74+
const contactService = React.useMemo(() => {
75+
if (!currentUserID) {
76+
return null;
77+
}
78+
79+
return {
80+
getContacts,
81+
getContactsByEmail,
82+
};
83+
}, [currentUserID, getContacts, getContactsByEmail]);
84+
85+
return { contactService };
86+
};

0 commit comments

Comments
 (0)