import { Component, h, Fragment } from 'preact'; import classes from './AnnotationsList.module.scss'; import { Annotation } from '../storage/Annotation'; import { getTargetUrls } from 'web-annotation-utils'; import { IAnnotationSource } from '../storage/AnnotationSource'; import { AnnotationBody } from '../content_script/AnnotationBody'; interface AnnotationsListProps { source?: IAnnotationSource; withTotal?: boolean; } interface AnnotationsListState { annotations?: Annotation[]; totalAnnotationCount?: number; } export class AnnotationsList extends Component< AnnotationsListProps, AnnotationsListState > { state: AnnotationsListState = {}; async componentDidMount() { await this.loadData(); } async componentDidUpdate(previousProps: Readonly) { if ( previousProps.source?.lastModified !== this.props.source?.lastModified ) { this.loadData(); } } async loadData() { let annotations; if (this.props.source) { if (this.props.source._id === undefined) throw new Error(`Got source without an _id`); annotations = await Annotation.getAnnotationsFromSource( this.props.source._id, ); } else { annotations = await Annotation.getAll(); } this.setState({ annotations }); } render( { withTotal }: AnnotationsListProps, { annotations }: AnnotationsListState, ) { const annotationList = annotations?.map((annotation) => { const webAnnotation = annotation.data.annotation; return (
  • On:{' '} {getTargetUrls(webAnnotation.target).map((url) => ( <>{url}{' '} ))}
    {' '} {/* */}
  • ); }); return ( <> {annotationList ? ( ) : (

    Loading…

    )} {withTotal && (

    Total: {this.state.annotations?.length ?? 'counting…'}

    )} ); } }