scm-ui: new repository layout

This commit is contained in:
Sebastian Sdorra
2019-10-07 10:57:09 +02:00
parent 09c7def874
commit c05798e254
417 changed files with 3620 additions and 52971 deletions

View File

@@ -0,0 +1,222 @@
// @flow
import React from "react";
import { translate } from "react-i18next";
import type { File, Repository } from "@scm-manager/ui-types";
import {
DateFromNow,
ButtonGroup,
FileSize,
ErrorNotification
} from "@scm-manager/ui-components";
import injectSheet from "react-jss";
import classNames from "classnames";
import FileButtonGroup from "../components/content/FileButtonGroup";
import SourcesView from "./SourcesView";
import HistoryView from "./HistoryView";
import { getSources } from "../modules/sources";
import { connect } from "react-redux";
import { ExtensionPoint } from "@scm-manager/ui-extensions";
type Props = {
loading: boolean,
file: File,
repository: Repository,
revision: string,
path: string,
classes: any,
t: string => string
};
type State = {
collapsed: boolean,
showHistory: boolean,
errorFromExtension?: Error
};
const styles = {
pointer: {
cursor: "pointer"
},
marginInHeader: {
marginRight: "0.5em"
},
isVerticalCenter: {
display: "flex",
alignItems: "center"
},
hasBackground: {
backgroundColor: "#FBFBFB"
}
};
class Content extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
collapsed: true,
showHistory: false
};
}
toggleCollapse = () => {
this.setState(prevState => ({
collapsed: !prevState.collapsed
}));
};
setShowHistoryState(showHistory: boolean) {
this.setState({
...this.state,
showHistory
});
}
handleExtensionError = (error: Error) => {
this.setState({ errorFromExtension: error });
};
showHeader() {
const { file, revision, classes } = this.props;
const { showHistory, collapsed } = this.state;
const icon = collapsed ? "fa-angle-right" : "fa-angle-down";
const selector = file._links.history ? (
<FileButtonGroup
file={file}
historyIsSelected={showHistory}
showHistory={(changeShowHistory: boolean) =>
this.setShowHistoryState(changeShowHistory)
}
/>
) : null;
return (
<span className={classes.pointer}>
<article className={classNames("media", classes.isVerticalCenter)}>
<div className="media-content" onClick={this.toggleCollapse}>
<i
className={classNames(
"fa is-medium",
icon,
classes.marginInHeader
)}
/>
<span className="is-word-break">{file.name}</span>
</div>
<div className="buttons is-grouped">
<div className={classes.marginInHeader}>{selector}</div>
<ExtensionPoint
name="repos.sources.content.actionbar"
props={{
file,
revision,
handleExtensionError: this.handleExtensionError
}}
renderAll={true}
/>
</div>
</article>
</span>
);
}
showMoreInformation() {
const collapsed = this.state.collapsed;
const { classes, file, revision, t, repository } = this.props;
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;
const fileSize = file.directory ? "" : <FileSize bytes={file.length} />;
if (!collapsed) {
return (
<div className={classNames("panel-block", classes.hasBackground)}>
<table className={classNames("table", classes.hasBackground)}>
<tbody>
<tr>
<td>{t("sources.content.path")}</td>
<td className="is-word-break">{file.path}</td>
</tr>
<tr>
<td>{t("sources.content.branch")}</td>
<td className="is-word-break">{revision}</td>
</tr>
<tr>
<td>{t("sources.content.size")}</td>
<td>{fileSize}</td>
</tr>
<tr>
<td>{t("sources.content.lastModified")}</td>
<td>{date}</td>
</tr>
<tr>
<td>{t("sources.content.description")}</td>
<td className="is-word-break">{description}</td>
</tr>
<ExtensionPoint
name="repos.content.metadata"
renderAll={true}
props={{ file, repository, revision }}
/>
</tbody>
</table>
</div>
);
}
return null;
}
render() {
const { file, revision, repository, path } = this.props;
const { showHistory, errorFromExtension } = this.state;
const header = this.showHeader();
const content =
showHistory && file._links.history ? (
<HistoryView file={file} repository={repository} />
) : (
<SourcesView
revision={revision}
file={file}
repository={repository}
path={path}
/>
);
const moreInformation = this.showMoreInformation();
return (
<div>
<div className="panel">
<div className="panel-heading">{header}</div>
{moreInformation}
{content}
</div>
{errorFromExtension && <ErrorNotification error={errorFromExtension} />}
</div>
);
}
}
const mapStateToProps = (state: any, ownProps: Props) => {
const { repository, revision, path } = ownProps;
const file = getSources(state, repository, revision, path);
return {
file
};
};
export default injectSheet(styles)(
connect(mapStateToProps)(translate("repos")(Content))
);

