Move trailers to own component

We have to use a dedicated component because we have to use the
translations from 'plugins', so that other plugins can contribute their
own trailer types with their own descriptions.
This commit is contained in:
René Pfeuffer
2020-06-08 14:32:06 +02:00
parent 031605a327
commit a7585b4fc0
6 changed files with 111 additions and 75 deletions

View File

@@ -100,15 +100,6 @@
"buttons": { "buttons": {
"details": "Details", "details": "Details",
"sources": "Sources" "sources": "Sources"
},
"trailer": {
"type": {
"Reviewed-by": "Reviewer",
"Co-authored-by": "Co-Autoren",
"Committed-by": "Committed von",
"Signed-off-by": "Signiert von",
"Pushed-by": "Pushed by"
}
} }
}, },
"repositoryForm": { "repositoryForm": {

View File

@@ -100,16 +100,6 @@
"buttons": { "buttons": {
"details": "Details", "details": "Details",
"sources": "Sources" "sources": "Sources"
},
"trailer": {
"type": {
"Reviewed-by": "Reviewers",
"Co-authored-by": "Co-Authors",
"Committed-by": "Commit by",
"Signed-off-by": "Signed-off",
"Pushed-by": "Pushed by"
},
"person": "Person"
} }
}, },
"repositoryForm": { "repositoryForm": {

View File

@@ -38,6 +38,7 @@ import {
DateFromNow, DateFromNow,
Level Level
} from "@scm-manager/ui-components"; } from "@scm-manager/ui-components";
import ContributorTable from "./ContributorTable";
type Props = WithTranslation & { type Props = WithTranslation & {
changeset: Changeset; changeset: Changeset;
@@ -62,10 +63,6 @@ const BottomMarginLevel = styled(Level)`
margin-bottom: 1rem !important; margin-bottom: 1rem !important;
`; `;
const SizedTd = styled.td`
width: 10rem;
`;
class ChangesetDetails extends React.Component<Props, State> { class ChangesetDetails extends React.Component<Props, State> {
constructor(props: Props) { constructor(props: Props) {
super(props); super(props);
@@ -74,28 +71,6 @@ class ChangesetDetails extends React.Component<Props, State> {
}; };
} }
collectAvailableTrailerTypes() {
const { changeset } = this.props;
// @ts-ignore
return [...new Set(changeset.trailers.map(trailer => trailer.trailerType))];
}
getPersonsByTrailersType(type: string) {
const { changeset } = this.props;
return changeset.trailers?.filter(trailer => trailer.trailerType === type).map(t => t.person);
}
getTrailersByType() {
const availableTrailerTypes: string[] = this.collectAvailableTrailerTypes();
const personsByTrailerType = [];
for (const type of availableTrailerTypes) {
personsByTrailerType.push({ type, persons: this.getPersonsByTrailersType(type) });
}
return personsByTrailerType;
}
render() { render() {
const { changeset, repository, t } = this.props; const { changeset, repository, t } = this.props;
const { collapsed } = this.state; const { collapsed } = this.state;
@@ -104,35 +79,6 @@ class ChangesetDetails extends React.Component<Props, State> {
const id = <ChangesetId repository={repository} changeset={changeset} link={false} />; const id = <ChangesetId repository={repository} changeset={changeset} link={false} />;
const date = <DateFromNow date={changeset.date} />; const date = <DateFromNow date={changeset.date} />;
const trailersByType = this.getTrailersByType();
const trailerTable = (
<table>
<tr>
<SizedTd>{t("changeset.author.label") + ":"}</SizedTd>
<td>
<a title={changeset?.author?.mail} href={"mailto:" + changeset?.author?.mail}>
{changeset?.author?.name}
</a>
</td>
</tr>
{trailersByType.map(trailer => (
<tr>
<SizedTd>{t("changeset.trailer.type." + trailer.type) + ":"}</SizedTd>
<td className="shorten-text is-marginless">
{trailer.persons
.map(person => (
<a title={person.mail} href={"mailto:" + person.mail}>
{person.name}
</a>
))
.reduce((prev, curr) => [prev, ", ", curr])}
</td>
</tr>
))}
</table>
);
return ( return (
<> <>
<div className={classNames("content", "is-marginless")}> <div className={classNames("content", "is-marginless")}>
@@ -155,7 +101,7 @@ class ChangesetDetails extends React.Component<Props, State> {
</RightMarginP> </RightMarginP>
</AvatarWrapper> </AvatarWrapper>
<div className="media-content"> <div className="media-content">
{trailerTable} <ContributorTable changeset={changeset} />
<p> <p>
<Trans i18nKey="repos:changeset.summary" components={[id, date]} /> <Trans i18nKey="repos:changeset.summary" components={[id, date]} />
</p> </p>

View File

@@ -0,0 +1,87 @@
/*
* 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, { FC } from "react";
import { Changeset } from "@scm-manager/ui-types/src";
import styled from "styled-components";
import { useTranslation } from "react-i18next";
type Props = {
changeset: Changeset;
};
const SizedTd = styled.td`
width: 10rem;
`;
const ContributorTable: FC<Props> = ({ changeset }) => {
const [t] = useTranslation("plugins");
const collectAvailableTrailerTypes = () => {
// @ts-ignore
return [...new Set(changeset.trailers.map(trailer => trailer.trailerType))];
};
const getPersonsByTrailersType = (type: string) => {
return changeset.trailers?.filter(trailer => trailer.trailerType === type).map(t => t.person);
};
const getTrailersByType = () => {
const availableTrailerTypes: string[] = collectAvailableTrailerTypes();
const personsByTrailerType = [];
for (const type of availableTrailerTypes) {
personsByTrailerType.push({ type, persons: getPersonsByTrailersType(type) });
}
return personsByTrailerType;
};
return (
<table>
<tr>
<SizedTd>{t("changeset.trailer.type.author") + ":"}</SizedTd>
<td>
<a title={changeset?.author?.mail} href={"mailto:" + changeset?.author?.mail}>
{changeset?.author?.name}
</a>
</td>
</tr>
{getTrailersByType().map(trailer => (
<tr>
<SizedTd>{t("changeset.trailer.type." + trailer.type) + ":"}</SizedTd>
<td className="shorten-text is-marginless">
{trailer.persons
.map(person => (
<a title={person.mail} href={"mailto:" + person.mail}>
{person.name}
</a>
))
.reduce((prev, curr) => [prev, ", ", curr])}
</td>
</tr>
))}
</table>
);
};
export default ContributorTable;

View File

@@ -1,4 +1,15 @@
{ {
"changeset": {
"trailer": {
"type": {
"author": "Autor",
"Reviewed-by": "Reviewer",
"Co-authored-by": "Co-Autoren",
"Committed-by": "Committed von",
"Signed-off-by": "Signiert von"
}
}
},
"permissions": { "permissions": {
"*": { "*": {
"displayName": "Globaler Administrator", "displayName": "Globaler Administrator",

View File

@@ -1,4 +1,15 @@
{ {
"changeset": {
"trailer": {
"type": {
"author": "Author",
"Reviewed-by": "Reviewers",
"Co-authored-by": "Co-Authors",
"Committed-by": "Commit by",
"Signed-off-by": "Signed-off"
}
}
},
"permissions": { "permissions": {
"*": { "*": {
"displayName": "Global administrator", "displayName": "Global administrator",