index.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import React from 'react';
  2. import queryString from 'query-string';
  3. import Icon from '../Icon';
  4. import { downloadFileWithUri, uploadFile } from '../../helpers/utility';
  5. import { parseAnnotationFromXml } from '../../helpers/annotation';
  6. import { Separator } from '../../global/otherStyled';
  7. import { Head, IconWrapper } from '../../global/sidebarStyled';
  8. type Props = {
  9. close: () => void;
  10. addAnnots: (annotations: AnnotationType[]) => void;
  11. hasAnnots: boolean;
  12. };
  13. const index: React.FC<Props> = ({ close, addAnnots, hasAnnots }: Props) => {
  14. const handleExport = (): void => {
  15. if (hasAnnots) {
  16. const parsed = queryString.parse(window.location.search);
  17. const uri = `/api/v1/output.xfdf?f=${parsed.token}`;
  18. downloadFileWithUri('output.xfdf', uri);
  19. }
  20. };
  21. const handleImport = (): void => {
  22. uploadFile('.xfdf').then(data => {
  23. const annotations = parseAnnotationFromXml(data);
  24. addAnnots(annotations);
  25. });
  26. };
  27. return (
  28. <Head>
  29. <IconWrapper>
  30. <Icon glyph="right-back" onClick={close} />
  31. </IconWrapper>
  32. <Separator />
  33. <IconWrapper>
  34. <Icon glyph="sort" />
  35. </IconWrapper>
  36. <IconWrapper onClick={handleExport}>
  37. <Icon glyph="annotation-export" isDisabled={!hasAnnots} />
  38. </IconWrapper>
  39. <IconWrapper onClick={handleImport}>
  40. <Icon glyph="import" />
  41. </IconWrapper>
  42. </Head>
  43. );
  44. };
  45. export default index;