index.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. let outputPath = `/api/v1/output.xfdf?f=${parsed.token}`;
  18. if (parsed.watermark) {
  19. outputPath = `/api/v1/output.xfdf?f=${parsed.token}&user_id=${parsed.watermark}`;
  20. }
  21. downloadFileWithUri('output.xfdf', outputPath);
  22. // downloadFileWithUri('output.xfdf', `http://127.0.0.1:3000${outputPath}`);
  23. }
  24. };
  25. const handleImport = (): void => {
  26. uploadFile('.xfdf').then((data) => {
  27. const annotations = parseAnnotationFromXml(data as string);
  28. addAnnots(annotations);
  29. });
  30. };
  31. return (
  32. <Head>
  33. <IconWrapper>
  34. <Icon glyph="right-back" onClick={close} />
  35. </IconWrapper>
  36. <Separator />
  37. <IconWrapper>
  38. <Icon glyph="sort" />
  39. </IconWrapper>
  40. <IconWrapper onClick={handleExport}>
  41. <Icon glyph="annotation-export" isDisabled={!hasAnnots} />
  42. </IconWrapper>
  43. <IconWrapper onClick={handleImport}>
  44. <Icon glyph="import" />
  45. </IconWrapper>
  46. </Head>
  47. );
  48. };
  49. export default index;