index.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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, is_arrow },
  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. return (
  49. <Item
  50. key={key}
  51. title={title}
  52. date={date}
  53. type={is_arrow ? 'Arrow' : obj_type}
  54. page={actualPage}
  55. showPageNum={actualPage !== prevPage}
  56. />
  57. );
  58. })}
  59. </div>
  60. </Body>
  61. );
  62. };
  63. export default AnnotationList;