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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
import { observable, runInAction, action } from "mobx";
import * as React from "react";
import MainViewModal from "../views/MainViewModal";
import { Doc, Opt, DocCastAsync } from "../../new_fields/Doc";
import { DocServer } from "../DocServer";
import { Cast, StrCast } from "../../new_fields/Types";
import * as RequestPromise from "request-promise";
import { Utils } from "../../Utils";
import "./SharingManager.scss";
import { Id } from "../../new_fields/FieldSymbols";
import { observer } from "mobx-react";
import { MainView } from "../views/MainView";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { library } from '@fortawesome/fontawesome-svg-core';
import * as fa from '@fortawesome/free-solid-svg-icons';
import { DocumentView } from "../views/nodes/DocumentView";
import { SelectionManager } from "./SelectionManager";
import { DocumentManager } from "./DocumentManager";
import { CollectionView } from "../views/collections/CollectionView";
import { DictationOverlay } from "../views/DictationOverlay";
library.add(fa.faCopy);
export interface User {
email: string;
userDocumentId: string;
}
export enum SharingPermissions {
None = "Not Shared",
View = "Can View",
Comment = "Can Comment",
Edit = "Can Edit"
}
const ColorMapping = new Map<string, string>([
[SharingPermissions.None, "red"],
[SharingPermissions.View, "maroon"],
[SharingPermissions.Comment, "blue"],
[SharingPermissions.Edit, "green"]
]);
const SharingKey = "sharingPermissions";
const PublicKey = "publicLinkPermissions";
const DefaultColor = "black";
interface ValidatedUser {
user: User;
notificationDoc: Doc;
}
const storage = "data";
@observer
export default class SharingManager extends React.Component<{}> {
public static Instance: SharingManager;
@observable private isOpen = false;
@observable private users: ValidatedUser[] = [];
@observable private targetDoc: Doc | undefined;
@observable private targetDocView: DocumentView | undefined;
@observable private copied = false;
@observable private dialogueBoxOpacity = 1;
@observable private overlayOpacity = 0.4;
private get linkVisible() {
return this.sharingDoc ? this.sharingDoc[PublicKey] !== SharingPermissions.None : false;
}
public open = (target: DocumentView) => {
SelectionManager.DeselectAll();
this.populateUsers().then(action(() => {
this.targetDocView = target;
this.targetDoc = target.props.Document;
DictationOverlay.Instance.hasActiveModal = true;
this.isOpen = true;
if (!this.sharingDoc) {
this.sharingDoc = new Doc;
}
}));
}
public close = action(() => {
this.isOpen = false;
this.users = [];
setTimeout(action(() => {
this.copied = false;
DictationOverlay.Instance.hasActiveModal = false;
this.targetDoc = undefined;
}), 500);
});
private get sharingDoc() {
return this.targetDoc ? Cast(this.targetDoc[SharingKey], Doc) as Doc : undefined;
}
private set sharingDoc(value: Doc | undefined) {
this.targetDoc && (this.targetDoc[SharingKey] = value);
}
constructor(props: {}) {
super(props);
SharingManager.Instance = this;
}
populateUsers = async () => {
let userList = await RequestPromise.get(Utils.prepend("/getUsers"));
const raw = JSON.parse(userList) as User[];
const evaluating = raw.map(async user => {
let isCandidate = user.email !== Doc.CurrentUserEmail;
if (isCandidate) {
const userDocument = await DocServer.GetRefField(user.userDocumentId);
if (userDocument instanceof Doc) {
const notificationDoc = await Cast(userDocument.optionalRightCollection, Doc);
runInAction(() => {
if (notificationDoc instanceof Doc) {
this.users.push({ user, notificationDoc });
}
});
}
}
});
return Promise.all(evaluating);
}
setInternalSharing = async (recipient: ValidatedUser, state: string) => {
const { user, notificationDoc } = recipient;
const target = this.targetDoc!;
const manager = this.sharingDoc!;
const key = user.userDocumentId;
if (state === SharingPermissions.None) {
const metadata = (await DocCastAsync(manager[key]));
if (metadata) {
let sharedAlias = (await DocCastAsync(metadata.sharedAlias))!;
Doc.RemoveDocFromList(notificationDoc, storage, sharedAlias);
manager[key] = undefined;
}
} else {
const sharedAlias = Doc.MakeAlias(target);
Doc.AddDocToList(notificationDoc, storage, sharedAlias);
const metadata = new Doc;
metadata.permissions = state;
metadata.sharedAlias = sharedAlias;
manager[key] = metadata;
}
}
private setExternalSharing = (state: string) => {
let sharingDoc = this.sharingDoc;
if (!sharingDoc) {
return;
}
sharingDoc[PublicKey] = state;
}
private get sharingUrl() {
if (!this.targetDoc) {
return undefined;
}
let baseUrl = Utils.prepend("/doc/" + this.targetDoc[Id]);
return `${baseUrl}?sharing=true`;
}
copy = action(() => {
if (this.sharingUrl) {
Utils.CopyText(this.sharingUrl);
this.copied = true;
}
});
private get sharingOptions() {
return Object.values(SharingPermissions).map(permission => {
return (
<option key={permission} value={permission}>
{permission}
</option>
);
});
}
private focusOn = (contents: string) => {
let title = this.targetDoc ? StrCast(this.targetDoc.title) : "";
return (
<span
className={"focus-span"}
title={title}
onClick={() => {
let context: Opt<CollectionView>;
if (this.targetDoc && this.targetDocView && (context = this.targetDocView.props.ContainingCollectionView)) {
DocumentManager.Instance.jumpToDocument(this.targetDoc, true, undefined, context.props.Document);
}
}}
onPointerEnter={action(() => {
if (this.targetDoc) {
Doc.BrushDoc(this.targetDoc);
this.dialogueBoxOpacity = 0.1;
this.overlayOpacity = 0.1;
}
})}
onPointerLeave={action(() => {
this.targetDoc && Doc.UnBrushDoc(this.targetDoc);
this.dialogueBoxOpacity = 1;
this.overlayOpacity = 0.4;
})}
>
{contents}
</span>
);
}
private computePermissions = (userKey: string) => {
const sharingDoc = this.sharingDoc;
if (!sharingDoc) {
return SharingPermissions.None;
}
const metadata = sharingDoc[userKey] as Doc;
if (!metadata) {
return SharingPermissions.None;
}
return StrCast(metadata.permissions, SharingPermissions.None);
}
private get sharingInterface() {
const existOtherUsers = this.users.length > 0;
return (
<div className={"sharing-interface"}>
<p className={"share-link"}>Manage the public link to {this.focusOn("this document...")}</p>
{!this.linkVisible ? (null) :
<div className={"link-container"}>
<div className={"link-box"} onClick={this.copy}>{this.sharingUrl}</div>
<div
title={"Copy link to clipboard"}
className={"copy"}
style={{ backgroundColor: this.copied ? "lawngreen" : "gainsboro" }}
onClick={this.copy}
>
<FontAwesomeIcon icon={fa.faCopy} />
</div>
</div>
}
<div className={"people-with-container"}>
{!this.linkVisible ? (null) : <p className={"people-with"}>People with this link</p>}
<select
className={"people-with-select"}
value={this.sharingDoc ? StrCast(this.sharingDoc[PublicKey], SharingPermissions.None) : SharingPermissions.None}
style={{
marginLeft: this.linkVisible ? 10 : 0,
color: this.sharingDoc ? ColorMapping.get(StrCast(this.sharingDoc[PublicKey], SharingPermissions.None)) : DefaultColor,
borderColor: this.sharingDoc ? ColorMapping.get(StrCast(this.sharingDoc[PublicKey], SharingPermissions.None)) : DefaultColor
}}
onChange={e => this.setExternalSharing(e.currentTarget.value)}
>
{this.sharingOptions}
</select>
</div>
<div className={"hr-substitute"} />
<p className={"share-individual"}>Privately share {this.focusOn("this document")} with an individual...</p>
<div className={"users-list"} style={{ display: existOtherUsers ? "block" : "flex", minHeight: existOtherUsers ? undefined : 200 }}>
{!existOtherUsers ? "There are no other users in your database." :
this.users.map(({ user, notificationDoc }) => {
const userKey = user.userDocumentId;
const permissions = this.computePermissions(userKey);
const color = ColorMapping.get(permissions);
return (
<div
key={userKey}
className={"container"}
>
<select
className={"permissions-dropdown"}
value={permissions}
style={{ color, borderColor: color }}
onChange={e => this.setInternalSharing({ user, notificationDoc }, e.currentTarget.value)}
>
{this.sharingOptions}
</select>
<span className={"padding"}>{user.email}</span>
</div>
);
})
}
</div>
<div className={"close-button"} onClick={this.close}>Done</div>
</div>
);
}
render() {
return (
<MainViewModal
contents={this.sharingInterface}
isDisplayed={this.isOpen}
interactive={true}
dialogueBoxDisplayedOpacity={this.dialogueBoxOpacity}
overlayDisplayedOpacity={this.overlayOpacity}
/>
);
}
}
|