refactoring content view

This commit is contained in:
Maren Süwer
2018-10-29 09:32:30 +01:00
parent 3f1f404456
commit 463a93553d
2 changed files with 69 additions and 46 deletions

View File

@@ -2,7 +2,7 @@
import React from "react"; import React from "react";
import { translate } from "react-i18next"; import { translate } from "react-i18next";
import type { File } from "@scm-manager/ui-types"; import type { File } from "@scm-manager/ui-types";
import { DownloadButton, DateFromNow } from "@scm-manager/ui-components"; import { DownloadButton } from "@scm-manager/ui-components";
type Props = { type Props = {
t: string => string, t: string => string,
@@ -12,40 +12,14 @@ type Props = {
class DownloadViewer extends React.Component<Props> { class DownloadViewer extends React.Component<Props> {
render() { render() {
const { t, file, revision } = this.props; const { t, file } = this.props;
return ( return (
<div> <div>
<article className="media">
<div className="content media-left">
<h4>{file.name}</h4>
</div>
<div className="media-content" />
<div className="media-right">
<DownloadButton <DownloadButton
url={file._links.self.href} url={file._links.self.href}
displayName={t("sources.content.downloadButton")} displayName={t("sources.content.downloadButton")}
/> />
</div> </div>
</article>
<table className="table">
<tbody>
<tr>
<td>{t("sources.description")}</td>
<td>{file.description}</td>
</tr>
<tr>
<td>{t("sources.lastModified")}</td>
<td>
<DateFromNow date={file.lastModified} />
</td>
</tr>
<tr>
<td>{t("sources.branch")}</td>
<td>{revision}</td>
</tr>
</tbody>
</table>
</div>
); );
} }
} }

View File

@@ -1,20 +1,23 @@
// @flow // @flow
import React from "react"; import React from "react";
import { translate } from "react-i18next"; import { Interpolate, translate } from "react-i18next";
import { apiClient } from "@scm-manager/ui-components"; import { apiClient } from "@scm-manager/ui-components";
import { getSources } from "../modules/sources"; import { getSources } from "../modules/sources";
import type { import type { Repository, File } from "@scm-manager/ui-types";
Repository,
File
} from "@scm-manager/ui-types";
import { import {
ErrorNotification, ErrorNotification,
Loading Loading,
DateFromNow
} from "@scm-manager/ui-components"; } from "@scm-manager/ui-components";
import { connect } from "react-redux"; import { connect } from "react-redux";
import ImageViewer from "../components/content/ImageViewer"; import ImageViewer from "../components/content/ImageViewer";
import SourcecodeViewer from "../components/content/SourcecodeViewer"; import SourcecodeViewer from "../components/content/SourcecodeViewer";
import DownloadViewer from "../components/content/DownloadViewer"; import DownloadViewer from "../components/content/DownloadViewer";
import FileSize from "../components/FileSize";
import AvatarWrapper from "../../components/changesets/AvatarWrapper";
import classNames from "classnames";
import AvatarImage from "../../components/changesets/AvatarImage";
import ChangesetAuthor from "../../components/changesets/ChangesetAuthor";
type Props = { type Props = {
t: string => string, t: string => string,
@@ -67,9 +70,47 @@ class Content extends React.Component<Props, State> {
.catch(err => {}); .catch(err => {});
} }
render() { showHeader() {
const { file, revision, t } = this.props;
const date = <DateFromNow date={file.lastModified} />;
const fileSize = file.directory ? "" : <FileSize bytes={file.length} />;
return (
<div className="content">
<h4>{file.name}</h4>
<article className="media">
<div className="media-content">
<p>
{file.description.split("\n").map((item, key) => {
return (
<span key={key}>
{item}
<br />
</span>
);
})}
</p>
</div>
<div className="media-right">{date}</div>
</article>
</div>
);
}
showContent() {
const { file, revision } = this.props; const { file, revision } = this.props;
const contentType = this.state.contentType; const contentType = this.state.contentType;
if (contentType.startsWith("image")) {
return <ImageViewer />;
} else if (contentType.startsWith("text")) {
return <SourcecodeViewer />;
} else {
return <DownloadViewer file={file} revision={revision} />;
}
}
render() {
const { file } = this.props;
const error = this.state.error; const error = this.state.error;
const hasError = this.state.hasError; const hasError = this.state.hasError;
@@ -79,15 +120,23 @@ class Content extends React.Component<Props, State> {
if (hasError) { if (hasError) {
return <ErrorNotification error={error} />; return <ErrorNotification error={error} />;
} }
if (contentType.startsWith("image")) {
return <ImageViewer />;
}
if (contentType.startsWith("text")) { const header = this.showHeader();
return <SourcecodeViewer />; const content = this.showContent();
} const fileSize = file.directory ? "" : <FileSize bytes={file.length} />;
return <DownloadViewer file={file} revision={revision}/>; return (
<div>
{header}
<nav className="panel">
<article className="panel-heading media">
<div className="media-content">{file.name}</div>
<div className="media-right">{fileSize}</div>
</article>
<div className="panel-block">{content}</div>
</nav>
</div>
);
} }
} }