LoadData.jsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import React from "react";
  2. import { useTranslation } from "react-i18next";
  3. import { useDropzone } from "react-dropzone";
  4. const LoadGame = ({ onLoad = () => {} }) => {
  5. const { t } = useTranslation();
  6. const onDrop = React.useCallback(
  7. (acceptedFiles) => {
  8. acceptedFiles.forEach((file) => {
  9. const reader = new FileReader();
  10. reader.onabort = () => console.log("file reading was aborted");
  11. reader.onerror = () => console.log("file reading has failed");
  12. reader.onload = () => {
  13. try {
  14. const result = JSON.parse(reader.result);
  15. onLoad(result);
  16. } catch (e) {
  17. console.log("File parsing failed", e);
  18. }
  19. };
  20. reader.readAsText(file);
  21. });
  22. },
  23. [onLoad]
  24. );
  25. const { getRootProps, getInputProps } = useDropzone({ onDrop });
  26. return (
  27. <div
  28. {...getRootProps()}
  29. style={{
  30. border: "1px dashed white",
  31. padding: "0.5em",
  32. cursor: "pointer",
  33. fontSize: "1.5em",
  34. }}
  35. >
  36. <input {...getInputProps()} />
  37. <p style={{ textAlign: "center", margin: "1em" }}>
  38. {t("Dragn drop file here")}
  39. </p>
  40. </div>
  41. );
  42. };
  43. export default LoadGame;