-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathContentSharingV2.tsx
More file actions
183 lines (161 loc) · 6.7 KB
/
ContentSharingV2.tsx
File metadata and controls
183 lines (161 loc) · 6.7 KB
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
import * as React from 'react';
import isEmpty from 'lodash/isEmpty';
import { UnifiedShareModal } from '@box/unified-share-modal';
import type { CollaborationRole, Collaborator, Item, SharedLink, User } from '@box/unified-share-modal';
import API from '../../api';
import Internationalize from '../common/Internationalize';
import Providers from '../common/Providers';
import { withBlueprintModernization } from '../common/withBlueprintModernization';
import { fetchAvatars, fetchCollaborators, fetchCurrentUser, fetchItem } from './apis';
import { useContactService, useSharingService } from './hooks';
import { convertCollabsResponse, convertItemResponse } from './utils';
import type { Collaborations, ItemType, StringMap } from '../../common/types/core';
import type { AvatarURLMap } from './types';
export interface ContentSharingV2Props {
/** api - API instance */
api: API;
/** children - Children for the element to open the Unified Share Modal */
children?: React.ReactElement;
/** itemID - Box file or folder ID */
itemID: string;
/** itemType - "file" or "folder" */
itemType: ItemType;
/** hasProviders - Whether the element has providers for USM already */
hasProviders?: boolean;
/** language - Language used for the element */
language?: string;
/** messages - Localized strings used by the element */
messages?: StringMap;
}
function ContentSharingV2({
api,
children,
itemID,
itemType,
hasProviders,
language,
messages,
}: ContentSharingV2Props) {
const [avatarURLMap, setAvatarURLMap] = React.useState<AvatarURLMap | null>(null);
const [item, setItem] = React.useState<Item | null>(null);
const [sharedLink, setSharedLink] = React.useState<SharedLink | null>(null);
const [sharingServiceProps, setSharingServiceProps] = React.useState(null);
const [currentUser, setCurrentUser] = React.useState<User | null>(null);
const [collaborationRoles, setCollaborationRoles] = React.useState<CollaborationRole[] | null>(null);
const [collaborators, setCollaborators] = React.useState<Collaborator[] | null>(null);
const [collaboratorsData, setCollaboratorsData] = React.useState<Collaborations | null>(null);
const [owner, setOwner] = React.useState({ id: '', email: '', name: '' });
const { sharingService } = useSharingService({
api,
item,
itemId: itemID,
itemType,
sharedLink,
sharingServiceProps,
setItem,
setSharedLink,
});
const { contactService } = useContactService(api, itemID, currentUser?.id);
// Handle successful GET requests to /files or /folders
const handleGetItemSuccess = React.useCallback(itemData => {
const {
collaborationRoles: collaborationRolesFromApi,
item: itemFromApi,
ownedBy,
sharedLink: sharedLinkFromApi,
sharingService: sharingServicePropsFromApi,
} = convertItemResponse(itemData);
setItem(itemFromApi);
setSharedLink(sharedLinkFromApi);
setSharingServiceProps(sharingServicePropsFromApi);
setCollaborationRoles(collaborationRolesFromApi);
setOwner({ id: ownedBy.id, email: ownedBy.login, name: ownedBy.name });
}, []);
// Reset state if the API has changed
React.useEffect(() => {
setItem(null);
setSharedLink(null);
setCurrentUser(null);
setCollaborationRoles(null);
setAvatarURLMap(null);
setCollaborators(null);
setCollaboratorsData(null);
}, [api]);
// Get initial data for the item
React.useEffect(() => {
if (!api || isEmpty(api) || item) return;
(async () => {
const itemData = await fetchItem({ api, itemID, itemType });
handleGetItemSuccess(itemData);
})();
}, [api, item, itemID, itemType, sharedLink, handleGetItemSuccess]);
// Get current user
React.useEffect(() => {
if (!api || isEmpty(api) || !item || currentUser) return;
const getUserSuccess = userData => {
const { id, enterprise, hostname } = userData;
setCurrentUser({
id,
enterprise: { name: enterprise ? enterprise.name : '' },
});
setSharedLink(prevSharedLink => ({ ...prevSharedLink, serverURL: hostname ? `${hostname}v/` : '' }));
};
(async () => {
const userData = await fetchCurrentUser({ api, itemID });
getUserSuccess(userData);
})();
}, [api, currentUser, item, itemID, itemType, sharedLink]);
// Get collaborators
React.useEffect(() => {
if (!api || isEmpty(api) || !item || collaboratorsData) return;
(async () => {
try {
const response = await fetchCollaborators({ api, itemID, itemType });
setCollaboratorsData(response);
} catch {
setCollaboratorsData({ entries: [], next_marker: null });
}
})();
}, [api, collaboratorsData, item, itemID, itemType]);
// Get avatars when collaborators are available
React.useEffect(() => {
if (avatarURLMap || !collaboratorsData || !collaboratorsData.entries) return;
(async () => {
const response = await fetchAvatars({ api, itemID, collaborators: collaboratorsData.entries });
setAvatarURLMap(response);
})();
}, [api, avatarURLMap, collaboratorsData, itemID]);
React.useEffect(() => {
if (avatarURLMap && collaboratorsData && currentUser && owner) {
const collaboratorsWithAvatars = convertCollabsResponse(
collaboratorsData,
currentUser.id,
owner,
avatarURLMap,
);
setCollaborators(collaboratorsWithAvatars);
}
}, [avatarURLMap, collaboratorsData, currentUser, owner]);
const config = { sharedLinkEmail: false };
return (
<Internationalize language={language} messages={messages}>
<Providers hasProviders={hasProviders}>
{item && (
<UnifiedShareModal
config={config}
collaborationRoles={collaborationRoles}
collaborators={collaborators}
contactService={contactService}
currentUser={currentUser}
item={item}
sharedLink={sharedLink}
sharingService={sharingService}
>
{children}
</UnifiedShareModal>
)}
</Providers>
</Internationalize>
);
}
export default withBlueprintModernization(ContentSharingV2);