index.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import React, { useEffect, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import Typography from '../Typography';
  4. import Item from '../AnnotationItem';
  5. import { Body } from '../../global/sidebarStyled';
  6. type Props = {
  7. isActive: boolean;
  8. annotations: AnnotationType[];
  9. };
  10. const AnnotationList: React.FC<Props> = ({
  11. isActive = false,
  12. annotations,
  13. }: Props) => {
  14. const { t } = useTranslation('sidebar');
  15. const [list, setList] = useState<AnnotationType[]>([]);
  16. useEffect(() => {
  17. if (isActive && annotations.length) {
  18. annotations.sort((a, b) => {
  19. if (a.obj_attr.page > b.obj_attr.page) {
  20. return 1;
  21. }
  22. if (a.obj_attr.page < b.obj_attr.page) {
  23. return -1;
  24. }
  25. return 0;
  26. });
  27. setList(annotations);
  28. }
  29. }, [isActive, annotations]);
  30. return (
  31. <Body>
  32. <div>
  33. <Typography light align="left">
  34. {`${annotations.length} ${t('annotation')}`}
  35. </Typography>
  36. {isActive &&
  37. list.map((ele, index) => {
  38. const key = `annotation_item_${index}`;
  39. const {
  40. obj_type,
  41. obj_attr: { page, title, date, style },
  42. } = ele;
  43. const actualPage = page + 1;
  44. const prevAnnot = annotations[index - 1];
  45. const prevPage =
  46. index > 0 && prevAnnot ? prevAnnot.obj_attr.page + 1 : -1;
  47. let annotType = obj_type;
  48. if (obj_type === 'checkbox' && style === '1') {
  49. annotType = 'radio';
  50. }
  51. return (
  52. <Item
  53. key={key}
  54. title={title}
  55. date={date}
  56. type={annotType}
  57. page={actualPage}
  58. showPageNum={actualPage !== prevPage}
  59. />
  60. );
  61. })}
  62. </div>
  63. </Body>
  64. );
  65. };
  66. export default AnnotationList;