View File

@@ -0,0 +1,113 @@
// @flow
import React from "react";
import type {
File,
Changeset,
Repository,
PagedCollection
} from "@scm-manager/ui-types";
import {
ErrorNotification,
Loading,
StatePaginator,
ChangesetList
} from "@scm-manager/ui-components";
import { getHistory } from "./history";
type Props = {
file: File,
repository: Repository
};
type State = {
loaded: boolean,
changesets: Changeset[],
page: number,
pageCollection?: PagedCollection,
error?: Error
};
class HistoryView extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
loaded: false,
page: 1,
changesets: []
};
}
componentDidMount() {
const { file } = this.props;
this.updateHistory(file._links.history.href);
}
updateHistory(link: string) {
getHistory(link)
.then(result => {
if (result.error) {
this.setState({
...this.state,
error: result.error,
loaded: true
});
} else {
this.setState({
...this.state,
loaded: true,
changesets: result.changesets,
pageCollection: result.pageCollection,
page: result.pageCollection.page
});
}
})
.catch(err => {});
}
updatePage(page: number) {
const { file } = this.props;
const internalPage = page - 1;
this.updateHistory(
file._links.history.href + "?page=" + internalPage.toString()
);
}
showHistory() {
const { repository } = this.props;
const { changesets, page, pageCollection } = this.state;
const currentPage = page + 1;
return (
<>
<div className="panel-block">
<ChangesetList repository={repository} changesets={changesets} />
</div>
<div className="panel-footer">
<StatePaginator
page={currentPage}
collection={pageCollection}
updatePage={(newPage: number) => this.updatePage(newPage)}
/>
</div>
</>
);
}
render() {
const { file } = this.props;
const { loaded, error } = this.state;
if (!file || !loaded) {
return <Loading />;
}
if (error) {
return <ErrorNotification error={error} />;
}
const history = this.showHistory();
return <>{history}</>;
}
}
export default HistoryView;

View File

