-
Notifications
You must be signed in to change notification settings - Fork 342
/
annotation-list.tsx
497 lines (459 loc) · 18.6 KB
/
annotation-list.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
import React, { Component, MouseEventHandler } from 'react'
import cx from 'classnames'
import analytics from 'src/analytics'
import { Annotation as AnnotationFlawed } from 'src/annotations/types'
import { AnnotationPrivacyLevels } from '@worldbrain/memex-common/lib/annotations/types'
import {
AnnotationSharingInfo,
AnnotationSharingAccess,
} from 'src/content-sharing/ui/types'
import {
contentSharing,
auth,
tags,
collections,
} from 'src/util/remote-functions-background'
// import SingleNoteShareMenu from 'src/overview/sharing/SingleNoteShareMenu'
import { INIT_FORM_STATE } from 'src/sidebar/annotations-sidebar/containers/logic'
import type {
EditForm,
EditForms,
} from 'src/sidebar/annotations-sidebar/containers/types'
import { copyToClipboard } from 'src/annotations/content_script/utils'
import { ContentSharingInterface } from 'src/content-sharing/background/types'
import { RemoteCopyPasterInterface } from 'src/copy-paster/background/types'
import { linkStreams } from 'openpgp'
import { getAnnotationPrivacyState } from '@worldbrain/memex-common/lib/content-sharing/utils'
const styles = require('./annotation-list.css')
// TODO (sidebar-refactor): somewhere this type regressed and `isBookmarked` got
// changed to `hasBookmark`
type Annotation = Omit<AnnotationFlawed, 'isBookmarked'> & {
hasBookmark: boolean
}
export interface Props {
activeShareMenuNoteId: string | undefined
activeTagPickerNoteId: string | undefined
activeListPickerNoteId: string | undefined
activeCopyPasterAnnotationId: string | undefined
/** Override for expanding annotations by default */
isExpandedOverride: boolean
/** Array of matched annotations, limited to 3 */
annotations: Annotation[]
/** URL of the page to which these annotations belong */
pageUrl: string
/** Opens the annotation sidebar with all of the annotations */
openAnnotationSidebar: MouseEventHandler
goToAnnotation: (annotation: Annotation) => void
// handleEditAnnotation: (url: string, comment: string, tags: string[]) => void
handleDeleteAnnotation: (url: string) => void
setActiveTagPickerNoteId: (id: string) => void
setActiveListPickerNoteId: (id: string) => void
setActiveShareMenuNoteId?: (id: string) => void
setActiveCopyPasterAnnotationId?: (id: string) => void
contentSharing: ContentSharingInterface
copyPaster: RemoteCopyPasterInterface
}
interface SharingInfo {
[annotationUrl: string]: AnnotationSharingInfo
}
interface State {
/** Boolean to denote whether the list is expanded or not */
isExpanded: boolean
/** The previous prop to compare in getDerivedStateFromProps */
prevIsExpandedOverride: boolean
/** Received annotations are stored and manipulated through edit/delete */
annotations: Annotation[]
editForms: EditForms
annotationsSharingInfo: SharingInfo
sharingAccess: AnnotationSharingAccess
}
class AnnotationList extends Component<Props, State> {
private authBG = auth
private tagsBG = tags
private collectionsBG = collections
private contentShareBG = contentSharing
state: State = {
/* The initial value is set to the isExpandedOverride which is
fetched from localStorage. */
isExpanded: this.props.isExpandedOverride,
prevIsExpandedOverride: this.props.isExpandedOverride,
// TODO: This shouldn't be in state - get it out and ensure wherever it gets passed down as props from properly handles state mutations
annotations: this.props.annotations,
editForms: this.props.annotations.reduce(
(acc, curr) => ({
...acc,
[curr.url]: {
...INIT_FORM_STATE,
commentText: curr.comment,
tags: curr.tags,
},
}),
{},
),
sharingAccess: 'sharing-allowed',
annotationsSharingInfo: {},
}
/**
* We compare if the previous isExpandedOverride prop is different from
* the current isExpandedOverride, then we set the state accordingly.
*/
static getDerivedStateFromProps(props: Props, state: State): State {
if (props.isExpandedOverride !== state.prevIsExpandedOverride) {
return {
...state,
isExpanded: props.isExpandedOverride,
prevIsExpandedOverride: props.isExpandedOverride,
}
}
return state
}
async componentDidMount() {
await this.detectSharedAnnotations()
}
private async detectSharedAnnotations() {
const annotationSharingInfo: SharingInfo = {}
const annotationUrls = this.props.annotations.map((a) => a.url)
const remoteIds = await this.contentShareBG.getRemoteAnnotationIds({
annotationUrls,
})
for (const localId of Object.keys(remoteIds)) {
annotationSharingInfo[localId] = {
status: 'shared',
taskState: 'pristine',
privacyLevel: AnnotationPrivacyLevels.SHARED,
}
}
this.setState(() => ({
annotationsSharingInfo: annotationSharingInfo,
}))
}
// private updateAnnotationShareState = (annotationUrl: string) => (
// info: AnnotationSharingInfo,
// ) =>
// this.setState((state) => ({
// annotationsSharingInfo: {
// ...state.annotationsSharingInfo,
// [annotationUrl]: info,
// },
// }))
// private toggleIsExpanded = () => {
// this.setState(
// (prevState: State): State => ({
// ...prevState,
// isExpanded: !prevState.isExpanded,
// }),
// )
// }
// private handleEditAnnotation = (url: string) => () => {
// const { annotations, editForms } = this.state
// const index = annotations.findIndex((annot) => annot.url === url)
// const form = editForms[url]
// const annotation: Annotation = annotations[index]
// if (
// !annotation ||
// (!annotation.body &&
// !form.commentText?.length &&
// !form.tags?.length)
// ) {
// return
// }
// const newAnnotations: Annotation[] = [
// ...annotations.slice(0, index),
// {
// ...annotation,
// comment: form.commentText,
// tags: form.tags,
// lastEdited: new Date(),
// },
// ...annotations.slice(index + 1),
// ]
// this.props.handleEditAnnotation(url, form.commentText, form.tags)
// this.setState((state) => ({
// annotations: newAnnotations,
// annotationModes: { ...state.annotationModes, [url]: 'default' },
// editForms: {
// ...state.editForms,
// [url]: {
// ...INIT_FORM_STATE,
// commentText: state.editForms[url].commentText,
// tags: state.editForms[url].tags,
// },
// },
// }))
// }
// private handleDeleteAnnotation = (url: string) => () => {
// this.props.handleDeleteAnnotation(url)
// // Delete the annotation in the state too
// const { annotations } = this.state
// const index = this.state.annotations.findIndex(
// (annot) => annot.url === url,
// )
// const newAnnotations = [
// ...annotations.slice(0, index),
// ...annotations.slice(index + 1),
// ]
// this.setState({
// annotations: newAnnotations,
// })
// }
// private handleGoToAnnotation = (annotation: Annotation) => () => {
// this.props.goToAnnotation(annotation)
// }
// private handleTagPickerClick = (url: string) => () => {
// this.props.setActiveTagPickerNoteId(url)
// }
// private handleListPickerClick = (url: string) => () => {
// this.props.setActiveListPickerNoteId(url)
// }
// private handleEditCancel = (url: string, commentText: string) => () =>
// this.setState((state) => ({
// annotationModes: { [url]: 'default' },
// editForms: {
// ...state.editForms,
// [url]: {
// ...state.editForms[url],
// commentText,
// },
// },
// }))
// private handleShareClick = (url: string) => () => {
// if (this.props.setActiveShareMenuNoteId != null) {
// this.props.setActiveShareMenuNoteId(url)
// }
// }
// private renderTagPicker(annot: Annotation) {
// if (this.props.activeTagPickerNoteId !== annot.url) {
// return null
// }
// return (
// <div className={styles.hoverBoxWrapper}>
// <HoverBox>
// {/* <TagPicker
// onUpdateEntrySelection={(args) =>
// this.tagsBG.updateTagForPage({
// ...args,
// url: annot.url,
// })
// }
// initialSelectedEntries={() => annot.tags}
// onClickOutside={() =>
// this.props.setActiveTagPickerNoteId(undefined)
// }
// /> */}
// </HoverBox>
// </div>
// )
// }
// private renderListPicker(annot: Annotation) {
// if (this.props.activeListPickerNoteId !== annot.url) {
// return null
// }
// return (
// <div className={styles.hoverBoxWrapper}>
// <HoverBox>
// {/* <CollectionPicker
// onUpdateEntrySelection={async (args) => {
// // TODO implement picker
// const name = args.added ?? args.deleted
// const list = await this.collectionsBG.fetchListByName(
// { name },
// )
// const id = list.id
// if (args.added != null) {
// this.contentShareBG.shareAnnotationToSomeLists({
// annotationUrl: annot.url,
// localListIds: [id],
// })
// } else if (args.deleted != null) {
// this.contentShareBG.unshareAnnotationFromSomeLists(
// {
// annotationUrl: annot.url,
// localListIds: [id],
// },
// )
// }
// }}
// initialSelectedEntries={() => annot.lists}
// onClickOutside={() =>
// this.props.setActiveListPickerNoteId(undefined)
// }
// /> */}
// </HoverBox>
// </div>
// )
// }
// private renderCopyPasterManager(annot: Annotation) {
// if (this.props.activeCopyPasterAnnotationId !== annot.url) {
// return null
// }
// return (
// <div className={styles.hoverBoxWrapper}>
// <HoverBox>
// <PageNotesCopyPaster
// copyPaster={this.props.copyPaster}
// annotationUrls={[annot.url]}
// normalizedPageUrls={[annot.pageUrl]}
// onClickOutside={() =>
// this.props.setActiveCopyPasterAnnotationId?.(
// undefined,
// )
// }
// />
// </HoverBox>
// </div>
// )
// }
// private renderShareMenu(annot: Annotation) {
// if (this.props.activeShareMenuNoteId !== annot.url) {
// return null
// }
// return (
// <div className={styles.hoverBoxWrapper}>
// <HoverBox>
// {/* <SingleNoteShareMenu
// contentSharingBG={this.props.contentSharing}
// copyLink={async (link) => {
// analytics.trackEvent({
// category: 'ContentSharing',
// action: 'copyNoteLink',
// })
// await copyToClipboard(link)
// }}
// annotationUrl={annot.url}
// postShareHook={(state) => {
// const privacyState = getAnnotationPrivacyState(
// state.privacyLevel,
// )
// return this.updateAnnotationShareState(annot.url)({
// status: privacyState.public
// ? 'shared'
// : 'unshared',
// taskState: 'success',
// privacyLevel: privacyState.protected
// ? AnnotationPrivacyLevels.PROTECTED
// : undefined,
// })
// }}
// closeShareMenu={() =>
// this.props.setActiveShareMenuNoteId?.(undefined)
// }
// /> */}
// </HoverBox>
// </div>
// )
// }
private handleEditFormUpdate = (
url: string,
deriveState: (state: State) => Partial<EditForm>,
) => {
this.setState((state) => ({
editForms: {
...state.editForms,
[url]: {
...state.editForms[url],
...deriveState(state),
},
},
}))
}
private renderAnnotations() {
return null
// return this.state.annotations.map((annot) => (
// <AnnotationEditable
// getListDetailsById={(i) => ({
// name: 'dead code',
// isShared: false,
// })}
// key={annot.url}
// {...annot}
// body={annot.body}
// comment={annot.comment}
// className={styles.annotation}
// createdWhen={annot.createdWhen!}
// isShared={false}
// isBulkShareProtected={false}
// mode={this.state.annotationModes[annot.url]}
// onGoToAnnotation={this.handleGoToAnnotation(annot)}
// renderShareMenuForAnnotation={() => this.renderShareMenu(annot)}
// renderCopyPasterForAnnotation={() =>
// this.renderCopyPasterManager(annot)
// }
// renderTagsPickerForAnnotation={() =>
// this.renderTagPicker(annot)
// }
// renderListsPickerForAnnotation={() =>
// this.renderListPicker(annot)
// }
// annotationEditDependencies={{
// comment: this.state.editForms[annot.url].commentText,
// onCommentChange: (commentText) =>
// this.handleEditFormUpdate(annot.url, () => ({
// commentText,
// })),
// onEditCancel: this.handleEditCancel(
// annot.url,
// annot.comment,
// ),
// onEditConfirm: () => this.handleEditAnnotation(annot.url),
// }}
// annotationFooterDependencies={{
// onEditIconClick: () =>
// this.setState({
// annotationModes: {
// [annot.url]: 'edit',
// },
// }),
// onDeleteCancel: this.handleEditCancel(
// annot.url,
// annot.comment,
// ),
// onDeleteConfirm: this.handleDeleteAnnotation(annot.url),
// onDeleteIconClick: () =>
// this.setState({
// annotationModes: { [annot.url]: 'delete' },
// }),
// onTagIconClick: this.handleTagPickerClick(annot.url),
// onListIconClick: this.handleListPickerClick(annot.url),
// onShareClick: this.handleShareClick(annot.url),
// onCopyPasterBtnClick:
// this.props.setActiveCopyPasterAnnotationId != null
// ? () =>
// this.props.setActiveCopyPasterAnnotationId(
// annot.url,
// )
// : undefined,
// }}
// />
// ))
}
render() {
const { isExpanded } = this.state
return undefined
// <div
// className={cx({
// [styles.parentExpanded]: isExpanded,
// })}
// >
// {/* Annotation count text and toggle arrow */}
// <div
// className={cx(styles.resultCount, {
// [styles.expandedCount]: this.state.isExpanded,
// })}
// onClick={this.toggleIsExpanded}
// >
// <b>{this.props.annotations.length}</b>{' '}
// <span className={styles.resultsText}>results</span>
// <span
// className={cx(styles.icon, {
// [styles.inverted]: this.state.isExpanded,
// })}
// />
// </div>
// {/* Container for displaying AnnotationBox */}
// <div className={styles.annotationList}>
// {isExpanded ? this.renderAnnotations() : null}
// </div>
// </div>
}
}
export default AnnotationList