refactor code section

This commit is contained in:
Eduard Heimbuch
2020-01-14 18:07:57 +01:00
parent e0e2ccec02
commit 0888006a74
5 changed files with 208 additions and 194 deletions

View File

@@ -0,0 +1,50 @@
import React, { FC } from "react";
import styled from "styled-components";
import { RouteComponentProps, withRouter } from "react-router-dom";
import { Level, BranchSelector } from "@scm-manager/ui-components";
import CodeViewSwitcher from "./CodeViewSwitcher";
import { useTranslation } from "react-i18next";
import { Branch } from "@scm-manager/ui-types";
const ActionBar = styled.div.attrs(() => ({}))`
background-color: whitesmoke;
border: 1px solid #dbdbdb;
border-radius: 4px;
color: #363636;
font-size: 1.25em;
font-weight: 300;
line-height: 1.25;
padding: 0.5em 0.75em;
margin-bottom: 1em;
`;
type Props = RouteComponentProps & {
selectedBranch?: string;
branches: Branch[];
onSelectBranch: () => void;
switchViewLink: string;
};
const CodeActionBar: FC<Props> = ({ selectedBranch, branches, onSelectBranch, switchViewLink, location }) => {
const { t } = useTranslation("repos");
return (
<ActionBar>
<Level
left={
branches?.length > 0 && (
<BranchSelector
label={t("code.branchSelector")}
branches={branches}
selectedBranch={selectedBranch}
onSelectBranch={onSelectBranch}
/>
)
}
right={<CodeViewSwitcher currentUrl={location.pathname} switchViewLink={switchViewLink} />}
/>
</ActionBar>
);
};
export default withRouter(CodeActionBar);

View File

