2018-10-15 16:45:54 +02:00
|
|
|
// @flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
import { translate } from "react-i18next";
|
|
|
|
|
import { apiClient } from "@scm-manager/ui-components";
|
2018-10-25 13:45:52 +02:00
|
|
|
import { getSources } from "../../sources/modules/sources";
|
|
|
|
|
import type { Repository, File } from "@scm-manager/ui-types";
|
|
|
|
|
import { ErrorNotification, Loading } from "@scm-manager/ui-components";
|
|
|
|
|
import { connect } from "react-redux";
|
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 = {
|
|
|
|
|
contentType: string
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Content extends React.Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
contentType: ""
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 13:45:52 +02:00
|
|
|
componentDidMount() {
|
|
|
|
|
const { file } = this.props;
|
|
|
|
|
getContentType(file._links.self.href).then(result => {
|
|
|
|
|
this.setState({
|
|
|
|
|
contentType: result
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-10-15 16:45:54 +02:00
|
|
|
|
|
|
|
|
render() {
|
2018-10-25 13:45:52 +02:00
|
|
|
const { file } = this.props;
|
2018-10-25 13:59:13 +02:00
|
|
|
const contentType = this.state.contentType;
|
2018-10-25 13:45:52 +02:00
|
|
|
if (!file) {
|
|
|
|
|
return <Loading />;
|
|
|
|
|
}
|
2018-10-25 13:59:13 +02:00
|
|
|
|
2018-10-25 13:45:52 +02:00
|
|
|
return this.state.contentType;
|
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 => {
|
|
|
|
|
return response.headers.get("Content-Type");
|
|
|
|
|
})
|
2018-10-15 16:45:54 +02:00
|
|
|
.catch(err => {
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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));
|