2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
2020-10-14 22:48:10 +02:00
|
|
|
import React, { FC, useEffect, useState } from "react";
|
2020-01-08 15:57:13 +01:00
|
|
|
import { apiClient, ErrorNotification, Loading, SyntaxHighlighter } from "@scm-manager/ui-components";
|
2020-01-15 10:49:01 +01:00
|
|
|
import { File, Link } from "@scm-manager/ui-types";
|
2020-12-14 09:15:18 +01:00
|
|
|
import { useLocation } from "react-router-dom";
|
2018-10-15 16:45:54 +02:00
|
|
|
|
2020-10-14 22:48:10 +02:00
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
file: File;
|
|
|
|
|
language: string;
|
2018-10-15 16:45:54 +02:00
|
|
|
};
|
|
|
|
|
|
2020-10-14 22:48:10 +02:00
|
|
|
const SourcecodeViewer: FC<Props> = ({ file, language }) => {
|
|
|
|
|
const [content, setContent] = useState("");
|
|
|
|
|
const [error, setError] = useState<Error | undefined>(undefined);
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
const [currentFileRevision, setCurrentFileRevision] = useState("");
|
2020-12-14 09:15:18 +01:00
|
|
|
const location = useLocation();
|
2020-10-14 22:48:10 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-10-20 10:18:44 +02:00
|
|
|
if (file.revision !== currentFileRevision) {
|
2020-10-14 22:48:10 +02:00
|
|
|
getContent((file._links.self as Link).href)
|
|
|
|
|
.then(content => {
|
|
|
|
|
setContent(content);
|
|
|
|
|
setCurrentFileRevision(file.revision);
|
|
|
|
|
setLoading(false);
|
2020-10-20 10:18:44 +02:00
|
|
|
})
|
|
|
|
|
.catch(setError);
|
2020-10-14 22:48:10 +02:00
|
|
|
}
|
2020-10-20 10:18:44 +02:00
|
|
|
}, [currentFileRevision, file]);
|
2018-10-15 16:45:54 +02:00
|
|
|
|
2020-10-20 10:18:44 +02:00
|
|
|
if (error) {
|
|
|
|
|
return <ErrorNotification error={error} />;
|
|
|
|
|
}
|
2018-10-15 16:45:54 +02:00
|
|
|
|
2020-10-14 22:48:10 +02:00
|
|
|
if (loading) {
|
|
|
|
|
return <Loading />;
|
2020-01-15 08:01:12 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-14 22:48:10 +02:00
|
|
|
if (!content) {
|
|
|
|
|
return null;
|
2020-01-15 08:01:12 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-14 09:15:18 +01:00
|
|
|
const permalink = replaceBranchWithRevision(location.pathname, currentFileRevision);
|
|
|
|
|
|
|
|
|
|
return <SyntaxHighlighter language={getLanguage(language)} value={content} permalink={permalink} />;
|
2020-10-14 22:48:10 +02:00
|
|
|
};
|
2018-10-15 16:45:54 +02:00
|
|
|
|
2020-12-14 09:15:18 +01:00
|
|
|
export function replaceBranchWithRevision(path: string, revision: string) {
|
|
|
|
|
const pathParts = path.split("/");
|
|
|
|
|
pathParts[6] = revision; // The branch is at the 7th position in the url
|
|
|
|
|
return pathParts.join("/");
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2020-01-15 08:01:12 +01:00
|
|
|
return apiClient.get(url).then(response => response.text());
|
2018-10-15 16:45:54 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-14 22:48:10 +02:00
|
|
|
export default SourcecodeViewer;
|