Skip to content

Commit 1cd271f

Browse files
committed
refactor: extract logic and clean up
1 parent a148b36 commit 1cd271f

File tree

11 files changed

+256
-116
lines changed

11 files changed

+256
-116
lines changed

frontend/web/components/modals/AssociatedSegmentOverrides.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class TheComponent extends Component {
157157
/>
158158
<div>
159159
<InputGroup
160+
className="col-4"
160161
component={
161162
<EnvironmentSelect
162163
projectId={this.props.projectId}

frontend/web/components/modals/CreateSegmentRulesTabForm.tsx

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -132,28 +132,26 @@ const CreateSegmentRulesTabForm: React.FC<CreateSegmentRulesTabFormProps> = ({
132132
</div>
133133
)}
134134
<div className='d-flex flex-md-row flex-sm-column gap-2'>
135-
<div className='col-md-6'>
135+
<div className='d-flex flex-column col-md-6'>
136136
<label htmlFor='segmentID'>Name*</label>
137-
<Flex>
138-
<Input
139-
data-test='segmentID'
140-
name='id'
141-
id='segmentID'
142-
maxLength={SEGMENT_ID_MAXLENGTH}
143-
value={name}
144-
onChange={(e: InputEvent) => {
145-
setValueChanged(true)
146-
setName(
147-
Format.enumeration
148-
.set(Utils.safeParseEventValue(e))
149-
.toLowerCase(),
150-
)
151-
}}
152-
isValid={name && name.length}
153-
type='text'
154-
placeholder='E.g. power_users'
155-
/>
156-
</Flex>
137+
<Input
138+
data-test='segmentID'
139+
name='id'
140+
id='segmentID'
141+
maxLength={SEGMENT_ID_MAXLENGTH}
142+
value={name}
143+
onChange={(e: InputEvent) => {
144+
setValueChanged(true)
145+
setName(
146+
Format.enumeration
147+
.set(Utils.safeParseEventValue(e))
148+
.toLowerCase(),
149+
)
150+
}}
151+
isValid={name && name.length}
152+
type='text'
153+
placeholder='E.g. power_users'
154+
/>
157155
</div>
158156
{!condensed && (
159157
<InputGroup

frontend/web/components/modals/CreateSegmentUsersTabContent.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const CreateSegmentUsersTabContent: React.FC<
9898
<FormGroup>
9999
<InputGroup
100100
title='Environment'
101+
className="col-4"
101102
component={
102103
<EnvironmentSelect
103104
projectId={`${projectId}`}

frontend/web/components/segments/Rule/Rule.tsx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
splitIfValue,
1717
isInvalidPercentageSplit,
1818
} from './utils'
19+
import { useConditionCache } from './hooks/useConditionCache'
1920

2021
interface RuleProps {
2122
index: number
@@ -41,6 +42,8 @@ const Rule: React.FC<RuleProps> = ({
4142
}) => {
4243
const { conditions: rules } = rule
4344

45+
const { cacheValue, getCachedValue } = useConditionCache()
46+
4447
const updateCondition = (
4548
conditionIndex: number,
4649
updates: Partial<SegmentCondition>,
@@ -56,7 +59,15 @@ const Rule: React.FC<RuleProps> = ({
5659
}
5760

5861
const setConditionProperty = (conditionIndex: number, property: string) => {
59-
updateCondition(conditionIndex, { property })
62+
const condition = rule.conditions[conditionIndex]
63+
64+
if (condition.property) {
65+
cacheValue(conditionIndex, condition.operator, condition.property, condition.value)
66+
}
67+
68+
const cachedValue = getCachedValue(conditionIndex, condition.operator, property)
69+
70+
updateCondition(conditionIndex, { property, value: cachedValue })
6071
}
6172

6273
const setConditionOperator = (
@@ -71,6 +82,10 @@ const Rule: React.FC<RuleProps> = ({
7182
)
7283
const newOperator = operators.find((op) => op.value === operatorValue)
7384

85+
if (condition.operator && condition.property) {
86+
cacheValue(conditionIndex, condition.operator, condition.property, condition.value)
87+
}
88+
7489
// Split the append part of the operator as not handled by backend
7590
const updates: Partial<SegmentCondition> = {
7691
operator: operatorValue?.split(':')[0],
@@ -103,6 +118,22 @@ const Rule: React.FC<RuleProps> = ({
103118
}
104119
}
105120

121+
const finalProperty = updates.property !== undefined ? updates.property : condition.property
122+
const finalOperator = operatorValue?.split(':')[0]
123+
124+
if (finalProperty && !newOperator?.hideValue && operatorValue !== 'PERCENTAGE_SPLIT') {
125+
const cachedValue = getCachedValue(conditionIndex, finalOperator, finalProperty)
126+
127+
if (cachedValue !== '') {
128+
// Use cached value if one exists (restore previous input)
129+
updates.value = cachedValue
130+
} else if (!updates.value) {
131+
// No cache and no value set by other logic - clear the input
132+
updates.value = ''
133+
}
134+
// Otherwise: preserve value set by append logic or existing value
135+
}
136+
106137
updateCondition(conditionIndex, updates)
107138
}
108139

frontend/web/components/segments/Rule/components/RuleConditionRow.tsx

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo, useState } from 'react'
1+
import React from 'react'
22
import Icon from 'components/Icon'
33
import Utils from 'common/utils/utils'
44
import {
@@ -13,8 +13,6 @@ import RuleConditionPropertySelect from './RuleConditionPropertySelect'
1313
import RuleConditionValueInput from './RuleConditionValueInput'
1414
import { RuleContextValues } from 'common/types/rules.types'
1515
import { useRuleOperator, useRuleContext } from 'components/segments/Rule/hooks'
16-
import MultiSelect from 'components/base/select/MultiSelect'
17-
import { useGetEnvironmentsQuery } from 'common/services/useEnvironment'
1816

1917
interface RuleConditionRowProps {
2018
rule: SegmentCondition
@@ -49,25 +47,20 @@ const RuleConditionRow: React.FC<RuleConditionRowProps> = ({
4947
setRuleProperty,
5048
showDescription,
5149
}) => {
52-
const [selectedEnvironments, setSelectedEnvironments] = useState<string[]>([])
5350
const lastIndex = rules.reduce((acc, v, i) => {
5451
if (!v.delete) {
5552
return i
5653
}
5754
return acc
5855
}, 0)
59-
const { data } = useGetEnvironmentsQuery({ projectId: projectId?.toString() })
60-
const environments = data?.results
61-
62-
const environmentOptions = useMemo(() => environments ? environments?.map(({ name }) => ({ label: name, value: name })) : [],[environments])
6356

6457
const isLastRule = ruleIndex === lastIndex
6558
const hasOr = ruleIndex > 0
6659

6760
const { displayValue, operator, operatorObj, valuePlaceholder } =
6861
useRuleOperator(rule, operators)
6962

70-
const { allowedContextValues, isValueFromContext, showEnvironmentDropdown } =
63+
const { allowedContextValues, isValueFromContext } =
7164
useRuleContext(operator, rule.property)
7265

7366
if (rule.delete) {
@@ -79,9 +72,6 @@ const RuleConditionRow: React.FC<RuleConditionRowProps> = ({
7972
operator === 'PERCENTAGE_SPLIT' &&
8073
rule.property === RuleContextValues.IDENTITY_KEY
8174

82-
83-
console.log('showEnvironmentDropdown', showEnvironmentDropdown)
84-
console.log('rule', rule)
8575
return (
8676
<div className='rule__row reveal' key={ruleIndex}>
8777
{hasOr && (
@@ -124,23 +114,15 @@ const RuleConditionRow: React.FC<RuleConditionRowProps> = ({
124114
className="col-10 col-md-3"
125115
/>
126116
)}
127-
{operator === 'IN' && rule.property === RuleContextValues.ENVIRONMENT_NAME ? (
128-
<MultiSelect
129-
selectedValues={selectedEnvironments}
130-
onSelectionChange={(selectedValues: string[]) => setSelectedEnvironments(selectedValues)}
131-
options={environmentOptions}
132-
className='col-10 col-md-4'
133-
/>
134-
) : (
135117
<RuleConditionValueInput
136118
readOnly={readOnly}
137119
data-test={`${dataTest}-value-${ruleIndex}`}
138120
value={displayValue || ''}
139121
placeholder={valuePlaceholder}
140122
disabled={operatorObj && operatorObj.hideValue}
141123
projectId={projectId}
142-
showEnvironmentDropdown={showEnvironmentDropdown}
143124
operator={operator}
125+
property={rule.property}
144126
onChange={(value: string) => {
145127
setRuleProperty(ruleIndex, 'value', {
146128
value:
@@ -152,7 +134,6 @@ const RuleConditionRow: React.FC<RuleConditionRowProps> = ({
152134
isValid={Utils.validateRule(rule) && !ruleErrors?.value}
153135
className='col-10 col-md-4'
154136
/>
155-
)}
156137
</div>
157138
<div className='d-flex flex-sm-column flex-md-row gap-2'>
158139
{isLastRule && !readOnly ? (

0 commit comments

Comments
 (0)