@@ -0,0 +1,228 @@
// @flow
import React from "react";
import { connect } from "react-redux";
import { withRouter } from "react-router-dom";
import type { Branch, Repository } from "@scm-manager/ui-types";
import FileTree from "../components/FileTree";
import {
BranchSelector,
Breadcrumb,
ErrorNotification,
Loading
} from "@scm-manager/ui-components";
import { translate } from "react-i18next";
import {
fetchBranches,
getBranches,
getFetchBranchesFailure,
isFetchBranchesPending
} from "../../branches/modules/branches";
import { compose } from "redux";
import Content from "./Content";
import { fetchSources, isDirectory } from "../modules/sources";
type Props = {
repository: Repository,
loading: boolean,
error: Error,
baseUrl: string,
branches: Branch[],
revision: string,
path: string,
currentFileIsDirectory: boolean,
// dispatch props
fetchBranches: Repository => void,
fetchSources: (Repository, string, string) => void,
// Context props
history: any,
match: any,
location: any,
t: string => string
};
type State = {
selectedBranch: any
};
class Sources extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
selectedBranch: null
};
}
componentDidMount() {
const {
fetchBranches,
repository,
revision,
path,
fetchSources
} = this.props;
fetchBranches(repository);
fetchSources(repository, revision, path);
this.redirectToDefaultBranch();
}
componentDidUpdate(prevProps) {
const { fetchSources, repository, revision, path } = this.props;
if (prevProps.revision !== revision || prevProps.path !== path) {
fetchSources(repository, revision, path);
}
this.redirectToDefaultBranch();
}
redirectToDefaultBranch = () => {
const { branches } = this.props;
if (this.shouldRedirectToDefaultBranch()) {
const defaultBranches = branches.filter(b => b.defaultBranch);
if (defaultBranches.length > 0) {
this.branchSelected(defaultBranches[0]);
}
}
};
shouldRedirectToDefaultBranch = () => {
const { branches, revision } = this.props;
return branches && !revision;
};
branchSelected = (branch?: Branch) => {
const { baseUrl, history, path } = this.props;
let url;
if (branch) {
this.setState({ selectedBranch: branch });
if (path) {
url = `${baseUrl}/${encodeURIComponent(branch.name)}/${path}`;
} else {
url = `${baseUrl}/${encodeURIComponent(branch.name)}/`;
}
} else {
this.setState({ selectedBranch: null });
url = `${baseUrl}/`;
}
history.push(url);
};
render() {
const {
repository,
baseUrl,
branches,
loading,
error,
revision,
path,
currentFileIsDirectory
} = this.props;
const { selectedBranch } = this.state;
if (error) {
return <ErrorNotification error={error} />;
}
if (loading) {
return <Loading />;
}
if (currentFileIsDirectory) {
return (
<div className="panel">
{this.renderBranchSelector()}
<Breadcrumb
revision={encodeURIComponent(revision)}
path={path}
baseUrl={baseUrl}
branch={selectedBranch}
defaultBranch={
branches && branches.filter(b => b.defaultBranch === true)[0]
}
branches={branches && branches}
repository={repository}
/>
<FileTree
repository={repository}
revision={revision}
path={path}
baseUrl={baseUrl}
/>
</div>
);
} else {
return (
<Content repository={repository} revision={revision} path={path} />
);
}
}
renderBranchSelector = () => {
const { branches, revision, t } = this.props;
if (branches) {
return (
<div className="panel-heading">
<BranchSelector
branches={branches}
selectedBranch={revision}
label={t("changesets.branchSelectorLabel")}
selected={(b: Branch) => {
this.branchSelected(b);
}}
/>
</div>
);
}
return null;
};
}
const mapStateToProps = (state, ownProps) => {
const { repository, match } = ownProps;
const { revision, path } = match.params;
const decodedRevision = revision ? decodeURIComponent(revision) : undefined;
const loading = isFetchBranchesPending(state, repository);
const error = getFetchBranchesFailure(state, repository);
const branches = getBranches(state, repository);
const currentFileIsDirectory = decodedRevision
? isDirectory(state, repository, decodedRevision, path)
: isDirectory(state, repository, revision, path);
return {
repository,
revision: decodedRevision,
path,
loading,
error,
branches,
currentFileIsDirectory
};
};
const mapDispatchToProps = dispatch => {
return {
fetchBranches: (repository: Repository) => {
dispatch(fetchBranches(repository));
},
fetchSources: (repository: Repository, revision: string, path: string) => {
dispatch(fetchSources(repository, revision, path));
}
};
};
export default compose(
translate("repos"),
withRouter,
connect(
mapStateToProps,
mapDispatchToProps
)
)(Sources);

View File

