Skip to content

Commit

Permalink
fix(flags): update payloads object when removing variants (#25580)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
dmarticus and github-actions[bot] authored Oct 17, 2024
1 parent 85260e1 commit caff879
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion frontend/src/scenes/feature-flags/FeatureFlag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ function UsageTab({ featureFlag }: { id: string; featureFlag: FeatureFlagType })
) {
enrichUsageDashboard()
}
}, [dashboard])
}, [dashboard, hasEnrichedAnalytics, enrichUsageDashboard])

const propertyFilter: AnyPropertyFilter[] = [
{
Expand Down
35 changes: 27 additions & 8 deletions frontend/src/scenes/feature-flags/featureFlagLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
FilterType,
InsightModel,
InsightType,
JsonType,
MultivariateFlagOptions,
MultivariateFlagVariant,
NewEarlyAccessFeatureType,
Expand Down Expand Up @@ -133,9 +134,11 @@ export const variantKeyToIndexFeatureFlagPayloads = (flag: FeatureFlagType): Fea
return flag
}

const newPayloads = {}
const newPayloads: Record<number, JsonType> = {}
flag.filters.multivariate?.variants.forEach((variant, index) => {
newPayloads[index] = flag.filters.payloads?.[variant.key]
if (flag.filters.payloads?.[variant.key] !== undefined) {
newPayloads[index] = flag.filters.payloads[variant.key]
}
})
return {
...flag,
Expand All @@ -148,11 +151,10 @@ export const variantKeyToIndexFeatureFlagPayloads = (flag: FeatureFlagType): Fea

const indexToVariantKeyFeatureFlagPayloads = (flag: Partial<FeatureFlagType>): Partial<FeatureFlagType> => {
if (flag.filters?.multivariate) {
const newPayloads = {}
flag.filters?.multivariate?.variants.forEach(({ key }, index) => {
const payload = flag.filters?.payloads?.[index]
if (payload) {
newPayloads[key] = payload
const newPayloads: Record<string, JsonType> = {}
flag.filters.multivariate.variants.forEach(({ key }, index) => {
if (flag.filters?.payloads?.[index] !== undefined) {
newPayloads[key] = flag.filters.payloads[index]
}
})
return {
Expand Down Expand Up @@ -319,6 +321,22 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
}
const variants = [...(state.filters.multivariate?.variants || [])]
variants.splice(index, 1)

const currentPayloads = { ...state.filters.payloads }
const newPayloads: Record<number, any> = {}

// TRICKY: In addition to modifying the variant array, we also need to shift the payload indices
// because the variant array is being modified and we need to make sure that the payloads object
// stays in sync with the variant array.
Object.keys(currentPayloads).forEach((key) => {
const payloadIndex = parseInt(key)
if (payloadIndex > index) {
newPayloads[payloadIndex - 1] = currentPayloads[payloadIndex]
} else if (payloadIndex < index) {
newPayloads[payloadIndex] = currentPayloads[payloadIndex]
}
})

return {
...state,
filters: {
Expand All @@ -327,6 +345,7 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
...state.filters.multivariate,
variants,
},
payloads: newPayloads,
},
}
},
Expand Down Expand Up @@ -642,7 +661,7 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
createScheduledChange: async () => {
const { scheduledChangeOperation, scheduleDateMarker, currentTeamId, schedulePayload } = values

const fields = {
const fields: Record<ScheduledChangeOperationType, keyof ScheduleFlagPayload> = {
[ScheduledChangeOperationType.UpdateStatus]: 'active',
[ScheduledChangeOperationType.AddReleaseCondition]: 'filters',
}
Expand Down

0 comments on commit caff879

Please sign in to comment.