-
Notifications
You must be signed in to change notification settings - Fork 516
Expand file tree
/
Copy pathCreateFeature.tsx
More file actions
191 lines (176 loc) · 5.74 KB
/
CreateFeature.tsx
File metadata and controls
191 lines (176 loc) · 5.74 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import React, { FC, useCallback, useEffect, useState } from 'react'
import { FeatureState, ProjectFlag } from 'common/types/responses'
import FeatureValue from './FeatureValue'
import FeatureSettings from './FeatureSettings'
import ErrorMessage from 'components/ErrorMessage'
import WarningMessage from 'components/WarningMessage'
import { useHasPermission } from 'common/providers/Permission'
import Switch from 'components/Switch'
import Tooltip from 'components/Tooltip'
import Icon from 'components/Icon'
import { useCreateTagMutation, useGetTagsQuery } from 'common/services/useTag'
type CreateFeatureTabProps = {
projectId: number
error: any
featureState: FeatureState
overrideFeatureState?: FeatureState
projectFlag: ProjectFlag | null
featureContentType: any
identity?: string
defaultExperiment?: boolean
onEnvironmentFlagChange: (changes: FeatureState) => void
onProjectFlagChange: (changes: ProjectFlag) => void
onRemoveMultivariateOption?: (id: number) => void
onHasMetadataRequiredChange: (hasMetadataRequired: boolean) => void
featureError?: string
featureWarning?: string
}
const CreateFeature: FC<CreateFeatureTabProps> = ({
defaultExperiment,
error,
featureContentType,
featureError,
featureState,
featureWarning,
identity,
onEnvironmentFlagChange,
onHasMetadataRequiredChange,
onProjectFlagChange,
onRemoveMultivariateOption,
overrideFeatureState,
projectFlag,
projectId,
}) => {
const { permission: createFeature } = useHasPermission({
id: projectId,
level: 'project',
permission: 'CREATE_FEATURE',
})
const { permission: projectAdmin } = useHasPermission({
id: projectId,
level: 'project',
permission: 'ADMIN',
})
const noPermissions = !createFeature && !projectAdmin
const showExperimentToggle =
Utils.getFlagsmithHasFeature('experimental_flags') && !identity
const { data: tags } = useGetTagsQuery(
{ projectId },
{ skip: !showExperimentToggle },
)
const [createTag] = useCreateTagMutation()
const [isExperimentFlag, setIsExperimentFlag] = useState(!!defaultExperiment)
const hasVariants = (projectFlag?.multivariate_options?.length ?? 0) > 0
// Auto-untoggle if all variants are removed
useEffect(() => {
if (!hasVariants && isExperimentFlag) {
setIsExperimentFlag(false)
if (projectFlag && tags) {
const experimentTag = tags.find(
(t) => t.label.toLowerCase() === 'experiment',
)
if (experimentTag) {
onProjectFlagChange({
...projectFlag,
tags: projectFlag.tags.filter((id) => id !== experimentTag.id),
})
}
}
}
}, [hasVariants]) // eslint-disable-line react-hooks/exhaustive-deps
const handleExperimentToggle = useCallback(
async (checked: boolean) => {
if (!projectFlag || !tags) return
setIsExperimentFlag(checked)
let experimentTag = tags.find(
(t) => t.label.toLowerCase() === 'experiment',
)
if (checked) {
if (!experimentTag) {
const result = await createTag({
projectId,
tag: {
color: '#6A52CF',
description: 'Experiment flag',
label: 'experiment',
},
}).unwrap()
experimentTag = result
}
if (experimentTag && !projectFlag.tags.includes(experimentTag.id)) {
onProjectFlagChange({
...projectFlag,
tags: [...projectFlag.tags, experimentTag.id],
})
}
} else {
if (experimentTag) {
onProjectFlagChange({
...projectFlag,
tags: projectFlag.tags.filter((id) => id !== experimentTag!.id),
})
}
}
},
[projectFlag, tags, createTag, projectId, onProjectFlagChange],
)
return (
<>
<ErrorMessage error={featureError} />
<WarningMessage warningMessage={featureWarning} />
{!!projectFlag && (
<>
<FeatureValue
error={error}
createFeature={createFeature}
hideValue={false}
isEdit={!!identity}
identity={identity}
noPermissions={noPermissions}
featureState={overrideFeatureState || featureState}
projectFlag={projectFlag}
onEnvironmentFlagChange={onEnvironmentFlagChange}
onProjectFlagChange={onProjectFlagChange}
onRemoveMultivariateOption={onRemoveMultivariateOption}
/>
{showExperimentToggle && (
<FormGroup className='my-4'>
<Tooltip
title={
<div className='flex-row'>
<Switch
checked={isExperimentFlag}
onChange={handleExperimentToggle}
disabled={!hasVariants}
className='ml-0'
/>
<div className='label-switch ml-3 mr-1'>
Experiment flag
</div>
<Icon name='info-outlined' />
</div>
}
>
{!hasVariants
? 'Add at least one variant to start an experiment'
: 'Tag this feature as an experiment'}
</Tooltip>
</FormGroup>
)}
<FeatureSettings
projectAdmin={projectAdmin}
createFeature={createFeature}
featureContentType={featureContentType}
identity={identity}
isEdit={!!identity}
projectId={projectId}
projectFlag={projectFlag}
onChange={onProjectFlagChange}
onHasMetadataRequiredChange={onHasMetadataRequiredChange}
/>
</>
)}
</>
)
}
export default CreateFeature