refactor codeViewSwitcher

This commit is contained in:
Eduard Heimbuch
2020-01-13 15:13:01 +01:00
parent 0e544d694f
commit d769c82afa

View File

@@ -30,16 +30,25 @@ const CodeViewSwitcher: FC<Props> = ({ baseUrl, currentUrl, branches, selectedBr
} }
let splittedUrl = currentUrl.split("/"); let splittedUrl = currentUrl.split("/");
splittedUrl[5] = destination; splittedUrl[5] = destination;
splittedUrl.splice(6, splittedUrl.length);
if (branch) { if (branch) {
splittedUrl[6] = branch; splittedUrl[6] = branch;
} }
splittedUrl.splice(7, splittedUrl.length);
if (suffix) { if (suffix) {
splittedUrl.push(suffix); splittedUrl.push(suffix);
} }
return splittedUrl.join("/"); return splittedUrl.join("/");
}; };
const evaluateDestinationBranch = () => {
return (
branches &&
(branches.filter(branch => branch.name === selectedBranch).length === 0
? branches.filter(branch => branch.defaultBranch === true)[0].name
: branches.filter(branch => branch.name === selectedBranch)[0].name)
);
};
return ( return (
<ButtonAddonsMarginRight> <ButtonAddonsMarginRight>
<SmallButton <SmallButton
@@ -52,13 +61,7 @@ const CodeViewSwitcher: FC<Props> = ({ baseUrl, currentUrl, branches, selectedBr
} }
link={ link={
branches branches
? branches.filter(branch => branch.name === selectedBranch).length === 0 ? createDestinationUrl("branch", evaluateDestinationBranch(), "changesets/")
? createDestinationUrl(
"branch",
branches.filter(branch => branch.defaultBranch === true)[0].name,
"changesets/"
)
: createDestinationUrl("branch", selectedBranch, "changesets/")
: createDestinationUrl("changesets") : createDestinationUrl("changesets")
} }
/> />
@@ -66,7 +69,7 @@ const CodeViewSwitcher: FC<Props> = ({ baseUrl, currentUrl, branches, selectedBr
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={currentUrl.includes("/code/sources") ? "link is-selected" : undefined}
link={createDestinationUrl("sources")} link={createDestinationUrl("sources", evaluateDestinationBranch())}
/> />
</ButtonAddonsMarginRight> </ButtonAddonsMarginRight>
); );