garage-box/src/ObjectList.tsx

163 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-02-28 16:08:49 +00:00
import React from 'react';
import { Link, useParams } from 'react-router-dom';
import { S3Client, ListObjectsV2Command } from '@aws-sdk/client-s3';
2022-02-28 16:59:06 +00:00
import Alert from 'react-bootstrap/Alert';
import Breadcrumb from 'react-bootstrap/Breadcrumb';
import Card from 'react-bootstrap/Card';
import ListGroup from 'react-bootstrap/ListGroup';
import { LinkContainer } from 'react-router-bootstrap'
2022-02-28 16:08:49 +00:00
type Props = {
client: S3Client;
bucket: string;
prefix: string;
};
type State = {
2022-02-28 16:15:39 +00:00
loaded: boolean;
2022-02-28 16:08:49 +00:00
folders: string[];
files: string[];
};
2022-02-28 16:59:06 +00:00
var cache: { [path: string]: State; } = {};
2022-02-28 16:08:49 +00:00
class ObjectList extends React.Component<Props, State> {
state = {
2022-02-28 16:15:39 +00:00
loaded: false,
2022-02-28 16:08:49 +00:00
folders: [],
files: [],
};
constructor(props: Props) {
super(props);
}
async componentDidMount() {
console.log(this.props);
2022-02-28 16:59:06 +00:00
if (cache[this.path()]) {
this.setState(cache[this.path()]);
}
2022-02-28 16:08:49 +00:00
let command = new ListObjectsV2Command({
Bucket: this.props.bucket,
Prefix: this.props.prefix,
Delimiter: '/',
});
try {
2022-02-28 16:15:39 +00:00
const pxlen = this.props.prefix.length;
2022-02-28 16:08:49 +00:00
const data = await this.props.client.send(command);
console.log("ok", data);
2022-02-28 16:59:06 +00:00
const folders = (data.CommonPrefixes || []).map((cp) => cp.Prefix!.substring(pxlen));
const files = (data.Contents || []).map((obj) => obj.Key!.substring(pxlen));
folders.sort();
files.sort();
2022-02-28 16:08:49 +00:00
this.setState({
2022-02-28 16:15:39 +00:00
loaded: true,
2022-02-28 16:59:06 +00:00
folders: folders,
files: files,
2022-02-28 16:08:49 +00:00
});
2022-02-28 16:59:06 +00:00
cache[this.path()] = this.state;
2022-02-28 16:08:49 +00:00
} catch(error) {
console.log("err", error);
}
}
2022-02-28 16:59:06 +00:00
path() {
return this.props.bucket + "/" + this.props.prefix;
}
renderBreadcrumbs() {
2022-02-28 16:15:39 +00:00
let spl = this.props.prefix.split("/");
2022-02-28 16:59:06 +00:00
let items = [];
for (var i = 0; i < spl.length - 1; i++) {
if (i < spl.length - 2) {
items.push(
<LinkContainer to={ "/" + this.props.bucket + "/" + spl.slice(0, i+1).join("/") + "/" }>
<Breadcrumb.Item>{ spl[i] }</Breadcrumb.Item>
</LinkContainer>
);
} else {
items.push(
<Breadcrumb.Item active>{ spl[i] }</Breadcrumb.Item>
);
}
}
2022-02-28 16:15:39 +00:00
return (
2022-02-28 16:59:06 +00:00
<Breadcrumb>
<LinkContainer to="/">
<Breadcrumb.Item>my buckets</Breadcrumb.Item>
</LinkContainer>
{ this.props.prefix == "" ?
<Breadcrumb.Item active>{ this.props.bucket }</Breadcrumb.Item>
:
<LinkContainer to={ "/" + this.props.bucket + "/" }>
<Breadcrumb.Item>{ this.props.bucket }</Breadcrumb.Item>
</LinkContainer>
}
{ items }
</Breadcrumb>
2022-02-28 16:15:39 +00:00
);
}
2022-02-28 16:08:49 +00:00
render() {
2022-02-28 16:15:39 +00:00
if (!this.state.loaded) {
return (
2022-02-28 16:59:06 +00:00
<>
{ this.renderBreadcrumbs() }
<Alert variant="secondary">Loading...</Alert>
</>
2022-02-28 16:15:39 +00:00
);
}
2022-02-28 16:08:49 +00:00
return (
2022-02-28 16:59:06 +00:00
<>
{ this.renderBreadcrumbs() }
<ListGroup>
{ this.state.folders.map((f) =>
<LinkContainer to={ "/" + this.props.bucket + "/" + this.props.prefix + f }>
<ListGroup.Item key={f + "/"} action>
{ f }
</ListGroup.Item>
</LinkContainer>
)}
{ this.state.files.map((f) =>
<ListGroup.Item key={f}>
{ f }
</ListGroup.Item>
)}
</ListGroup>
</>
2022-02-28 16:08:49 +00:00
);
}
}
interface IClient {
client: S3Client;
}
export const ObjectList1 = ({ client }: IClient) => {
const params = useParams();
const bucket = params["bucket"]!;
return <>
<ObjectList client={client} bucket={bucket} prefix="" key={bucket} />
</>;
};
export const ObjectList2 = ({ client }: IClient) => {
const params = useParams();
const bucket = params["bucket"]!;
const prefix = params["*"] || "";
const key = bucket + "/" + prefix;
return <>
<ObjectList client={client} bucket={bucket} prefix={prefix} key={key} />
</>;
};