index.tsx 1.5 KB

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