Files
SCM-Manager/scm-ui/ui-webapp/src/repos/containers/RepositoryRoot.tsx

238 lines
8.8 KiB
TypeScript
Raw Normal View History

import React from "react";
import { connect } from "react-redux";
import { Redirect, Route, Switch } from "react-router-dom";
import { WithTranslation, withTranslation } from "react-i18next";
import { History } from "history";
import { binder, ExtensionPoint } from "@scm-manager/ui-extensions";
import { Repository } from "@scm-manager/ui-types";
2019-10-21 10:57:56 +02:00
import { ErrorPage, Loading, Navigation, NavLink, Page, Section, SubNavigation } from "@scm-manager/ui-components";
import { fetchRepoByName, getFetchRepoFailure, getRepository, isFetchRepoPending } from "../modules/repos";
import RepositoryDetails from "../components/RepositoryDetails";
import EditRepo from "./EditRepo";
import BranchesOverview from "../branches/containers/BranchesOverview";
import CreateBranch from "../branches/containers/CreateBranch";
import Permissions from "../permissions/containers/Permissions";
import EditRepoNavLink from "../components/EditRepoNavLink";
import BranchRoot from "../branches/containers/BranchRoot";
import PermissionsNavLink from "../components/PermissionsNavLink";
import RepositoryNavLink from "../components/RepositoryNavLink";
import { getLinks, getRepositoriesLink } from "../../modules/indexResource";
import CodeOverview from "../codeSection/containers/CodeOverview";
import ChangesetView from "./ChangesetView";
import SourceExtensions from "../sources/containers/SourceExtensions";
type Props = WithTranslation & {
namespace: string;
name: string;
repository: Repository;
loading: boolean;
error: Error;
repoLink: string;
indexLinks: object;
// dispatch functions
fetchRepoByName: (link: string, namespace: string, name: string) => void;
// context props
history: History;
match: any;
};
class RepositoryRoot extends React.Component<Props> {
componentDidMount() {
2018-11-05 13:52:46 +01:00
const { fetchRepoByName, namespace, name, repoLink } = this.props;
fetchRepoByName(repoLink, namespace, name);
}
stripEndingSlash = (url: string) => {
if (url.endsWith("/")) {
2019-03-07 17:49:20 +01:00
return url.substring(0, url.length - 1);
}
return url;
};
matchedUrl = () => {
return this.stripEndingSlash(this.props.match.url);
};
matchesBranches = (route: any) => {
const url = this.matchedUrl();
const regex = new RegExp(`${url}/branch/.+/info`);
return route.location.pathname.match(regex);
};
matchesCode = (route: any) => {
2018-10-09 11:53:06 +02:00
const url = this.matchedUrl();
const regex = new RegExp(`${url}(/code)/.*`);
2018-10-09 11:53:06 +02:00
return route.location.pathname.match(regex);
};
getCodeLinkname = () => {
const { repository } = this.props;
if (repository?._links?.sources) {
return "sources";
}
if (repository?._links?.changesets) {
return "changesets";
}
return "";
};
evaluateDestinationForCodeLink = () => {
const { repository } = this.props;
let url = `${this.matchedUrl()}/code`;
if (repository?._links?.sources) {
return `${url}/sources/`;
}
return `${url}/changesets`;
};
render() {
const { loading, error, indexLinks, repository, t } = this.props;
if (error) {
2019-03-28 14:47:57 +01:00
return (
2019-10-21 10:57:56 +02:00
<ErrorPage title={t("repositoryRoot.errorTitle")} subtitle={t("repositoryRoot.errorSubtitle")} error={error} />
2019-03-28 14:47:57 +01:00
);
}
if (!repository || loading) {
return <Loading />;
}
const url = this.matchedUrl();
const extensionProps = {
repository,
url,
indexLinks
};
2019-10-21 10:57:56 +02:00
const redirectUrlFactory = binder.getExtension("repository.redirect", this.props);
2019-03-07 17:49:20 +01:00
let redirectedUrl;
2019-03-28 14:47:57 +01:00
if (redirectUrlFactory) {
redirectedUrl = url + redirectUrlFactory(this.props);
2019-03-28 14:47:57 +01:00
} else {
redirectedUrl = url + "/info";
}
return (
<Page title={repository.namespace + "/" + repository.name}>
<div className="columns">
2019-07-24 08:56:12 +02:00
<div className="column is-three-quarters">
<Switch>
2019-03-28 14:47:57 +01:00
<Redirect exact from={this.props.match.url} to={redirectedUrl} />
2020-01-08 15:17:07 +01:00
<Redirect from={`${url}/changeset/:id`} to={`${url}/code/changeset/:id`} />
<Redirect exact from={`${url}/sources`} to={`${url}/code/sources`} />
<Redirect from={`${url}/sources/:revision/:path*`} to={`${url}/code/sources/:revision/:path*`} />
<Redirect exact from={`${url}/changesets`} to={`${url}/code/changesets`} />
<Redirect from={`${url}/branch/:branch/changesets`} to={`${url}/code/changesets/:branch`} />
2019-10-21 10:57:56 +02:00
<Route path={`${url}/info`} exact component={() => <RepositoryDetails repository={repository} />} />
<Route path={`${url}/settings/general`} component={() => <EditRepo repository={repository} />} />
2018-10-17 15:54:25 +02:00
<Route
path={`${url}/settings/permissions`}
2018-10-18 15:56:51 +02:00
render={() => (
2019-10-21 10:57:56 +02:00
<Permissions namespace={this.props.repository.namespace} repoName={this.props.repository.name} />
2018-10-17 15:54:25 +02:00
)}
/>
<Route
exact
path={`${url}/code/changeset/:id`}
render={() => <ChangesetView repository={repository} />}
/>
<Route
path={`${url}/code/sourceext/:extension`}
exact={true}
render={() => <SourceExtensions repository={repository} />}
/>
<Route
path={`${url}/code/sourceext/:extension/:revision/:path*`}
render={() => <SourceExtensions repository={repository} baseUrl={`${url}/code/sources`} />}
/>
2018-10-18 08:50:49 +02:00
<Route
path={`${url}/code`}
render={() => <CodeOverview baseUrl={`${url}/code`} repository={repository} />}
/>
<Route
2019-04-04 11:15:34 +02:00
path={`${url}/branch/:branch`}
2019-10-21 10:57:56 +02:00
render={() => <BranchRoot repository={repository} baseUrl={`${url}/branch`} />}
/>
2019-03-28 11:51:00 +01:00
<Route
path={`${url}/branches`}
2019-03-28 14:47:57 +01:00
exact={true}
2019-10-21 10:57:56 +02:00
render={() => <BranchesOverview repository={repository} baseUrl={`${url}/branch`} />}
/>
2019-10-21 10:57:56 +02:00
<Route path={`${url}/branches/create`} render={() => <CreateBranch repository={repository} />} />
<ExtensionPoint name="repository.route" props={extensionProps} renderAll={true} />
</Switch>
</div>
<div className="column">
<Navigation>
<Section label={t("repositoryRoot.menu.navigationLabel")}>
2019-10-21 10:57:56 +02:00
<ExtensionPoint name="repository.navigation.topLevel" props={extensionProps} renderAll={true} />
<NavLink
to={`${url}/info`}
2019-01-23 17:01:12 +01:00
icon="fas fa-info-circle"
label={t("repositoryRoot.menu.informationNavLink")}
/>
2019-03-28 11:51:00 +01:00
<RepositoryNavLink
repository={repository}
linkName="branches"
to={`${url}/branches/`}
icon="fas fa-code-branch"
label={t("repositoryRoot.menu.branchesNavLink")}
activeWhenMatch={this.matchesBranches}
2019-03-28 11:51:00 +01:00
activeOnlyWhenExact={false}
/>
<RepositoryNavLink
repository={repository}
linkName={this.getCodeLinkname()}
to={this.evaluateDestinationForCodeLink()}
2018-12-21 13:53:59 +01:00
icon="fas fa-code"
label={t("repositoryRoot.menu.sourcesNavLink")}
activeWhenMatch={this.matchesCode}
activeOnlyWhenExact={false}
/>
2019-10-21 10:57:56 +02:00
<ExtensionPoint name="repository.navigation" props={extensionProps} renderAll={true} />
<SubNavigation to={`${url}/settings/general`} label={t("repositoryRoot.menu.settingsNavLink")}>
<EditRepoNavLink repository={repository} editUrl={`${url}/settings/general`} />
<PermissionsNavLink permissionUrl={`${url}/settings/permissions`} repository={repository} />
<ExtensionPoint name="repository.setting" props={extensionProps} renderAll={true} />
</SubNavigation>
2018-08-07 15:08:44 +02:00
</Section>
</Navigation>
</div>
</div>
</Page>
);
}
}
const mapStateToProps = (state: any, ownProps: Props) => {
const { namespace, name } = ownProps.match.params;
const repository = getRepository(state, namespace, name);
const loading = isFetchRepoPending(state, namespace, name);
const error = getFetchRepoFailure(state, namespace, name);
const repoLink = getRepositoriesLink(state);
const indexLinks = getLinks(state);
return {
namespace,
name,
repository,
loading,
error,
repoLink,
indexLinks
};
};
const mapDispatchToProps = (dispatch: any) => {
return {
2018-11-05 13:52:46 +01:00
fetchRepoByName: (link: string, namespace: string, name: string) => {
dispatch(fetchRepoByName(link, namespace, name));
}
};
};
export default connect(mapStateToProps, mapDispatchToProps)(withTranslation("repos")(RepositoryRoot));