2019-10-20 18:02:52 +02:00
|
|
|
import React from "react";
|
2018-11-26 15:20:44 +01:00
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
import SourcecodeViewer from "../components/content/SourcecodeViewer";
|
|
|
|
|
import ImageViewer from "../components/content/ImageViewer";
|
|
|
|
|
import DownloadViewer from "../components/content/DownloadViewer";
|
|
|
|
|
import { ExtensionPoint } from "@scm-manager/ui-extensions";
|
|
|
|
|
import { getContentType } from "./contentType";
|
|
|
|
|
import { File, Repository } from "@scm-manager/ui-types";
|
|
|
|
|
import { ErrorNotification, Loading } from "@scm-manager/ui-components";
|
2018-11-26 15:20:44 +01:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
repository: Repository;
|
|
|
|
|
file: File;
|
|
|
|
|
revision: string;
|
|
|
|
|
path: string;
|
2018-11-26 15:20:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type State = {
|
2019-10-19 16:38:07 +02:00
|
|
|
contentType: string;
|
|
|
|
|
language: string;
|
|
|
|
|
loaded: boolean;
|
|
|
|
|
error?: Error;
|
2018-11-26 15:20:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class SourcesView extends React.Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
2019-10-20 18:02:52 +02:00
|
|
|
contentType: "",
|
|
|
|
|
language: "",
|
|
|
|
|
loaded: false
|
2018-11-26 15:20:44 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
const { file } = this.props;
|
|
|
|
|
getContentType(file._links.self.href)
|
|
|
|
|
.then(result => {
|
2020-01-15 08:25:59 +01:00
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
contentType: result.type,
|
|
|
|
|
language: result.language,
|
|
|
|
|
loaded: true
|
|
|
|
|
});
|
2018-11-26 15:20:44 +01:00
|
|
|
})
|
2020-01-15 08:25:59 +01:00
|
|
|
.catch(error => {
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
error,
|
|
|
|
|
loaded: true
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-11-26 15:20:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showSources() {
|
|
|
|
|
const { file, revision } = this.props;
|
|
|
|
|
const { contentType, language } = this.state;
|
2019-10-20 18:02:52 +02:00
|
|
|
if (contentType.startsWith("image/")) {
|
2018-11-26 15:20:44 +01:00
|
|
|
return <ImageViewer file={file} />;
|
|
|
|
|
} else if (language) {
|
|
|
|
|
return <SourcecodeViewer file={file} language={language} />;
|
2019-10-20 18:02:52 +02:00
|
|
|
} else if (contentType.startsWith("text/")) {
|
2018-11-26 15:20:44 +01:00
|
|
|
return <SourcecodeViewer file={file} language="none" />;
|
|
|
|
|
} else {
|
|
|
|
|
return (
|
|
|
|
|
<ExtensionPoint
|
|
|
|
|
name="repos.sources.view"
|
2019-10-19 16:38:07 +02:00
|
|
|
props={{
|
|
|
|
|
file,
|
|
|
|
|
contentType,
|
2019-10-20 18:02:52 +02:00
|
|
|
revision
|
2019-10-19 16:38:07 +02:00
|
|
|
}}
|
2018-11-26 15:20:44 +01:00
|
|
|
>
|
|
|
|
|
<DownloadViewer file={file} />
|
|
|
|
|
</ExtensionPoint>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2019-02-01 10:12:45 +01:00
|
|
|
const { file } = this.props;
|
2018-11-26 15:20:44 +01:00
|
|
|
const { loaded, error } = this.state;
|
|
|
|
|
|
|
|
|
|
if (!file || !loaded) {
|
|
|
|
|
return <Loading />;
|
|
|
|
|
}
|
|
|
|
|
if (error) {
|
|
|
|
|
return <ErrorNotification error={error} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sources = this.showSources();
|
|
|
|
|
|
2019-02-01 10:12:45 +01:00
|
|
|
return <div className="panel-block">{sources}</div>;
|
2018-11-26 15:20:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-01 10:12:45 +01:00
|
|
|
export default SourcesView;
|