index.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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) {
  18. const tmpArray = [...annotations];
  19. tmpArray.sort((a, b) => {
  20. if (a.obj_attr.page > b.obj_attr.page) {
  21. return 1;
  22. }
  23. if (a.obj_attr.page < b.obj_attr.page) {
  24. return -1;
  25. }
  26. return 0;
  27. });
  28. setList(tmpArray);
  29. }
  30. }, [isActive, annotations]);
  31. return (
  32. <Body>
  33. <div>
  34. <Typography light align="left">
  35. {`${annotations.length} ${t('annotation')}`}
  36. </Typography>
  37. {isActive &&
  38. list.map((ele, index) => {
  39. const key = `annotation_item_${index}`;
  40. const {
  41. obj_type,
  42. obj_attr: { page, title, date, style },
  43. } = ele;
  44. const actualPage = page + 1;
  45. const prevAnnot = list[index - 1];
  46. const prevPage =
  47. index > 0 && prevAnnot ? prevAnnot.obj_attr.page + 1 : -1;
  48. let annotType = obj_type;
  49. if (obj_type === 'checkbox' && style === '1') {
  50. annotType = 'radio';
  51. }
  52. return (
  53. <Item
  54. key={key}
  55. title={title}
  56. date={date}
  57. type={annotType}
  58. page={actualPage}
  59. showPageNum={actualPage !== prevPage}
  60. />
  61. );
  62. })}
  63. </div>
  64. </Body>
  65. );
  66. };
  67. export default AnnotationList;