Files
SCM-Manager/scm-ui/src/repos/sources/containers/Content.js

203 lines
5.1 KiB
JavaScript
Raw Normal View History

2018-10-15 16:45:54 +02:00
// @flow
import React from "react";
2018-10-29 11:50:55 +01:00
import { translate } from "react-i18next";
2018-11-21 16:55:13 +01:00
import type { File, Repository } from "@scm-manager/ui-types";
import { DateFromNow } from "@scm-manager/ui-components";
2018-10-29 09:32:30 +01:00
import FileSize from "../components/FileSize";
2018-10-29 09:59:04 +01:00
import injectSheet from "react-jss";
2018-10-29 09:32:30 +01:00
import classNames from "classnames";
2019-01-29 14:02:36 +01:00
import FileButtonGroup from "../components/content/FileButtonGroup";
import SourcesView from "./SourcesView";
import HistoryView from "./HistoryView";
import { getSources } from "../modules/sources";
import { connect } from "react-redux";
2019-02-28 17:15:20 +01:00
import { ExtensionPoint } from "@scm-manager/ui-extensions";
2018-10-15 16:45:54 +02:00
type Props = {
2018-10-25 13:45:52 +02:00
loading: boolean,
error: Error,
file: File,
repository: Repository,
revision: string,
path: string,
classes: any,
2018-11-01 10:23:08 +01:00
t: string => string
2018-10-15 16:45:54 +02:00
};
type State = {
2018-11-01 10:33:35 +01:00
collapsed: boolean,
showHistory: boolean
2018-10-15 16:45:54 +02:00
};
2018-10-29 09:59:04 +01:00
const styles = {
2018-10-29 16:45:32 +01:00
pointer: {
cursor: "pointer"
},
marginInHeader: {
marginRight: "0.5em"
2018-11-28 10:33:06 +01:00
},
isVerticalCenter: {
display: "flex",
alignItems: "center"
},
hasBackground: {
backgroundColor: "#FBFBFB"
2018-10-29 09:59:04 +01:00
}
};
2018-10-15 16:45:54 +02:00
class Content extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
collapsed: true,
showHistory: false
2018-10-15 16:45:54 +02:00
};
}
2018-10-29 16:45:32 +01:00
toggleCollapse = () => {
this.setState(prevState => ({
collapsed: !prevState.collapsed
}));
};
setShowHistoryState(showHistory: boolean) {
this.setState({
...this.state,
showHistory
});
}
2018-10-29 09:32:30 +01:00
showHeader() {
2018-11-26 15:56:41 +01:00
const { file, classes } = this.props;
const { showHistory, collapsed } = this.state;
2018-10-29 16:45:32 +01:00
const icon = collapsed ? "fa-angle-right" : "fa-angle-down";
2018-10-29 09:32:30 +01:00
2018-11-28 09:28:45 +01:00
const selector = file._links.history ? (
2019-01-29 14:02:36 +01:00
<FileButtonGroup
2018-11-28 09:28:45 +01:00
file={file}
historyIsSelected={showHistory}
showHistory={(changeShowHistory: boolean) =>
this.setShowHistoryState(changeShowHistory)
}
/>
) : null;
2018-10-29 09:32:30 +01:00
return (
<span className={classes.pointer}>
2018-11-28 10:33:06 +01:00
<article className={classNames("media", classes.isVerticalCenter)}>
<div className="media-content" onClick={this.toggleCollapse}>
<i
className={classNames(
"fa is-medium",
icon,
classes.marginInHeader
)}
/>
2018-12-19 11:07:39 +01:00
<span className="is-word-break">{file.name}</span>
2018-10-29 16:45:32 +01:00
</div>
2018-11-28 09:28:45 +01:00
<div className="media-right">{selector}</div>
2018-10-29 09:32:30 +01:00
</article>
2018-10-29 16:45:32 +01:00
</span>
2018-10-29 09:32:30 +01:00
);
}
2018-10-29 16:45:32 +01:00
showMoreInformation() {
const collapsed = this.state.collapsed;
2019-02-25 16:24:05 +01:00
const { classes, file, revision, t, repository } = this.props;
2018-11-01 09:19:29 +01:00
const date = <DateFromNow date={file.lastModified} />;
const description = file.description ? (
<p>
{file.description.split("\n").map((item, key) => {
return (
<span key={key}>
{item}
<br />
</span>
);
})}
</p>
) : null;
2018-11-21 16:55:13 +01:00
const fileSize = file.directory ? "" : <FileSize bytes={file.length} />;
2018-10-29 16:45:32 +01:00
if (!collapsed) {
return (
2019-02-28 17:15:20 +01:00
<div className={classNames("panel-block", classes.hasBackground)}>
<table className={classNames("table", classes.hasBackground)}>
2018-11-01 09:19:29 +01:00
<tbody>
<tr>
2018-11-21 16:55:13 +01:00
<td>{t("sources.content.path")}</td>
2018-12-19 11:07:39 +01:00
<td className="is-word-break">{file.path}</td>
2018-11-01 09:19:29 +01:00
</tr>
<tr>
2018-11-21 16:55:13 +01:00
<td>{t("sources.content.branch")}</td>
2018-12-19 11:07:39 +01:00
<td className="is-word-break">{revision}</td>
2018-11-01 09:19:29 +01:00
</tr>
<tr>
2018-11-21 16:55:13 +01:00
<td>{t("sources.content.size")}</td>
<td>{fileSize}</td>
</tr>
<tr>
<td>{t("sources.content.lastModified")}</td>
2018-11-01 10:58:47 +01:00
<td>{date}</td>
2018-11-01 09:19:29 +01:00
</tr>
<tr>
2018-11-21 16:55:13 +01:00
<td>{t("sources.content.description")}</td>
2018-12-19 11:07:39 +01:00
<td className="is-word-break">{description}</td>
2018-11-01 09:19:29 +01:00
</tr>
2019-02-21 17:08:23 +01:00
<ExtensionPoint
name="repos.content.metadata"
2019-02-28 17:15:20 +01:00
renderAll={true}
props={{ file, repository, revision }}
/>
2018-11-01 09:19:29 +01:00
</tbody>
</table>
2018-10-29 16:45:32 +01:00
</div>
);
}
return null;
}
2018-10-29 09:32:30 +01:00
render() {
const { file, revision, repository, path } = this.props;
2018-11-26 15:56:41 +01:00
const { showHistory } = this.state;
2018-10-29 09:32:30 +01:00
const header = this.showHeader();
2018-11-28 09:28:45 +01:00
const content =
showHistory && file._links.history ? (
<HistoryView file={file} repository={repository} />
) : (
<SourcesView
revision={revision}
file={file}
repository={repository}
path={path}
/>
);
2018-10-29 16:45:32 +01:00
const moreInformation = this.showMoreInformation();
2018-10-29 09:32:30 +01:00
return (
<div>
2019-02-01 17:03:00 +01:00
<div className="panel">
<div className="panel-heading">{header}</div>
2018-10-29 16:45:32 +01:00
{moreInformation}
{content}
2019-02-01 17:03:00 +01:00
</div>
2018-10-29 09:32:30 +01:00
</div>
);
2018-10-15 16:45:54 +02:00
}
}
2018-10-25 13:45:52 +02:00
const mapStateToProps = (state: any, ownProps: Props) => {
const { repository, revision, path } = ownProps;
const file = getSources(state, repository, revision, path);
return {
file
};
};
2018-10-29 09:59:04 +01:00
export default injectSheet(styles)(
connect(mapStateToProps)(translate("repos")(Content))
);