@@ -15,40 +15,29 @@ const ButtonAddonsMarginRight = styled(ButtonAddons)`
`; `;
type Props = { type Props = {
baseUrl: string;
currentUrl: string; currentUrl: string;
branches: Branch[]; switchViewLink: string;
selectedBranch: string;
}; };
const CodeViewSwitcher: FC<Props> = ({ baseUrl, currentUrl, branches, selectedBranch }) => { const CodeViewSwitcher: FC<Props> = ({ currentUrl, switchViewLink }) => {
const [t] = useTranslation("repos"); const [t] = useTranslation("repos");
const createDestinationUrl = (destination: string, branch?: string, suffix?: string) => { const resolveLocation = () => {
if (!branches) { if (currentUrl.includes("/code/branch") || currentUrl.includes("/code/changesets")) {
return baseUrl + "/" + destination + "/"; return "changesets";
} }
let splittedUrl = currentUrl.split("/"); if (currentUrl.includes("/code/sources")) {
splittedUrl[5] = destination; return "sources";
splittedUrl.splice(6, splittedUrl.length);
if (branch) {
splittedUrl[6] = branch;
} }
if (suffix) { return "";
splittedUrl.push(suffix);
}
return splittedUrl.join("/");
}; };
const evaluateDestinationBranch = () => { const isSourcesTab = () => {
return ( return resolveLocation() === "sources";
branches && };
encodeURIComponent(
branches.filter(branch => branch.name === selectedBranch).length === 0 const isChangesetsTab = () => {
? branches.filter(branch => branch.defaultBranch === true)[0].name return resolveLocation() === "changesets";
: branches.filter(branch => branch.name === selectedBranch)[0].name
)
);
}; };
return ( return (
@@ -56,22 +45,14 @@ const CodeViewSwitcher: FC<Props> = ({ baseUrl, currentUrl, branches, selectedBr
<SmallButton <SmallButton
label={t("code.commits")} label={t("code.commits")}
icon="fa fa-exchange-alt" icon="fa fa-exchange-alt"
color={ color={isChangesetsTab() ? "link is-selected" : undefined}
currentUrl.includes("/code/branch") || currentUrl.includes("/code/changesets") link={isSourcesTab() ? switchViewLink : undefined}
? "link is-selected"
: undefined
}
link={
branches
? createDestinationUrl("branch", evaluateDestinationBranch(), "changesets/")
: createDestinationUrl("changesets")
}
/> />
<SmallButton <SmallButton
label={t("code.sources")} label={t("code.sources")}
icon="fa fa-code" icon="fa fa-code"
color={currentUrl.includes("/code/sources") ? "link is-selected" : undefined} color={isSourcesTab() ? "link is-selected" : undefined}
link={createDestinationUrl("sources", evaluateDestinationBranch())} link={isChangesetsTab() ? switchViewLink: undefined}
/> />
</ButtonAddonsMarginRight> </ButtonAddonsMarginRight>
); );

View File

@@ -1,11 +1,9 @@
import React from "react"; import React from "react";
import styled from "styled-components";
import { Route, RouteComponentProps, withRouter } from "react-router-dom"; import { Route, RouteComponentProps, withRouter } from "react-router-dom";
import Sources from "../../sources/containers/Sources"; import Sources from "../../sources/containers/Sources";
import ChangesetsRoot from "../../containers/ChangesetsRoot"; import ChangesetsRoot from "../../containers/ChangesetsRoot";
import { Branch, Repository } from "@scm-manager/ui-types"; import { Branch, Repository } from "@scm-manager/ui-types";
import { BranchSelector, ErrorPage, Level, Loading } from "@scm-manager/ui-components"; import { ErrorPage, Loading } from "@scm-manager/ui-components";
import CodeViewSwitcher from "../components/CodeViewSwitcher";
import { compose } from "redux"; import { compose } from "redux";
import { WithTranslation, withTranslation } from "react-i18next"; import { WithTranslation, withTranslation } from "react-i18next";
import { connect } from "react-redux"; import { connect } from "react-redux";
@@ -31,55 +29,11 @@ type Props = RouteComponentProps &
fetchBranches: (p: Repository) => void; fetchBranches: (p: Repository) => void;
}; };
const CodeActionBar = styled.div.attrs(() => ({}))`
background-color: whitesmoke;
border: 1px solid #dbdbdb;
border-radius: 4px;
color: #363636;
font-size: 1.25em;
font-weight: 300;
line-height: 1.25;
padding: 0.5em 0.75em;
margin-bottom: 1em;
`;
class CodeOverview extends React.Component<Props> { class CodeOverview extends React.Component<Props> {
componentDidMount() { componentDidMount() {
const { repository, branches } = this.props; const { repository } = this.props;
new Promise(() => {
this.props.fetchBranches(repository); this.props.fetchBranches(repository);
}).then(() => {
if (this.props.branches?.length > 0) {
const defaultBranch = branches.filter((branch: Branch) => branch.defaultBranch === true)[0];
this.branchSelected(defaultBranch);
} }
});
}
findSelectedBranch = () => {
const { selectedBranch, branches } = this.props;
return branches?.find((branch: Branch) => branch.name === selectedBranch);
};
branchSelected = (branch?: Branch) => {
let splittedUrl = this.props.location.pathname.split("/");
if (
this.props.location.pathname.includes("/code/sources") ||
this.props.location.pathname.includes("/code/branch")
) {
if (branch) {
splittedUrl[6] = encodeURIComponent(branch.name);
}
this.props.history.push(splittedUrl.join("/"));
}
if (this.props.location.pathname.includes("/code/changesets")) {
this.props.history.push(
`${splittedUrl[0]}/${splittedUrl[1]}/${splittedUrl[2]}/${splittedUrl[3]}/${
splittedUrl[4]
}/branch/${encodeURIComponent(branch.name)}/${splittedUrl[5]}/`
);
}
};
render() { render() {
const { repository, baseUrl, branches, selectedBranch, error, loading, t } = this.props; const { repository, baseUrl, branches, selectedBranch, error, loading, t } = this.props;
@@ -97,39 +51,29 @@ class CodeOverview extends React.Component<Props> {
return ( return (
<div> <div>
<CodeActionBar>
<Level
left={
<BranchSelector
label={t("code.branchSelector")}
branches={branches}
selectedBranch={selectedBranch}
onSelectBranch={this.branchSelected}
/>
}
right={<CodeViewSwitcher baseUrl={url} currentUrl={this.props.location.pathname} branches={branches} selectedBranch={selectedBranch}/>}
/>
</CodeActionBar>
<Route <Route
path={`${url}/sources`} path={`${url}/sources`}
exact={true} exact={true}
render={() => <Sources repository={repository} baseUrl={`${url}/sources`} branches={branches} />} render={() => <Sources repository={repository} baseUrl={`${url}`} branches={branches} />}
/> />
<Route <Route
path={`${url}/sources/:revision/:path*`} path={`${url}/sources/:revision/:path*`}
render={() => <Sources repository={repository} baseUrl={`${url}/sources`} branches={branches} />} render={() => (
<Sources repository={repository} baseUrl={`${url}`} branches={branches} selectedBranch={selectedBranch} />
)}
/> />
<Route <Route
path={`${url}/changesets`} path={`${url}/changesets`}
render={() => <ChangesetsRoot repository={repository} baseUrl={`${url}/changesets`} />} render={() => <ChangesetsRoot repository={repository} baseUrl={`${url}`} branches={branches} />}
/> />
<Route <Route
path={`${url}/branch/:branch/changesets/`} path={`${url}/branch/:branch/changesets/`}
render={() => ( render={() => (
<ChangesetsRoot <ChangesetsRoot
repository={repository} repository={repository}
baseUrl={`${url}/changesets`} baseUrl={`${url}`}
selectedBranch={branches && branches.filter(b => b.name === this.props.selectedBranch)[0]} branches={branches}
selectedBranch={selectedBranch}
/> />
)} )}
/> />

View File

@@ -1,21 +1,28 @@
import React from "react"; import React from "react";
import { Route, withRouter } from "react-router-dom"; import { Route, withRouter, RouteComponentProps } from "react-router-dom";
import { WithTranslation, withTranslation } from "react-i18next"; import { WithTranslation, withTranslation } from "react-i18next";
import { Repository } from "@scm-manager/ui-types"; import { Repository, Branch } from "@scm-manager/ui-types";
import Changesets from "./Changesets"; import Changesets from "./Changesets";
import { compose } from "redux"; import { compose } from "redux";
import CodeActionBar from "../codeSection/components/CodeActionBar";
type Props = WithTranslation & { type Props = WithTranslation &
RouteComponentProps & {
repository: Repository; repository: Repository;
selectedBranch: string; selectedBranch: string;
baseUrl: string; baseUrl: string;
branches: Branch[];
// Context props };
history: any; // TODO flow type
match: any;
};
class ChangesetsRoot extends React.Component<Props> { class ChangesetsRoot extends React.Component<Props> {
componentDidMount() {
const { branches, baseUrl } = this.props;
if (branches?.length > 0 && this.isSelectedBranchIsNotABranch()) {
const defaultBranch = branches?.filter(b => b.defaultBranch === true)[0];
this.props.history.push(`${baseUrl}/branch/${encodeURIComponent(defaultBranch.name)}/changesets/`);
}
}
stripEndingSlash = (url: string) => { stripEndingSlash = (url: string) => {
if (url.endsWith("/")) { if (url.endsWith("/")) {
return url.substring(0, url.length - 1); return url.substring(0, url.length - 1);
@@ -23,8 +30,29 @@ class ChangesetsRoot extends React.Component<Props> {
return url; return url;
}; };
isSelectedBranchIsNotABranch = () => {
const { branches, selectedBranch } = this.props;
return branches?.filter(b => b.name === selectedBranch).length === 0;
};
evaluateSwitchViewLink = () => {
const { baseUrl, selectedBranch } = this.props;
if (selectedBranch) {
return `${baseUrl}/sources/${encodeURIComponent(selectedBranch)}/`;
}
return `${baseUrl}/sources/`;
};
onSelectBranch = (branch?: Branch) => {
const { baseUrl, history } = this.props;
if (branch) {
let url = `${baseUrl}/branch/${encodeURIComponent(branch.name)}/changesets/`;
history.push(url);
}
};
render() { render() {
const { repository, match, selectedBranch } = this.props; const { repository, branches, match, selectedBranch } = this.props;
if (!repository) { if (!repository) {
return null; return null;
@@ -33,12 +61,22 @@ class ChangesetsRoot extends React.Component<Props> {
const url = this.stripEndingSlash(match.url); const url = this.stripEndingSlash(match.url);
return ( return (
<>
<CodeActionBar
branches={branches}
selectedBranch={!this.isSelectedBranchIsNotABranch() ? selectedBranch : undefined}
onSelectBranch={this.onSelectBranch}
switchViewLink={this.evaluateSwitchViewLink()}
/>
<div className="panel"> <div className="panel">
<Route <Route
path={`${url}/:page?`} path={`${url}/:page?`}
component={() => <Changesets repository={repository} branch={selectedBranch} />} component={() => (
<Changesets repository={repository} branch={branches?.filter(b => b.name === selectedBranch)[0]} />
)}
/> />
</div> </div>
</>
); );
} }
} }

View File

@@ -1,6 +1,6 @@
import React from "react"; import React from "react";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { withRouter } from "react-router-dom"; import { withRouter, RouteComponentProps } from "react-router-dom";
import { WithTranslation, withTranslation } from "react-i18next"; import { WithTranslation, withTranslation } from "react-i18next";
import { Branch, Repository } from "@scm-manager/ui-types"; import { Branch, Repository } from "@scm-manager/ui-types";
import { Breadcrumb, ErrorNotification, Loading } from "@scm-manager/ui-components"; import { Breadcrumb, ErrorNotification, Loading } from "@scm-manager/ui-components";
@@ -9,8 +9,10 @@ import { getFetchBranchesFailure, isFetchBranchesPending } from "../../branches/
import { compose } from "redux"; import { compose } from "redux";
import Content from "./Content"; import Content from "./Content";
import { fetchSources, getSources, isDirectory } from "../modules/sources"; import { fetchSources, getSources, isDirectory } from "../modules/sources";
import CodeActionBar from "../../codeSection/components/CodeActionBar";
type Props = WithTranslation & { type Props = WithTranslation &
RouteComponentProps & {
repository: Repository; repository: Repository;
loading: boolean; loading: boolean;
error: Error; error: Error;
@@ -20,35 +22,20 @@ type Props = WithTranslation & {
path: string; path: string;
currentFileIsDirectory: boolean; currentFileIsDirectory: boolean;
sources: File; sources: File;
selectedBranch: string;
// dispatch props // dispatch props
fetchSources: (p1: Repository, p2: string, p3: string) => void; fetchSources: (p1: Repository, p2: string, p3: string) => void;
// Context props
history: any;
match: any;
location: any;
};
type State = {
selectedBranch: any;
};
class Sources extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
selectedBranch: null
}; };
}
class Sources extends React.Component<Props> {
componentDidMount() { componentDidMount() {
const { repository, revision, path, fetchSources } = this.props; const { repository, branches, selectedBranch, baseUrl, revision, path, fetchSources } = this.props;
fetchSources(repository, this.decodeRevision(revision), path); fetchSources(repository, this.decodeRevision(revision), path);
if (branches?.length > 0 && !selectedBranch) {
this.redirectToDefaultBranch(); const defaultBranch = branches?.filter(b => b.defaultBranch === true)[0];
this.props.history.push(`${baseUrl}/sources/${encodeURIComponent(defaultBranch.name)}/`);
}
} }
componentDidUpdate(prevProps: Props) { componentDidUpdate(prevProps: Props) {
@@ -62,45 +49,42 @@ class Sources extends React.Component<Props, State> {
return revision ? decodeURIComponent(revision) : revision; return revision ? decodeURIComponent(revision) : revision;
}; };
redirectToDefaultBranch = () => { onSelectBranch = (branch?: Branch) => {
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; const { baseUrl, history, path } = this.props;
let url; let url;
if (branch) { if (branch) {
this.setState({
selectedBranch: branch
});
if (path) { if (path) {
url = `${baseUrl}/${encodeURIComponent(branch.name)}/${path}`; url = `${baseUrl}/sources/${encodeURIComponent(branch.name)}/${path}`;
url = !url.endsWith("/") ? url + "/" : url;
} else { } else {
url = `${baseUrl}/${encodeURIComponent(branch.name)}/`; url = `${baseUrl}/sources/${encodeURIComponent(branch.name)}/`;
} }
} else { } else {
this.setState({ return;
selectedBranch: null
});
url = `${baseUrl}/`;
} }
history.push(url); history.push(url);
}; };
evaluateSwitchViewLink = () => {
const { baseUrl, selectedBranch } = this.props;
if (selectedBranch) {
return `${baseUrl}/branch/${encodeURIComponent(selectedBranch)}/changesets/`;
}
return `${baseUrl}/changesets/`;
};
render() { render() {
const { repository, baseUrl, loading, error, revision, path, currentFileIsDirectory } = this.props; const {
repository,
baseUrl,
branches,
selectedBranch,
loading,
error,
revision,
path,
currentFileIsDirectory
} = this.props;
if (error) { if (error) {
return <ErrorNotification error={error} />; return <ErrorNotification error={error} />;
@@ -112,28 +96,45 @@ class Sources extends React.Component<Props, State> {
if (currentFileIsDirectory) { if (currentFileIsDirectory) {
return ( return (
<>
<CodeActionBar
selectedBranch={selectedBranch}
branches={branches}
onSelectBranch={this.onSelectBranch}
switchViewLink={this.evaluateSwitchViewLink()}
/>
<div className="panel"> <div className="panel">
{this.renderBreadcrumb()} {this.renderBreadcrumb()}
<FileTree repository={repository} revision={revision} path={path} baseUrl={baseUrl} /> <FileTree repository={repository} revision={revision} path={path} baseUrl={baseUrl + "/sources"} />
</div> </div>
</>
); );
} else { } else {
return <Content repository={repository} revision={revision} path={path} breadcrumb={this.renderBreadcrumb()} />; return (
<>
<CodeActionBar
selectedBranch={selectedBranch}
branches={branches}
onSelectBranch={this.onSelectBranch}
switchViewLink={this.evaluateSwitchViewLink()}
/>
<Content repository={repository} revision={revision} path={path} breadcrumb={this.renderBreadcrumb()} />
</>
);
} }
} }
renderBreadcrumb = () => { renderBreadcrumb = () => {
const { revision, path, baseUrl, branches, sources, repository } = this.props; const { revision, selectedBranch, path, baseUrl, branches, sources, repository } = this.props;
const { selectedBranch } = this.state;
return ( return (
<Breadcrumb <Breadcrumb
repository={repository} repository={repository}
revision={revision} revision={revision}
path={path} path={path}
baseUrl={baseUrl} baseUrl={baseUrl + "/sources"}
branch={selectedBranch} branch={branches?.filter(b => b.name === selectedBranch)[0]}
defaultBranch={branches && branches.filter(b => b.defaultBranch === true)[0]} defaultBranch={branches?.filter(b => b.defaultBranch === true)[0]}
sources={sources} sources={sources}
/> />
); );