|
1 | | -import React from "react" |
| 1 | +import React, { useState, useEffect } from "react" |
| 2 | +import Box from "@material-ui/core/Box" |
| 3 | +import CircularProgress from "@material-ui/core/CircularProgress" |
2 | 4 | import SimpleDialog from "../SimpleDialog" |
| 5 | +import S3PathSelector from "../S3PathSelector" |
| 6 | +import getSampleFromUrl from "../PasteUrlsDialog/get-sample-from-url.js" |
| 7 | +// import { useAppConfig } from "../AppConfig" |
| 8 | +import useIAMS3API from "../../utils/auth-handlers/use-iam-s3-api" |
3 | 9 |
|
4 | | -export const ImportFromS3Dialog = ({ open, onClose }) => { |
| 10 | +export const ImportFromS3Dialog = ({ open, onClose, onAddSamples }) => { |
| 11 | + const { listBuckets, listBucketItemsAt } = useIAMS3API() |
| 12 | + const [s3Path, setS3Path] = useState("") |
| 13 | + const [{ options, optionsLoading }, setOptions] = useState({ |
| 14 | + optionsLoading: true, |
| 15 | + }) |
| 16 | + useEffect(() => { |
| 17 | + if (!open) return |
| 18 | + if (!listBuckets) return |
| 19 | + async function loadS3Path() { |
| 20 | + if (s3Path === "") { |
| 21 | + setOptions({ |
| 22 | + optionsLoading: false, |
| 23 | + options: ( |
| 24 | + await listBuckets() |
| 25 | + ).ListAllMyBucketsResult.Buckets.Bucket.map((b) => ({ |
| 26 | + name: b.Name, |
| 27 | + type: "bucket", |
| 28 | + })), |
| 29 | + }) |
| 30 | + } else { |
| 31 | + setOptions({ |
| 32 | + optionsLoading: false, |
| 33 | + options: ( |
| 34 | + (await listBucketItemsAt(s3Path)).ListBucketResult.CommonPrefixes || |
| 35 | + [] |
| 36 | + ).map((a) => ({ |
| 37 | + name: a.Prefix.split("/").slice(-2).join("/"), |
| 38 | + type: "directory", |
| 39 | + })), |
| 40 | + }) |
| 41 | + } |
| 42 | + } |
| 43 | + loadS3Path() |
| 44 | + // eslint-disable-next-line |
| 45 | + }, [s3Path, listBuckets, listBucketItemsAt, open]) |
| 46 | + if (!open) return null |
5 | 47 | return ( |
6 | | - <SimpleDialog onClose={onClose} open={open} title="Import from S3"> |
7 | | - This is a work in progress! Available soon!{" "} |
8 | | - <a href="https://github.com/UniversalDataTool/universal-data-tool/issues/205"> |
9 | | - Track the progress here. |
10 | | - </a> |
| 48 | + <SimpleDialog |
| 49 | + onClose={onClose} |
| 50 | + open={open} |
| 51 | + title="Import from S3" |
| 52 | + actions={[ |
| 53 | + { |
| 54 | + onClick: async () => { |
| 55 | + const bucket = s3Path.match(/s3:\/\/([^/]+)/)[1] |
| 56 | + |
| 57 | + // TODO support more than 1000 file import by using |
| 58 | + // continuation-token on S3 |
| 59 | + const items = ( |
| 60 | + await listBucketItemsAt(s3Path, { files: true }) |
| 61 | + ).ListBucketResult.Contents.filter( |
| 62 | + (item) => !item.Key.endsWith("/") |
| 63 | + ) |
| 64 | + .map((item) => `https://s3.amazonaws.com/${bucket}/${item.Key}`) |
| 65 | + .map((url) => getSampleFromUrl(url, { returnNulls: true })) |
| 66 | + .filter(Boolean) |
| 67 | + onAddSamples(items) |
| 68 | + }, |
| 69 | + disabled: !s3Path, |
| 70 | + text: "Import All Files in Directory", |
| 71 | + }, |
| 72 | + ]} |
| 73 | + > |
| 74 | + {optionsLoading ? ( |
| 75 | + <Box height={300} width={400} paddingTop={4} textAlign="center"> |
| 76 | + <CircularProgress size={100} /> |
| 77 | + </Box> |
| 78 | + ) : ( |
| 79 | + <S3PathSelector |
| 80 | + currentPath={s3Path} |
| 81 | + options={options || []} |
| 82 | + onChangePath={(newS3Path) => { |
| 83 | + setS3Path(newS3Path) |
| 84 | + setOptions({ optionsLoading: true }) |
| 85 | + }} |
| 86 | + /> |
| 87 | + )} |
11 | 88 | </SimpleDialog> |
12 | 89 | ) |
13 | 90 | } |
|
0 commit comments