Files
SCM-Manager/scm-ui/ui-webapp/src/repos/sources/components/content/SourcecodeViewer.tsx

75 lines
2.5 KiB
TypeScript
Raw Normal View History

/*
* 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";
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";
2018-10-15 16:45:54 +02:00
2020-10-14 22:48:10 +02:00
type Props = {
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("");
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-10-14 22:48:10 +02:00
if (!content) {
return null;
}
2020-10-14 22:48:10 +02:00
return <SyntaxHighlighter language={getLanguage(language)} value={content} />;
};
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-15 16:45:54 +02:00
export function getContent(url: string) {
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;