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

48 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-01-15 20:29:18 +01:00
import React, { FC, useEffect, useState } from "react";
import { getContent } from "./SourcecodeViewer";
import { Link, File } from "@scm-manager/ui-types";
2020-01-16 10:00:44 +01:00
import { Loading, ErrorNotification, MarkdownView } from "@scm-manager/ui-components";
import styled from "styled-components";
2020-01-15 20:29:18 +01:00
type Props = {
file: File;
};
2020-01-16 10:00:44 +01:00
const MarkdownContent = styled.div`
padding: 0.5rem;
`;
2020-01-15 20:29:18 +01:00
const MarkdownViewer: FC<Props> = ({ file }) => {
2020-01-16 10:00:44 +01:00
const [loading, setLoading] = useState(true);
2020-01-15 20:29:18 +01:00
const [error, setError] = useState<Error | undefined>(undefined);
2020-01-16 10:00:44 +01:00
const [content, setContent] = useState("");
2020-01-15 20:29:18 +01:00
useEffect(() => {
getContent((file._links.self as Link).href)
.then(content => {
setLoading(false);
setContent(content);
})
.catch(error => {
setLoading(false);
setError(error);
});
}, [file]);
if (loading) {
return <Loading />;
}
if (error) {
return <ErrorNotification error={error} />;
}
2020-01-16 10:00:44 +01:00
return (
<MarkdownContent>
<MarkdownView content={content} />
</MarkdownContent>
);
2020-01-15 20:29:18 +01:00
};
export default MarkdownViewer;