Files
SCM-Manager/scm-ui/src/repos/sources/containers/Content.js

116 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-10-15 16:45:54 +02:00
// @flow
import React from "react";
import { translate } from "react-i18next";
2018-10-25 14:37:26 +02:00
import { apiClient } from "@scm-manager/ui-components";
import { getSources } from "../modules/sources";
import type {
Repository,
File
2018-10-25 14:37:26 +02:00
} from "@scm-manager/ui-types";
import {
ErrorNotification,
Loading
2018-10-25 14:37:26 +02:00
} from "@scm-manager/ui-components";
2018-10-25 13:45:52 +02:00
import { connect } from "react-redux";
import ImageViewer from "../components/content/ImageViewer";
import SourcecodeViewer from "../components/content/SourcecodeViewer";
import DownloadViewer from "../components/content/DownloadViewer";
2018-10-15 16:45:54 +02:00
type Props = {
2018-10-25 13:45:52 +02:00
t: string => string,
loading: boolean,
error: Error,
file: File,
repository: Repository,
revision: string,
path: string,
// context props
classes: any,
t: string => string,
match: any
2018-10-15 16:45:54 +02:00
};
type State = {
2018-10-25 14:24:53 +02:00
contentType: string,
error: Error,
hasError: boolean
2018-10-15 16:45:54 +02:00
};
class Content extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
2018-10-25 14:24:53 +02:00
contentType: "",
error: new Error(),
hasError: false
2018-10-15 16:45:54 +02:00
};
}
2018-10-25 13:45:52 +02:00
componentDidMount() {
const { file } = this.props;
2018-10-25 14:24:53 +02:00
getContentType(file._links.self.href)
.then(result => {
if (result.error) {
this.setState({
...this.state,
hasError: true,
error: result.error
});
} else {
this.setState({
...this.state,
contentType: result.type
});
}
})
.catch(err => {});
2018-10-25 13:45:52 +02:00
}
2018-10-15 16:45:54 +02:00
render() {
2018-10-25 15:37:40 +02:00
const { file, revision } = this.props;
const contentType = this.state.contentType;
2018-10-25 14:24:53 +02:00
const error = this.state.error;
const hasError = this.state.hasError;
2018-10-25 13:45:52 +02:00
if (!file) {
return <Loading />;
}
2018-10-25 14:24:53 +02:00
if (hasError) {
return <ErrorNotification error={error} />;
}
if (contentType.startsWith("image")) {
return <ImageViewer />;
}
if (contentType.startsWith("text")) {
return <SourcecodeViewer />;
}
2018-10-25 15:37:40 +02:00
return <DownloadViewer file={file} revision={revision}/>;
2018-10-15 16:45:54 +02:00
}
}
2018-10-25 13:45:52 +02:00
export function getContentType(url: string, state: any) {
2018-10-15 16:45:54 +02:00
return apiClient
.head(url)
2018-10-25 13:45:52 +02:00
.then(response => {
2018-10-25 14:24:53 +02:00
return { type: response.headers.get("Content-Type") };
2018-10-25 13:45:52 +02:00
})
2018-10-15 16:45:54 +02:00
.catch(err => {
2018-10-25 14:24:53 +02:00
return { error: err };
2018-10-15 16:45:54 +02:00
});
}
2018-10-25 13:45:52 +02:00
const mapStateToProps = (state: any, ownProps: Props) => {
const { repository, revision, path } = ownProps;
const file = getSources(state, repository, revision, path);
return {
file
};
};
export default connect(mapStateToProps)(translate("repos")(Content));