/
FeatureOverviewEnvSwitch.tsx
133 lines (125 loc) · 4.69 KB
/
FeatureOverviewEnvSwitch.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { ENVIRONMENT_STRATEGY_ERROR } from 'constants/apiErrors';
import useFeatureApi from 'hooks/api/actions/useFeatureApi/useFeatureApi';
import { useFeature } from 'hooks/api/getters/useFeature/useFeature';
import useToast from 'hooks/useToast';
import { IFeatureEnvironment } from 'interfaces/featureToggle';
import PermissionSwitch from 'component/common/PermissionSwitch/PermissionSwitch';
import StringTruncator from 'component/common/StringTruncator/StringTruncator';
import { UPDATE_FEATURE_ENVIRONMENT } from 'component/providers/AccessProvider/permissions';
import React from 'react';
import { formatUnknownError } from 'utils/formatUnknownError';
import { useStyles } from './FeatureOverviewEnvSwitch.styles';
import { useRequiredPathParam } from 'hooks/useRequiredPathParam';
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
import { useSuggestToggle } from 'hooks/useSuggestToggle';
import { SuggestChangesDialogue } from 'component/suggestChanges/SuggestChangeConfirmDialog/SuggestChangeConfirmDialog';
interface IFeatureOverviewEnvSwitchProps {
env: IFeatureEnvironment;
callback?: () => void;
text?: string;
showInfoBox: () => void;
}
const FeatureOverviewEnvSwitch = ({
env,
callback,
text,
showInfoBox,
}: IFeatureOverviewEnvSwitchProps) => {
const projectId = useRequiredPathParam('projectId');
const featureId = useRequiredPathParam('featureId');
const { toggleFeatureEnvironmentOn, toggleFeatureEnvironmentOff } =
useFeatureApi();
const { refetchFeature } = useFeature(projectId, featureId);
const { setToastData, setToastApiError } = useToast();
const { classes: styles } = useStyles();
const { uiConfig } = useUiConfig();
const {
onSuggestToggle,
onSuggestToggleClose,
onSuggestToggleConfirm,
suggestChangesDialogDetails,
} = useSuggestToggle(projectId);
const handleToggleEnvironmentOn = async () => {
try {
await toggleFeatureEnvironmentOn(projectId, featureId, env.name);
setToastData({
type: 'success',
title: `Available in ${env.name}`,
text: `${featureId} is now available in ${env.name} based on its defined strategies.`,
});
refetchFeature();
if (callback) {
callback();
}
} catch (error: unknown) {
if (
error instanceof Error &&
error.message === ENVIRONMENT_STRATEGY_ERROR
) {
showInfoBox();
} else {
setToastApiError(formatUnknownError(error));
}
}
};
const handleToggleEnvironmentOff = async () => {
try {
await toggleFeatureEnvironmentOff(projectId, featureId, env.name);
setToastData({
type: 'success',
title: `Unavailable in ${env.name}`,
text: `${featureId} is unavailable in ${env.name} and its strategies will no longer have any effect.`,
});
refetchFeature();
if (callback) {
callback();
}
} catch (error: unknown) {
setToastApiError(formatUnknownError(error));
}
};
const toggleEnvironment = async (e: React.ChangeEvent) => {
if (uiConfig?.flags?.suggestChanges && env.name === 'production') {
e.preventDefault();
onSuggestToggle(featureId, env.name, env.enabled);
return;
}
if (env.enabled) {
await handleToggleEnvironmentOff();
return;
}
await handleToggleEnvironmentOn();
};
let content = text ? (
text
) : (
<>
{' '}
<span data-loading>{env.enabled ? 'enabled' : 'disabled'} in</span>
<StringTruncator text={env.name} maxWidth="120" maxLength={15} />
</>
);
return (
<div>
<label className={styles.label}>
<PermissionSwitch
permission={UPDATE_FEATURE_ENVIRONMENT}
projectId={projectId}
checked={env.enabled}
onChange={toggleEnvironment}
environmentId={env.name}
/>
{content}
</label>
<SuggestChangesDialogue
isOpen={suggestChangesDialogDetails.isOpen}
onClose={onSuggestToggleClose}
featureName={featureId}
environment={suggestChangesDialogDetails?.environment}
onConfirm={onSuggestToggleConfirm}
/>
</div>
);
};
export default FeatureOverviewEnvSwitch;