/* * 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"; import { connect } from "react-redux"; import { Redirect, Route, Switch, RouteComponentProps } from "react-router-dom"; import { WithTranslation, withTranslation } from "react-i18next"; import { binder, ExtensionPoint } from "@scm-manager/ui-extensions"; import { Repository } from "@scm-manager/ui-types"; import { ErrorPage, Loading, NavLink, Page, SecondaryNavigation, SubNavigation, MenuContext, storeMenuCollapsed, isMenuCollapsed } 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 = RouteComponentProps & 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; }; type State = { menuCollapsed: boolean; }; class RepositoryRoot extends React.Component { constructor(props: Props) { super(props); this.state = { menuCollapsed: isMenuCollapsed() }; } componentDidMount() { const { fetchRepoByName, namespace, name, repoLink } = this.props; fetchRepoByName(repoLink, namespace, name); } stripEndingSlash = (url: string) => { if (url.endsWith("/")) { 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) => { const url = this.matchedUrl(); const regex = new RegExp(`${url}(/code)/.*`); 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`; }; onCollapseRepositoryMenu = (collapsed: boolean) => { this.setState({ menuCollapsed: collapsed }, () => storeMenuCollapsed(collapsed)); }; render() { const { loading, error, indexLinks, repository, t } = this.props; const { menuCollapsed } = this.state; if (error) { return ( ); } if (!repository || loading) { return ; } const url = this.matchedUrl(); const extensionProps = { repository, url, indexLinks }; const redirectUrlFactory = binder.getExtension("repository.redirect", this.props); let redirectedUrl; if (redirectUrlFactory) { redirectedUrl = url + redirectUrlFactory(this.props); } else { redirectedUrl = url + "/info"; } return ( this.setState({ menuCollapsed: collapsed }) }} >
{/* redirect pre 2.0.0-rc2 links */} } /> } /> ( )} /> } /> } /> } /> } /> } /> } /> } />
this.onCollapseRepositoryMenu(!menuCollapsed)} collapsed={menuCollapsed} >
); } } 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 { fetchRepoByName: (link: string, namespace: string, name: string) => { dispatch(fetchRepoByName(link, namespace, name)); } }; }; export default connect(mapStateToProps, mapDispatchToProps)(withTranslation("repos")(RepositoryRoot));