/
App.tsx
545 lines (516 loc) · 14.5 KB
/
App.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
import * as React from 'react';
import { IMetadataCommentsService } from 'jupyterlab-metadata-service';
import { IMetadataPeopleService } from 'jupyterlab-metadata-service';
// Components
import { AppBody } from './AppBody';
import { CommentCard } from './CommentCard';
import { AppHeader } from './AppHeader';
import { AppHeaderOptions } from './AppHeaderOptions';
import { NewThreadCard } from './NewThreadCard';
import { UserSet } from './UserSet';
/**
* React States interface
*/
interface IAppStates {
/**
* Card unique id that is expanded / full screen
*
* @type string
*/
expandedCard: string;
/**
* Card unique id that has the reply active
*
* @type string
*/
replyActiveCard: string;
/**
* Current state of the sort dropdown in the header
*
* @type string
*/
sortState: string;
/**
* Check box state in the header
*
* @type boolean
*/
showResolved: boolean;
/**
* State for if new thread button pressed
*
* @type boolean
*/
newThreadActive: boolean;
/**
* File to add new thread to
*
* @type string
*/
newThreadFile: string;
/**
* Tracks when a user is set
*
* @type boolean
*/
userSet: boolean;
/**
* Hold the users information
*
* @type Person
*/
creator: object;
/**
* State of threads to be rendered
*
* @type React.ReactNode[]
*/
myThreads: React.ReactNode[];
/**
* State to hold last response
*
* @type any
*/
response: any;
/**
* State to track when to query
*
* @type boolean
*/
shouldQuery: boolean;
/**
* Tracks if the current target has threads or not
*
* @type boolean
*/
curTargetHasThreads: boolean;
}
/**
* React Props interface
*/
interface IAppProps {
/**
* Comments Service that communicates with graphql server
*
* @type IMetadataCommentsService
*/
commentsService?: IMetadataCommentsService;
/**
* People Service that communicates with graphql server
*/
peopleService?: IMetadataPeopleService;
/**
* Path of open file, used as unique id to fetch comments and annotations
*
* @type string
*/
target: string;
/**
* Name of the open file used in the commenting header
*
* @type: string
*/
targetName: string;
}
/**
* The interval function that is used to pull in threads / comments every set interval
*/
let periodicUpdate: number;
/**
* Main App React Component
*/
export default class App extends React.Component<IAppProps, IAppStates> {
/**
* Constructor
*
* @param props React props
*/
constructor(props: IAppProps) {
super(props);
this.state = {
expandedCard: ' ',
replyActiveCard: ' ',
newThreadFile: '',
sortState: 'latest',
showResolved: false,
newThreadActive: false,
userSet: false,
shouldQuery: true,
curTargetHasThreads: false,
creator: {},
myThreads: [],
response: { data: { annotationsByTarget: { length: 0 } } }
};
this.getAllCommentCards = this.getAllCommentCards.bind(this);
this.setExpandedCard = this.setExpandedCard.bind(this);
this.checkExpandedCard = this.checkExpandedCard.bind(this);
this.setSortState = this.setSortState.bind(this);
this.showResolved = this.showResolved.bind(this);
this.putComment = this.putComment.bind(this);
this.putThread = this.putThread.bind(this);
this.setCardValue = this.setCardValue.bind(this);
this.setNewThreadActive = this.setNewThreadActive.bind(this);
this.setReplyActiveCard = this.setReplyActiveCard.bind(this);
this.checkReplyActiveCard = this.checkReplyActiveCard.bind(this);
this.setUserInfo = this.setUserInfo.bind(this);
this.shouldQuery = this.shouldQuery.bind(this);
this.getNewThreadButton = this.getNewThreadButton.bind(this);
}
/**
* Called when the component will mount
*/
componentWillMount() {
// Sets the interval of when to periodically query for comments
periodicUpdate = setInterval(this.shouldQuery, 1000);
}
/**
* Called when component will unmount
*/
componentWillUnmount() {
// Stops the periodic query of comments
clearInterval(periodicUpdate);
}
/**
* Called each time the component updates
*/
componentDidUpdate(): void {
// Queries new data if there is a new target
if (this.state.response.data.annotationsByTarget !== undefined) {
if (this.state.response.data.annotationsByTarget.length !== 0) {
if (
this.state.response.data.annotationsByTarget[0].target !==
this.props.target &&
!this.state.shouldQuery
) {
this.setState({
expandedCard: ' ',
newThreadActive: false,
shouldQuery: true
});
}
}
}
// Handles querying new data
if (this.state.shouldQuery) {
if (this.props.target !== undefined) {
this.props.commentsService
.queryAllByTarget(this.props.target)
.then((response: any) => {
if (response.data.annotationsByTarget.length !== 0) {
this.setState({
myThreads: this.getAllCommentCards(
response.data.annotationsByTarget
),
response: response,
shouldQuery: false,
curTargetHasThreads: true
});
} else {
this.state.myThreads.length !== 0 &&
this.setState({
myThreads: [],
shouldQuery: false,
curTargetHasThreads: false,
newThreadActive: false
});
}
});
}
}
// Handles when there is no target
if (this.props.target === undefined) {
this.state.myThreads.length !== 0 &&
this.setState({
myThreads: [],
shouldQuery: false,
curTargetHasThreads: false,
newThreadActive: false
});
}
}
/**
* React render function
*/
render() {
return this.state.userSet ? (
<div className="--jp-commenting-window">
<AppHeader
header={this.props.targetName}
cardExpanded={this.state.expandedCard !== ' '}
threadOpen={this.state.newThreadActive}
setExpandedCard={this.setExpandedCard}
headerOptions={
<AppHeaderOptions
setSortState={this.setSortState}
showResolvedState={this.showResolved}
cardExpanded={this.state.expandedCard !== ' '}
header={this.props.targetName}
hasThreads={this.state.curTargetHasThreads}
showResolved={this.state.showResolved}
sortState={this.state.sortState}
/>
}
/>
<AppBody
cards={
this.state.newThreadActive
? [
<NewThreadCard
putThread={this.putThread}
setNewThreadActive={this.setNewThreadActive}
creator={this.state.creator}
/>
]
: this.state.myThreads
}
expanded={this.state.expandedCard !== ' '}
newThreadButton={
this.state.newThreadActive || this.props.target === undefined
? undefined
: this.getNewThreadButton()
}
/>
</div>
) : (
<div className="--jp-commenting-window">
<UserSet setUserInfo={this.setUserInfo} />
</div>
);
}
/**
* Creates and returns all CommentCard components with correct data
*
* @param allData Type: any - Comment data from this.props.data
* @return Type: React.ReactNode[] - List of CommentCard Components / ReactNodes
*/
getAllCommentCards(allData: any): React.ReactNode[] {
let cards: React.ReactNode[] = [];
for (let key in allData) {
if (
this.shouldRenderCard(
allData[key].resolved,
this.state.expandedCard !== ' ',
this.state.expandedCard === allData[key].id
)
) {
cards.push(
<CommentCard
data={allData[key]}
threadId={allData[key].id}
setExpandedCard={this.setExpandedCard}
checkExpandedCard={this.checkExpandedCard}
setReplyActiveCard={this.setReplyActiveCard}
checkReplyActiveCard={this.checkReplyActiveCard}
resolved={allData[key].resolved}
putComment={this.putComment}
setCardValue={this.setCardValue}
target={this.props.target}
/>
);
}
}
return cards.reverse();
}
/**
* JSX of new thread button
*
* @return Type: React.ReactNode - New thread button
*/
getNewThreadButton(): React.ReactNode {
return (
<div
className="--jp-commenting-new-thread-button"
onClick={() => this.setNewThreadActive(true)}
>
<span className="--jp-commenting-new-thread-label">
New Comment Thread
</span>
<span className="jp-AddIcon jp-Icon jp-ToolbarButtonComponent-icon jp-Icon-16" />
</div>
);
}
/**
* Checks if a card should be rendered in based on the states of
* the current view
*
* @param resolved Type: boolean - resolved state of the card
* @param expandedCard Type: boolean - State if there is a card expanded
* @param curCardExpanded Type: boolean - State if the current card is expanded
*/
shouldRenderCard(
resolved: boolean,
expandedCard: boolean,
curCardExpanded: boolean
): boolean {
if (!this.state.showResolved) {
if (!resolved) {
if (expandedCard) {
return curCardExpanded;
}
return true;
} else {
return false;
}
} else {
if (expandedCard) {
return curCardExpanded;
}
return true;
}
}
/**
* Sets the state should query to query when component updates
*/
shouldQuery(): void {
this.setState({ shouldQuery: true });
}
/**
* Pushed comment back to MetadataCommentsService
*
* @param value Type: string - comment message
* @param threadId Type: string - commend card / thread the comment applies to
*/
putComment(threadId: string, value: string): void {
this.props.commentsService.createComment(
threadId,
value,
this.state.creator
);
this.shouldQuery();
}
/**
* Pushes new thread to GraphQL server
*
* @param value Type: string - comment message
* @param label Type: string - label / tag of a thread
*/
putThread(value: string): void {
this.props.commentsService.createThread(
this.props.target,
value,
this.state.creator
);
this.shouldQuery();
}
/**
* Used to set a specific field of a card
*
* @param target Type: string - id of a thread
* @param threadId Type: string - id of a specific card
* @param key Type: string - key of value to set
* @param value Type: string - value to set
*/
setCardValue(target: string, threadId: string, value: boolean): void {
this.props.commentsService.setResolvedValue(target, threadId, value);
}
/**
* Used to check if the cardId passed in is the current expanded card
*
* @param threadId Type: string - CommentCard unique id
* @return Type: boolean - True if cardId is expanded, false if cardId is not expanded
*/
checkExpandedCard(threadId: string): boolean {
return threadId === this.state.expandedCard;
}
/**
* Sets this.state.expandedCard to the passed in cardId
*
* @param threadId Type: string - CommentCard unique id
*/
setExpandedCard(threadId: string) {
this.setState({ expandedCard: threadId });
this.shouldQuery();
}
/**
* Sets this.state.replyActiveCard to the passed in cardId
*
* @param threadId Type: string - CommentCard unique id
*/
setReplyActiveCard(threadId: string) {
this.setState({ replyActiveCard: threadId });
this.shouldQuery();
}
/**
* Used to check if the cardId passed in has reply box active
*
* @param threadId Type: string - CommentCard unique id
* @return type: boolean - True if cardId has reply box open, false if not active
*/
checkReplyActiveCard(threadId: string): boolean {
return threadId === this.state.replyActiveCard;
}
/**
* Sets this.state fields for active new thread card
*
* @param state Type: boolean - State to set if new thread card is active
* @param target Type: string - target of the file to add new thread to
*/
setNewThreadActive(state: boolean) {
this.setState({ newThreadActive: state });
this.setState({ newThreadFile: this.props.target });
this.shouldQuery();
}
/**
* Sets this.state.sortState to the selected sort by
*
* @param state Type: string - Sort by type
*/
setSortState(state: string) {
this.setState({ sortState: state });
this.shouldQuery();
}
/**
* Sets this.state.showResolved to the state of the checkbox
* "Show resolved"
*/
showResolved(state: boolean) {
this.setState({ showResolved: state });
this.shouldQuery();
}
/**
* Uses Github API to fetch users name and photo
*
* @param user Type: string - users github username
*/
async setUserInfo(user: string) {
const response = await fetch('https://api.github.com/users/' + user);
const myJSON = await response.json();
// If users does not have a name set, use username
const name = myJSON.name === null ? myJSON.login : myJSON.name;
if (myJSON.message !== 'Not Found') {
this.props.peopleService.queryAll().then((response: any) => {
if (response.data.people.length !== 0) {
for (let index in response.data.people) {
if (
response.data.people[index].name === name &&
!this.state.userSet
) {
this.setState({
creator: {
id: response.data.people[index].id,
name: name,
image: myJSON.avatar_url
},
userSet: true
});
}
}
if (!this.state.userSet) {
this.props.peopleService.create(name, '', myJSON.avatar_url);
let personCount: number = Number(response.data.people.length + 2);
this.setState({
creator: {
id: 'person/' + personCount,
name: name,
image: myJSON.avatar_url
},
userSet: true
});
}
}
});
} else {
window.alert('Username not found');
}
}
}