1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

feat: add badges to modified strategies (#2492)

<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->

## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

* Adds a badge to strategies if the change can be found and the change
is relevant:
<img width="893" alt="Skjermbilde 2022-11-22 kl 11 18 32"
src="https://user-images.githubusercontent.com/16081982/203289081-b59ecaaf-e6cd-4802-b633-9cc118ca405d.png">

* I was considering making this more ergonomic using the slots vs props
pattern, but since this component is used in multiple places I left it
as is for now.

<!-- Does it close an issue? Multiple? -->
Closes #

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->
This commit is contained in:
Fredrik Strand Oseberg 2022-11-22 12:07:50 +01:00 committed by GitHub
parent 2a4ca96da2
commit a53d970263
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 3 deletions

View File

@ -1,10 +1,14 @@
import { DragEventHandler, RefObject, useRef } from 'react';
import { Box } from '@mui/material';
import { Box, useMediaQuery, useTheme } from '@mui/material';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import { StrategySeparator } from 'component/common/StrategySeparator/StrategySeparator';
import { IFeatureEnvironment } from 'interfaces/featureToggle';
import { IFeatureStrategy } from 'interfaces/strategy';
import { StrategyItem } from './StrategyItem/StrategyItem';
import { useRequiredPathParam } from 'hooks/useRequiredPathParam';
import { Badge } from 'component/common/Badge/Badge';
import { IChangeRequestEvent } from 'component/changeRequest/changeRequest.types';
import { useStrategyChangeFromRequest } from './StrategyItem/useStrategyChangeFromRequest';
interface IStrategyDraggableItemProps {
strategy: IFeatureStrategy;
@ -32,7 +36,15 @@ export const StrategyDraggableItem = ({
onDragOver,
onDragEnd,
}: IStrategyDraggableItemProps) => {
const projectId = useRequiredPathParam('projectId');
const featureId = useRequiredPathParam('featureId');
const ref = useRef<HTMLDivElement>(null);
const change = useStrategyChangeFromRequest(
projectId,
featureId,
environmentName,
strategy.id
);
return (
<Box
@ -53,7 +65,34 @@ export const StrategyDraggableItem = ({
onDragStart={onDragStartRef(ref, index)}
onDragEnd={onDragEnd}
orderNumber={index + 1}
headerChildren={<ChangeRequestStatusBadge change={change} />}
/>
</Box>
);
};
const ChangeRequestStatusBadge = ({
change,
}: {
change: IChangeRequestEvent | undefined;
}) => {
const theme = useTheme();
const isSmallScreen = useMediaQuery(theme.breakpoints.down('sm'));
if (isSmallScreen) {
return null;
}
return (
<>
<ConditionallyRender
condition={change?.action === 'updateStrategy'}
show={<Badge color="warning">Modified in draft</Badge>}
/>
<ConditionallyRender
condition={change?.action === 'deleteStrategy'}
show={<Badge color="error">Deleted in draft</Badge>}
/>
</>
);
};

View File

@ -1,4 +1,4 @@
import { DragEventHandler, VFC } from 'react';
import { DragEventHandler, FC } from 'react';
import { Edit } from '@mui/icons-material';
import { Link } from 'react-router-dom';
import { IFeatureEnvironment } from 'interfaces/featureToggle';
@ -20,15 +20,17 @@ interface IStrategyItemProps {
onDragEnd?: DragEventHandler<HTMLButtonElement>;
otherEnvironments?: IFeatureEnvironment['name'][];
orderNumber?: number;
headerChildren?: JSX.Element[] | JSX.Element;
}
export const StrategyItem: VFC<IStrategyItemProps> = ({
export const StrategyItem: FC<IStrategyItemProps> = ({
environmentId,
strategy,
onDragStart,
onDragEnd,
otherEnvironments,
orderNumber,
headerChildren,
}) => {
const projectId = useRequiredPathParam('projectId');
const featureId = useRequiredPathParam('featureId');
@ -48,6 +50,7 @@ export const StrategyItem: VFC<IStrategyItemProps> = ({
orderNumber={orderNumber}
actions={
<>
{headerChildren}
<ConditionallyRender
condition={Boolean(
otherEnvironments && otherEnvironments?.length > 0

View File

@ -0,0 +1,27 @@
import { useChangeRequestOpen } from 'hooks/api/getters/useChangeRequestOpen/useChangeRequestOpen';
export const useStrategyChangeFromRequest = (
projectId: string,
featureId: string,
environment: string,
strategyId: string
) => {
const { draft } = useChangeRequestOpen(projectId);
const environmentDraft = draft?.find(
draft => draft.environment === environment
);
const feature = environmentDraft?.features.find(
feature => feature.name === featureId
);
const change = feature?.changes.find(change => {
if (
change.action === 'updateStrategy' ||
change.action === 'deleteStrategy'
) {
return change.payload.id === strategyId;
}
});
return change;
};