@@ -0,0 +1,97 @@
// @flow
import React from "react";
import SourcecodeViewer from "../components/content/SourcecodeViewer";
import ImageViewer from "../components/content/ImageViewer";
import DownloadViewer from "../components/content/DownloadViewer";
import { ExtensionPoint } from "@scm-manager/ui-extensions";
import { getContentType } from "./contentType";
import type { File, Repository } from "@scm-manager/ui-types";
import { ErrorNotification, Loading } from "@scm-manager/ui-components";
type Props = {
repository: Repository,
file: File,
revision: string,
path: string
};
type State = {
contentType: string,
language: string,
loaded: boolean,
error?: Error
};
class SourcesView extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
contentType: "",
language: "",
loaded: false
};
}
componentDidMount() {
const { file } = this.props;
getContentType(file._links.self.href)
.then(result => {
if (result.error) {
this.setState({
...this.state,
error: result.error,
loaded: true
});
} else {
this.setState({
...this.state,
contentType: result.type,
language: result.language,
loaded: true
});
}
})
.catch(err => {});
}
showSources() {
const { file, revision } = this.props;
const { contentType, language } = this.state;
if (contentType.startsWith("image/")) {
return <ImageViewer file={file} />;
} else if (language) {
return <SourcecodeViewer file={file} language={language} />;
} else if (contentType.startsWith("text/")) {
return <SourcecodeViewer file={file} language="none" />;
} else {
return (
<ExtensionPoint
name="repos.sources.view"
props={{ file, contentType, revision }}
>
<DownloadViewer file={file} />
</ExtensionPoint>
);
}
}
render() {
const { file } = this.props;
const { loaded, error } = this.state;
if (!file || !loaded) {
return <Loading />;
}
if (error) {
return <ErrorNotification error={error} />;
}
const sources = this.showSources();
return <div className="panel-block">{sources}</div>;
}
}
export default SourcesView;

View File

@@ -0,0 +1,16 @@
//@flow
import { apiClient } from "@scm-manager/ui-components";
export function getContentType(url: string) {
return apiClient
.head(url)
.then(response => {
return {
type: response.headers.get("Content-Type"),
language: response.headers.get("X-Programming-Language")
};
})
.catch(err => {
return { error: err };
});
}

View File

@@ -0,0 +1,29 @@
//@flow
import fetchMock from "fetch-mock";
import { getContentType } from "./contentType";
describe("get content type", () => {
const CONTENT_URL = "/repositories/scmadmin/TestRepo/content/testContent";
afterEach(() => {
fetchMock.reset();
fetchMock.restore();
});
it("should return content", done => {
let headers = {
"Content-Type": "application/text",
"X-Programming-Language": "JAVA"
};
fetchMock.head("/api/v2" + CONTENT_URL, {
headers
});
getContentType(CONTENT_URL).then(content => {
expect(content.type).toBe("application/text");
expect(content.language).toBe("JAVA");
done();
});
});
});

View File

@@ -0,0 +1,22 @@
//@flow
import { apiClient } from "@scm-manager/ui-components";
export function getHistory(url: string) {
return apiClient
.get(url)
.then(response => response.json())
.then(result => {
return {
changesets: result._embedded.changesets,
pageCollection: {
_embedded: result._embedded,
_links: result._links,
page: result.page,
pageTotal: result.pageTotal
}
};
})
.catch(err => {
return { error: err };
});
}

View File

@@ -0,0 +1,53 @@
//@flow
import fetchMock from "fetch-mock";
import { getHistory } from "./history";
describe("get content type", () => {
const FILE_URL = "/repositories/scmadmin/TestRepo/history/file";
afterEach(() => {
fetchMock.reset();
fetchMock.restore();
});
const history = {
page: 0,
pageTotal: 10,
_links: {
self: {
href: "/repositories/scmadmin/TestRepo/history/file?page=0&pageSize=10"
},
first: {
href: "/repositories/scmadmin/TestRepo/history/file?page=0&pageSize=10"
},
next: {
href: "/repositories/scmadmin/TestRepo/history/file?page=1&pageSize=10"
},
last: {
href: "/repositories/scmadmin/TestRepo/history/file?page=9&pageSize=10"
}
},
_embedded: {
changesets: [
{
id: "1234"
},
{
id: "2345"
}
]
}
};
it("should return history", done => {
fetchMock.get("/api/v2" + FILE_URL, history);
getHistory(FILE_URL).then(content => {
expect(content.changesets).toEqual(history._embedded.changesets);
expect(content.pageCollection.page).toEqual(history.page);
expect(content.pageCollection.pageTotal).toEqual(history.pageTotal);
expect(content.pageCollection._links).toEqual(history._links);
done();
});
});
});