2018-10-15 16:45:54 +02:00
|
|
|
// @flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
import { translate } from "react-i18next";
|
2018-10-29 11:50:55 +01:00
|
|
|
import { apiClient } from "@scm-manager/ui-components";
|
|
|
|
|
import type { File } from "@scm-manager/ui-types";
|
|
|
|
|
import { ErrorNotification, Loading } from "@scm-manager/ui-components";
|
|
|
|
|
import SyntaxHighlighter from "react-syntax-highlighter";
|
|
|
|
|
import { arduinoLight } from "react-syntax-highlighter/styles/hljs";
|
2018-10-15 16:45:54 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2018-10-29 11:50:55 +01:00
|
|
|
t: string => string,
|
|
|
|
|
file: File,
|
2018-10-29 15:57:31 +01:00
|
|
|
language: string
|
2018-10-15 16:45:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type State = {
|
2018-10-29 11:50:55 +01:00
|
|
|
content: string,
|
|
|
|
|
error: Error,
|
|
|
|
|
hasError: boolean,
|
|
|
|
|
loaded: boolean
|
2018-10-15 16:45:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class SourcecodeViewer extends React.Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
2018-10-29 11:50:55 +01:00
|
|
|
content: "",
|
|
|
|
|
error: new Error(),
|
|
|
|
|
hasError: false,
|
|
|
|
|
loaded: false
|
2018-10-15 16:45:54 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 11:50:55 +01:00
|
|
|
componentDidMount() {
|
|
|
|
|
const { file } = this.props;
|
|
|
|
|
getContent(file._links.self.href)
|
|
|
|
|
.then(result => {
|
|
|
|
|
if (result.error) {
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
hasError: true,
|
|
|
|
|
error: result.error,
|
|
|
|
|
loaded: true
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
content: result,
|
|
|
|
|
loaded: true
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {});
|
|
|
|
|
}
|
2018-10-15 16:45:54 +02:00
|
|
|
|
|
|
|
|
render() {
|
2018-10-29 11:50:55 +01:00
|
|
|
const content = this.state.content;
|
|
|
|
|
const error = this.state.error;
|
|
|
|
|
const hasError = this.state.hasError;
|
|
|
|
|
const loaded = this.state.loaded;
|
2018-10-29 15:57:31 +01:00
|
|
|
const language = this.props.language;
|
2018-10-29 11:50:55 +01:00
|
|
|
|
|
|
|
|
if (hasError) {
|
|
|
|
|
return <ErrorNotification error={error} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!loaded) {
|
|
|
|
|
return <Loading />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!content) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<SyntaxHighlighter
|
|
|
|
|
showLineNumbers="true"
|
2018-10-29 14:53:24 +01:00
|
|
|
language={getLanguage(language)}
|
2018-10-29 11:50:55 +01:00
|
|
|
style={arduinoLight}
|
|
|
|
|
>
|
|
|
|
|
{content}
|
|
|
|
|
</SyntaxHighlighter>
|
|
|
|
|
);
|
2018-10-15 16:45:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 14:53:24 +01:00
|
|
|
export function getLanguage(language: string) {
|
|
|
|
|
return language.toLowerCase();
|
|
|
|
|
}
|
2018-10-29 13:19:16 +01:00
|
|
|
|
2018-10-15 16:45:54 +02:00
|
|
|
export function getContent(url: string) {
|
|
|
|
|
return apiClient
|
|
|
|
|
.get(url)
|
|
|
|
|
.then(response => response.text())
|
2018-10-29 14:53:24 +01:00
|
|
|
.then(response => {
|
|
|
|
|
return response;
|
2018-10-29 11:50:55 +01:00
|
|
|
})
|
2018-10-15 16:45:54 +02:00
|
|
|
.catch(err => {
|
2018-10-29 11:50:55 +01:00
|
|
|
return { error: err };
|
2018-10-15 16:45:54 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default translate("repos")(SourcecodeViewer);
|