-
-
Notifications
You must be signed in to change notification settings - Fork 658
/
ContextFieldUsage.tsx
89 lines (80 loc) · 3.12 KB
/
ContextFieldUsage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { Alert, styled } from '@mui/material';
import { formatEditStrategyPath } from 'component/feature/FeatureStrategy/FeatureStrategyEdit/FeatureStrategyEdit';
import { IFeatureStrategy } from 'interfaces/strategy';
import { Link } from 'react-router-dom';
import { formatStrategyName } from 'utils/strategyNames';
import { useStrategiesByContext } from 'hooks/api/getters/useStrategiesByContext/useStrategiesByContext';
import useProjects from 'hooks/api/getters/useProjects/useProjects';
const StyledUl = styled('ul')(({ theme }) => ({
listStyle: 'none',
paddingLeft: 0,
}));
const StyledAlert = styled(Alert)(({ theme }) => ({
marginTop: theme.spacing(1),
}));
interface IContextFieldUsageProps {
contextName: string;
}
export const ContextFieldUsage = ({ contextName }: IContextFieldUsageProps) => {
const { strategies } = useStrategiesByContext(contextName);
const { projects } = useProjects();
const projectsUsed = Array.from(
new Set<string>(
strategies.map(({ projectId }) => projectId!).filter(Boolean)
)
);
const projectList = (
<StyledUl>
{projectsUsed.map(projectId => (
<li key={projectId}>
<Link
to={`/projects/${projectId}`}
target="_blank"
rel="noreferrer"
>
{projects.find(({ id }) => id === projectId)?.name ??
projectId}
</Link>
<ul>
{strategies
?.filter(
strategy => strategy.projectId === projectId
)
.map(strategy => (
<li key={strategy.id}>
<Link
to={formatEditStrategyPath(
strategy.projectId!,
strategy.featureName!,
strategy.environment!,
strategy.id
)}
target="_blank"
rel="noreferrer"
>
{strategy.featureName!}{' '}
{formatStrategyNameParens(strategy)}
</Link>
</li>
))}
</ul>
</li>
))}
</StyledUl>
);
if (projectsUsed.length > 0) {
return (
<StyledAlert severity="info">
Usage of this context field:
{projectList}
</StyledAlert>
);
}
return null;
};
const formatStrategyNameParens = (strategy: IFeatureStrategy): string => {
if (!strategy.strategyName) {
return '';
}
return `(${formatStrategyName(strategy.strategyName)})`;
};