-
Notifications
You must be signed in to change notification settings - Fork 467
Description
Summary
Users encounter a crash when opening the Create Segment modal under certain conditions. The error Cannot read properties of null (reading 'reduce') (or null is not an object (evaluating 'e.options.reduce') on Safari) occurs when react-select attempts to process an undefined options prop.
Sentry Issues
- FLAGSMITH-FRONTEND-4G8 - 3 events, 1 user
- FLAGSMITH-FRONTEND-4G9 - 2 events, 1 user
- FLAGSMITH-FRONTEND-4DP - 1 event, 1 user (ongoing 2 months)
- FLAGSMITH-FRONTEND-4DQ - 1 event, 1 user (ongoing 2 months)
Root Cause
The RuleConditionValueInput component renders a MultiSelect for environment selection when certain operators are selected. However, when projectId is undefined or the environments query hasn't completed, react-select receives invalid options and crashes.
Affected file: frontend/web/components/segments/Rule/components/RuleConditionValueInput.tsx
// Current code (lines 40-50)
const { data } = useGetEnvironmentsQuery(
{ projectId: projectId?.toString() || '' },
{ skip: !projectId },
)
const environmentOptions = useMemo(
() =>
data?.results
? data.results.map(({ name }) => ({ label: name, value: name }))
: [],
[data],
)The issue is that showMultiEnvironmentSelect can be true (based on operator/property) while projectId is still undefined, causing the component to render before data is ready.
Reproduction Steps
- Navigate to a project's Segments page
- Click "Create Segment" button
- Add a rule and select an operator that triggers multi-environment selection
- Error occurs during modal render
Proposed Fix
Add defensive checks before rendering the MultiSelect:
if (showMultiEnvironmentSelect) {
if (!projectId || !data?.results) {
return (
<div className={className}>
<MultiSelect
selectedValues={[]}
onSelectionChange={() => {}}
placeholder='Loading environments...'
options={[]}
className='w-100'
disabled
inline
/>
</div>
)
}
return (
<div className={className}>
<MultiSelect
selectedValues={safeParseArray(value)}
onSelectionChange={(selectedValues: string[]) => {
onChange?.(selectedValues.join(','))
}}
placeholder='Select environments...'
options={environmentOptions}
className='w-100'
hideSelectedOptions={false}
inline
/>
</div>
)
}Acceptance Criteria
- Opening the Create Segment modal no longer crashes
- Environment-based operators show a loading state while environments are fetched
- MultiSelect displays correctly once environments are loaded
- Add E2E test covering segment creation with environment-based operators
Additional Context
- The error manifests differently across browsers (Chrome vs Safari error messages)
- The older issues (4DP/4DQ) suggest this has been a low-frequency edge case for ~2 months
- Consider also adding null safety to the
MultiSelectcomponent itself as defence in depth