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

387 lines
14 KiB
TypeScript
Raw Normal View History

/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import React from "react";
2020-08-12 12:53:59 +02:00
import { connect } from "react-redux";
import { Link, Redirect, Route, RouteComponentProps, Switch } from "react-router-dom";
2020-08-12 12:53:59 +02:00
import { WithTranslation, withTranslation } from "react-i18next";
import { binder, ExtensionPoint } from "@scm-manager/ui-extensions";
import { Changeset, Repository } from "@scm-manager/ui-types";
import {
2020-08-10 20:48:08 +02:00
CustomQueryFlexWrappedColumns,
ErrorPage,
FileControlFactory,
JumpToFileButton,
Loading,
NavLink,
Page,
PrimaryContentColumn,
2020-03-11 15:47:40 +01:00
SecondaryNavigation,
2020-08-10 20:48:08 +02:00
SecondaryNavigationColumn,
StateMenuContextProvider,
SubNavigation,
Tooltip,
urls
} from "@scm-manager/ui-components";
2020-08-12 12:53:59 +02:00
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";
2020-08-12 12:53:59 +02:00
import { getLinks, getRepositoriesLink } from "../../modules/indexResource";
import CodeOverview from "../codeSection/containers/CodeOverview";
import ChangesetView from "./ChangesetView";
import SourceExtensions from "../sources/containers/SourceExtensions";
2020-09-16 12:45:22 +02:00
import TagsOverview from "../tags/container/TagsOverview";
import TagRoot from "../tags/container/TagRoot";
import styled from "styled-components";
type Props = RouteComponentProps &
WithTranslation & {
2020-08-12 12:53:59 +02:00
namespace: string;
name: string;
repository: Repository;
loading: boolean;
error: Error;
repoLink: string;
indexLinks: object;
2020-08-12 12:53:59 +02:00
// dispatch functions
fetchRepoByName: (link: string, namespace: string, name: string) => void;
};
const RepositoryTag = styled.span`
margin-left: 0.2rem;
background-color: #9a9a9a;
padding: 0.4rem;
border-radius: 5px;
color: white;
font-weight: bold;
`;
class RepositoryRoot extends React.Component<Props> {
componentDidMount() {
2020-08-12 12:53:59 +02:00
const { fetchRepoByName, namespace, name, repoLink } = this.props;
2018-11-05 13:52:46 +01:00
fetchRepoByName(repoLink, namespace, name);
}
componentDidUpdate(prevProps: Props) {
2020-08-12 12:53:59 +02:00
const { fetchRepoByName, namespace, name, repoLink } = this.props;
if (namespace !== prevProps.namespace || name !== prevProps.name) {
fetchRepoByName(repoLink, namespace, name);
}
}
matchesBranches = (route: any) => {
2020-09-18 17:38:10 +02:00
const url = urls.matchedUrl(this.props);
const regex = new RegExp(`${url}/branch/.+/info`);
return route.location.pathname.match(regex);
};
2020-09-16 12:45:22 +02:00
matchesTags = (route: any) => {
2020-09-18 17:38:10 +02:00
const url = urls.matchedUrl(this.props);
2020-09-16 12:45:22 +02:00
const regex = new RegExp(`${url}/tag/.+/info`);
return route.location.pathname.match(regex);
};
matchesCode = (route: any) => {
2020-09-18 17:38:10 +02:00
const url = urls.matchedUrl(this.props);
const regex = new RegExp(`${url}(/code)/.*`);
2018-10-09 11:53:06 +02:00
return route.location.pathname.match(regex);
};
getCodeLinkname = () => {
2020-08-12 12:53:59 +02:00
const { repository } = this.props;
if (repository?._links?.sources) {
return "sources";
}
if (repository?._links?.changesets) {
return "changesets";
}
return "";
};
evaluateDestinationForCodeLink = () => {
2020-08-12 12:53:59 +02:00
const { repository } = this.props;
2020-09-18 17:38:10 +02:00
const url = `${urls.matchedUrl(this.props)}/code`;
if (repository?._links?.sources) {
return `${url}/sources/`;
}
return `${url}/changesets`;
};
render() {
2020-08-12 12:53:59 +02:00
const { loading, error, indexLinks, repository, t } = this.props;
if (error) {
2019-03-28 14:47:57 +01:00
return (
2020-08-12 12:53:59 +02:00
<ErrorPage title={t("repositoryRoot.errorTitle")} subtitle={t("repositoryRoot.errorSubtitle")} error={error} />
2019-03-28 14:47:57 +01:00
);
}
if (!repository || loading) {
2020-08-12 12:53:59 +02:00
return <Loading />;
}
2020-09-18 17:38:10 +02:00
const url = urls.matchedUrl(this.props);
2020-03-02 14:13:51 +01:00
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";
}
const fileControlFactoryFactory: (changeset: Changeset) => FileControlFactory = changeset => file => {
2020-08-10 20:48:08 +02:00
const baseUrl = `${url}/code/sources`;
const sourceLink = file.newPath && {
2020-08-10 20:48:08 +02:00
url: `${baseUrl}/${changeset.id}/${file.newPath}/`,
label: t("diff.jumpToSource")
2020-08-10 20:48:08 +02:00
};
const targetLink = file.oldPath &&
changeset._embedded?.parents?.length === 1 && {
url: `${baseUrl}/${changeset._embedded.parents[0].id}/${file.oldPath}`,
label: t("diff.jumpToTarget")
};
2020-08-10 20:48:08 +02:00
const links = [];
switch (file.type) {
case "add":
if (sourceLink) {
links.push(sourceLink);
}
2020-08-10 20:48:08 +02:00
break;
case "delete":
if (targetLink) {
links.push(targetLink);
}
break;
default:
if (targetLink && sourceLink) {
links.push(targetLink, sourceLink); // Target link first because its the previous file
} else if (sourceLink) {
2020-08-10 20:48:08 +02:00
links.push(sourceLink);
}
}
return links ? links.map(({ url, label }) => <JumpToFileButton tooltip={label} link={url} />) : null;
2020-08-10 20:48:08 +02:00
};
const repositoryFlags = [];
if (repository.archived) {
repositoryFlags.push(
<Tooltip message={t("archive.tooltip")}>
<RepositoryTag className="is-size-6">{t("repository.archived")}</RepositoryTag>
</Tooltip>
);
}
if (repository.exporting) {
repositoryFlags.push(
<Tooltip message={t("exporting.tooltip")}>
<RepositoryTag className="is-size-6">{t("repository.exporting")}</RepositoryTag>
</Tooltip>
);
}
const titleComponent = (
<>
<Link to={`/repos/${repository.namespace}/`} className={"has-text-dark"}>
{repository.namespace}
</Link>
/{repository.name}
</>
);
return (
<StateMenuContextProvider>
<Page
title={titleComponent}
documentTitle={`${repository.namespace}/${repository.name}`}
afterTitle={
<>
<ExtensionPoint name={"repository.afterTitle"} props={{ repository }} />
{repositoryFlags.map(flag => flag)}
</>
}
>
<CustomQueryFlexWrappedColumns>
<PrimaryContentColumn>
<Switch>
2020-08-12 12:53:59 +02:00
<Redirect exact from={this.props.match.url} to={redirectedUrl} />
2020-01-15 10:49:01 +01:00
{/* redirect pre 2.0.0-rc2 links */}
2020-08-12 12:53:59 +02: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/branch/:branch/changesets/`} />
2020-01-15 10:49:01 +01:00
2020-08-12 12:53:59 +02:00
<Route path={`${url}/info`} exact component={() => <RepositoryDetails repository={repository} />} />
<Route path={`${url}/settings/general`} component={() => <EditRepo repository={repository} />} />
<Route
path={`${url}/settings/permissions`}
render={() => (
2020-08-12 12:53:59 +02:00
<Permissions namespace={this.props.repository.namespace} repoName={this.props.repository.name} />
)}
/>
<Route
exact
path={`${url}/code/changeset/:id`}
2020-08-12 12:53:59 +02:00
render={() => (
<ChangesetView repository={repository} fileControlFactoryFactory={fileControlFactoryFactory} />
)}
/>
<Route
path={`${url}/code/sourceext/:extension`}
exact={true}
2020-08-12 12:53:59 +02:00
render={() => <SourceExtensions repository={repository} />}
/>
<Route
path={`${url}/code/sourceext/:extension/:revision/:path*`}
2020-08-12 12:53:59 +02:00
render={() => <SourceExtensions repository={repository} baseUrl={`${url}/code/sources`} />}
/>
<Route
path={`${url}/code`}
2020-08-12 12:53:59 +02:00
render={() => <CodeOverview baseUrl={`${url}/code`} repository={repository} />}
/>
<Route
path={`${url}/branch/:branch`}
2020-08-12 12:53:59 +02:00
render={() => <BranchRoot repository={repository} baseUrl={`${url}/branch`} />}
2019-03-28 11:51:00 +01:00
/>
<Route
path={`${url}/branches`}
exact={true}
2020-08-12 12:53:59 +02:00
render={() => <BranchesOverview repository={repository} baseUrl={`${url}/branch`} />}
/>
2020-08-12 12:53:59 +02:00
<Route path={`${url}/branches/create`} render={() => <CreateBranch repository={repository} />} />
2020-09-16 12:45:22 +02:00
<Route
path={`${url}/tag/:tag`}
render={() => <TagRoot repository={repository} baseUrl={`${url}/tag`} />}
/>
<Route
path={`${url}/tags`}
exact={true}
render={() => <TagsOverview repository={repository} baseUrl={`${url}/tag`} />}
/>
2020-08-12 12:53:59 +02:00
<ExtensionPoint name="repository.route" props={extensionProps} renderAll={true} />
</Switch>
</PrimaryContentColumn>
<SecondaryNavigationColumn>
<SecondaryNavigation label={t("repositoryRoot.menu.navigationLabel")}>
2020-08-12 12:53:59 +02:00
<ExtensionPoint name="repository.navigation.topLevel" props={extensionProps} renderAll={true} />
<NavLink
to={`${url}/info`}
icon="fas fa-info-circle"
label={t("repositoryRoot.menu.informationNavLink")}
title={t("repositoryRoot.menu.informationNavLink")}
/>
<RepositoryNavLink
repository={repository}
linkName="branches"
to={`${url}/branches/`}
icon="fas fa-code-branch"
label={t("repositoryRoot.menu.branchesNavLink")}
activeWhenMatch={this.matchesBranches}
activeOnlyWhenExact={false}
title={t("repositoryRoot.menu.branchesNavLink")}
/>
2020-09-16 12:45:22 +02:00
<RepositoryNavLink
repository={repository}
linkName="tags"
to={`${url}/tags/`}
icon="fas fa-tags"
label={t("repositoryRoot.menu.tagsNavLink")}
activeWhenMatch={this.matchesTags}
activeOnlyWhenExact={false}
title={t("repositoryRoot.menu.tagsNavLink")}
/>
<RepositoryNavLink
repository={repository}
linkName={this.getCodeLinkname()}
to={this.evaluateDestinationForCodeLink()}
icon="fas fa-code"
label={t("repositoryRoot.menu.sourcesNavLink")}
activeWhenMatch={this.matchesCode}
activeOnlyWhenExact={false}
title={t("repositoryRoot.menu.sourcesNavLink")}
/>
2020-08-12 12:53:59 +02:00
<ExtensionPoint name="repository.navigation" props={extensionProps} renderAll={true} />
<SubNavigation
to={`${url}/settings/general`}
label={t("repositoryRoot.menu.settingsNavLink")}
title={t("repositoryRoot.menu.settingsNavLink")}
>
2020-08-12 12:53:59 +02:00
<EditRepoNavLink repository={repository} editUrl={`${url}/settings/general`} />
<PermissionsNavLink permissionUrl={`${url}/settings/permissions`} repository={repository} />
<ExtensionPoint name="repository.setting" props={extensionProps} renderAll={true} />
</SubNavigation>
2020-03-11 15:47:40 +01:00
</SecondaryNavigation>
</SecondaryNavigationColumn>
</CustomQueryFlexWrappedColumns>
2020-03-02 14:13:51 +01:00
</Page>
</StateMenuContextProvider>
);
}
}
const mapStateToProps = (state: any, ownProps: Props) => {
2020-08-12 12:53:59 +02:00
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));