2018-10-15 16:45:54 +02:00
|
|
|
// @flow
|
|
|
|
|
import React from "react";
|
2018-10-29 11:50:55 +01:00
|
|
|
import { translate } from "react-i18next";
|
2018-10-25 14:37:26 +02:00
|
|
|
import { apiClient } from "@scm-manager/ui-components";
|
2018-10-25 14:03:45 +02:00
|
|
|
import { getSources } from "../modules/sources";
|
2018-10-29 09:32:30 +01:00
|
|
|
import type { Repository, File } from "@scm-manager/ui-types";
|
2018-10-25 14:03:45 +02:00
|
|
|
import {
|
|
|
|
|
ErrorNotification,
|
2018-10-29 09:32:30 +01:00
|
|
|
Loading,
|
|
|
|
|
DateFromNow
|
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";
|
2018-10-25 14:03:45 +02:00
|
|
|
import ImageViewer from "../components/content/ImageViewer";
|
|
|
|
|
import SourcecodeViewer from "../components/content/SourcecodeViewer";
|
|
|
|
|
import DownloadViewer from "../components/content/DownloadViewer";
|
2018-10-29 09:32:30 +01:00
|
|
|
import FileSize from "../components/FileSize";
|
2018-10-29 09:59:04 +01:00
|
|
|
import injectSheet from "react-jss";
|
2018-10-29 09:32:30 +01:00
|
|
|
import classNames from "classnames";
|
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,
|
2018-10-29 15:57:31 +01:00
|
|
|
language: string,
|
2018-10-25 14:24:53 +02:00
|
|
|
error: Error,
|
2018-10-29 15:57:31 +01:00
|
|
|
hasError: boolean,
|
2018-10-29 16:45:32 +01:00
|
|
|
loaded: boolean,
|
|
|
|
|
collapsed: boolean
|
2018-10-15 16:45:54 +02:00
|
|
|
};
|
|
|
|
|
|
2018-10-29 09:59:04 +01:00
|
|
|
const styles = {
|
|
|
|
|
toCenterContent: {
|
|
|
|
|
display: "block"
|
2018-10-29 16:45:32 +01:00
|
|
|
},
|
|
|
|
|
pointer: {
|
|
|
|
|
cursor: "pointer"
|
2018-10-29 09:59:04 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
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: "",
|
2018-10-29 15:57:31 +01:00
|
|
|
language: "",
|
2018-10-25 14:24:53 +02:00
|
|
|
error: new Error(),
|
2018-10-29 15:57:31 +01:00
|
|
|
hasError: false,
|
2018-10-29 16:45:32 +01:00
|
|
|
loaded: false,
|
|
|
|
|
collapsed: 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,
|
2018-10-29 15:57:31 +01:00
|
|
|
error: result.error,
|
|
|
|
|
loaded: true
|
2018-10-25 14:24:53 +02:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
2018-10-29 15:57:31 +01:00
|
|
|
contentType: result.type,
|
|
|
|
|
language: result.language,
|
|
|
|
|
loaded: true
|
2018-10-25 14:24:53 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {});
|
2018-10-25 13:45:52 +02:00
|
|
|
}
|
2018-10-15 16:45:54 +02:00
|
|
|
|
2018-10-29 16:45:32 +01:00
|
|
|
toggleCollapse = () => {
|
|
|
|
|
this.setState(prevState => ({
|
|
|
|
|
collapsed: !prevState.collapsed
|
|
|
|
|
}));
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-29 09:32:30 +01:00
|
|
|
showHeader() {
|
2018-10-29 16:45:32 +01:00
|
|
|
const { file, revision, classes } = this.props;
|
|
|
|
|
const collapsed = this.state.collapsed;
|
2018-10-29 09:32:30 +01:00
|
|
|
const date = <DateFromNow date={file.lastModified} />;
|
2018-10-29 13:56:16 +01:00
|
|
|
const description = file.description ? (
|
|
|
|
|
<p>
|
|
|
|
|
{file.description.split("\n").map((item, key) => {
|
|
|
|
|
return (
|
|
|
|
|
<span key={key}>
|
|
|
|
|
{item}
|
|
|
|
|
<br />
|
|
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</p>
|
|
|
|
|
) : null;
|
2018-10-29 14:11:37 +01:00
|
|
|
const branch = "[" + revision + "]";
|
2018-10-29 16:45:32 +01:00
|
|
|
const icon = collapsed ? "fa-angle-right" : "fa-angle-down";
|
2018-10-29 09:32:30 +01:00
|
|
|
|
|
|
|
|
return (
|
2018-10-29 16:45:32 +01:00
|
|
|
<span className={classes.pointer} onClick={this.toggleCollapse}>
|
2018-10-29 09:32:30 +01:00
|
|
|
<article className="media">
|
2018-10-29 16:45:32 +01:00
|
|
|
<div className="media-left">
|
|
|
|
|
<i className={classNames("fa", icon)} />
|
|
|
|
|
</div>
|
|
|
|
|
<div className="media-content">
|
|
|
|
|
<div className="content">{file.name}</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="media-right">{date} + Size</div>
|
2018-10-29 09:32:30 +01:00
|
|
|
</article>
|
2018-10-29 16:45:32 +01:00
|
|
|
</span>
|
2018-10-29 09:32:30 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 16:45:32 +01:00
|
|
|
showMoreInformation() {
|
|
|
|
|
const collapsed = this.state.collapsed;
|
|
|
|
|
const { classes } = this.props;
|
|
|
|
|
if (!collapsed) {
|
|
|
|
|
return (
|
|
|
|
|
<div className={classNames("panel-block", classes.toCenterContent)}>
|
|
|
|
|
"Filename": ... "Path": ... "Branch" ...
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 09:32:30 +01:00
|
|
|
showContent() {
|
2018-10-29 15:57:31 +01:00
|
|
|
const { file } = this.props;
|
2018-10-25 13:59:13 +02:00
|
|
|
const contentType = this.state.contentType;
|
2018-10-29 15:57:31 +01:00
|
|
|
const language = this.state.language;
|
|
|
|
|
if (contentType.startsWith("image/")) {
|
2018-10-29 10:46:16 +01:00
|
|
|
return <ImageViewer file={file} />;
|
2018-10-29 15:57:31 +01:00
|
|
|
} else if (language) {
|
2018-10-29 16:45:32 +01:00
|
|
|
return <SourcecodeViewer file={file} language={language} />;
|
2018-10-29 09:32:30 +01:00
|
|
|
} else {
|
2018-10-29 15:57:31 +01:00
|
|
|
return <DownloadViewer file={file} />;
|
2018-10-29 09:32:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2018-10-29 09:59:04 +01:00
|
|
|
const { file, classes } = this.props;
|
2018-10-25 14:24:53 +02:00
|
|
|
const error = this.state.error;
|
|
|
|
|
const hasError = this.state.hasError;
|
2018-10-29 15:57:31 +01:00
|
|
|
const loaded = this.state.loaded;
|
2018-10-25 14:24:53 +02:00
|
|
|
|
2018-10-29 15:57:31 +01:00
|
|
|
if (!file || !loaded) {
|
2018-10-25 13:45:52 +02:00
|
|
|
return <Loading />;
|
|
|
|
|
}
|
2018-10-25 14:24:53 +02:00
|
|
|
if (hasError) {
|
|
|
|
|
return <ErrorNotification error={error} />;
|
|
|
|
|
}
|
2018-10-25 14:03:45 +02:00
|
|
|
|
2018-10-29 09:32:30 +01:00
|
|
|
const header = this.showHeader();
|
|
|
|
|
const content = this.showContent();
|
2018-10-29 16:45:32 +01:00
|
|
|
const moreInformation = this.showMoreInformation();
|
2018-10-29 09:32:30 +01:00
|
|
|
const fileSize = file.directory ? "" : <FileSize bytes={file.length} />;
|
2018-10-25 13:59:13 +02:00
|
|
|
|
2018-10-29 09:32:30 +01:00
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<nav className="panel">
|
2018-10-29 16:45:32 +01:00
|
|
|
<article className="panel-heading">{header}</article>
|
|
|
|
|
{moreInformation}
|
2018-10-29 09:59:04 +01:00
|
|
|
<div className={classNames("panel-block", classes.toCenterContent)}>
|
|
|
|
|
{content}
|
|
|
|
|
</div>
|
2018-10-29 09:32:30 +01:00
|
|
|
</nav>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
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-29 15:57:31 +01:00
|
|
|
return {
|
|
|
|
|
type: response.headers.get("Content-Type"),
|
|
|
|
|
language: response.headers.get("X-Programming-Language")
|
|
|
|
|
};
|
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
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-29 09:59:04 +01:00
|
|
|
export default injectSheet(styles)(
|
|
|
|
|
connect(mapStateToProps)(translate("repos")(Content))
|
|
|
|
|
);
|