Skip to content

Commit

Permalink
👻 Adds colors to drawer tags & removes boilerplate (#1481)
Browse files Browse the repository at this point in the history
Follow up to: #1470 

  - Added a reusable component for drawer labels. 
  - Adds random colors to the tags just for differentiation between the
    tags when viewing in the drawer.

---------

Signed-off-by: Ian Bolton <ibolton@redhat.com>
  • Loading branch information
ibolton336 authored Oct 31, 2023
1 parent 8b0c97d commit eb632fe
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 62 deletions.
51 changes: 51 additions & 0 deletions client/src/app/components/labels-from-items/labels-from-items.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from "react";
import { LabelGroup, LabelProps } from "@patternfly/react-core";
import { LabelCustomColor } from "../LabelCustomColor";
import { useTranslation } from "react-i18next";

interface RandomColorLabelProps extends LabelProps {}

export const RandomColorLabel: React.FC<RandomColorLabelProps> = ({
...props
}) => {
const getRandomColor = (() => {
"use strict";

const randomInt = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};

return () => {
const h = randomInt(0, 360);
const s = randomInt(42, 98);
const l = randomInt(40, 90);
return `hsl(${h},${s}%,${l}%)`;
};
})();

const randomColor = getRandomColor();

return <LabelCustomColor color={randomColor} {...props} />;
};

export function LabelsFromItems<T extends { name: string }>({
items,
noneMessage,
}: {
items?: T[];
noneMessage?: string;
}): JSX.Element {
const { t } = useTranslation();

if (items?.length ?? 0 === 0) {
return <div>{noneMessage || t("terms.none")}</div>;
}

return (
<LabelGroup>
{items?.map((item, index) => (
<RandomColorLabel key={index}>{item.name}</RandomColorLabel>
))}
</LabelGroup>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
Text,
Title,
Label,
LabelGroup,
} from "@patternfly/react-core";
import spacing from "@patternfly/react-styles/css/utilities/Spacing/spacing";

Expand All @@ -22,6 +21,7 @@ import {
import { ReviewedArchetypeItem } from "./reviewed-archetype-item";
import { ReviewFields } from "./review-fields";
import { RiskLabel } from "@app/components/RiskLabel";
import { LabelsFromItems } from "@app/components/labels-from-items/labels-from-items";

export interface IApplicationDetailDrawerAssessmentProps
extends Pick<IApplicationDetailDrawerProps, "application" | "onCloseClick"> {
Expand Down Expand Up @@ -57,9 +57,7 @@ export const ApplicationDetailDrawerAssessment: React.FC<
</DescriptionListTerm>
<DescriptionListDescription>
{application?.archetypes?.length ?? 0 > 0 ? (
<ArchetypeLabels
archetypeRefs={application?.archetypes as Ref[]}
/>
<ArchetypeLabels archetypeRefs={application?.archetypes} />
) : (
<EmptyTextMessage message={t("terms.none")} />
)}
Expand Down Expand Up @@ -105,17 +103,7 @@ export const ApplicationDetailDrawerAssessment: React.FC<
/>
);
};

const ArchetypeLabels: React.FC<{ archetypeRefs?: Ref[] }> = ({
archetypeRefs,
}) =>
(archetypeRefs?.length ?? 0) === 0 ? null : (
<LabelGroup>
{(archetypeRefs as Ref[])
.sort((a, b) => a.name.localeCompare(b.name))
.map((ref) => (
<Label color="grey" key={ref.id}>
{ref.name}
</Label>
))}
</LabelGroup>
);
}) => <LabelsFromItems items={archetypeRefs} />;
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
DescriptionListGroup,
DescriptionListTerm,
DescriptionListDescription,
LabelGroup,
Label,
Stack,
StackItem,
} from "@patternfly/react-core";
Expand All @@ -22,6 +20,7 @@ import { EmptyTextMessage } from "@app/components/EmptyTextMessage";
import { PageDrawerContent } from "@app/components/PageDrawerContext";

import { dedupeArrayOfObjects } from "@app/utils/utils";
import { LabelsFromItems } from "@app/components/labels-from-items/labels-from-items";

export interface IArchetypeDetailDrawerProps {
onCloseClick: () => void;
Expand Down Expand Up @@ -170,56 +169,18 @@ const ArchetypeDetailDrawer: React.FC<IArchetypeDetailDrawerProps> = ({

const ApplicationLabels: React.FC<{ applicationRefs?: Ref[] }> = ({
applicationRefs,
}) =>
(applicationRefs?.length ?? 0) === 0 ? null : (
<LabelGroup>
{(applicationRefs as Ref[])
.sort((a, b) => a.name.localeCompare(b.name))
.map((ref) => (
<Label color="grey" key={ref.id}>
{ref.name}
</Label>
))}
</LabelGroup>
);
}) => <LabelsFromItems items={applicationRefs as Ref[]} />;

const TagLabels: React.FC<{ tags?: Tag[] }> = ({ tags }) =>
(tags?.length ?? 0) === 0 ? null : (
<LabelGroup>
{(tags as Tag[])
.sort((a, b) => a.name.localeCompare(b.name))
.map((sh) => (
<Label color="grey" key={sh.id}>
{sh.name}
</Label>
))}
</LabelGroup>
);
const TagLabels: React.FC<{ tags?: Tag[] }> = ({ tags }) => (
<LabelsFromItems items={tags} />
);

const StakeholderLabels: React.FC<{ archetype: Archetype }> = ({
archetype,
}) =>
(archetype.stakeholders?.length ?? 0) === 0 ? null : (
<LabelGroup>
{archetype.stakeholders?.map((sh) => (
<Label color="orange" key={sh.id}>
{sh.name}
</Label>
))}
</LabelGroup>
);
}) => <LabelsFromItems items={archetype.stakeholders as Ref[]} />;

const StakeholderGroupsLabels: React.FC<{ archetype: Archetype }> = ({
archetype,
}) =>
(archetype.stakeholderGroups?.length ?? 0) === 0 ? null : (
<LabelGroup>
{archetype.stakeholderGroups?.map((sh) => (
<Label color="green" key={sh.id}>
{sh.name}
</Label>
))}
</LabelGroup>
);
}) => <LabelsFromItems items={archetype.stakeholderGroups as Ref[]} />;

export default ArchetypeDetailDrawer;

0 comments on commit eb632fe

Please sign in to comment.