Skip to content

Fix: TypeError when opening Create Segment modal with environment-based operators #6544

@talissoncosta

Description

@talissoncosta

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

  1. Navigate to a project's Segments page
  2. Click "Create Segment" button
  3. Add a rule and select an operator that triggers multi-environment selection
  4. 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 MultiSelect component itself as defence in depth

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions