File size: 1,284 Bytes
e1d589b | 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 | import React, { PureComponent } from "react";
import PropTypes from "prop-types";
import { hash } from "utils/string";
export default class AnnotationGroupedBySubject extends PureComponent {
static displayName = "Annotation.GroupBy.Subject";
static propTypes = {
annotations: PropTypes.array.isRequired,
render: PropTypes.func.isRequired
};
static defaultProps = {
annotations: []
};
/* eslint-disable no-param-reassign */
get grouped() {
const grouped = this.props.annotations.reduce((memo, annotation) => {
const key = hash(annotation.attributes.subject.trim());
if (!memo.hasOwnProperty(key)) {
memo[key] = {
annotations: [],
selection: {
hash: key,
subject: annotation.attributes.subject,
startNode: annotation.attributes.startNode,
startChar: annotation.attributes.startChar,
endNode: annotation.attributes.endNode,
endChar: annotation.attributes.endChar
}
};
}
memo[key].annotations.push(annotation);
return memo;
}, {});
return Object.values(grouped);
}
/* eslint-enable no-param-reassign */
render() {
return <>{this.grouped.map(group => this.props.render(group))}</>;
}
}
|